34 lines
1.8 KiB
Python
34 lines
1.8 KiB
Python
from odoo import models, api
|
|
|
|
class LoyaltyCard(models.Model):
|
|
_inherit = 'loyalty.card'
|
|
|
|
@api.model_create_multi
|
|
def create(self, vals_list):
|
|
cards = super().create(vals_list)
|
|
for card in cards:
|
|
if card.partner_id and card.active and card.program_id.manual_membership and card.program_id.program_type == 'loyalty':
|
|
# Set partner's membership level to this program
|
|
card.partner_id.sudo().write({'membership_level_id': card.program_id.id})
|
|
return cards
|
|
|
|
def write(self, vals):
|
|
res = super().write(vals)
|
|
if 'active' in vals or 'partner_id' in vals or 'program_id' in vals:
|
|
for card in self:
|
|
if card.partner_id and card.active and card.program_id.manual_membership and card.program_id.program_type == 'loyalty':
|
|
card.partner_id.sudo().write({'membership_level_id': card.program_id.id})
|
|
elif 'active' in vals and not vals['active']:
|
|
# Card was archived, if it matched the partner's current membership level, clear it or let auto-leveling recalculate
|
|
if card.partner_id and card.partner_id.membership_level_id == card.program_id:
|
|
# Only clear if there are no other active cards for this program
|
|
other_active = self.env['loyalty.card'].sudo().search_count([
|
|
('partner_id', '=', card.partner_id.id),
|
|
('program_id', '=', card.program_id.id),
|
|
('active', '=', True),
|
|
('id', '!=', card.id),
|
|
])
|
|
if not other_active:
|
|
card.partner_id.sudo().write({'membership_level_id': False})
|
|
return res
|