[FIX] account: synchronize Invoice Line Dates with Invoice Date

Currently, when the `invoice_date` of an invoice is updated (triggering
the recomputation of `date`) and if a system flush occurs before any
line's date is accessed, the invoice lines' dates do not get updated.
The following test illustrates this issue:

```py
move = self.init_invoice(
    move_type='in_invoice',
    partner=self.partner_a,
    amounts=[1000.0],
)

move.invoice_date = fields.Date.from_string('2024-01-01')
self.env.flush_all()

for line in move.line_ids:
    self.assertEqual(line.date, move.date) # will fail
```

Cause
-----
The `date` of a move is a computed field dependent on the move's
`invoice_date`. The `date` of a move line is a related field, pointing
to its parent move's `date` (note: related fields are computed fields).
During a flush, the system recomputes all fields that need to be. Here,
the system first processes 'account.move.date' and calls its computation
(`_compute_date`). However, the `_affect_tax_report()` call within
`_compute_date` triggers a recalculation of `account.move.line.date`,
but as this happens within `_compute_date`, the invoice lines' `date` is
recalculated using the old invoice `date`.

Fix
---
Force a recalculation of the invoice lines' dates whenever the invoice's
date is changed.

opw-3759472
opw-3875405
opw-3872006
opw-3884013

closes odoo/odoo#163530

X-original-commit: e9d955c5a52902cdac86c6285204c54876c68eac
Signed-off-by: William André (wan) <wan@odoo.com>
Signed-off-by: Séna Serge Nshimiyimana (sesn) <sesn@odoo.com>
This commit is contained in:
sesn-odoo
2024-04-26 18:54:36 +00:00
parent 0d5bf820c3
commit b9491abd1f
2 changed files with 15 additions and 0 deletions
+2
View File
@@ -671,6 +671,8 @@ class AccountMove(models.Model):
accounting_date = move._get_accounting_date(move.invoice_date, move._affect_tax_report())
if accounting_date and accounting_date != move.date:
move.date = accounting_date
# _affect_tax_report may trigger premature recompute of line_ids.date
self.env.add_to_compute(move.line_ids._fields['date'], move.line_ids)
# might be protected because `_get_accounting_date` requires the `name`
self.env.add_to_compute(self._fields['name'], move)
@@ -4079,3 +4079,16 @@ class TestAccountMoveOutInvoiceOnchanges(AccountTestInvoicingCommon):
move_reversal.refund_moves()
self.assertEqual(error_catcher.exception.args[0], "All selected moves for reversal must belong to the same company.")
def test_update_lines_date_when_invoice_date_changes(self):
move = self.init_invoice(
move_type='in_invoice',
partner=self.partner_a,
amounts=[1000.0],
)
move.invoice_date = fields.Date.from_string('2024-01-01')
self.env.flush_all()
for line in move.line_ids:
self.assertEqual(line.date, move.date)