[FIX] account_edi_ubl_cii: wrong journal selected when uploading an invoice in a company with multiple journals of the same type

Steps to reproduce:
- Create two sales journals
- Select the second journal
- Upload an invoice
- The invoice is in the first journal instead of the second

Fix:
We can simply use the context to retrieve the current selected journal
if it's given, otherwise we use the previous fallback (infering the
journal with the move type)

task-id 3258956

closes odoo/odoo#118258

X-original-commit: 12f22cd06a7b25fc4740a689aad63e0ebc357e4c
Signed-off-by: William André (wan) <wan@odoo.com>
This commit is contained in:
Ricardo Gomes Rodrigues (rigr)
2023-04-11 19:48:40 +02:00
parent 82787d2b98
commit 698e2bcfa8
5 changed files with 53 additions and 10 deletions
+2
View File
@@ -719,6 +719,8 @@ class AccountJournal(models.Model):
def _create_document_from_attachment(self, attachment_ids):
""" Create the invoices from files."""
if not self:
self = self.env['account.journal'].browse(self._context.get("default_journal_id"))
move_type = self._context.get("default_move_type", "entry")
if not self:
if move_type in self.env['account.move'].get_sale_types():
+11 -10
View File
@@ -663,13 +663,22 @@ class AccountMove(models.Model):
@api.depends('move_type')
def _compute_journal_id(self):
for record in self:
for record in self.filtered(lambda r: r.journal_id.type not in r._get_valid_journal_types()):
record.journal_id = record._search_default_journal()
if not record.company_id or record.company_id != record.journal_id.company_id:
self.env.add_to_compute(self._fields['company_id'], record)
if not record.currency_id or record.journal_id.currency_id and record.currency_id != record.journal_id.currency_id:
self.env.add_to_compute(self._fields['currency_id'], record)
def _get_valid_journal_types(self):
if self.is_sale_document(include_receipts=True):
return ['sale']
elif self.is_purchase_document(include_receipts=True):
return ['purchase']
elif self.payment_id or self.env.context.get('is_payment'):
return ['bank', 'cash']
return ['general']
def _search_default_journal(self):
if self.payment_id and self.payment_id.journal_id:
return self.payment_id.journal_id
@@ -678,15 +687,7 @@ class AccountMove(models.Model):
if self.statement_line_ids.statement_id.journal_id:
return self.statement_line_ids.statement_id.journal_id[:1]
if self.is_sale_document(include_receipts=True):
journal_types = ['sale']
elif self.is_purchase_document(include_receipts=True):
journal_types = ['purchase']
elif self.payment_id or self.env.context.get('is_payment'):
journal_types = ['bank', 'cash']
else:
journal_types = ['general']
journal_types = self._get_valid_journal_types()
company_id = (self.company_id or self.env.company).id
domain = [('company_id', '=', company_id), ('type', 'in', journal_types)]
@@ -245,6 +245,28 @@ class TestUBLCommon(AccountTestInvoicingCommon):
new_invoice = self.company_data['default_journal_purchase']._create_document_from_attachment(attachment.ids)
self.assertEqual(seller, new_invoice.partner_id)
def _test_import_in_journal(self, attachment):
"""
If the context contains the info about the current default journal, we should use it
instead of infering the journal from the move type.
"""
journal2 = self.company_data['default_journal_sale'].copy()
journal2.default_account_id = self.company_data['default_account_revenue'].id
journal3 = journal2.copy()
journal3.default_account_id = self.company_data['default_account_revenue'].id # Not copied
# Use the journal if it's set
new_invoice = journal2._create_document_from_attachment(attachment.id)
self.assertEqual(new_invoice.journal_id, journal2)
# If no journal, fallback on the context
new_invoice2 = self.env['account.journal'].with_context(default_journal_id=journal3.id)._create_document_from_attachment(attachment.id)
self.assertEqual(new_invoice2.journal_id, journal3)
# If no journal and no journal in the context, fallback on the move type
new_invoice3 = self.env['account.journal'].with_context(default_move_type='out_invoice')._create_document_from_attachment(attachment.id)
self.assertEqual(new_invoice3.journal_id, self.company_data['default_journal_sale'])
def _test_encoding_in_attachment(self, attachment, filename):
"""
Generate an invoice, assert that the tag '<?xml version='1.0' encoding='UTF-8'?>' is present in the attachment
@@ -304,6 +304,15 @@ class TestCIIFR(TestUBLCommon):
)
self._test_import_partner(invoice.ubl_cii_xml_id, self.partner_1, self.partner_2)
def test_import_in_journal_facturx(self):
invoice = self._generate_move(
seller=self.partner_1,
buyer=self.partner_2,
move_type='out_invoice',
invoice_line_ids=[{'product_id': self.product_a.id}],
)
self._test_import_in_journal(invoice.ubl_cii_xml_id)
def test_import_and_create_partner_facturx(self):
""" Tests whether the partner is created at import if no match is found when decoding the EDI attachment
"""
@@ -219,6 +219,15 @@ class TestUBLBE(TestUBLCommon):
)
self._test_import_partner(invoice.ubl_cii_xml_id, self.partner_1, self.partner_2)
def test_import_in_journal_ubl(self):
invoice = self._generate_move(
seller=self.partner_1,
buyer=self.partner_2,
move_type='out_invoice',
invoice_line_ids=[{'product_id': self.product_a.id}],
)
self._test_import_in_journal(invoice.ubl_cii_xml_id)
def test_import_and_create_partner_ubl(self):
""" Tests whether the partner is created at import if no match is found when decoding the EDI attachment
"""