Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
37797 amit 1
-- 2026-09-24 - Mark debit notes settled by the pre-workflow returns flow as APPROVED.
2
--
3
-- APPLIED on hadb1 2026-09-24. Recorded here for the audit trail; do not re-run blindly.
4
--
5
-- Why: returns settled before the receive workflow (first purchase_return_order 2026-03-16 12:52) wrote the
6
-- warehouse SALE_RET scan, a returnorderinfo row and a wallet refund, but never touched the debit note. So
7
-- 5,557 fully settled notes still read status='CREATED' with no purchase_return_order, and every receive
8
-- screen decides on exactly those two things - the note looked like it was still awaiting receipt, Receive
9
-- button included. Re-receiving one (DN UPBLY975/4, IMEI 864973083197734) handed a partner back a phone that
10
-- had already been returned, refunded and resold, and a duplicate debit note followed.
11
--
12
-- Evidence rule (mirrors PurchaseReturnServiceImpl.applyReceipt, which refuses to return a unit twice):
13
-- a note qualifies only when EVERY unit on it is
14
--   (a) returned  - a SALE_RET / DOA_IN / SALE_RET_UNUSABLE scan for that unit against the order its
15
--                   invoice was billed on, and
16
--   (b) refunded  - the order's refunded returnorderinfo quantity covers its returned units.
17
-- Serialized units match by IMEI through line_item_imei (holds even where the SALE scan was deleted);
18
-- non-serialized ones qualify only when the invoice has no returnable quantity left for that item.
19
-- End state written is the one the new flow writes at refund: debit_note APPROVED, items RETURNED with
20
-- return_timestamp = the return scan and refund_timestamp = the refund. No purchase_return_order is
21
-- invented - these were never received through the workflow, and a PRO would falsify who received what.
22
--
23
-- Result: 4,154 debit notes and 6,639 return items. Open notes 5,557 -> 1,404.
24
-- Backups: fofo._bak_dn_status_20260924, fofo._bak_dn_pri_status_20260924.
25
-- Per-unit evidence and bucket per unit: fofo._dn_backfill_20260922 (+ _dn_backfill_dn_20260922 per note).
26
--
27
-- NOT backfilled, left at CREATED for review (see the bucket column):
28
--   C_REFUNDED_NOT_RETURNED      1,011 notes - refunded with no return scan (refund-leak / re-bill pattern)
29
--   E_NOT_RETURNED_NOT_REFUNDED    293 notes - raised and abandoned, oldest 2018
30
--   B_RETURNED_NOT_REFUNDED         82 notes - goods back, no refund recorded: partners may be owed money
31
--   D_NO_SALE_SCAN                  24 notes - sale scan deleted or IMEI mismatch
32
--   X_ORDER_NOT_FOUND                2 notes
33
-- Only 5 of the remainder belong to internal stores (fofo_store.internal = 1).
34
 
35
-- Guards matter: the tables below are a snapshot, and the live flow keeps settling notes. Both statements
36
-- re-check status and the absence of a purchase_return_order at execution time (DN 7228 was received
37
-- normally between the snapshot and the run, and correctly dropped out).
38
 
39
UPDATE fofo.debit_note dn
40
  JOIN fofo._dn_backfill_dn_20260922 b ON b.dn = dn.id AND b.units = b.ok_units
41
  SET dn.status = 'APPROVED'
42
  WHERE dn.status = 'CREATED'
43
    AND NOT EXISTS (SELECT 1 FROM fofo.purchase_return_order pro WHERE pro.debit_note_id = dn.id);
44
 
45
UPDATE fofo.purchase_return_item pri
46
  JOIN fofo._dn_backfill_20260922 u ON u.pri = pri.id
47
  JOIN fofo.debit_note dn ON dn.id = pri.debit_note_id AND dn.status = 'APPROVED'
48
  JOIN fofo._dn_backfill_dn_20260922 b ON b.dn = dn.id AND b.units = b.ok_units
49
  SET pri.status = 'RETURNED', pri.return_timestamp = u.returned_at, pri.refund_timestamp = u.refunded_at
50
  WHERE pri.status = 'DEBIT_NOTE_CREATED' AND u.bucket = 'A_RETURNED_REFUNDED'
51
    AND NOT EXISTS (SELECT 1 FROM fofo.purchase_return_order pro WHERE pro.debit_note_id = dn.id);
52
 
53
-- Verify
54
-- SELECT status, COUNT(*) FROM fofo.debit_note GROUP BY status;  -- APPROVED 4539, CANCELLED 6, CREATED 1404
55
-- SELECT COUNT(*) FROM fofo.debit_note dn WHERE dn.status='CREATED'
56
--   AND NOT EXISTS (SELECT 1 FROM fofo.purchase_return_order pro WHERE pro.debit_note_id=dn.id);  -- 1404