From c38dfc37e9ab6a085797f30f457035d19f4edc0d Mon Sep 17 00:00:00 2001 From: sbejaoui Date: Sun, 28 Feb 2021 00:59:43 +0100 Subject: [PATCH] [IMP] - Chronology check depends on invoice type if journal is set to Dedicated Credit Note Sequence --- .../account_invoice_constraint_chronology.pot | 2 ++ .../model/account_invoice.py | 16 +++++++++-- .../readme/CONTRIBUTORS.rst | 1 + .../test_account_constraint_chronology.py | 28 +++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/account_invoice_constraint_chronology/i18n/account_invoice_constraint_chronology.pot b/account_invoice_constraint_chronology/i18n/account_invoice_constraint_chronology.pot index cb94ca43..e66dd272 100644 --- a/account_invoice_constraint_chronology/i18n/account_invoice_constraint_chronology.pot +++ b/account_invoice_constraint_chronology/i18n/account_invoice_constraint_chronology.pot @@ -21,12 +21,14 @@ msgstr "" #. module: account_invoice_constraint_chronology #: code:addons/account_invoice_constraint_chronology/model/account_invoice.py:54 +#: code:addons/account_invoice_constraint_chronology/model/account_invoice.py:66 #, python-format msgid "Chronology Error. Please confirm older draft invoices before {date_invoice} and try again." msgstr "" #. module: account_invoice_constraint_chronology #: code:addons/account_invoice_constraint_chronology/model/account_invoice.py:70 +#: code:addons/account_invoice_constraint_chronology/model/account_invoice.py:82 #, python-format msgid "Chronology Error. There exist at least one invoice with a later date to {date_invoice}." msgstr "" diff --git a/account_invoice_constraint_chronology/model/account_invoice.py b/account_invoice_constraint_chronology/model/account_invoice.py index a5ac7826..1e17263f 100644 --- a/account_invoice_constraint_chronology/model/account_invoice.py +++ b/account_invoice_constraint_chronology/model/account_invoice.py @@ -13,7 +13,7 @@ class AccountInvoice(models.Model): @api.model def _prepare_previous_invoices_domain(self, invoice): - return [ + domain = [ ('state', 'not in', ['open', 'paid', 'cancel', @@ -24,14 +24,26 @@ class AccountInvoice(models.Model): ('date_invoice', '<', invoice.date_invoice), ('journal_id', '=', invoice.journal_id.id), ] + if ( + invoice.journal_id.refund_sequence + and invoice.journal_id.sequence_id != invoice.journal_id.refund_sequence_id + ): + domain.append(('type', '=', invoice.type)) + return domain @api.model def _prepare_later_invoices_domain(self, invoice): - return [ + domain = [ ('state', 'in', ['open', 'in_payment', 'paid']), ('date_invoice', '>', invoice.date_invoice), ('journal_id', '=', invoice.journal_id.id), ] + if ( + invoice.journal_id.refund_sequence + and invoice.journal_id.sequence_id != invoice.journal_id.refund_sequence_id + ): + domain.append(('type', '=', invoice.type)) + return domain @api.multi def action_move_create(self): diff --git a/account_invoice_constraint_chronology/readme/CONTRIBUTORS.rst b/account_invoice_constraint_chronology/readme/CONTRIBUTORS.rst index ef64cb6e..3c2da210 100644 --- a/account_invoice_constraint_chronology/readme/CONTRIBUTORS.rst +++ b/account_invoice_constraint_chronology/readme/CONTRIBUTORS.rst @@ -2,3 +2,4 @@ * Gilles Gilles * Francesco Apruzzese * Thomas Binsfeld +* Souheil Bejaoui diff --git a/account_invoice_constraint_chronology/tests/test_account_constraint_chronology.py b/account_invoice_constraint_chronology/tests/test_account_constraint_chronology.py index 6392c80a..ad90409b 100644 --- a/account_invoice_constraint_chronology/tests/test_account_constraint_chronology.py +++ b/account_invoice_constraint_chronology/tests/test_account_constraint_chronology.py @@ -133,3 +133,31 @@ class TestAccountConstraintChronology(common.SavepointCase): self.account_journal_sale.type = 'bank' self.account_journal_sale._onchange_type() self.assertFalse(self.account_journal_sale.check_chronology) + + def test_invoice_refund(self): + journal = self.get_journal_check(True) + today = datetime.now() + tomorrow = today + timedelta(days=1) + date_tomorrow = tomorrow.strftime(DEFAULT_SERVER_DATE_FORMAT) + invoice_1 = self.create_simple_invoice(journal.id, date_tomorrow) + self.assertTrue( + (invoice_1.state == "draft"), "Initial invoice state is not Draft" + ) + invoice_1.action_invoice_open() + date = today.strftime(DEFAULT_SERVER_DATE_FORMAT) + refund_invoice_wiz = ( + self.env["account.invoice.refund"] + .with_context(active_ids=[invoice_1.id]) + .create( + { + "description": "test_invoice_refund", + "filter_refund": "cancel", + "date": date, + "date_invoice": date, + } + ) + ) + with self.assertRaises(UserError): + refund_invoice_wiz.invoice_refund() + invoice_1.journal_id.refund_sequence = True + refund_invoice_wiz.invoice_refund()