res_users.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. # For copyright and license notices, see __openerp__.py file in module root
  4. # directory
  5. ##############################################################################
  6. from openerp import models, api, fields, _
  7. from openerp.exceptions import Warning
  8. class discount_restriction(models.Model):
  9. _name = 'res.users.discount_restriction'
  10. _description = 'Discount Restriction'
  11. pricelist_id = fields.Many2one(
  12. 'product.pricelist',
  13. 'Pricelist',
  14. ondelete='cascade',)
  15. min_discount = fields.Float('Min. Discount', required=True)
  16. max_discount = fields.Float('Max. Discount', required=True)
  17. user_id = fields.Many2one(
  18. 'res.users',
  19. 'User',
  20. required=True,
  21. ondelete='cascade',
  22. )
  23. class users(models.Model):
  24. _inherit = 'res.users'
  25. discount_restriction_ids = fields.One2many(
  26. 'res.users.discount_restriction',
  27. 'user_id',
  28. string='Discount Restrictions')
  29. @api.multi
  30. def check_discount(self, discount, pricelist_id, do_not_raise=False):
  31. """
  32. We add do_not_raise for compatibility with other modules
  33. """
  34. self.ensure_one()
  35. error = False
  36. if discount and discount != 0.0:
  37. disc_restriction_env = self.env['res.users.discount_restriction']
  38. domain = [
  39. ('pricelist_id', '=', pricelist_id), ('user_id', '=', self.id)]
  40. disc_restriction = disc_restriction_env.search(domain, limit=1)
  41. print 'disc_restriction', disc_restriction
  42. if not disc_restriction:
  43. domain = [
  44. ('user_id', '=', self.id)]
  45. disc_restriction = disc_restriction_env.search(domain, limit=1)
  46. # User can not make any discount
  47. if not disc_restriction:
  48. error = _(
  49. 'You can not give any discount greater than pricelist '
  50. 'discounts')
  51. else:
  52. if (
  53. discount < disc_restriction.min_discount or
  54. discount > disc_restriction.max_discount
  55. ):
  56. error = _(
  57. 'The applied discount is out of range with respect to '
  58. 'the allowed. The discount can be between %s and %s '
  59. 'for the current price list') % (
  60. disc_restriction.min_discount,
  61. disc_restriction.max_discount)
  62. if not do_not_raise and error:
  63. raise Warning(error)
  64. return error