[FIX] account: assign a vehicle to a posted journal entry

steps to reproduce:

- Go to Accounting > Accounting > Actions > Lock Dates
- Set a Tax Return Lock Date to a date of the previous month
- Create and confirm vendor Bill with a taxed line of positive price
- Go to Accounting > Reporting > Audit Reports > Journal Report
- Hover over the Vendor Bills and click on "journal items"
- Add the optional vehicle field to the recods
- Select your expense line and try to add a vehicle

> Invalid Operation "You cannot modify the taxes related to a posted
  journal item..."

Cause of the issue:

Modifying the "vehicle_id" of the account move line will trigger a call
of the `_sync_dynamic_line` method in order to modify other account move
line linked to the same account move:
https://github.com/odoo/odoo/blob/6a4808802c67f98696338d0b6f6a07934c8003fb/addons/account/models/account_move.py#L2285-L2287
During the call of this write method, the account move line that we
did not directly modified will not be excluded:
https://github.com/odoo/odoo/blob/6a4808802c67f98696338d0b6f6a07934c8003fb/addons/account/models/account_move_line.py#L1571
as `..._field_will_change(line, vals,"vehicule_id")` will be `True`.
The error will therefore be raised two lines later because a 'tax_id' is
present in vals as a 'tax_id' was set on our related account move line.

Expected behaviour:

Since the 'tax_id' present in vals is the same as the one already set on
our account move line, we are not modifying the taxes related to a
posted journal item and we should not raise the error.

opw-3810718

closes odoo/odoo#161387

X-original-commit: 3cd87c61c852eef96195a39a4011a6baa39e3078
Signed-off-by: Claire Bretton (clbr) <clbr@odoo.com>
This commit is contained in:
lase@odoo.com
2024-04-11 08:12:58 +00:00
parent 3fa4d2d3ad
commit 61231577a2
+2 -3
View File
@@ -1572,9 +1572,8 @@ class AccountMoveLine(models.Model):
line_to_write -= line
continue
if line.parent_state == 'posted':
if any(key in vals for key in ('tax_ids', 'tax_line_id')):
raise UserError(_('You cannot modify the taxes related to a posted journal item, you should reset the journal entry to draft to do so.'))
if line.parent_state == 'posted' and any(self.env['account.move']._field_will_change(line, vals, field_name) for field_name in ('tax_ids', 'tax_line_id')):
raise UserError(_('You cannot modify the taxes related to a posted journal item, you should reset the journal entry to draft to do so.'))
# Check the lock date.
if line.parent_state == 'posted' and any(self.env['account.move']._field_will_change(line, vals, field_name) for field_name in protected_fields['fiscal']):