You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
2.0 KiB

11 months ago
  1. # License AGPL-3 - See https://www.gnu.org/licenses/agpl-3.0
  2. from odoo import models, fields, api, _
  3. from odoo.exceptions import ValidationError
  4. DISCOUNTS = [
  5. ("none", "No initial discount"),
  6. ("fixed", "Fixed amount"),
  7. ("percent", "Percentage of the price"),
  8. ]
  9. class ProductTemplate(models.Model):
  10. _inherit = "product.template"
  11. initial_discount = fields.Selection(
  12. selection=DISCOUNTS,
  13. default="none",
  14. string="Initial discount",
  15. required=True,
  16. )
  17. fixed_discount = fields.Float(
  18. string="Discount amount",
  19. digits="Product Price",
  20. )
  21. percent_discount = fields.Float(
  22. string="Discount (%)",
  23. digits=(12, 2),
  24. )
  25. @api.constrains("list_price", "initial_discount", "fixed_discount")
  26. def check_fixed_discount(self):
  27. for product in self:
  28. if (
  29. product.initial_discount == "fixed"
  30. and product.list_price <= product.fixed_discount
  31. ):
  32. raise ValidationError(
  33. _(
  34. "Fixed discount for 1st membership must be less than the product template, or any of its variants, sale price."
  35. )
  36. )
  37. @api.constrains("initial_discount", "percent_discount")
  38. def check_percent_discount(self):
  39. for product in self:
  40. if product.initial_discount == "percent" and product.percent_discount < 0.0:
  41. raise ValidationError(
  42. _(
  43. 'Percent discount cannot handle a negative value.\nIf you wan to apply extra fees, please install "Membership initial fee" module (OCA/vertical-association)'
  44. )
  45. )
  46. elif (
  47. product.initial_discount == "percent"
  48. and product.percent_discount >= 100.0
  49. ):
  50. raise ValidationError(
  51. _("Percent discount must handle a value smaller than 100%.")
  52. )