From 67bdf004e4f508e415d7ce4a18f1b3bc272887a0 Mon Sep 17 00:00:00 2001 From: Suherdy Yacob Date: Tue, 2 Jun 2026 16:51:20 +0700 Subject: [PATCH] refactor: update loyalty card cleanup to target only active cards and reset points upon archival --- models/pos_order.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/models/pos_order.py b/models/pos_order.py index 821b1f3..fc5fb52 100644 --- a/models/pos_order.py +++ b/models/pos_order.py @@ -71,10 +71,11 @@ 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 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([ ('partner_id', '=', partner.id), ('program_id', 'in', other_programs), + ('active', '=', True), ]) if all_old_cards: @@ -90,11 +91,12 @@ class PosOrder(models.Model): '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: pts = old_card.points if abs(pts) > 0.0001: new_card.points += pts + old_card.points = 0.0 # Transfer loyalty history (point transactions) to the new card 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, # because pos_order_line.coupon_id may reference them (FK constraint). # 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})