addons-cm/account_invoice_triple_discount_readonly/models/triple_discount_mixin.py

28 lines
1.1 KiB
Python

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)