diff --git a/account_invoice_triple_discount_readonly/README.md b/account_invoice_triple_discount_readonly/README.md new file mode 100644 index 0000000..1e162cb --- /dev/null +++ b/account_invoice_triple_discount_readonly/README.md @@ -0,0 +1,61 @@ +# Account Invoice Triple Discount Readonly + +## Description + +This module fixes a bug in `account_invoice_triple_discount` when combined with `purchase_triple_discount`. + +### Original Problem + +In purchase orders, when configuring a discount in the `discount2` or `discount3` columns, upon saving: +- All discounts were accumulated in the `discount1` column +- The `discount2` and `discount3` fields were reset to zero + +This occurred because the original mixin's `write` method always reset `discount2` and `discount3` to 0 when the computed `discount` field was included in the write (which happens when any discount is modified). + +### Solution + +The module overrides the `write` method of `triple.discount.mixin` to: +1. Detect if `discount1`, `discount2` or `discount3` are being explicitly modified +2. If there are explicit discounts, ignore the computed `discount` field +3. If only the `discount` field comes, maintain legacy behavior (map to `discount1` and reset the rest) + +Additionally, it makes the `discount` field (total discount) **readonly** in: +- Partner form view (`res.partner`) +- Supplier info in product form (`product.supplierinfo`) +- Base mixin (`triple.discount.mixin`) + +This forces users to work only with the `discount1`, `discount2` and `discount3` columns, avoiding confusion. + +## Installation + +1. Make sure `account_invoice_triple_discount` and `purchase_triple_discount` are installed +2. Update the module list +3. Install `account_invoice_triple_discount_readonly` + +## Usage + +After installation: +- The total discount field will be readonly and automatically calculated +- Users must use discount1, discount2, and discount3 fields to set discounts +- When saving, the individual discount values will be preserved correctly + +## Bug Tracker + +Bugs are tracked on [GitHub Issues](https://github.com/OCA/account-invoicing/issues). +In case of trouble, please check there if your issue has already been reported. + +## Credits + +### Contributors + +* Coop Makers + +### Maintainers + +This module is maintained by the OCA. + +## Compatibility + +- Odoo 16.0 +- account_invoice_triple_discount +- purchase_triple_discount diff --git a/account_invoice_triple_discount_readonly/__init__.py b/account_invoice_triple_discount_readonly/__init__.py new file mode 100644 index 0000000..0650744 --- /dev/null +++ b/account_invoice_triple_discount_readonly/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/account_invoice_triple_discount_readonly/__manifest__.py b/account_invoice_triple_discount_readonly/__manifest__.py new file mode 100644 index 0000000..a9ce583 --- /dev/null +++ b/account_invoice_triple_discount_readonly/__manifest__.py @@ -0,0 +1,18 @@ +# Copyright 2025 +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +{ + "name": "Account Invoice Triple Discount Readonly", + "version": "16.0.1.0.0", + "summary": "Make total discount readonly and fix discount2/discount3 write issue", + "license": "AGPL-3", + "author": "Criptomart", + "website": "https://github.com/OCA/account-invoicing", + "depends": ["account_invoice_triple_discount", "purchase_triple_discount"], + "data": [ + "views/product_supplierinfo_view.xml", + "views/res_partner_view.xml", + "views/purchase_order_view.xml", + "views/account_move_view.xml", + ], + "installable": True, +} diff --git a/account_invoice_triple_discount_readonly/models/__init__.py b/account_invoice_triple_discount_readonly/models/__init__.py new file mode 100644 index 0000000..3f1cb7b --- /dev/null +++ b/account_invoice_triple_discount_readonly/models/__init__.py @@ -0,0 +1 @@ +from . import triple_discount_mixin diff --git a/account_invoice_triple_discount_readonly/models/triple_discount_mixin.py b/account_invoice_triple_discount_readonly/models/triple_discount_mixin.py new file mode 100644 index 0000000..d222b00 --- /dev/null +++ b/account_invoice_triple_discount_readonly/models/triple_discount_mixin.py @@ -0,0 +1,28 @@ +from odoo import fields, models + + +class TripleDiscountMixin(models.AbstractModel): + _inherit = "triple.discount.mixin" + + # Make the discount field readonly to prevent manual edits + discount = fields.Float(readonly=True) + + def write(self, vals): + discount_fields = self._get_multiple_discount_field_names() + + if "discount" in vals: + # Check if any of discount1, discount2 or discount3 + # are being explicitly modified + has_explicit_discounts = any(field in vals for field in discount_fields) + + if has_explicit_discounts: + # If there are explicit discounts, remove the computed 'discount' field + # to avoid overwriting the explicit values + vals.pop("discount") + else: + # If only 'discount' comes, apply legacy behavior: + # map to discount1 and reset discount2 and discount3 + vals["discount1"] = vals.pop("discount") + vals.update({field: 0 for field in discount_fields[1:]}) + + return super().write(vals) diff --git a/account_invoice_triple_discount_readonly/views/account_move_view.xml b/account_invoice_triple_discount_readonly/views/account_move_view.xml new file mode 100644 index 0000000..b44c4d2 --- /dev/null +++ b/account_invoice_triple_discount_readonly/views/account_move_view.xml @@ -0,0 +1,28 @@ + + + + + + account.invoice.triple.discount.readonly + account.move + + + + 1 + + + 1 + + + + + diff --git a/account_invoice_triple_discount_readonly/views/product_supplierinfo_view.xml b/account_invoice_triple_discount_readonly/views/product_supplierinfo_view.xml new file mode 100644 index 0000000..1d5cf0e --- /dev/null +++ b/account_invoice_triple_discount_readonly/views/product_supplierinfo_view.xml @@ -0,0 +1,32 @@ + + + + + + product.supplierinfo + + + + 1 + + + + + + + product.supplierinfo + + + + 1 + + + + + diff --git a/account_invoice_triple_discount_readonly/views/purchase_order_view.xml b/account_invoice_triple_discount_readonly/views/purchase_order_view.xml new file mode 100644 index 0000000..c082397 --- /dev/null +++ b/account_invoice_triple_discount_readonly/views/purchase_order_view.xml @@ -0,0 +1,28 @@ + + + + + + purchase.order.triple.discount.readonly + purchase.order + + + + 1 + + + 1 + + + + + diff --git a/account_invoice_triple_discount_readonly/views/res_partner_view.xml b/account_invoice_triple_discount_readonly/views/res_partner_view.xml new file mode 100644 index 0000000..8a18830 --- /dev/null +++ b/account_invoice_triple_discount_readonly/views/res_partner_view.xml @@ -0,0 +1,18 @@ + + + + + + res.partner + + + + 1 + + + + +