| 36795 |
amit |
1 |
-- ============================================================================
|
|
|
2 |
-- Cleanup: abandoned unpaid draft orders ("2020 draft class")
|
|
|
3 |
-- ============================================================================
|
|
|
4 |
-- These are status=0 (PAYMENT_PENDING) orders, billing/refund NULL, whose
|
|
|
5 |
-- transaction NEVER posted a wallet PURCHASE debit -- bulk draft batches created
|
|
|
6 |
-- programmatically (mostly 2019-2021), never submitted/paid, predating the
|
|
|
7 |
-- bulk-PO approval workflow. They have inflated pending indent (and partner
|
|
|
8 |
-- account-statement closing balances) ever since, with ZERO underlying money
|
|
|
9 |
-- movement. The problem is closed: newest is 2024-05; nothing in 2025/2026.
|
|
|
10 |
--
|
|
|
11 |
-- DISTINCT from the PO_REJECTED fix (backfill_po_rejected_orders.sql): those were
|
|
|
12 |
-- REJECTED bulk POs whose wallet WAS debited then refunded. This set was never
|
|
|
13 |
-- debited at all, so there is no wallet/rejection event to anchor to.
|
|
|
14 |
--
|
|
|
15 |
-- Mechanism:
|
|
|
16 |
-- status -> PAYMENT_FAILED(1) (existing status -> NO code/enum change,
|
|
|
17 |
-- NO deploy needed; accurate: payment never
|
|
|
18 |
-- completed. Not in openOrders; refund
|
|
|
19 |
-- reports are status-gated and exclude it.)
|
|
|
20 |
-- refund_timestamp -> created_timestamp (retroactive: these were never real, so
|
|
|
21 |
-- remove them from ALL historical pending
|
|
|
22 |
-- indent. This is the lever that drops them
|
|
|
23 |
-- from PI and every refundTimestamp-IS-NULL
|
|
|
24 |
-- "active order" query.)
|
|
|
25 |
--
|
|
|
26 |
-- Safe because the wallet was never touched: no financial ledger changes, and the
|
|
|
27 |
-- daily Reconciliation cron only examines transactions with a PURCHASE wallet entry
|
|
|
28 |
-- on the reconcile date -- these have none, so it never sees them.
|
|
|
29 |
--
|
|
|
30 |
-- Scope guards (must all hold): status=0, not billed, not refunded, NOT a rejected
|
|
|
31 |
-- transaction (those go through PO_REJECTED), and NO PURCHASE wallet debit ever.
|
|
|
32 |
-- Entire set is 2+ years old, so no in-flight order can be caught.
|
|
|
33 |
--
|
|
|
34 |
-- IDEMPOTENT: guarded on status=0. Re-run matches 0 rows.
|
|
|
35 |
-- Expected: 1970 rows, 112 partners, ~Rs 3.14 cr pending indent released.
|
|
|
36 |
-- ============================================================================
|
|
|
37 |
|
|
|
38 |
UPDATE transaction.`order` o
|
|
|
39 |
LEFT JOIN transaction.transaction_approval ta
|
|
|
40 |
ON ta.id = o.transaction_id AND ta.status = 'REJECTED'
|
|
|
41 |
LEFT JOIN (
|
|
|
42 |
SELECT reference
|
|
|
43 |
FROM transaction.userwallethistory
|
|
|
44 |
WHERE reference_type = 'PURCHASE' AND amount < 0
|
|
|
45 |
GROUP BY reference
|
|
|
46 |
) d ON d.reference = CAST(o.transaction_id AS CHAR)
|
|
|
47 |
SET o.status = 1, -- OrderStatus.PAYMENT_FAILED
|
|
|
48 |
o.refund_timestamp = o.created_timestamp, -- retroactive void (never-real phantom draft)
|
|
|
49 |
o.refunded_by = 'system-cleanup',
|
|
|
50 |
o.refund_reason = 'Abandoned unpaid draft (status=0, wallet never debited, pre-approval-workflow)'
|
|
|
51 |
WHERE o.status = 0
|
|
|
52 |
AND o.billing_timestamp IS NULL
|
|
|
53 |
AND o.refund_timestamp IS NULL
|
|
|
54 |
AND ta.id IS NULL -- not a rejected transaction (Group A/B handled separately)
|
|
|
55 |
AND d.reference IS NULL; -- wallet NEVER debited = phantom draft
|
|
|
56 |
|
|
|
57 |
-- ---------------------------------------------------------------------------
|
|
|
58 |
-- Verification (after UPDATE): the draft class should now be 0.
|
|
|
59 |
-- SELECT COUNT(*) FROM transaction.`order` o
|
|
|
60 |
-- LEFT JOIN transaction.transaction_approval ta ON ta.id=o.transaction_id AND ta.status='REJECTED'
|
|
|
61 |
-- LEFT JOIN (SELECT reference FROM transaction.userwallethistory
|
|
|
62 |
-- WHERE reference_type='PURCHASE' AND amount<0 GROUP BY reference) d
|
|
|
63 |
-- ON d.reference=CAST(o.transaction_id AS CHAR)
|
|
|
64 |
-- WHERE o.status=0 AND o.billing_timestamp IS NULL AND o.refund_timestamp IS NULL
|
|
|
65 |
-- AND ta.id IS NULL AND d.reference IS NULL; -- expect 0
|
|
|
66 |
-- ---------------------------------------------------------------------------
|