pos_loyalty_marketing_access/models/loyalty_program.py

68 lines
2.7 KiB
Python

# -*- coding: utf-8 -*-
from odoo import _, api, fields, models
from odoo.exceptions import UserError
class LoyaltyProgram(models.Model):
_name = 'loyalty.program'
_inherit = ['loyalty.program', 'mail.thread', 'mail.activity.mixin']
state = fields.Selection([
('draft', 'Draft'),
('pending', 'Pending Approval'),
('approved', 'Approved')
], default='draft', string='Status', tracking=True)
def action_request_approval(self):
for program in self:
if program.state != 'draft':
continue
program.write({'state': 'pending'})
program.message_post(body=_("Approval requested for this marketing program."))
def action_approve(self):
for program in self:
if program.state != 'pending':
continue
# Check authorization
target_company = program.company_id or self.env.company
approver = target_company._get_marketing_program_approver()
if approver:
if self.env.user.id != approver.id:
raise UserError(_("Only the designated Marketing Program Approver (%s) can approve this program.") % approver.name)
else:
is_manager = self.env.user.has_group('pos_loyalty_marketing_access.group_marketing_manager')
if not is_manager:
raise UserError(_("Only a Marketing Manager can approve this program as no designated approver is configured."))
program.write({'state': 'approved'})
program.message_post(body=_("Marketing program has been approved and is now active."))
def action_reset_draft(self):
for program in self:
program.write({'state': 'draft'})
program.message_post(body=_("Marketing program reset to Draft."))
def write(self, vals):
# If we are only modifying state, skip the write reset checks
if len(vals) == 1 and 'state' in vals:
return super().write(vals)
# Skip reset logic if Odoo is installing/upgrading modules or explicitly bypassed
if (self.env.context.get('install_mode')
or self.env.context.get('module')
or self.env.context.get('no_marketing_reset')):
return super().write(vals)
# For modifications, check if they are in pending/approved state
programs_to_reset = self.filtered(lambda p: p.state in ['pending', 'approved'])
if programs_to_reset:
vals['state'] = 'draft'
for program in programs_to_reset:
program.message_post(body=_("The marketing program was modified and has been reset to Draft status for re-approval."))
return super().write(vals)