# -*- coding: utf-8 -*- from odoo import _, api, fields, models from odoo.exceptions import UserError, ValidationError class LoyaltyGenerateWizard(models.TransientModel): _inherit = 'loyalty.generate.wizard' def generate_coupons(self): # 1. Validation check for Marketing User if self.env.user.has_group('pos_loyalty_marketing_access.group_marketing_user') and not self.env.user.has_group('pos_loyalty_marketing_access.group_marketing_manager'): raise UserError(_("Marketing Users are not authorized to generate vouchers.")) # 2. Intercept and create a Voucher Request instead of direct generation for Marketing group is_marketing = self.env.user.has_group('pos_loyalty_marketing_access.group_marketing_user') or self.env.user.has_group('pos_loyalty_marketing_access.group_marketing_manager') if is_marketing: if any(not wizard.program_id for wizard in self): raise ValidationError(_("Can not generate coupon, no program is set.")) if any(wizard.coupon_qty <= 0 for wizard in self): raise ValidationError(_("Invalid quantity.")) requests = self.env['loyalty.voucher.generation.request'] for wizard in self: req = self.env['loyalty.voucher.generation.request'].create({ 'program_id': wizard.program_id.id, 'mode': wizard.mode, 'customer_ids': [(6, 0, wizard.customer_ids.ids)] if wizard.customer_ids else False, 'customer_tag_ids': [(6, 0, wizard.customer_tag_ids.ids)] if wizard.customer_tag_ids else False, 'coupon_qty': wizard.coupon_qty, 'points_granted': wizard.points_granted, 'valid_until': wizard.valid_until, 'description': wizard.description, 'state': 'pending', }) requests |= req action = { 'name': _('Voucher Generation Requests'), 'type': 'ir.actions.act_window', 'res_model': 'loyalty.voucher.generation.request', 'view_mode': 'form', 'views': [[False, 'form']], 'res_id': requests[0].id, 'target': 'current', } return { 'type': 'ir.actions.client', 'tag': 'display_notification', 'params': { 'title': _('Request Submitted'), 'message': _('Your voucher generation request has been submitted and is pending approval.'), 'type': 'warning', 'sticky': True, 'next': action, } } return super().generate_coupons()