diff --git a/addons/account/models/account_move.py b/addons/account/models/account_move.py index 79614cc6702..4d7eaae350f 100644 --- a/addons/account/models/account_move.py +++ b/addons/account/models/account_move.py @@ -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: