diff --git a/CHANGELOG b/CHANGELOG index 7d4ef78732..ea05b0ac09 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Use the date of the statement line as date for the clearing move + Version 5.4.0 - 2019-11-04 * Bug fixes (see mercurial logs for details) * Relax Account domain on Payment diff --git a/statement.py b/statement.py index be8e5d42f4..8057bbd743 100644 --- a/statement.py +++ b/statement.py @@ -1,5 +1,7 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. +from collections import defaultdict + from trytond.pool import PoolMeta, Pool from trytond.model import fields from trytond.pyson import Eval, If, Bool @@ -17,8 +19,8 @@ class Statement(metaclass=PoolMeta): moves = super(Statement, cls).create_move(statements) - to_success = set() - to_fail = set() + to_success = defaultdict(set) + to_fail = defaultdict(set) for move, statement, lines in moves: for line in lines: if line.payment: @@ -30,15 +32,19 @@ class Statement(metaclass=PoolMeta): else: continue if (kind == 'receivable') == (line.amount >= 0): - to_success.update(payments) + to_success[line.date].update(payments) else: - to_fail.update(payments) + to_fail[line.date].update(payments) # The failing should be done last because success is usually not a # definitive state if to_success: - Payment.succeed(Payment.browse(to_success)) + for date, payments in to_success.items(): + with Transaction().set_context(clearing_date=date): + Payment.succeed(Payment.browse(payments)) if to_fail: - Payment.fail(Payment.browse(to_fail)) + for date, payments in to_fail.items(): + with Transaction().set_context(clearing_date=date): + Payment.fail(Payment.browse(payments)) for move, statement, lines in moves: assert len({l.payment for l in lines}) == 1 diff --git a/tests/scenario_account_payment_clearing.rst b/tests/scenario_account_payment_clearing.rst index eb1ac7a1cc..6acc25714c 100644 --- a/tests/scenario_account_payment_clearing.rst +++ b/tests/scenario_account_payment_clearing.rst @@ -445,7 +445,7 @@ Create statement for the payment:: ... journal=statement_journal, ... start_balance=Decimal('0.00'), ... end_balance=Decimal('-50.00')) - >>> line = statement.lines.new(date=today) + >>> line = statement.lines.new(date=yesterday) >>> line.payment = payment >>> line.party == supplier True @@ -471,3 +471,5 @@ Validate statement and check the payment is confirmed:: >>> debit_line, = [l for l in payment.clearing_move.lines if l.debit > 0] >>> debit_line.debit Decimal('50.00') + >>> debit_line.date == yesterday + True