From 0d077539bf6901bed9bda9ad8fc3c85e6578a381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sun, 25 Aug 2024 13:08:56 +0200 Subject: [PATCH] Store computation of invoice sequence type as cache after numbering Closes #12080 --- modules/account_es/account.py | 2 +- modules/account_es_sii/account.py | 2 +- modules/account_export_winbooks/account.py | 2 +- modules/account_invoice/invoice.py | 24 ++++++++++++++++------ 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/modules/account_es/account.py b/modules/account_es/account.py index 14d4109ced..c6be18541f 100644 --- a/modules/account_es/account.py +++ b/modules/account_es/account.py @@ -164,7 +164,7 @@ class Invoice(metaclass=PoolMeta): @property def es_vat_book_type(self): - if 'credit_note' in self._sequence_field: + if self.sequence_type == 'credit_note': return 'R0' if not self.party_tax_identifier: return 'F2' diff --git a/modules/account_es_sii/account.py b/modules/account_es_sii/account.py index 7bafa952dd..ee8d3aff05 100644 --- a/modules/account_es_sii/account.py +++ b/modules/account_es_sii/account.py @@ -332,7 +332,7 @@ class InvoiceSII(ModelSQL, ModelView): @property def invoice_type(self): tax_identifier = bool(self.invoice.es_sii_party_tax_identifier) - if 'credit_note' in self.invoice._sequence_field: + if self.invoice.sequence_type == 'credit_note': if tax_identifier: return 'R1' else: diff --git a/modules/account_export_winbooks/account.py b/modules/account_export_winbooks/account.py index 18c5584bab..eba673329b 100644 --- a/modules/account_export_winbooks/account.py +++ b/modules/account_export_winbooks/account.py @@ -246,7 +246,7 @@ class MoveExport(metaclass=PoolMeta): dbktype = '' if isinstance(move.origin, Invoice): invoice = move.origin - sequence_field = invoice._sequence_field[:-len('_sequence')] + sequence_field = f'{invoice.type}_{invoice.sequence_type}' dbktype = getattr(DBKTYPE, sequence_field.upper()) elif isinstance(move.origin, Statement): dbktype = DBKTYPE.STATEMENT diff --git a/modules/account_invoice/invoice.py b/modules/account_invoice/invoice.py index 9c34151dcf..c4b2868e52 100644 --- a/modules/account_invoice/invoice.py +++ b/modules/account_invoice/invoice.py @@ -145,6 +145,11 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin, InvoiceReportMixin): help="The date from which the payment term is calculated.\n" "Leave empty to use the invoice date.") sequence = fields.Integer("Sequence", readonly=True) + sequence_type_cache = fields.Selection([ + (None, ""), + ('invoice', "Invoice"), + ('credit_note', "Credit Note"), + ], "Sequence Type Cache", readonly=True) party = fields.Many2One( 'party.party', 'Party', required=True, states=_states, context={ @@ -1294,6 +1299,7 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin, InvoiceReportMixin): if not invoice.invoice_date and invoice.type == 'out': invoice.invoice_date = today + invoice.sequence_type_cache = invoice._sequence_type sequence, sequence_date = invoice._number_sequence() to_number[(sequence, sequence_date)].append(invoice) if invoice.type == 'out' and sequence not in sequences: @@ -1354,15 +1360,21 @@ class Invoice(Workflow, ModelSQL, ModelView, TaxableMixin, InvoiceReportMixin): fiscalyear=fiscalyear.rec_name)) @property - def _sequence_field(self): - "Returns the field name of invoice_sequence to use" - field = self.type + def _sequence_type(self): if (all(l.amount <= 0 for l in self.line_lines) and self.total_amount < 0): - field += '_credit_note' + return 'credit_note' else: - field += '_invoice' - return field + '_sequence' + return 'invoice' + + @property + def sequence_type(self): + return self.sequence_type_cache or self._sequence_type + + @property + def _sequence_field(self): + "Returns the field name of invoice_sequence to use" + return f'{self.type}_{self.sequence_type}_sequence' def get_tax_identifier(self): "Return the default computed tax identifier"