feat: migrate loyalty history and coupon references to the new card during level upgrade

This commit is contained in:
Suherdy Yacob 2026-05-22 14:07:39 +07:00
parent c7ba4478c3
commit 7e6fc22714

View File

@ -90,6 +90,27 @@ class PosOrder(models.Model):
if abs(pts) > 0.0001:
new_card.points += pts
# Transfer loyalty history (point transactions) to the new card
old_histories = self.env['loyalty.history'].sudo().search([
('card_id', 'in', all_old_cards.ids)
])
if old_histories:
old_histories.write({'card_id': new_card.id})
# Transfer POS order line coupon references to the new card
old_pos_lines = self.env['pos.order.line'].sudo().search([
('coupon_id', 'in', all_old_cards.ids)
])
if old_pos_lines:
old_pos_lines.write({'coupon_id': new_card.id})
# Transfer Sales order line coupon references to the new card
old_sale_lines = self.env['sale.order.line'].sudo().search([
('coupon_id', 'in', all_old_cards.ids)
])
if old_sale_lines:
old_sale_lines.write({'coupon_id': new_card.id})
# 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.