loyalty_generate_wizard.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import _, api, fields, models
  4. from odoo.exceptions import ValidationError
  5. from odoo.osv import expression
  6. class LoyaltyGenerateWizard(models.TransientModel):
  7. _name = 'loyalty.generate.wizard'
  8. _description = 'Generate Coupons'
  9. program_id = fields.Many2one('loyalty.program', required=True, default=lambda self: self.env.context.get('active_id', False) or self.env.context.get('default_program_id', False))
  10. program_type = fields.Selection(related='program_id.program_type')
  11. mode = fields.Selection([
  12. ('anonymous', 'Anonymous Customers'),
  13. ('selected', 'Selected Customers')],
  14. string='For', required=True, default='anonymous'
  15. )
  16. customer_ids = fields.Many2many('res.partner', string='Customers')
  17. customer_tag_ids = fields.Many2many('res.partner.category', string='Customer Tags')
  18. coupon_qty = fields.Integer('Quantity',
  19. compute='_compute_coupon_qty', readonly=False, store=True)
  20. points_granted = fields.Float('Grant', required=True, default=1)
  21. points_name = fields.Char(related='program_id.portal_point_name', readonly=True)
  22. valid_until = fields.Date()
  23. will_send_mail = fields.Boolean(compute='_compute_will_send_mail')
  24. def _get_partners(self):
  25. self.ensure_one()
  26. if self.mode != 'selected':
  27. return self.env['res.partner']
  28. domain = []
  29. if self.customer_ids:
  30. domain = [('id', 'in', self.customer_ids.ids)]
  31. if self.customer_tag_ids:
  32. domain = expression.OR([domain, [('category_id', 'in', self.customer_tag_ids.ids)]])
  33. return self.env['res.partner'].search(domain)
  34. @api.depends('customer_ids', 'customer_tag_ids', 'mode')
  35. def _compute_coupon_qty(self):
  36. for wizard in self:
  37. if wizard.mode == 'selected':
  38. wizard.coupon_qty = len(wizard._get_partners())
  39. else:
  40. wizard.coupon_qty = wizard.coupon_qty or 0
  41. @api.depends("mode", "program_id")
  42. def _compute_will_send_mail(self):
  43. for wizard in self:
  44. wizard.will_send_mail = wizard.mode == 'selected' and 'create' in wizard.program_id.mapped('communication_plan_ids.trigger')
  45. def _get_coupon_values(self, partner):
  46. self.ensure_one()
  47. return {
  48. 'program_id': self.program_id.id,
  49. 'points': self.points_granted,
  50. 'expiration_date': self.valid_until,
  51. 'partner_id': partner.id if self.mode == 'selected' else False,
  52. }
  53. def generate_coupons(self):
  54. if any(not wizard.program_id for wizard in self):
  55. raise ValidationError(_("Can not generate coupon, no program is set."))
  56. if any(wizard.coupon_qty <= 0 for wizard in self):
  57. raise ValidationError(_("Invalid quantity."))
  58. coupon_create_vals = []
  59. for wizard in self:
  60. customers = wizard._get_partners() or range(wizard.coupon_qty)
  61. for partner in customers:
  62. coupon_create_vals.append(wizard._get_coupon_values(partner))
  63. self.env['loyalty.card'].create(coupon_create_vals)
  64. return True