feat: implement automatic status reset to draft for loyalty programs upon modification, addition, or removal of rewards and rules

This commit is contained in:
Suherdy Yacob 2026-05-28 14:20:08 +07:00
parent 9900d297fc
commit 15917ee277
4 changed files with 97 additions and 15 deletions

View File

@ -5,4 +5,5 @@ from . import loyalty_program
from . import sale_order from . import sale_order
from . import loyalty_voucher_generation_request from . import loyalty_voucher_generation_request
from . import loyalty_reward from . import loyalty_reward
from . import loyalty_rule

View File

@ -51,20 +51,11 @@ class LoyaltyProgram(models.Model):
return super().write(vals) return super().write(vals)
# For modifications, check if they are in pending/approved state # For modifications, check if they are in pending/approved state
for program in self: programs_to_reset = self.filtered(lambda p: p.state in ['pending', 'approved'])
if program.state in ['pending', 'approved']: if programs_to_reset:
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:
vals['state'] = 'draft' vals['state'] = 'draft'
program.message_post(body=_("The approved marketing program was modified by a standard user and has been reset to Draft status for re-approval.")) for program in programs_to_reset:
else: program.message_post(body=_("The marketing program was modified and has been reset to Draft status for re-approval."))
is_manager = self.env.user.has_group('pos_loyalty_marketing_access.group_marketing_manager')
if not is_manager:
vals['state'] = 'draft'
program.message_post(body=_("The approved marketing program was modified by a standard user and has been reset to Draft status for re-approval."))
return super().write(vals) return super().write(vals)

View File

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from odoo import api, fields, models from odoo import _, api, fields, models
class LoyaltyReward(models.Model): class LoyaltyReward(models.Model):
_inherit = 'loyalty.reward' _inherit = 'loyalty.reward'
@ -33,3 +33,45 @@ class LoyaltyReward(models.Model):
vals['property_account_expense_id'] = account.id vals['property_account_expense_id'] = account.id
return res return res
@api.model_create_multi
def create(self, vals_list):
records = super().create(vals_list)
for record in records:
if record.program_id and record.program_id.state in ['pending', 'approved']:
record.program_id.write({'state': 'draft'})
record.program_id.message_post(body=_("A loyalty reward was added, resetting the program to Draft status for re-approval."))
return records
def write(self, vals):
programs_to_reset = self.env['loyalty.program']
for record in self:
if record.program_id and record.program_id.state in ['pending', 'approved']:
programs_to_reset |= record.program_id
res = super().write(vals)
if 'program_id' in vals:
new_program = self.env['loyalty.program'].browse(vals['program_id'])
if new_program and new_program.state in ['pending', 'approved']:
programs_to_reset |= new_program
for program in programs_to_reset:
program.write({'state': 'draft'})
program.message_post(body=_("A loyalty reward was modified, resetting the program to Draft status for re-approval."))
return res
def unlink(self):
programs_to_reset = self.env['loyalty.program']
for record in self:
if record.program_id and record.program_id.state in ['pending', 'approved']:
programs_to_reset |= record.program_id
res = super().unlink()
for program in programs_to_reset:
program.write({'state': 'draft'})
program.message_post(body=_("A loyalty reward was deleted, resetting the program to Draft status for re-approval."))
return res

48
models/loyalty_rule.py Normal file
View File

@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
from odoo import _, api, fields, models
class LoyaltyRule(models.Model):
_inherit = 'loyalty.rule'
@api.model_create_multi
def create(self, vals_list):
records = super().create(vals_list)
for record in records:
if record.program_id and record.program_id.state in ['pending', 'approved']:
record.program_id.write({'state': 'draft'})
record.program_id.message_post(body=_("A loyalty rule was added, resetting the program to Draft status for re-approval."))
return records
def write(self, vals):
programs_to_reset = self.env['loyalty.program']
for record in self:
if record.program_id and record.program_id.state in ['pending', 'approved']:
programs_to_reset |= record.program_id
res = super().write(vals)
if 'program_id' in vals:
new_program = self.env['loyalty.program'].browse(vals['program_id'])
if new_program and new_program.state in ['pending', 'approved']:
programs_to_reset |= new_program
for program in programs_to_reset:
program.write({'state': 'draft'})
program.message_post(body=_("A loyalty rule was modified, resetting the program to Draft status for re-approval."))
return res
def unlink(self):
programs_to_reset = self.env['loyalty.program']
for record in self:
if record.program_id and record.program_id.state in ['pending', 'approved']:
programs_to_reset |= record.program_id
res = super().unlink()
for program in programs_to_reset:
program.write({'state': 'draft'})
program.message_post(body=_("A loyalty rule was deleted, resetting the program to Draft status for re-approval."))
return res