diff --git a/addons/account/models/account_journal.py b/addons/account/models/account_journal.py index d4b87105dce..b13de45a91c 100644 --- a/addons/account/models/account_journal.py +++ b/addons/account/models/account_journal.py @@ -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(): diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index c5d7f644e15..d93340606df 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -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)] diff --git a/addons/l10n_account_edi_ubl_cii_tests/tests/common.py b/addons/l10n_account_edi_ubl_cii_tests/tests/common.py index 44269f62490..c8b24c970b9 100644 --- a/addons/l10n_account_edi_ubl_cii_tests/tests/common.py +++ b/addons/l10n_account_edi_ubl_cii_tests/tests/common.py @@ -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 '' is present in the attachment diff --git a/addons/l10n_account_edi_ubl_cii_tests/tests/test_xml_cii_fr.py b/addons/l10n_account_edi_ubl_cii_tests/tests/test_xml_cii_fr.py index 1d97e7e73ae..3f844f4f41d 100644 --- a/addons/l10n_account_edi_ubl_cii_tests/tests/test_xml_cii_fr.py +++ b/addons/l10n_account_edi_ubl_cii_tests/tests/test_xml_cii_fr.py @@ -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 """ diff --git a/addons/l10n_account_edi_ubl_cii_tests/tests/test_xml_ubl_be.py b/addons/l10n_account_edi_ubl_cii_tests/tests/test_xml_ubl_be.py index f12513fb60f..3f027961e4c 100644 --- a/addons/l10n_account_edi_ubl_cii_tests/tests/test_xml_ubl_be.py +++ b/addons/l10n_account_edi_ubl_cii_tests/tests/test_xml_ubl_be.py @@ -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 """