From 2c38fbc5c48a3702dc9f071d9ef66fb5173891ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sat, 8 Apr 2023 17:51:06 +0200 Subject: [PATCH] Compute related to from statement rules all at once This ensures that only unambiguous value is set and only once. Closes #12123 --- modules/account_payment_clearing/statement.py | 31 +++++++++++----- modules/account_statement_rule/account.py | 35 +++++++++++++++---- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/modules/account_payment_clearing/statement.py b/modules/account_payment_clearing/statement.py index 6872c1284b..cfdf628f97 100644 --- a/modules/account_payment_clearing/statement.py +++ b/modules/account_payment_clearing/statement.py @@ -206,15 +206,28 @@ class StatementLine(metaclass=PoolMeta): class StatementRuleLine(metaclass=PoolMeta): __name__ = 'account.statement.rule.line' - def get_line(self, origin, keywords, **context): - line = super().get_line(origin, keywords, **context) - if line: - line.payment = self._get_payment(origin, keywords) - if (line.payment and line.party - and line.payment.party != line.party): - return - line.payment_group = self._get_payment_group(origin, keywords) - return line + def _get_related_to(self, origin, keywords): + return super()._get_related_to(origin, keywords) | { + self._get_payment(origin, keywords), + self._get_payment_group(origin, keywords), + } + + def _get_party_from(self, related_to): + pool = Pool() + Payment = pool.get('account.payment') + party = super()._get_party_from(related_to) + if isinstance(related_to, Payment): + party = related_to.party + return party + + def _get_account_from(self, related_to): + pool = Pool() + Payment = pool.get('account.payment') + PaymentGroup = pool.get('account.payment.group') + account = super()._get_account_from(related_to) + if isinstance(related_to, (Payment, PaymentGroup)): + account = related_to.journal.clearing_account + return account def _get_payment(self, origin, keywords): pool = Pool() diff --git a/modules/account_statement_rule/account.py b/modules/account_statement_rule/account.py index 02e07cf318..00171174b3 100644 --- a/modules/account_statement_rule/account.py +++ b/modules/account_statement_rule/account.py @@ -323,17 +323,23 @@ class StatementRuleLine(sequence_ordered(), ModelSQL, ModelView): currency = origin.statement.journal.currency amount = currency.round(simple_eval(decistmt(self.amount), **context)) party = self._get_party(origin, keywords) - invoice = self._get_invoice(origin, keywords) + related_to = list(filter(None, self._get_related_to(origin, keywords))) + if len(related_to) == 1: + related_to, = related_to + else: + related_to = None + related_to_party = self._get_party_from(related_to) - if invoice and party and invoice.party != party: + if related_to_party and party and related_to_party != party: return - if invoice and not party: - party = invoice.party + if related_to_party and not party: + party = related_to_party account = self.account if not account: - if invoice: - account = invoice.account + related_to_account = self._get_account_from(related_to) + if related_to_account: + account = related_to_account elif party: with Transaction().set_context(date=origin.date): if amount > Decimal('0.0'): @@ -359,7 +365,7 @@ class StatementRuleLine(sequence_ordered(), ModelSQL, ModelView): line.date = origin.date line.party = party line.account = account - line.related_to = invoice + line.related_to = related_to return line def _get_party(self, origin, keywords): @@ -411,6 +417,21 @@ class StatementRuleLine(sequence_ordered(), ModelSQL, ModelView): party = line.party return party + def _get_related_to(self, origin, keywords): + return {self._get_invoice(origin, keywords)} + + def _get_party_from(self, related_to): + pool = Pool() + Invoice = pool.get('account.invoice') + if isinstance(related_to, Invoice): + return related_to.party + + def _get_account_from(self, related_to): + pool = Pool() + Invoice = pool.get('account.invoice') + if isinstance(related_to, Invoice): + return related_to.account + def _get_invoice(self, origin, keywords): pool = Pool() Invoice = pool.get('account.invoice')