[FIX] account: Prevent deletion of tax lines

Improvement of the current fixes:
https://github.com/odoo/odoo/commit/ac3cf2ed59c1dd5a6c91da201a0d4093212b26ae
https://github.com/odoo/odoo/commit/feb4e839dc4e35ab16527e5e264b5cc1c0a76c8e

- A tax line is linked to a tax repartition line. If you suppose a tax with 2 repartition lines then, removing one tax line should recompute the tax line.
- There are multiple taxes configuration leading to a tax line with a zero amount. For example, python tax, fixed tax with quantity = 0, a tax with a tiny amount...etc

closes odoo/odoo#95342

X-original-commit: 33d3b17e88d17314b88264f17780d500d67d72f1
Signed-off-by: William André (wan) <wan@odoo.com>
This commit is contained in:
Laurent Smet
2022-07-06 11:49:57 +02:00
committed by william-andre
parent f1856cce24
commit e96757194c
+30 -9
View File
@@ -1214,16 +1214,37 @@ class AccountMove(models.Model):
'''
for invoice in self:
# Dispatch lines and pre-compute some aggregated values like taxes.
if (
recompute_all_taxes
or any(line.recompute_tax_line for line in invoice.line_ids)
or invoice.line_ids.tax_ids.flatten_taxes_hierarchy()._origin > invoice.line_ids.tax_line_id._origin
):
invoice.line_ids.recompute_tax_line = False
invoice._recompute_tax_lines()
expected_tax_rep_lines = set()
current_tax_rep_lines = set()
inv_recompute_all_taxes = recompute_all_taxes
for line in invoice.line_ids:
if line.recompute_tax_line:
inv_recompute_all_taxes = True
line.recompute_tax_line = False
if line.tax_repartition_line_id:
current_tax_rep_lines.add(line.tax_repartition_line_id._origin)
elif line.tax_ids:
if invoice.is_invoice(include_receipts=True):
is_refund = invoice.move_type in ('out_refund', 'in_refund')
else:
tax_type = line.tax_ids[0].type_tax_use
is_refund = (tax_type == 'sale' and line.debit) or (tax_type == 'purchase' and line.credit)
taxes = line.tax_ids.flatten_taxes_hierarchy()
if is_refund:
tax_rep_lines = taxes.refund_repartition_line_ids._origin.filtered(lambda x: x.repartition_type == "tax")
else:
tax_rep_lines = taxes.invoice_repartition_line_ids._origin.filtered(lambda x: x.repartition_type == "tax")
for tax_rep_line in tax_rep_lines:
expected_tax_rep_lines.add(tax_rep_line)
delta_tax_rep_lines = expected_tax_rep_lines - current_tax_rep_lines
if recompute_tax_base_amount:
# Compute taxes.
if inv_recompute_all_taxes:
invoice._recompute_tax_lines()
elif recompute_tax_base_amount:
invoice._recompute_tax_lines(recompute_tax_base_amount=True)
elif delta_tax_rep_lines and not self._context.get('move_reverse_cancel'):
invoice._recompute_tax_lines()
if invoice.is_invoice(include_receipts=True):
@@ -2854,7 +2875,7 @@ class AccountMove(models.Model):
move_vals_list.append(move.with_context(move_reverse_cancel=cancel)._reverse_move_vals(default_values, cancel=cancel))
reverse_moves = self.env['account.move'].create(move_vals_list)
for move, reverse_move in zip(self, reverse_moves.with_context(check_move_validity=False)):
for move, reverse_move in zip(self, reverse_moves.with_context(check_move_validity=False, move_reverse_cancel=cancel)):
# Update amount_currency if the date has changed.
if move.date != reverse_move.date:
for line in reverse_move.line_ids: