Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
36795 amit 1
-- ============================================================================
2
-- Cleanup: phantom PAYMENT_FAILED orders inflating "Unbilled PO" (pending indent)
3
-- ============================================================================
4
-- selectPendingIndentWalletAmount counts ANY order with billing/refund NULL,
5
-- regardless of status. So old PAYMENT_FAILED(1) orders (payment never completed,
6
-- never a committed PO) were counted as unbilled PO and drove the finance reco
7
-- "gaps" (e.g. Metro Refrigeration 18L, Mobile Planet 10.3L -- all phantom).
8
-- All such orders are 2017-2019 (none since; the order flow changed).
9
--
10
-- DO NOT blanket-exclude PAYMENT_FAILED:
11
--   - PAYMENT_PENDING(0) is the legit awaiting-approval bulk-PO state -> must stay.
12
--   - Some PAYMENT_FAILED orders are NET-COMMITTED (wallet debited, never refunded)
13
--     = a REAL payable; dropping them would hide money owed to the partner.
14
-- So we drop ONLY the phantom: no wallet debit, OR transaction net wallet >= 0
15
-- (refunded by other means). Net-committed orders are KEPT (finance refund list).
16
--
17
-- Mechanism: set refund_timestamp = created_timestamp on phantom rows so they drop
18
-- out of pending indent (status stays PAYMENT_FAILED -- correct). Data-only, no code.
19
--
20
-- IMPORTANT - use this TEMP-TABLE form, NOT a single UPDATE with the wallet
21
-- subqueries inline: the broad "all PURCHASE debits" aggregate over
22
-- userwallethistory is a full-table scan that runs for minutes and holds locks on
23
-- a LIVE DB. Scope the wallet computation to just the ~1.2k candidate transactions,
24
-- then UPDATE by primary key (short lock). Run with `transaction` as default DB
25
-- (CREATE TEMPORARY TABLE needs a selected schema).
26
--
27
-- Scope: status=1, billing/refund NULL, wallet_amount>0.
28
-- Expected on hadb1 (2026-06-08): 1162 candidates -> 945 phantom dropped
29
--   (no-debit ~1.67cr + net-settled ~14.6L), 217 net-committed kept (~9.38L,
30
--   of which 9.04L is the internal Test Account; real retail payable ~33.6k).
31
-- IDEMPOTENT: guarded on refund_timestamp IS NULL.
32
-- ============================================================================
33
 
34
-- run with:  mysql ... transaction < cleanup_phantom_payment_failed.sql
35
 
36
CREATE TEMPORARY TABLE cand AS
37
  SELECT o.id AS order_id, CAST(o.transaction_id AS CHAR) AS ref
38
  FROM transaction.`order` o
39
  WHERE o.status = 1
40
    AND o.billing_timestamp IS NULL
41
    AND o.refund_timestamp IS NULL
42
    AND COALESCE(o.original_wallet_amount, o.wallet_amount) > 0;
43
ALTER TABLE cand ADD INDEX(ref);
44
 
45
CREATE TEMPORARY TABLE refstat AS
46
  SELECT h.reference AS ref,
47
         SUM(h.amount) AS net,
48
         MAX(CASE WHEN h.reference_type = 'PURCHASE' AND h.amount < 0 THEN 1 ELSE 0 END) AS has_debit
49
  FROM transaction.userwallethistory h
50
  WHERE h.reference IN (SELECT ref FROM cand)
51
  GROUP BY h.reference;
52
ALTER TABLE refstat ADD INDEX(ref);
53
 
54
-- phantom = no wallet debit, OR debit fully reversed (net >= 0). Net-committed (net < 0) excluded.
55
CREATE TEMPORARY TABLE phantom_ids AS
56
  SELECT c.order_id
57
  FROM cand c
58
  LEFT JOIN refstat r ON r.ref = c.ref
59
  WHERE r.has_debit IS NULL OR r.has_debit = 0 OR COALESCE(r.net, 0) >= -1;
60
 
61
UPDATE transaction.`order` o
62
  JOIN phantom_ids p ON p.order_id = o.id
63
SET o.refund_timestamp = o.created_timestamp,
64
    o.refunded_by      = 'system-cleanup',
65
    o.refund_reason    = 'Phantom PAYMENT_FAILED order (payment never completed; not a committed PO) - excluded from unbilled PO';
66
 
67
-- Verify: remaining status=1 in PI = the net-committed payable only.
68
-- SELECT COUNT(*), ROUND(SUM(COALESCE(original_wallet_amount,wallet_amount)),0)
69
-- FROM transaction.`order`
70
-- WHERE status=1 AND billing_timestamp IS NULL AND refund_timestamp IS NULL
71
--   AND COALESCE(original_wallet_amount,wallet_amount) > 0;   -- expect ~217 / ~9.38L