From 577eb578b01782ecc189dfb98ed2fd9723f81f7a Mon Sep 17 00:00:00 2001 From: Julien Van Roy Date: Mon, 31 Oct 2022 13:32:54 +0000 Subject: [PATCH] [REF] point_of_sale: remove the `real_amount` field To be able to compute the taxes offline, the POS JS code mimicked the `compute_all` function in `account_tax.py`. At some point, the POS added an extra field on `account.tax` (see: https://github.com/odoo/odoo/commit/8fb53c53c3128e8cea7ef20b9ab4946ac2f9b7d9): `real_amount` which is defined as: real_amount = tax.amount * sum(tax.repartition_line.factor) Then, when fetching the data from the server to the client, the `real_amount` was passed in place of the `amount`. But then, the `amount` field in the JS is no longer the same as in the python code and it is very error prone. In addition, it's easy to get rid of this extra field by using the sum of the factors from the repartition lines. Now, we pass the sum(tax.repartition_lines.factor_percent)/100, which is equivalent to sum(tax.repartition_line.factor) to the client, and use it in the `compute_all` as done in the python function, and leave `tax.amount` untouched. Both `compute_all` functions now operate using the same `tax.amount` and we get rid of the `real_amount` field. closes odoo/odoo#112063 Related: odoo/upgrade#4307 Signed-off-by: Trinh Jacky (trj) --- addons/account/models/account_tax.py | 8 -------- addons/point_of_sale/models/pos_session.py | 18 +++++++++++++----- addons/point_of_sale/static/src/js/models.js | 18 ++++++++++-------- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/addons/account/models/account_tax.py b/addons/account/models/account_tax.py index f429629b087..dab9587751b 100644 --- a/addons/account/models/account_tax.py +++ b/addons/account/models/account_tax.py @@ -114,7 +114,6 @@ class AccountTax(models.Model): sequence = fields.Integer(required=True, default=1, help="The sequence field is used to define order in which the tax lines are applied.") amount = fields.Float(required=True, digits=(16, 4), default=0.0) - real_amount = fields.Float(string='Real amount to apply', compute='_compute_real_amount', store=True) description = fields.Char(string='Label on Invoices') price_include = fields.Boolean(string='Included in Price', default=False, help="Check this if the price you use on the product and invoices includes this tax.") @@ -321,13 +320,6 @@ class AccountTax(models.Model): if self.price_include: self.include_base_amount = True - @api.depends('invoice_repartition_line_ids', 'amount', 'invoice_repartition_line_ids.factor') - def _compute_real_amount(self): - for tax in self: - tax_repartition_lines = tax.invoice_repartition_line_ids.filtered(lambda x: x.repartition_type == 'tax') - total_factor = sum(tax_repartition_lines.mapped('factor')) - tax.real_amount = tax.amount * total_factor - def _compute_amount(self, base_amount, price_unit, quantity=1.0, product=None, partner=None, fixed_multiplicator=1): """ Returns the amount of a single tax. base_amount is the actual amount on which the tax is applied, which is price_unit * quantity eventually affected by previous taxes (if tax is include_base_amount XOR price_include) diff --git a/addons/point_of_sale/models/pos_session.py b/addons/point_of_sale/models/pos_session.py index 6378fd9acee..0363e667eb3 100644 --- a/addons/point_of_sale/models/pos_session.py +++ b/addons/point_of_sale/models/pos_session.py @@ -1716,18 +1716,26 @@ class PosSession(models.Model): 'search_params': { 'domain': [('company_id', '=', self.company_id.id)], 'fields': [ - 'name', 'real_amount', 'price_include', 'include_base_amount', 'is_base_affected', - 'amount_type', 'children_tax_ids' + 'name', 'price_include', 'include_base_amount', 'is_base_affected', + 'amount_type', 'children_tax_ids', 'amount', 'id' ], }, } def _get_pos_ui_account_tax(self, params): taxes = self.env['account.tax'].search_read(**params['search_params']) - # TODO: rename amount to real_amount in front end + + # Add the 'sum_repartition_factor' as needed in the compute_all + # Note that the factor = factor_percent/100 + groups = self.env['account.tax.repartition.line'].read_group( + domain=[('invoice_tax_id', 'in', tuple([t['id'] for t in taxes])), ('repartition_type', '=', 'tax')], + fields=["factor_percent:sum"], + groupby=["invoice_tax_id"], + ) + tax_id_to_factor_sum = {g['invoice_tax_id'][0]: g['factor_percent']/100 for g in groups} for tax in taxes: - tax['amount'] = tax['real_amount'] - del tax['real_amount'] + tax['sum_repartition_factor'] = tax_id_to_factor_sum[tax['id']] + return taxes def _loader_params_pos_session(self): diff --git a/addons/point_of_sale/static/src/js/models.js b/addons/point_of_sale/static/src/js/models.js index 297925478c8..44ef48be33c 100644 --- a/addons/point_of_sale/static/src/js/models.js +++ b/addons/point_of_sale/static/src/js/models.js @@ -1197,11 +1197,11 @@ export class PosGlobalState extends PosModel { } if (tax.price_include) { if (tax.amount_type === "percent") { - incl_percent_amount += tax.amount; + incl_percent_amount += tax.amount * tax.sum_repartition_factor; } else if (tax.amount_type === "division") { - incl_division_amount += tax.amount; + incl_division_amount += tax.amount * tax.sum_repartition_factor; } else if (tax.amount_type === "fixed") { - incl_fixed_amount += Math.abs(quantity) * tax.amount; + incl_fixed_amount += Math.abs(quantity) * tax.amount * tax.sum_repartition_factor; } else { var tax_amount = self._compute_all(tax, base, quantity); incl_fixed_amount += tax_amount; @@ -1241,7 +1241,8 @@ export class PosGlobalState extends PosModel { if ( !skip_checkpoint && tax.price_include && - total_included_checkpoints[i] !== undefined + total_included_checkpoints[i] !== undefined && + tax.sum_repartition_factor != 0 ) { var tax_amount = total_included_checkpoints[i] - (base + cumulated_tax_included_amount); @@ -1251,26 +1252,27 @@ export class PosGlobalState extends PosModel { } tax_amount = round_pr(tax_amount, currency_rounding); + var factorized_tax_amount = round_pr(tax_amount * tax.sum_repartition_factor, currency_rounding) if (tax.price_include && total_included_checkpoints[i] === undefined) { - cumulated_tax_included_amount += tax_amount; + cumulated_tax_included_amount += factorized_tax_amount; } taxes_vals.push({ id: tax.id, name: tax.name, - amount: sign * tax_amount, + amount: sign * factorized_tax_amount, base: sign * round_pr(tax_base_amount, currency_rounding), }); if (tax.include_base_amount) { - base += tax_amount; + base += factorized_tax_amount; if (!tax.price_include) { skip_checkpoint = true; } } - total_included += tax_amount; + total_included += factorized_tax_amount; i += 1; });