From 941ed35e42f941708c8581106ca5bcbed9cdd058 Mon Sep 17 00:00:00 2001 From: Suherdy Yacob Date: Mon, 8 Jun 2026 17:03:37 +0700 Subject: [PATCH] fix: include archived loyalty cards in search and reactivate them during level transitions --- models/pos_order.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/models/pos_order.py b/models/pos_order.py index fc5fb52..1684b78 100644 --- a/models/pos_order.py +++ b/models/pos_order.py @@ -71,15 +71,16 @@ class PosOrder(models.Model): other_programs = [pid for pid in all_multi_level_program_ids if pid != matched_program.id] if other_programs: - # Find ALL active old cards (with or without points) to clean them up - all_old_cards = self.env['loyalty.card'].sudo().search([ + # Find ALL old cards (active or archived, with or without points) to clean them up. + # 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), ('program_id', 'in', other_programs), - ('active', '=', True), ]) 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), ('program_id', '=', matched_program.id), ], limit=1) @@ -90,6 +91,9 @@ class PosOrder(models.Model): 'program_id': matched_program.id, '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 for old_card in all_old_cards: