[FIX] l10n_it_edi: import company to company invoice

As we imported invoices from the IAP proxy we refused files when the
same filename was already present in the database.

Import invoices sent by Company A to Company B on the same Odoo database
was therefore impeded. The CRON would try to import the Company B's
vendor bill with the same attachment's filename as the Company A's
invoice and reject the file.

Now we fill in the `company_id` field on the attachment and we search
for attachments which belong to the company we're importing for, so the
case is covered.

An import test has been added.

opw-3673508

closes odoo/odoo#157755

Signed-off-by: Habib Ayob (ayh) <ayh@odoo.com>
This commit is contained in:
Paolo Gatti (pgi)
2024-03-22 18:27:03 +00:00
parent 0d9fd71de3
commit 388466f42f
2 changed files with 51 additions and 4 deletions
+4 -3
View File
@@ -687,12 +687,13 @@ class AccountMove(models.Model):
:param proxy_user: the AccountEdiProxyClientUser to use for decrypting the file
"""
# Name should be unique, the invoice already exists
Attachment = self.env['ir.attachment']
# Name should be unique per company, the invoice already exists
Attachment = self.env['ir.attachment'].sudo().with_company(proxy_user.company_id)
if Attachment.search_count([
('name', '=', filename),
('res_model', '=', 'account.move'),
('res_field', '=', 'l10n_it_edi_attachment_file'),
('company_id', '=', proxy_user.company_id.id),
], limit=1):
_logger.warning('E-invoice already exists: %s', filename)
return False
@@ -705,7 +706,7 @@ class AccountMove(models.Model):
return False
# Create the attachment, an empty move, then attach the two and commit
move = self.create({})
move = self.with_company(proxy_user.company_id).create({})
attachment = Attachment.create({
'name': filename,
'raw': decrypted_content,
+47 -1
View File
@@ -5,7 +5,7 @@ import uuid
from freezegun import freeze_time
from unittest.mock import patch
from odoo import fields, sql_db, tools
from odoo import fields, sql_db, tools, Command
from odoo.tests import tagged
from odoo.addons.l10n_it_edi.tests.common import TestItEdi
@@ -96,6 +96,52 @@ class TestItEdiImport(TestItEdi):
invoices = self.env['account.move'].with_company(self.company).search([('name', '=', 'BILL/2019/01/0001')])
self.assertEqual(len(invoices), 1)
def test_cron_receives_bill_from_another_company(self):
""" Ensure that when from one of your company, you bill the other, the
import isn't impeded because of conflicts with the filename """
other_company = self.company_data['company']
filename = 'IT01234567890_FPR02.xml'
def mock_commit(self):
pass
invoice = self.env['account.move'].with_company(other_company).create({
'move_type': 'out_invoice',
'invoice_line_ids': [
Command.create({
'name': "something not price included",
'price_unit': 800.40,
'tax_ids': [Command.set(self.company_data['default_tax_sale'].ids)],
}),
],
})
self.env['ir.attachment'].with_company(other_company).create({
'name': filename,
'datas': self.fake_test_content,
'res_model': 'account.move',
'res_id': invoice.id,
'res_field': 'l10n_it_edi_attachment_file',
})
with (patch.object(self.proxy_user.__class__, '_decrypt_data', return_value=self.fake_test_content),
patch.object(sql_db.Cursor, "commit", mock_commit)):
self.env['account.move'].with_company(self.company)._l10n_it_edi_process_downloads(
{'999999999': {
'filename': filename,
'file': self.fake_test_content,
'key': str(uuid.uuid4()),
}},
self.proxy_user,
)
attachment = self.env['ir.attachment'].search([
('name', '=', 'IT01234567890_FPR02.xml'),
('res_model', '=', 'account.move'),
('res_field', '=', 'l10n_it_edi_attachment_file'),
('company_id', '=', self.company.id),
])
self.assertTrue(attachment)
self.assertTrue(self.env['account.move'].browse(attachment.res_id))
def test_receive_same_vendor_bill_twice(self):
""" Test that the second time we are receiving an SdiCoop invoice, the second is discarded """