Spaces:
Runtime error
Runtime error
Commit ·
ebb7306
1
Parent(s): 1720144
feat: Implement bulk stock movement and item detail retrieval functions
Browse files
app/sql/apply_bulk_stock_movements.sql
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-- FUNCTION: trans.apply_bulk_stock_movements(jsonb)
|
| 2 |
+
|
| 3 |
+
-- DROP FUNCTION IF EXISTS trans.apply_bulk_stock_movements(jsonb);
|
| 4 |
+
|
| 5 |
+
CREATE OR REPLACE FUNCTION trans.apply_bulk_stock_movements(
|
| 6 |
+
p_movements jsonb)
|
| 7 |
+
RETURNS jsonb
|
| 8 |
+
LANGUAGE 'plpgsql'
|
| 9 |
+
COST 100
|
| 10 |
+
VOLATILE PARALLEL UNSAFE
|
| 11 |
+
AS $BODY$
|
| 12 |
+
DECLARE
|
| 13 |
+
v_movement JSONB;
|
| 14 |
+
v_results JSONB := '[]'::JSONB;
|
| 15 |
+
v_result JSONB;
|
| 16 |
+
v_success BOOLEAN := TRUE;
|
| 17 |
+
v_error_msg TEXT;
|
| 18 |
+
BEGIN
|
| 19 |
+
-- Process each movement in the array
|
| 20 |
+
FOR v_movement IN SELECT * FROM jsonb_array_elements(p_movements)
|
| 21 |
+
LOOP
|
| 22 |
+
BEGIN
|
| 23 |
+
-- Apply individual stock movement using the 9-parameter function
|
| 24 |
+
PERFORM trans.apply_stock_movement(
|
| 25 |
+
(v_movement->>'merchant_id')::TEXT,
|
| 26 |
+
(v_movement->>'location_id')::TEXT,
|
| 27 |
+
(v_movement->>'sku')::TEXT,
|
| 28 |
+
(v_movement->>'batch_no')::TEXT,
|
| 29 |
+
(v_movement->>'catalogue_id')::TEXT,
|
| 30 |
+
(v_movement->>'expiry_date')::DATE,
|
| 31 |
+
(v_movement->>'uom')::TEXT,
|
| 32 |
+
(v_movement->>'qty')::NUMERIC,
|
| 33 |
+
(v_movement->>'txn_type')::TEXT,
|
| 34 |
+
(v_movement->>'ref_type')::TEXT,
|
| 35 |
+
(v_movement->>'ref_id')::UUID,
|
| 36 |
+
(v_movement->>'created_by')::TEXT
|
| 37 |
+
);
|
| 38 |
+
|
| 39 |
+
-- Build success result object
|
| 40 |
+
v_result := jsonb_build_object(
|
| 41 |
+
'ledger_id', gen_random_uuid(),
|
| 42 |
+
'sku', v_movement->>'sku',
|
| 43 |
+
'qty', v_movement->>'qty',
|
| 44 |
+
'success', true,
|
| 45 |
+
'error', null
|
| 46 |
+
);
|
| 47 |
+
|
| 48 |
+
EXCEPTION WHEN OTHERS THEN
|
| 49 |
+
-- Handle individual movement errors
|
| 50 |
+
v_success := FALSE;
|
| 51 |
+
v_error_msg := SQLERRM;
|
| 52 |
+
|
| 53 |
+
v_result := jsonb_build_object(
|
| 54 |
+
'ledger_id', null,
|
| 55 |
+
'sku', v_movement->>'sku',
|
| 56 |
+
'qty', v_movement->>'qty',
|
| 57 |
+
'success', false,
|
| 58 |
+
'error', v_error_msg
|
| 59 |
+
);
|
| 60 |
+
END;
|
| 61 |
+
|
| 62 |
+
-- Add to results array
|
| 63 |
+
v_results := v_results || v_result;
|
| 64 |
+
END LOOP;
|
| 65 |
+
|
| 66 |
+
-- If any movement failed, rollback the entire transaction
|
| 67 |
+
IF NOT v_success THEN
|
| 68 |
+
RAISE EXCEPTION 'One or more stock movements failed. Transaction rolled back.';
|
| 69 |
+
END IF;
|
| 70 |
+
|
| 71 |
+
RETURN v_results;
|
| 72 |
+
END;
|
| 73 |
+
$BODY$;
|
| 74 |
+
|
| 75 |
+
ALTER FUNCTION trans.apply_bulk_stock_movements(jsonb)
|
| 76 |
+
OWNER TO trans_owner;
|
| 77 |
+
|
app/sql/apply_stock_movement.sql
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-- FUNCTION: trans.apply_stock_movement(text, text, text, text, text, date, text, numeric, text, text, uuid, text)
|
| 2 |
+
|
| 3 |
+
-- DROP FUNCTION IF EXISTS trans.apply_stock_movement(text, text, text, text, text, date, text, numeric, text, text, uuid, text);
|
| 4 |
+
|
| 5 |
+
CREATE OR REPLACE FUNCTION trans.apply_stock_movement(
|
| 6 |
+
p_merchant_id text,
|
| 7 |
+
p_location_id text,
|
| 8 |
+
p_sku text,
|
| 9 |
+
p_batch_no text,
|
| 10 |
+
p_catalogue_id text,
|
| 11 |
+
p_expiry_date date,
|
| 12 |
+
p_uom text,
|
| 13 |
+
p_qty numeric,
|
| 14 |
+
p_txn_type text,
|
| 15 |
+
p_ref_type text,
|
| 16 |
+
p_ref_id uuid,
|
| 17 |
+
p_user text)
|
| 18 |
+
RETURNS void
|
| 19 |
+
LANGUAGE 'plpgsql'
|
| 20 |
+
COST 100
|
| 21 |
+
VOLATILE PARALLEL UNSAFE
|
| 22 |
+
AS $BODY$
|
| 23 |
+
DECLARE
|
| 24 |
+
v_ledger_id uuid := gen_random_uuid();
|
| 25 |
+
v_qty_change numeric;
|
| 26 |
+
BEGIN
|
| 27 |
+
----------------------------------------------------------------
|
| 28 |
+
-- Idempotency check
|
| 29 |
+
----------------------------------------------------------------
|
| 30 |
+
IF EXISTS (
|
| 31 |
+
SELECT 1
|
| 32 |
+
FROM trans.scm_stock_ledger
|
| 33 |
+
WHERE ref_type = p_ref_type
|
| 34 |
+
AND ref_id = p_ref_id
|
| 35 |
+
AND sku = p_sku
|
| 36 |
+
AND batch_no = p_batch_no
|
| 37 |
+
) THEN
|
| 38 |
+
RETURN;
|
| 39 |
+
END IF;
|
| 40 |
+
|
| 41 |
+
----------------------------------------------------------------
|
| 42 |
+
-- Determine quantity change based on transaction type
|
| 43 |
+
----------------------------------------------------------------
|
| 44 |
+
CASE p_txn_type
|
| 45 |
+
WHEN 'GRN_IN' THEN
|
| 46 |
+
v_qty_change := p_qty; -- Add stock
|
| 47 |
+
WHEN 'ADJUST_IN' THEN
|
| 48 |
+
v_qty_change := p_qty; -- Add stock
|
| 49 |
+
WHEN 'RETURN_IN' THEN
|
| 50 |
+
v_qty_change := p_qty; -- Add stock
|
| 51 |
+
WHEN 'TRANSFER_IN' THEN
|
| 52 |
+
v_qty_change := p_qty; -- Add stock
|
| 53 |
+
WHEN 'ADJUST_OUT' THEN
|
| 54 |
+
v_qty_change := -p_qty; -- Subtract stock (negate the positive qty)
|
| 55 |
+
WHEN 'SALE_OUT' THEN
|
| 56 |
+
v_qty_change := -p_qty; -- Subtract stock
|
| 57 |
+
WHEN 'TRANSFER_OUT' THEN
|
| 58 |
+
v_qty_change := -p_qty; -- Subtract stock
|
| 59 |
+
ELSE
|
| 60 |
+
RAISE EXCEPTION 'Unknown transaction type: %', p_txn_type;
|
| 61 |
+
END CASE;
|
| 62 |
+
|
| 63 |
+
Raise Notice 'in';
|
| 64 |
+
RAISE NOTICE 'Transaction type: %, Input qty: %, Calculated change: %',
|
| 65 |
+
p_txn_type, p_qty, v_qty_change;
|
| 66 |
+
|
| 67 |
+
RAISE NOTICE 'Inserting ledger: %, %, %, %, %, %, %, %, %, %, %',
|
| 68 |
+
v_ledger_id,
|
| 69 |
+
p_merchant_id,
|
| 70 |
+
p_location_id,
|
| 71 |
+
p_sku,
|
| 72 |
+
p_batch_no,
|
| 73 |
+
p_txn_type,
|
| 74 |
+
v_qty_change, -- Store the calculated change in ledger
|
| 75 |
+
p_ref_type,
|
| 76 |
+
p_ref_id,
|
| 77 |
+
p_user,
|
| 78 |
+
NOW();
|
| 79 |
+
|
| 80 |
+
----------------------------------------------------------------
|
| 81 |
+
-- Ledger insert (store the ACTUAL qty change with proper sign)
|
| 82 |
+
----------------------------------------------------------------
|
| 83 |
+
INSERT INTO trans.scm_stock_ledger (
|
| 84 |
+
ledger_id,
|
| 85 |
+
merchant_id,
|
| 86 |
+
location_id,
|
| 87 |
+
sku,
|
| 88 |
+
batch_no,
|
| 89 |
+
txn_type,
|
| 90 |
+
qty,
|
| 91 |
+
ref_type,
|
| 92 |
+
ref_id,
|
| 93 |
+
created_by,
|
| 94 |
+
created_at
|
| 95 |
+
) VALUES (
|
| 96 |
+
v_ledger_id,
|
| 97 |
+
p_merchant_id,
|
| 98 |
+
p_location_id,
|
| 99 |
+
p_sku,
|
| 100 |
+
p_batch_no,
|
| 101 |
+
p_txn_type,
|
| 102 |
+
v_qty_change, -- Store with correct sign
|
| 103 |
+
p_ref_type,
|
| 104 |
+
p_ref_id,
|
| 105 |
+
p_user,
|
| 106 |
+
NOW()
|
| 107 |
+
);
|
| 108 |
+
|
| 109 |
+
RAISE NOTICE 'Stock ledger done';
|
| 110 |
+
|
| 111 |
+
----------------------------------------------------------------
|
| 112 |
+
-- Stock snapshot upsert (ALL TABLE COLUMNS)
|
| 113 |
+
----------------------------------------------------------------
|
| 114 |
+
INSERT INTO trans.scm_stock (
|
| 115 |
+
merchant_id,
|
| 116 |
+
location_id,
|
| 117 |
+
sku,
|
| 118 |
+
batch_no,
|
| 119 |
+
catalogue_id,
|
| 120 |
+
uom,
|
| 121 |
+
qty_on_hand,
|
| 122 |
+
qty_reserved,
|
| 123 |
+
qty_available,
|
| 124 |
+
cost_price,
|
| 125 |
+
expiry_date,
|
| 126 |
+
ledger_id,
|
| 127 |
+
created_at,
|
| 128 |
+
updated_at
|
| 129 |
+
) VALUES (
|
| 130 |
+
p_merchant_id,
|
| 131 |
+
p_location_id,
|
| 132 |
+
p_sku,
|
| 133 |
+
p_batch_no,
|
| 134 |
+
p_catalogue_id,
|
| 135 |
+
p_uom,
|
| 136 |
+
GREATEST(v_qty_change, 0), -- Use calculated change (don't allow negative on insert)
|
| 137 |
+
0, -- qty_reserved
|
| 138 |
+
GREATEST(v_qty_change, 0), -- qty_available
|
| 139 |
+
NULL, -- cost_price
|
| 140 |
+
p_expiry_date, -- expiry_date
|
| 141 |
+
v_ledger_id,
|
| 142 |
+
NOW(),
|
| 143 |
+
NOW()
|
| 144 |
+
)
|
| 145 |
+
ON CONFLICT (merchant_id, location_id, catalogue_id, batch_no)
|
| 146 |
+
DO UPDATE SET
|
| 147 |
+
qty_on_hand = GREATEST(trans.scm_stock.qty_on_hand + v_qty_change, 0),
|
| 148 |
+
qty_available = GREATEST((trans.scm_stock.qty_on_hand + v_qty_change) - trans.scm_stock.qty_reserved, 0),
|
| 149 |
+
ledger_id = v_ledger_id,
|
| 150 |
+
updated_at = NOW();
|
| 151 |
+
|
| 152 |
+
RAISE NOTICE 'Stock updated. Final qty change applied: %', v_qty_change;
|
| 153 |
+
END;
|
| 154 |
+
$BODY$;
|
| 155 |
+
|
| 156 |
+
ALTER FUNCTION trans.apply_stock_movement(text, text, text, text, text, date, text, numeric, text, text, uuid, text)
|
| 157 |
+
OWNER TO trans_owner;
|
| 158 |
+
|
app/sql/fn_get_po_item_details.sql
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-- FUNCTION: trans.fn_get_po_item_details(uuid)
|
| 2 |
+
|
| 3 |
+
-- DROP FUNCTION IF EXISTS trans.fn_get_po_item_details(uuid);
|
| 4 |
+
|
| 5 |
+
CREATE OR REPLACE FUNCTION trans.fn_get_po_item_details(
|
| 6 |
+
p_po_id uuid)
|
| 7 |
+
RETURNS TABLE(po_id uuid, catalogue_code text, catalogue_name text, hsn_code text, gst_rate numeric, ord_qty numeric, dispatched_qty numeric, rcvd_qty numeric, rejected_qty numeric, returned_qty numeric, net_accepted_qty numeric, unit_price numeric, taxable_amount numeric, gst_amount numeric, final_amount numeric)
|
| 8 |
+
LANGUAGE 'plpgsql'
|
| 9 |
+
COST 100
|
| 10 |
+
VOLATILE PARALLEL UNSAFE
|
| 11 |
+
ROWS 1000
|
| 12 |
+
|
| 13 |
+
AS $BODY$
|
| 14 |
+
BEGIN
|
| 15 |
+
RETURN QUERY
|
| 16 |
+
SELECT
|
| 17 |
+
poi.po_id,
|
| 18 |
+
|
| 19 |
+
cr.catalogue_code,
|
| 20 |
+
cr.catalogue_name,
|
| 21 |
+
cr.hsn_code,
|
| 22 |
+
cr.gst_rate,
|
| 23 |
+
|
| 24 |
+
poi.ord_qty,
|
| 25 |
+
poi.dispatched_qty,
|
| 26 |
+
poi.rcvd_qty,
|
| 27 |
+
poi.rejected_qty,
|
| 28 |
+
poi.returned_qty,
|
| 29 |
+
(poi.rcvd_qty - poi.rejected_qty - poi.returned_qty) AS net_accepted_qty,
|
| 30 |
+
|
| 31 |
+
poi.unit_price,
|
| 32 |
+
poi.line_amt AS taxable_amount,
|
| 33 |
+
poi.tax_amt AS gst_amount,
|
| 34 |
+
|
| 35 |
+
(poi.line_amt + poi.tax_amt) AS final_amount
|
| 36 |
+
FROM trans.scm_po_item poi
|
| 37 |
+
JOIN trans.catalogue_ref cr
|
| 38 |
+
ON cr.catalogue_id = poi.catalogue_id
|
| 39 |
+
WHERE poi.po_id = p_po_id;
|
| 40 |
+
END;
|
| 41 |
+
$BODY$;
|
| 42 |
+
|
| 43 |
+
ALTER FUNCTION trans.fn_get_po_item_details(uuid)
|
| 44 |
+
OWNER TO trans_owner;
|
| 45 |
+
|