From eda1f8e7de07d3d25c8b12cf660e744b2ef0b2c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 24 Dec 2024 14:54:47 +0100 Subject: [PATCH] Merge tax line with the same key but computed separately Closes #13777 --- modules/account/tax.py | 9 +++++--- modules/account/tests/test_module.py | 31 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/modules/account/tax.py b/modules/account/tax.py index 66f5c44ea2..ea8d5678e8 100644 --- a/modules/account/tax.py +++ b/modules/account/tax.py @@ -1312,9 +1312,12 @@ class TaxableMixin(object): self._round_taxes(current_taxes) if tax_rounding == 'document': self._round_taxes(taxes) - if all_taxes.keys() & taxes.keys(): - raise ValueError("Duplicate tax line keys") - all_taxes.update(taxes) + for key, taxline in taxes.items(): + if key not in all_taxes: + all_taxes[key] = taxline + else: + all_taxes[key]['base'] += taxline['base'] + all_taxes[key]['amount'] += taxline['amount'] return all_taxes diff --git a/modules/account/tests/test_module.py b/modules/account/tests/test_module.py index aa188f9120..04fc119634 100644 --- a/modules/account/tests/test_module.py +++ b/modules/account/tests/test_module.py @@ -1297,6 +1297,37 @@ class AccountTestCase( self.assertEqual(tax['base'], Decimal('100.00')) self.assertEqual(tax['amount'], Decimal('20.02')) + @with_transaction() + def test_taxable_mixin_many_taxable_lines(self): + "Test TaxableMixin with many taxable lines" + pool = Pool() + Tax = pool.get('account.tax') + currency = create_currency('cur') + + company = create_company() + with set_company(company): + create_chart(company, tax=True) + tax2, = Tax.search([]) + tax1, = Tax.copy([tax2], default={'rate': Decimal('0.1')}) + + taxable = self.Taxable( + currency=currency, + taxable_lines=[ + ([tax1], Decimal('10'), 1, None), + ([tax1, tax2], Decimal('20'), 1, None), + ], + company=company) + + taxes = taxable._get_taxes() + + self.assertEqual(len(taxes), 2) + tax_line1, = [t for t in taxes if t['tax'] == tax1.id] + tax_line2, = [t for t in taxes if t['tax'] == tax2.id] + self.assertEqual(tax_line1['base'], Decimal('30.00')) + self.assertEqual(tax_line1['amount'], Decimal('3.00')) + self.assertEqual(tax_line2['base'], Decimal('20.00')) + self.assertEqual(tax_line2['amount'], Decimal('4.00')) + @with_transaction() def test_taxable_mixin_tax_residual_rounding(self): "Test TaxableMixin for rounding with residual amount"