Compute related to from statement rules all at once
This ensures that only unambiguous value is set and only once. Closes #12123
This commit is contained in:
1 parent
b0b173f84d
commit
2c38fbc5c4
2 files changed
+50
-16
No files matched your search
@@ -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()
|
||||
|
||||
@@ -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')
|
||||
|
||||
Reference in new issue
Block a user