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.

44 lines
1.4 KiB

  1. # Copyright 2019 Tecnativa - David Vidal
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from odoo import fields, models
  4. class GlobalDiscount(models.Model):
  5. _name = "global.discount"
  6. _description = "Global Discount"
  7. _order = "sequence, id desc"
  8. sequence = fields.Integer(help="Gives the order to apply discounts")
  9. name = fields.Char(string="Discount Name", required=True)
  10. discount = fields.Float(digits="Discount", required=True, default=0.0)
  11. discount_scope = fields.Selection(
  12. selection=[("sale", "Sales"), ("purchase", "Purchases")],
  13. default="sale",
  14. required="True",
  15. string="Discount Scope",
  16. )
  17. company_id = fields.Many2one(
  18. comodel_name="res.company",
  19. string="Company",
  20. default=lambda self: self.env.company,
  21. )
  22. def name_get(self):
  23. result = []
  24. for one in self:
  25. result.append((one.id, "{} ({:.2f}%)".format(one.name, one.discount)))
  26. return result
  27. def _get_global_discount_vals(self, base, **kwargs):
  28. """Prepare the dict of values to create to obtain the discounted
  29. amount
  30. :param float base: the amount to discount
  31. :return: dict with the discounted amount
  32. """
  33. self.ensure_one()
  34. return {
  35. "global_discount": self,
  36. "base": base,
  37. "base_discounted": base * (1 - (self.discount / 100)),
  38. }