diff --git a/account_invoice_fixed_discount/__manifest__.py b/account_invoice_fixed_discount/__manifest__.py index a278b84b6d75..f7bb01cbd06e 100644 --- a/account_invoice_fixed_discount/__manifest__.py +++ b/account_invoice_fixed_discount/__manifest__.py @@ -4,7 +4,7 @@ { "name": "Account Fixed Discount", "summary": "Allows to apply fixed amount discounts in invoices.", - "version": "15.0.1.0.0", + "version": "16.0.1.0.0", "category": "Accounting & Finance", "website": "https://github.com/OCA/account-invoicing", "author": "ForgeFlow, Odoo Community Association (OCA)", diff --git a/account_invoice_fixed_discount/models/__init__.py b/account_invoice_fixed_discount/models/__init__.py index 44b2c7a5e83a..cc589d1c321e 100644 --- a/account_invoice_fixed_discount/models/__init__.py +++ b/account_invoice_fixed_discount/models/__init__.py @@ -1,3 +1,3 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) -from . import account_move +from . import account_move_line diff --git a/account_invoice_fixed_discount/models/account_move.py b/account_invoice_fixed_discount/models/account_move.py deleted file mode 100644 index 553223ccdff5..000000000000 --- a/account_invoice_fixed_discount/models/account_move.py +++ /dev/null @@ -1,124 +0,0 @@ -# Copyright 2017 ForgeFlow S.L. -# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) - -from odoo import _, api, fields, models -from odoo.exceptions import ValidationError - - -class AccountMove(models.Model): - _inherit = "account.move" - - def _recompute_tax_lines( - self, recompute_tax_base_amount=False, tax_rep_lines_to_recompute=None - ): - vals = {} - for line in self.invoice_line_ids.filtered("discount_fixed"): - vals[line] = {"price_unit": line.price_unit} - price_unit = line.price_unit - line.discount_fixed - line.update({"price_unit": price_unit}) - res = super(AccountMove, self)._recompute_tax_lines( - recompute_tax_base_amount=recompute_tax_base_amount, - tax_rep_lines_to_recompute=tax_rep_lines_to_recompute, - ) - for line in vals.keys(): - line.update(vals[line]) - return res - - -class AccountMoveLine(models.Model): - _inherit = "account.move.line" - - discount_fixed = fields.Float( - string="Discount (Fixed)", - digits="Product Price", - default=0.00, - help="Fixed amount discount.", - ) - - @api.onchange("discount") - def _onchange_discount(self): - if self.discount: - self.discount_fixed = 0.0 - - @api.onchange("discount_fixed") - def _onchange_discount_fixed(self): - if self.discount_fixed: - self.discount = 0.0 - - @api.constrains("discount", "discount_fixed") - def _check_only_one_discount(self): - for rec in self: - for line in rec: - if line.discount and line.discount_fixed: - raise ValidationError( - _("You can only set one type of discount per line.") - ) - - @api.onchange("quantity", "discount", "price_unit", "tax_ids", "discount_fixed") - def _onchange_price_subtotal(self): - return super(AccountMoveLine, self)._onchange_price_subtotal() - - @api.model - def _get_price_total_and_subtotal_model( - self, - price_unit, - quantity, - discount, - currency, - product, - partner, - taxes, - move_type, - ): - if self.discount_fixed != 0: - discount = ((self.discount_fixed) / price_unit) * 100 or 0.00 - return super(AccountMoveLine, self)._get_price_total_and_subtotal_model( - price_unit, quantity, discount, currency, product, partner, taxes, move_type - ) - - @api.model - def _get_fields_onchange_balance_model( - self, - quantity, - discount, - amount_currency, - move_type, - currency, - taxes, - price_subtotal, - force_computation=False, - ): - if self.discount_fixed != 0: - discount = ((self.discount_fixed) / self.price_unit) * 100 or 0.00 - return super(AccountMoveLine, self)._get_fields_onchange_balance_model( - quantity, - discount, - amount_currency, - move_type, - currency, - taxes, - price_subtotal, - force_computation=force_computation, - ) - - @api.model_create_multi - def create(self, vals_list): - prev_discount = [] - for vals in vals_list: - if vals.get("discount_fixed"): - prev_discount.append( - {"discount_fixed": vals.get("discount_fixed"), "discount": 0.00} - ) - fixed_discount = ( - vals.get("discount_fixed") / vals.get("price_unit") - ) * 100 - vals.update({"discount": fixed_discount, "discount_fixed": 0.00}) - elif vals.get("discount"): - prev_discount.append({"discount": vals.get("discount")}) - res = super(AccountMoveLine, self).create(vals_list) - i = 0 - for rec in res: - if rec.discount and prev_discount: - rec.write(prev_discount[i]) - i += 1 - return res diff --git a/account_invoice_fixed_discount/models/account_move_line.py b/account_invoice_fixed_discount/models/account_move_line.py new file mode 100644 index 000000000000..a479910859ec --- /dev/null +++ b/account_invoice_fixed_discount/models/account_move_line.py @@ -0,0 +1,37 @@ +# Copyright 2017 ForgeFlow S.L. +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) + +from odoo import api, fields, models + + +class AccountMoveLine(models.Model): + _inherit = "account.move.line" + + discount_fixed = fields.Float( + string="Discount (Fixed)", + digits="Product Price", + default=0.00, + help="Fixed amount discount.", + ) + + @api.onchange("discount_fixed") + def _onchange_discount_fixed(self): + """When the discount fixed is changed, we set the ``discount`` on the line to the + appropriate value and apply the default downstream implementation. + + When the display_type is not ``'product'`` we reset the discount_fixed to 0.0. + + """ + if self.display_type != "product": + self.discount_fixed = 0.0 + return + + if self.discount_fixed: + self.discount = self.discount_fixed / self.price_unit * 100.0 + + @api.onchange("discount") + def _onchange_discount(self): + """Reset the ``discount_fixed`` when ``discount`` is changed.""" + if self.env.context.get("ignore_discount_fixed", False): + return + self.discount_fixed = 0.0 diff --git a/account_invoice_fixed_discount/tests/test_account_fixed_discount.py b/account_invoice_fixed_discount/tests/test_account_fixed_discount.py index 12f690ed9e17..fb696ef1b1fb 100644 --- a/account_invoice_fixed_discount/tests/test_account_fixed_discount.py +++ b/account_invoice_fixed_discount/tests/test_account_fixed_discount.py @@ -2,30 +2,21 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) from odoo.exceptions import ValidationError -from odoo.tests import TransactionCase +from odoo.tests import SavepointCase -class TestInvoiceFixedDiscount(TransactionCase): +class TestInvoiceFixedDiscount(SavepointCase): @classmethod def setUpClass(cls): super(TestInvoiceFixedDiscount, cls).setUpClass() cls.partner = cls.env["res.partner"].create({"name": "Test"}) cls.product = cls.env.ref("product.product_product_3") cls.account = cls.env["account.account"].search( - [ - ( - "user_type_id", - "=", - cls.env.ref("account.data_account_type_revenue").id, - ) - ], + [("account_type", "=", "income")], limit=1, ) - type_current_liability = cls.env.ref( - "account.data_account_type_current_liabilities" - ) cls.output_vat_acct = cls.env["account.account"].create( - {"name": "10", "code": "10", "user_type_id": type_current_liability.id} + {"name": "10", "code": "10", "account_type": "liability_current"} ) cls.tax_group_vat = cls.env["account.tax.group"].create({"name": "VAT"}) cls.vat = cls.env["account.tax"].create( diff --git a/account_invoice_fixed_discount/views/account_move_view.xml b/account_invoice_fixed_discount/views/account_move_view.xml index c54fc7bca06d..dd42452adf65 100644 --- a/account_invoice_fixed_discount/views/account_move_view.xml +++ b/account_invoice_fixed_discount/views/account_move_view.xml @@ -14,19 +14,18 @@ name="discount_fixed" groups="base.group_no_one" optional="show" + context="{'ignore_discount_fixed': True}" /> - - - - +