pos_loyalty_marketing_access/models/loyalty_reward.py

93 lines
3.7 KiB
Python

# -*- coding: utf-8 -*-
from odoo import _, api, fields, models
class LoyaltyReward(models.Model):
_inherit = 'loyalty.reward'
def _get_discount_product_values(self):
res = super()._get_discount_product_values()
# Search for the dedicated category "OT / Saleable / PoS / Discounts"
category = self.env['product.category'].sudo().search([
('complete_name', '=', 'OT / Saleable / PoS / Discounts')
], limit=1)
if not category:
# Fallback to any category named "Discounts" if not found
category = self.env['product.category'].sudo().search([
('name', '=', 'Discounts')
], limit=1)
for reward, vals in zip(self, res):
if category:
vals['categ_id'] = category.id
# Search for account "412201" matching the company of the reward
company = reward.company_id or self.env.company
account = self.env['account.account'].sudo().search([
('code', '=', '412201'),
('company_ids', '=', company.id)
], limit=1)
if account:
vals['property_account_income_id'] = account.id
vals['property_account_expense_id'] = account.id
return res
@api.model_create_multi
def create(self, vals_list):
records = super().create(vals_list)
if (self.env.context.get('install_mode')
or self.env.context.get('module')
or self.env.context.get('no_marketing_reset')):
return records
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):
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)
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):
if (self.env.context.get('install_mode')
or self.env.context.get('module')
or self.env.context.get('no_marketing_reset')):
return super().unlink()
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