diff --git a/account_invoice_triple_discount/__manifest__.py b/account_invoice_triple_discount/__manifest__.py index bf636e2dd0ec..7af62e57f611 100644 --- a/account_invoice_triple_discount/__manifest__.py +++ b/account_invoice_triple_discount/__manifest__.py @@ -3,7 +3,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). { "name": "Account Invoice Triple Discount", - "version": "15.0.1.0.0", + "version": "16.0.1.0.0", "category": "Accounting & Finance", "author": "QubiQ, Tecnativa, Odoo Community Association (OCA)", "website": "https://github.com/OCA/account-invoicing", diff --git a/account_invoice_triple_discount/models/__init__.py b/account_invoice_triple_discount/models/__init__.py index 0d5ab6a2fc6c..8795b3bea648 100644 --- a/account_invoice_triple_discount/models/__init__.py +++ b/account_invoice_triple_discount/models/__init__.py @@ -1,2 +1 @@ -from . import account_move from . import account_move_line diff --git a/account_invoice_triple_discount/models/account_move.py b/account_invoice_triple_discount/models/account_move.py deleted file mode 100644 index 89237317e442..000000000000 --- a/account_invoice_triple_discount/models/account_move.py +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright 2017 Tecnativa - David Vidal -# Copyright 2017 Tecnativa - Pedro M. Baeza -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). - -from odoo import models - - -class AccountMove(models.Model): - - _inherit = "account.move" - - def _recompute_tax_lines(self, **kwargs): - """ - As the taxes are recalculated based on a single discount, we need to - simulate a multiple discount by changing the unit price. Values are - restored after the original process is done - """ - old_values_by_line_id = {} - digits = self.line_ids._fields["price_unit"]._digits - self.line_ids._fields["price_unit"]._digits = (16, 16) - for line in self.line_ids: - aggregated_discount = line._compute_aggregated_discount(line.discount) - old_values_by_line_id[line.id] = { - "price_unit": line.price_unit, - "discount": line.discount, - } - price_unit = line.price_unit * (1 - aggregated_discount / 100) - line.update({"price_unit": price_unit, "discount": 0}) - self.line_ids._fields["price_unit"]._digits = digits - res = super(AccountMove, self)._recompute_tax_lines(**kwargs) - for line in self.line_ids: - if line.id not in old_values_by_line_id: - continue - line.update(old_values_by_line_id[line.id]) - return res - - def _has_discount(self): - self.ensure_one() - return any( - [ - line._compute_aggregated_discount(line.discount) > 0 - for line in self.invoice_line_ids - ] - ) diff --git a/account_invoice_triple_discount/models/account_move_line.py b/account_invoice_triple_discount/models/account_move_line.py index ac8506529c76..e0ee9f7b79f3 100644 --- a/account_invoice_triple_discount/models/account_move_line.py +++ b/account_invoice_triple_discount/models/account_move_line.py @@ -54,30 +54,73 @@ def create(self, values_list): record.write(old_values[index]) return records - @api.onchange( + @api.depends( + "quantity", "discount", "price_unit", "tax_ids", - "quantity", + "currency_id", "discount2", "discount3", ) - def _onchange_price_subtotal(self): - return super(AccountMoveLine, self)._onchange_price_subtotal() + def _compute_totals(self): + """ + As the totals are recalculated based on a single discount, we need to + simulate a multiple discount by changing the discount value. Values are + restored after the original process is done + """ + old_values_by_line_id = {} + digits = self._fields["discount"]._digits + self._fields["discount"]._digits = (16, 16) + for line in self: + aggregated_discount = line._compute_aggregated_discount(line.discount) + old_values_by_line_id[line.id] = {"discount": line.discount} + line.update({"discount": aggregated_discount}) + res = super(AccountMoveLine, self)._compute_totals() + self._fields["discount"]._digits = digits + for line in self: + if line.id not in old_values_by_line_id: + continue + line.update(old_values_by_line_id[line.id]) + return res - def _get_price_total_and_subtotal(self, **kwargs): - self.ensure_one() - kwargs["discount"] = self._compute_aggregated_discount( - kwargs.get("discount") or self.discount - ) - return super(AccountMoveLine, self)._get_price_total_and_subtotal(**kwargs) + @api.depends( + "tax_ids", + "currency_id", + "partner_id", + "analytic_distribution", + "balance", + "partner_id", + "move_id.partner_id", + "price_unit", + "discount2", + "discount3", + ) + def _compute_all_tax(self): + """ + As the taxes are recalculated based on a single discount, we need to + simulate a multiple discount by changing discount value. Values are + restored after the original process is done + """ + digits = self._fields["discount"]._digits + self._fields["discount"]._digits = (16, 16) + old_values_by_line_id = {} + for line in self: + aggregated_discount = line._compute_aggregated_discount(line.discount) + old_values_by_line_id[line.id] = {"discount": line.discount} + line.update({"discount": aggregated_discount}) + res = super(AccountMoveLine, self)._compute_all_tax() + self._fields["discount"]._digits = digits + for line in self: + if line.id not in old_values_by_line_id: + continue + line.update(old_values_by_line_id[line.id]) + return res - def _get_fields_onchange_balance(self, **kwargs): - self.ensure_one() - kwargs["discount"] = self._compute_aggregated_discount( - kwargs.get("discount") or self.discount - ) - return super(AccountMoveLine, self)._get_fields_onchange_balance(**kwargs) + def _convert_to_tax_base_line_dict(self): + res = super(AccountMoveLine, self)._convert_to_tax_base_line_dict() + res["discount"] = self._compute_aggregated_discount(res["discount"]) + return res def _compute_aggregated_discount(self, base_discount): self.ensure_one() diff --git a/account_invoice_triple_discount/tests/test_invoice_triple_discount.py b/account_invoice_triple_discount/tests/test_invoice_triple_discount.py index 06e9293856e1..7f672107d236 100644 --- a/account_invoice_triple_discount/tests/test_invoice_triple_discount.py +++ b/account_invoice_triple_discount/tests/test_invoice_triple_discount.py @@ -9,10 +9,10 @@ class TestInvoiceTripleDiscount(TransactionCase): @classmethod def setUpClass(cls): super(TestInvoiceTripleDiscount, cls).setUpClass() + cls.env.user.groups_id += cls.env.ref("product.group_discount_per_so_line") cls.Account = cls.env["account.account"] cls.AccountMove = cls.env["account.move"] cls.AccountTax = cls.env["account.tax"] - cls.AccountType = cls.env["account.account.type"] cls.Partner = cls.env["res.partner"] cls.Journal = cls.env["account.journal"] @@ -26,14 +26,11 @@ def setUpClass(cls): "country_id": cls.env.ref("base.us").id, } ) - cls.account_type = cls.AccountType.create( - {"name": "Test", "type": "receivable", "internal_group": "income"} - ) cls.account = cls.Account.create( { "name": "Test account", "code": "TEST", - "user_type_id": cls.account_type.id, + "account_type": "asset_receivable", "reconcile": True, } ) diff --git a/account_invoice_triple_discount/views/account_move.xml b/account_invoice_triple_discount/views/account_move.xml index dbf287266a04..c57fa78a30b0 100644 --- a/account_invoice_triple_discount/views/account_move.xml +++ b/account_invoice_triple_discount/views/account_move.xml @@ -9,30 +9,15 @@ expr="//field[@name='invoice_line_ids']//tree//field[@name='discount']" position="after" > - - + + - - - - - - + +