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
This commit is contained in:
Cédric Krier
2021-10-28 08:57:33 +02:00
parent 766e905110
commit c525696c68
3 changed files with 32 additions and 12 deletions
+27 -10
View File
@@ -201,6 +201,9 @@ class InvoiceDeferred(Workflow, ModelSQL, ModelView):
# 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')
@@ -219,6 +222,17 @@ class InvoiceDeferred(Workflow, ModelSQL, ModelView):
deferral=deferral.rec_name))
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()
@@ -276,7 +290,7 @@ class InvoiceDeferred(Workflow, ModelSQL, ModelView):
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 @@ class InvoiceDeferred(Workflow, ModelSQL, ModelView):
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 @@ class InvoiceDeferred(Workflow, ModelSQL, ModelView):
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 @@ class InvoiceDeferred(Workflow, ModelSQL, ModelView):
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
+2 -2
View File
@@ -104,7 +104,7 @@ Check moves on invoice deferred::
>>> 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 @@ Check moves on invoice deferred::
>>> deferral.state
'closed'
>>> len(deferral.moves)
17
18
>>> accounts['deferred_expense'].reload()
>>> accounts['deferred_expense'].balance
Decimal('0.00')
+3
View File
@@ -17,6 +17,7 @@ def add_deferred_accounts(accounts, company=None, config=None):
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 @@ def add_deferred_accounts(accounts, company=None, config=None):
], 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)