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
1.6 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. from odoo.addons import decimal_precision as dp
  5. class GlobalDiscount(models.Model):
  6. _name = 'global.discount'
  7. _description = 'Global Discount'
  8. _order = "sequence, id desc"
  9. sequence = fields.Integer(
  10. help='Gives the order to apply discounts',
  11. )
  12. name = fields.Char(
  13. string='Discount Name',
  14. required=True,
  15. )
  16. discount = fields.Float(
  17. digits=dp.get_precision('Discount'),
  18. required=True,
  19. default=0.0,
  20. )
  21. discount_scope = fields.Selection(
  22. selection=[
  23. ('sale', 'Sales'),
  24. ('purchase', 'Purchases'),
  25. ],
  26. default='sale',
  27. required='True',
  28. string='Discount Scope',
  29. )
  30. company_id = fields.Many2one(
  31. comodel_name='res.company',
  32. string='Company',
  33. default=lambda self: self.env.user.company_id,
  34. )
  35. def name_get(self):
  36. result = []
  37. for one in self:
  38. result.append(
  39. (one.id, '{} ({:.2f}%)'.format(one.name, one.discount)))
  40. return result
  41. def _get_global_discount_vals(self, base, **kwargs):
  42. """ Prepare the dict of values to create to obtain the discounted
  43. amount
  44. :param float base: the amount to discount
  45. :return: dict with the discounted amount
  46. """
  47. self.ensure_one()
  48. return {
  49. 'global_discount': self,
  50. 'base': base,
  51. 'base_discounted': base * (1 - (self.discount / 100)),
  52. }