Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
36796 amit 1
-- ============================================================================
2
-- Void net-settled stranded orders (status=0 left in "Unbilled PO")
3
-- ============================================================================
4
-- selectPendingIndentWalletAmount counts any billing/refund-NULL order. Some
5
-- status=0 orders had their wallet PURCHASE debit REVERSED via a non-standard
6
-- wallet entry (e.g. OTHERS "Oder Cancelled", "order not placed") -- so the
7
-- transaction nets to ~0 (fully refunded) but the order row stayed status=0 and
8
-- kept inflating unbilled PO. Two sub-classes:
9
--   (a) rejected-txn (transaction_approval REJECTED) the PO_REJECTED backfill
10
--       missed because the refund wasn't the standard "Order canceled" PURCHASE
11
--       entry  -> mark PO_REJECTED(91).
12
--   (b) non-rejected, refunded-by-other-means -> keep status=0, just drop from PI.
13
--
14
-- SAFETY: only net-settled (transaction net wallet >= 0) orders are voided. A
15
-- genuinely committed / awaiting-approval PO is net-DEBITED (net < 0) and is left
16
-- untouched -- this is what protects awaiting-approval bulk POs and real payables.
17
--
18
-- Mechanism: refund_timestamp = created_timestamp (drops from pending indent).
19
-- Use the TEMP-TABLE form (scope wallet net to candidate txns) -- the broad
20
-- PURCHASE-debit aggregate is a full scan that locks the LIVE DB (hadb1). Run with
21
-- `transaction` as default schema.
22
--
23
-- Ran on hadb1 (live) 2026-06-08: Group-B rejected 55 -> status 91; net-settled
24
-- non-rejected 186 -> refund_timestamp. IDEMPOTENT (guard refund_timestamp NULL).
25
-- ============================================================================
26
 
27
-- run with:  mysql ... transaction < void_net_settled_stranded_orders.sql
28
 
29
CREATE TEMPORARY TABLE cand AS
30
  SELECT o.id AS order_id, CAST(o.transaction_id AS CHAR) AS ref,
31
         CASE WHEN ta.id IS NOT NULL THEN 1 ELSE 0 END AS is_rejected
32
  FROM transaction.`order` o
33
  LEFT JOIN transaction.transaction_approval ta ON ta.id=o.transaction_id AND ta.status='REJECTED'
34
  WHERE o.status = 0
35
    AND o.billing_timestamp IS NULL
36
    AND o.refund_timestamp IS NULL
37
    AND COALESCE(o.original_wallet_amount, o.wallet_amount) > 0;
38
ALTER TABLE cand ADD INDEX(ref);
39
 
40
CREATE TEMPORARY TABLE refnet AS
41
  SELECT h.reference AS ref, SUM(h.amount) AS net
42
  FROM transaction.userwallethistory h
43
  WHERE h.reference IN (SELECT ref FROM cand)
44
  GROUP BY h.reference;
45
ALTER TABLE refnet ADD INDEX(ref);
46
 
47
-- net-settled only (net >= -1). Net-committed (net < -1) excluded = real payable / awaiting-approval.
48
CREATE TEMPORARY TABLE void_ids AS
49
  SELECT c.order_id, c.is_rejected
50
  FROM cand c
51
  LEFT JOIN refnet r ON r.ref = c.ref
52
  WHERE COALESCE(r.net, 0) >= -1;
53
 
54
UPDATE transaction.`order` o
55
  JOIN void_ids v ON v.order_id = o.id
56
SET o.status           = CASE WHEN v.is_rejected = 1 THEN 91 ELSE o.status END,  -- 91=PO_REJECTED for rejected txns
57
    o.refund_timestamp = o.created_timestamp,
58
    o.refunded_by      = 'system-cleanup',
59
    o.refund_reason    = CASE WHEN v.is_rejected = 1
60
                              THEN 'PO rejected (refunded via non-standard wallet entry; order row stranded) - excluded from unbilled PO'
61
                              ELSE 'Stranded order, wallet debit already reversed (net-settled); excluded from unbilled PO' END;
62
 
63
-- Verify: no net-settled stranded status=0 orders should remain (only net-committed kept).