From a16a965845780858e0497cc8a3e99bf7c9fc48b0 Mon Sep 17 00:00:00 2001 From: Alexey Pelykh Date: Thu, 8 Apr 2021 07:41:03 +0200 Subject: [PATCH] wip --- .../model/account_move.py | 34 +++++++++++++++++++ .../readme/DESCRIPTION.rst | 1 + ...t_account_invoice_constraint_chronology.py | 13 +++++++ 3 files changed, 48 insertions(+) diff --git a/account_invoice_constraint_chronology/model/account_move.py b/account_invoice_constraint_chronology/model/account_move.py index fa90e6628..56135830d 100644 --- a/account_invoice_constraint_chronology/model/account_move.py +++ b/account_invoice_constraint_chronology/model/account_move.py @@ -61,6 +61,36 @@ class AccountMove(models.Model): ).format(date_invoice=format_date(self.env, self.invoice_date)) ) + def _get_sequence_order_conflicting_invoices_domain(self): + self.ensure_one() + + if not self.name or self.name == "/": + return expression.FALSE_DOMAIN + + last_sequence = self._get_last_sequence() + if not last_sequence or self.name > last_sequence: + return expression.FALSE_DOMAIN + + return expression.AND( + [ + [("name", "=", last_sequence)], + self._get_conflicting_invoices_domain(), + [("state", "=", "posted"), ("invoice_date", "<", self.invoice_date)], + ] + ) + + def _raise_sequence_ordering_conflict(self): + self.ensure_one() + raise UserError( + _( + "Chronology conflict: An invoice with a higher number {highest_name}" + " dated before {date_invoice} exists." + ).format( + highest_name=self._get_last_sequence(), + date_invoice=format_date(self.env, self.invoice_date), + ) + ) + def write(self, vals): if vals.get("state") != "posted": return super().write(vals) @@ -68,6 +98,10 @@ class AccountMove(models.Model): newly_posted = self.filtered(lambda move: move.state != "posted") res = super().write(vals) for move in newly_posted & self.filtered("journal_id.check_chronology"): + if self.search( + move._get_sequence_order_conflicting_invoices_domain(), limit=1 + ): + move._raise_sequence_ordering_conflict() if self.search(move._get_older_conflicting_invoices_domain(), limit=1): move._raise_older_conflicting_invoices() if self.search(move._get_newer_conflicting_invoices_domain(), limit=1): diff --git a/account_invoice_constraint_chronology/readme/DESCRIPTION.rst b/account_invoice_constraint_chronology/readme/DESCRIPTION.rst index 62f42dcec..6d6fa1696 100644 --- a/account_invoice_constraint_chronology/readme/DESCRIPTION.rst +++ b/account_invoice_constraint_chronology/readme/DESCRIPTION.rst @@ -4,3 +4,4 @@ It prevents the validation of invoices when: * there are draft invoices with a prior date * there are validated invoices with a later date +* there are validated invoices with a higher number diff --git a/account_invoice_constraint_chronology/tests/test_account_invoice_constraint_chronology.py b/account_invoice_constraint_chronology/tests/test_account_invoice_constraint_chronology.py index 03a03fc80..499355c14 100644 --- a/account_invoice_constraint_chronology/tests/test_account_invoice_constraint_chronology.py +++ b/account_invoice_constraint_chronology/tests/test_account_invoice_constraint_chronology.py @@ -120,3 +120,16 @@ class TestAccountInvoiceConstraintChronology(common.SavepointCase): refund = self.AccountMove.browse(refund["res_id"]) with self.assertRaises(UserError): refund.action_post() + + def test_invoice_higher_number(self): + self.invoice_1.invoice_date = self.yesterday + self.invoice_1.action_post() + self.invoice_1.button_draft() + self.invoice_1.invoice_date = False + + self.invoice_2.invoice_date = self.today + self.invoice_2.action_post() + + self.invoice_1.invoice_date = self.tomorrow + with self.assertRaisesRegex(UserError, "higher number"): + self.invoice_1.action_post()