# License AGPL-3 - See https://www.gnu.org/licenses/agpl-3.0 from odoo import models, fields, api, _ from odoo.exceptions import ValidationError DISCOUNTS = [ ("none", "No initial discount"), ("fixed", "Fixed amount"), ("percent", "Percentage of the price"), ] class ProductTemplate(models.Model): _inherit = "product.template" initial_discount = fields.Selection( selection=DISCOUNTS, default="none", string="Initial discount", required=True, ) fixed_discount = fields.Float( string="Discount amount", digits="Product Price", ) percent_discount = fields.Float( string="Discount (%)", digits=(12, 2), ) @api.constrains("list_price", "initial_discount", "fixed_discount") def check_fixed_discount(self): for product in self: if ( product.initial_discount == "fixed" and product.list_price <= product.fixed_discount ): raise ValidationError( _( "Fixed discount for 1st membership must be less than the product template, or any of its variants, sale price." ) ) @api.constrains("initial_discount", "percent_discount") def check_percent_discount(self): for product in self: if product.initial_discount == "percent" and product.percent_discount < 0.0: raise ValidationError( _( 'Percent discount cannot handle a negative value.\nIf you wan to apply extra fees, please install "Membership initial fee" module (OCA/vertical-association)' ) ) elif ( product.initial_discount == "percent" and product.percent_discount >= 100.0 ): raise ValidationError( _("Percent discount must handle a value smaller than 100%.") )