fix: include archived loyalty cards in search and reactivate them during level transitions

This commit is contained in:
Suherdy Yacob 2026-06-08 17:03:37 +07:00
parent 67bdf004e4
commit 941ed35e42

View File

@ -71,15 +71,16 @@ 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 active old cards (with or without points) to clean them up # Find ALL old cards (active or archived, with or without points) to clean them up.
all_old_cards = self.env['loyalty.card'].sudo().search([ # We use with_context(active_test=False) to include archived cards.
all_old_cards = self.env['loyalty.card'].sudo().with_context(active_test=False).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:
new_card = self.env['loyalty.card'].sudo().search([ # Find the target level card (even if archived) to avoid duplicates.
new_card = self.env['loyalty.card'].sudo().with_context(active_test=False).search([
('partner_id', '=', partner.id), ('partner_id', '=', partner.id),
('program_id', '=', matched_program.id), ('program_id', '=', matched_program.id),
], limit=1) ], limit=1)
@ -90,6 +91,9 @@ class PosOrder(models.Model):
'program_id': matched_program.id, 'program_id': matched_program.id,
'points': 0, 'points': 0,
}) })
elif not new_card.active:
# Reactivate the card if it was archived
new_card.active = True
# Transfer non-zero points to the new card, then archive 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: