26 lines
1.3 KiB
Python
26 lines
1.3 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:
|
|
card.partner_id.sudo().write({'membership_level_id': False})
|
|
return res
|