changeset e0e0ce0953cd in modules/account_invoice_defer:default
details:
https://hg.tryton.org/modules/account_invoice_defer?cmd=changeset&node=e0e0ce0953cd
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
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 bc9a07071482 -r e0e0ce0953cd account.py
--- a/account.py Sat Aug 21 09:09:55 2021 +0200
+++ b/account.py Thu Oct 28 08:57:33 2021 +0200
@@ -201,6 +201,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')
@@ -220,6 +223,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')
@@ -276,7 +290,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')
@@ -286,18 +300,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
@@ -309,6 +319,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
@@ -319,6 +335,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 bc9a07071482 -r e0e0ce0953cd tests/scenario_account_invoice_defer.rst
--- a/tests/scenario_account_invoice_defer.rst Sat Aug 21 09:09:55 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 bc9a07071482 -r e0e0ce0953cd tests/tools.py
--- a/tests/tools.py Sat Aug 21 09:09:55 2021 +0200
+++ b/tests/tools.py Thu Oct 28 08:57:33 2021 +0200
@@ -17,6 +17,7 @@
if not company:
company = get_company(config=config)
+ 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)