refactor: update loyalty card cleanup to target only active cards and reset points upon archival

This commit is contained in:
Suherdy Yacob 2026-06-02 16:51:20 +07:00
parent 7631a3e93e
commit 67bdf004e4

View File

@ -71,10 +71,11 @@ class PosOrder(models.Model):
other_programs = [pid for pid in all_multi_level_program_ids if pid != matched_program.id] other_programs = [pid for pid in all_multi_level_program_ids if pid != matched_program.id]
if other_programs: if other_programs:
# Find ALL old cards (with or without points) to clean them up # Find ALL active old cards (with or without points) to clean them up
all_old_cards = self.env['loyalty.card'].sudo().search([ all_old_cards = self.env['loyalty.card'].sudo().search([
('partner_id', '=', partner.id), ('partner_id', '=', partner.id),
('program_id', 'in', other_programs), ('program_id', 'in', other_programs),
('active', '=', True),
]) ])
if all_old_cards: if all_old_cards:
@ -90,11 +91,12 @@ class PosOrder(models.Model):
'points': 0, 'points': 0,
}) })
# Transfer non-zero points to the new card, then delete ALL old cards # Transfer non-zero points to the new card, then archive ALL old cards
for old_card in all_old_cards: for old_card in all_old_cards:
pts = old_card.points pts = old_card.points
if abs(pts) > 0.0001: if abs(pts) > 0.0001:
new_card.points += pts new_card.points += pts
old_card.points = 0.0
# Transfer loyalty history (point transactions) to the new card # Transfer loyalty history (point transactions) to the new card
old_histories = self.env['loyalty.history'].sudo().search([ old_histories = self.env['loyalty.history'].sudo().search([
@ -120,4 +122,4 @@ class PosOrder(models.Model):
# Archive old-level cards (active=False) instead of deleting, # Archive old-level cards (active=False) instead of deleting,
# because pos_order_line.coupon_id may reference them (FK constraint). # because pos_order_line.coupon_id may reference them (FK constraint).
# Archiving hides them from the UI while preserving order history. # Archiving hides them from the UI while preserving order history.
all_old_cards.write({'active': False}) all_old_cards.write({'active': False, 'points': 0.0})