changeset 8a76fe602e6e in modules/account_invoice_defer:6.0 details: https://hg.tryton.org/modules/account_invoice_defer?cmd=changeset&node=8a76fe602e6e description: Ensure always to post to defer account the invoice amount
If the deferred period does not contain the invoice period, we must create a move for this period when starting to run. issue10871 review375801002 (grafted from e0e0ce0953cd1717dcae060676facb1fa5ab9807) diffstat: account.py | 37 +++++++++++++++++++++++-------- tests/scenario_account_invoice_defer.rst | 4 +- tests/tools.py | 3 ++ 3 files changed, 32 insertions(+), 12 deletions(-) diffs (131 lines): diff -r 5e7067912965 -r 8a76fe602e6e account.py --- a/account.py Mon May 03 15:50:27 2021 +0200 +++ b/account.py Thu Oct 28 08:57:33 2021 +0200 @@ -206,6 +206,9 @@ # Set state before create moves to pass assert cls.write(deferrals, {'state': 'running'}) cls.create_moves(deferrals) + # defer_amount is called after create_moves to be sure that + # create_moves call get_move with the invoice period if needed. + cls.defer_amount(deferrals) @classmethod @Workflow.transition('closed') @@ -225,6 +228,17 @@ return super().delete(deferrals) @classmethod + def defer_amount(cls, deferrals): + pool = Pool() + Move = pool.get('account.move') + moves = [] + for deferral in deferrals: + assert deferral.state == 'running' + moves.append(deferral.get_move()) + Move.save(moves) + Move.post(moves) + + @classmethod def create_moves(cls, deferrals): pool = Pool() Period = pool.get('account.period') @@ -281,7 +295,7 @@ l.credit for m in self.moves for l in m.lines if m.period != period) - def get_move(self, period): + def get_move(self, period=None): pool = Pool() Move = pool.get('account.move') Line = pool.get('account.move.line') @@ -291,18 +305,14 @@ company=self.company, origin=self, journal=self.journal, - period=period, - date=period.start_date, ) invoice = self.invoice_line.invoice - days = ( - min(period.end_date, self.end_date) - - max(period.start_date, self.start_date)).days + 1 - amount = self.company.currency.round(self.amount_daily * days) - income = Line(account=self.invoice_line.account.current(move.date)) - if period == invoice.move.period: - amount = self.amount - amount + income = Line() + if period is None: + move.period = invoice.move.period + move.date = invoice.move.date + amount = self.amount if amount >= 0: if invoice.type == 'out': income.debit, income.credit = amount, 0 @@ -314,6 +324,12 @@ else: income.debit, income.credit = -amount, 0 else: + move.period = period + move.date = period.start_date + days = ( + min(period.end_date, self.end_date) + - max(period.start_date, self.start_date)).days + 1 + amount = self.company.currency.round(self.amount_daily * days) if amount >= 0: if invoice.type == 'out': income.debit, income.credit = 0, amount @@ -324,6 +340,7 @@ income.debit, income.credit = -amount, 0 else: income.debit, income.credit = 0, -amount + income.account = self.invoice_line.account.current(move.date) if income.account.party_required: income.party = invoice.party diff -r 5e7067912965 -r 8a76fe602e6e tests/scenario_account_invoice_defer.rst --- a/tests/scenario_account_invoice_defer.rst Mon May 03 15:50:27 2021 +0200 +++ b/tests/scenario_account_invoice_defer.rst Thu Oct 28 08:57:33 2021 +0200 @@ -104,7 +104,7 @@ >>> deferral.state 'running' >>> len(deferral.moves) - 12 + 13 >>> accounts['deferred_expense'].reload() >>> accounts['deferred_expense'].balance in {Decimal('270'), Decimal('271')} True @@ -125,7 +125,7 @@ >>> deferral.state 'closed' >>> len(deferral.moves) - 17 + 18 >>> accounts['deferred_expense'].reload() >>> accounts['deferred_expense'].balance Decimal('0.00') diff -r 5e7067912965 -r 8a76fe602e6e tests/tools.py --- a/tests/tools.py Mon May 03 15:50:27 2021 +0200 +++ b/tests/tools.py Thu Oct 28 08:57:33 2021 +0200 @@ -17,6 +17,7 @@ if not company: company = get_company() + root, = Account.find([('parent', '=', None)], limit=1) asset_type, = AccountType.find([ ('statement', '=', 'balance'), ('name', '=', "Asset"), @@ -29,11 +30,13 @@ ], limit=1) accounts['deferred_revenue'] = Account( + parent=root, name="Deferred Revenue", type=asset_type, company=company) accounts['deferred_revenue'].save() accounts['deferred_expense'] = Account( + parent=root, name="Deferred Expense", type=liability_type, company=company)