From 26cfd920ea57c439d7d425a2e22bf10f487d3c2d Mon Sep 17 00:00:00 2001 From: "Cyril Dutrieux (cydu)" Date: Thu, 13 Apr 2023 09:50:52 +0000 Subject: [PATCH] [FIX] account: Fix tax recomputation with tax cash rounding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Step to reproduce: 1. Set up Cash rounding for Tax: Accounting> Configuration > cash rounding 2. Set up recurrent product: Service type - Prepaid - tax VAT 7.7 3. Create an order with that product - set price of 45 (to have the rounding) > confirm order > Create an invoice 4. In the new invoice > "Other info" tab > add the rounding method 5. Change the quantity Current behavior: tax doesn't update if there is a rounding line Expected: tax should be updated. Why: What happens is that odoo trying to update the new tax amount on the wrong line (the rounded tax amount line). That's because the `existing_after` dict is poorly defined. It is defined by the `existing()` inner function of `_sync_dynamic_line` thanks to the "tax_key" of each line.​ The two account move lines (VAT line and rounding on the VAT line) share the same tax_key,​ and because of that the VAT line is overwritten by the rounding line in the dict returned by`existing()`. Then the cash rounding is recomputed from the VAT line(that still holds the outdated values) and overwrites the updated values on the rounding line. Solution: Adding the type of line in the tax_key to differentiate between a VAT line and a rounding VAT line opw-3224743 closes odoo/odoo#121426 X-original-commit: c27236d49d4f60940d79471e0ae66a0aed3c572d Signed-off-by: Quentin De Paoli Signed-off-by: Grazioso Andrea (agr) --- addons/account/models/account_move_line.py | 2 + .../test_account_move_line_tax_details.py | 41 +++++++++++++++++++ .../tests/test_early_payment_discount.py | 3 +- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/addons/account/models/account_move_line.py b/addons/account/models/account_move_line.py index 00161ddb7d0..1a63589d804 100644 --- a/addons/account/models/account_move_line.py +++ b/addons/account/models/account_move_line.py @@ -889,6 +889,7 @@ class AccountMoveLine(models.Model): 'tax_tag_ids': [(6, 0, line.tax_tag_ids.ids)], 'partner_id': line.partner_id.id, 'move_id': line.move_id.id, + 'display_type': line.display_type, }) else: line.tax_key = frozendict({'id': line.id}) @@ -933,6 +934,7 @@ class AccountMoveLine(models.Model): 'tax_tag_ids': [(6, 0, tax['tag_ids'])], 'partner_id': line.move_id.partner_id.id or line.partner_id.id, 'move_id': line.move_id.id, + 'display_type': line.display_type, }): { 'name': tax['name'], 'balance': tax['amount'] / rate, diff --git a/addons/account/tests/test_account_move_line_tax_details.py b/addons/account/tests/test_account_move_line_tax_details.py index 167b54cf2d3..b34eaba3ac5 100644 --- a/addons/account/tests/test_account_move_line_tax_details.py +++ b/addons/account/tests/test_account_move_line_tax_details.py @@ -654,6 +654,47 @@ class TestAccountTaxDetailsReport(AccountTestInvoicingCommon): ) self.assertTotalAmounts(invoice, tax_details) + def test_round_per_line_update(self): + self.env.company.tax_calculation_rounding_method = 'round_per_line' + + tax_8 = self.env['account.tax'].create({ + 'name': "tax_8", + 'amount_type': 'percent', + 'amount': 8.0, + }) + + invoice = self.env['account.move'].create({ + 'move_type': 'out_invoice', + 'partner_id': self.partner_a.id, + 'invoice_date': '2019-01-01', + 'invoice_cash_rounding_id': self.cash_rounding_b.id, + 'invoice_line_ids': [ + Command.create({ + 'name': 'line1', + 'account_id': self.company_data['default_account_revenue'].id, + 'price_unit': 45.45, + 'tax_ids': [Command.set(tax_8.ids)], + }) + ] + }) + invoice.invoice_line_ids.write({"price_unit": 4545}) + + base_lines, tax_lines = self._dispatch_move_lines(invoice) + + tax_details = self._get_tax_details() + self.assertTaxDetailsValues( + tax_details, + [ + { + 'base_line_id': base_lines[0].id, + 'tax_line_id': tax_lines[0].id, + 'base_amount': -4545.0, + 'tax_amount': -363.6, + }, + ], + ) + self.assertTotalAmounts(invoice, tax_details) + def test_partitioning_lines_by_moves(self): tax_20_affect = self.env['account.tax'].create({ 'name': "tax_20_affect", diff --git a/addons/account/tests/test_early_payment_discount.py b/addons/account/tests/test_early_payment_discount.py index 00fe85a6826..4b2720fca1f 100644 --- a/addons/account/tests/test_early_payment_discount.py +++ b/addons/account/tests/test_early_payment_discount.py @@ -87,7 +87,8 @@ class TestAccountEarlyPaymentDiscount(AccountTestInvoicingCommon): {'display_type': 'epd', 'balance': -100.0}, {'display_type': 'epd', 'balance': 100.0}, {'display_type': 'product', 'balance': -1000.0}, - {'display_type': 'tax', 'balance': -135.0}, + {'display_type': 'tax', 'balance': -150}, + {'display_type': 'tax', 'balance': 15.0}, {'display_type': 'tax', 'balance': -0.05}, {'display_type': 'payment_term', 'balance': 1135.05}, ], {