Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
36713 amit 1
-- Migration: Add original_wallet_amount to order + order_wallet_adjustment table
2
-- Purpose: Make PI (Pending Indent) calculation idempotent for historical dates.
3
--          updatePriceDrop/changeQuantity mutate wallet_amount in place, causing
4
--          account statement discrepancies when queried for past periods.
5
-- Date: 2026-05-27
6
 
7
-- ============================================================================
8
-- STEP 1: Add original_wallet_amount column to order table
9
-- ============================================================================
10
ALTER TABLE transaction.`order` ADD COLUMN original_wallet_amount FLOAT DEFAULT NULL;
11
 
12
-- ============================================================================
13
-- STEP 2: Create order_wallet_adjustment table
14
-- ============================================================================
15
CREATE TABLE IF NOT EXISTS transaction.order_wallet_adjustment (
16
  id INT AUTO_INCREMENT PRIMARY KEY,
17
  order_id INT NOT NULL,
18
  delta FLOAT NOT NULL COMMENT 'Change to wallet_amount: positive = increased, negative = decreased',
19
  adjusted_at DATETIME NOT NULL,
20
  reason VARCHAR(255),
21
  KEY idx_owa_order_adjusted (order_id, adjusted_at)
22
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
23
 
24
-- ============================================================================
25
-- STEP 3: Backfill original_wallet_amount = wallet_amount for ALL orders
26
--         (correct for orders that never had price drops)
27
-- ============================================================================
28
UPDATE transaction.`order` SET original_wallet_amount = wallet_amount;
29
 
30
-- ============================================================================
31
-- STEP 4: Fix original_wallet_amount for orders that had price drops
32
--         original = wallet_amount + SUM(price_drop_wallet_entries)
33
--         because: wallet_entry.amount = quantity*(oldPrice-newPrice)
34
--                  price increase → negative entry → original < current
35
--                  price drop → positive entry → original > current
36
-- ============================================================================
37
UPDATE transaction.`order` o
38
JOIN (
39
  SELECT
40
    CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(description, 'Order #', -1), ',', 1) AS UNSIGNED) as order_id,
41
    SUM(amount) as total_price_adj
42
  FROM transaction.userwallethistory
43
  WHERE reference_type = 'PURCHASE'
44
  AND (description LIKE 'Price increased%' OR description LIKE 'Price dropped%')
45
  GROUP BY order_id
46
) adj ON adj.order_id = o.id
47
SET o.original_wallet_amount = o.wallet_amount + adj.total_price_adj;
48
 
49
-- ============================================================================
50
-- STEP 5: Backfill order_wallet_adjustment from historical price drop entries
51
--         delta = -wallet_entry.amount (wallet_entry is priceDiff, delta is
52
--         the change to wallet_amount which is the opposite)
53
-- ============================================================================
54
INSERT INTO transaction.order_wallet_adjustment (order_id, delta, adjusted_at, reason)
55
SELECT
56
  CAST(SUBSTRING_INDEX(SUBSTRING_INDEX(description, 'Order #', -1), ',', 1) AS UNSIGNED) as order_id,
57
  -amount as delta,
58
  timestamp as adjusted_at,
59
  LEFT(description, 255) as reason
60
FROM transaction.userwallethistory
61
WHERE reference_type = 'PURCHASE'
62
AND (description LIKE 'Price increased%' OR description LIKE 'Price dropped%');