From 5093d3d0212351e0377f7d6be8e03e34ee9a337e Mon Sep 17 00:00:00 2001 From: Eduard Brahas Date: Tue, 1 Oct 2024 12:17:41 +0200 Subject: [PATCH] [FIX] sale_triple_discount: Fixed tax amount on report --- sale_triple_discount/models/sale_order.py | 75 +++++++++++++++++------ 1 file changed, 55 insertions(+), 20 deletions(-) diff --git a/sale_triple_discount/models/sale_order.py b/sale_triple_discount/models/sale_order.py index 01e8b2bfcb6f..38302035bbea 100644 --- a/sale_triple_discount/models/sale_order.py +++ b/sale_triple_discount/models/sale_order.py @@ -3,7 +3,10 @@ # Copyright 2017 - 2019 Alex Comba - Agile Business Group # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from functools import partial + from odoo import api, models +from odoo.tools import formatLang class SaleOrder(models.Model): @@ -17,24 +20,56 @@ def _amount_all(self): super()._amount_all() self.env["sale.order.line"].triple_discount_postprocess(prev_values) - def _get_tax_amount_by_group(self): + def _amount_by_group(self): + super()._amount_by_group() + # Copy/paste from standard method in sale - self.ensure_one() - res = {} - for line in self.order_line: - price_reduce = line.price_reduce # changed - taxes = line.tax_id.compute_all( - price_reduce, - quantity=line.product_uom_qty, - product=line.product_id, - partner=self.partner_shipping_id, - )["taxes"] - for tax in line.tax_id: - group = tax.tax_group_id - res.setdefault(group, 0.0) - for t in taxes: - if t["id"] == tax.id or t["id"] in tax.children_tax_ids.ids: - res[group] += t["amount"] - res = sorted(list(res.items()), key=lambda l: l[0].sequence) - res = [(line[0].name, line[1]) for line in res] - return res + for order in self: + currency = order.currency_id or order.company_id.currency_id + fmt = partial( + formatLang, + self.with_context(lang=order.partner_id.lang).env, + currency_obj=currency, + ) + res = {} + + for line in order.order_line: + # Use price_reduce for tax computation + line._get_price_reduce() + price_reduce = line.price_reduce # This line is adapted + + # Calculate taxes based on price_reduce + taxes = line.tax_id.compute_all( + price_reduce, + quantity=line.product_uom_qty, + product=line.product_id, + partner=order.partner_shipping_id, + )["taxes"] + + for tax in line.tax_id: + group = tax.tax_group_id + res.setdefault(group, {"amount": 0.0, "base": 0.0}) + + for t in taxes: + if t["id"] == tax.id or t["id"] in tax.children_tax_ids.ids: + res[group]["amount"] += t["amount"] + res[group]["base"] += t["base"] + + res = sorted(res.items(), key=lambda l: l[0].sequence) + + # Round amount and prevent -0.00 + for group_data in res: + group_data[1]["amount"] = currency.round(group_data[1]["amount"]) + 0.0 + group_data[1]["base"] = currency.round(group_data[1]["base"]) + 0.0 + + order.amount_by_group = [ + ( + line[0].name, + line[1]["amount"], + line[1]["base"], + fmt(line[1]["amount"]), + fmt(line[1]["base"]), + len(res), + ) + for line in res + ]