[FIX] account: Fix tax recomputation with tax cash rounding

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 <qdp@odoo.com>
Signed-off-by: Grazioso Andrea (agr) <agr@odoo.com>
This commit is contained in:
Cyril Dutrieux (cydu)
2023-05-16 13:37:30 +02:00
committed by qdp-odoo
parent 0adf2b7723
commit 26cfd920ea
3 changed files with 45 additions and 1 deletions
@@ -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,
@@ -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",
@@ -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},
], {