use partgroup to get price history, and don't overwrite the json if the target is null

This commit is contained in:
Paul Trowbridge 2025-07-29 14:53:20 -04:00
parent d31c557862
commit 42dd68b611
2 changed files with 29 additions and 7 deletions

View File

@ -45,8 +45,10 @@ DECLARE
_list_code TEXT;
_hist JSONB := '{}'::jsonb;
_expl JSONB := '{}'::jsonb;
_this JSONB := '{}'::jsonb;
_guidance_price NUMERIC;
_guidance_reason TEXT;
_partgroup TEXT;
BEGIN
------------------------------------------------------------------
-- Step 1: Resolve customer metadata
@ -80,13 +82,16 @@ BEGIN
ELSE bc.plevel
END
ELSE bc.plevel
END
INTO _pltq, _chan, _tier, _cust, _plevel
END,
i.partgroup
INTO _pltq, _chan, _tier, _cust, _plevel, _partgroup
FROM rlarp.cust bc
LEFT JOIN rlarp.cust sc ON sc.code = _ship
LEFT JOIN "CMS.CUSLG".itemm i ON i.item = _part
WHERE bc.code = _bill;
-- RAISE NOTICE 'Step 1: %', _expl;
------------------------------------------------------------------
-- Step 2: Target price logic
------------------------------------------------------------------
@ -102,7 +107,7 @@ BEGIN
'tier', TRIM(_tier),
'target math', tp.math
)
INTO _tprice, _expl
INTO _tprice, _this
FROM pricequote.target_prices tp
WHERE tp.stlc = _stlc
AND tp.ds = _v1ds
@ -110,6 +115,12 @@ BEGIN
AND tp.tier = _tier
AND FLOOR(_vol / NULLIF(_pltq, 0))::int <@ tp.vol;
IF _this IS NOT NULL THEN
_expl := _expl || _this;
END IF;
-- RAISE NOTICE 'Step 2: %', _expl;
------------------------------------------------------------------
-- Step 3: Last sale data
------------------------------------------------------------------
@ -123,7 +134,7 @@ BEGIN
_last_price, _last_date, _last_order, _last_quote, _hist
FROM pricequote.lastprice lp
WHERE lp.customer = _cust
AND lp.partgroup = SUBSTRING(_part, 1, 8);
AND lp.partgroup = _partgroup;
_expl := _expl || jsonb_build_object(
'last_price', _last_price,
@ -133,6 +144,8 @@ BEGIN
-- 'full_history_________', _hist
);
-- RAISE NOTICE 'Step 3: %', _expl;
------------------------------------------------------------------
-- Step 4: List price
------------------------------------------------------------------
@ -157,6 +170,8 @@ BEGIN
'list_code', _list_code
);
-- RAISE NOTICE 'Step 4: %', _expl;
------------------------------------------------------------------
-- Step 5: Compute guidance price and embed it
------------------------------------------------------------------
@ -174,6 +189,9 @@ BEGIN
'guidance_reason', _guidance_reason
);
-- RAISE NOTICE 'Step 5: %', _expl;
------------------------------------------------------------------
-- Final: Return row
------------------------------------------------------------------

View File

@ -22,7 +22,9 @@ CREATE TABLE pricequote.queue (
list_code TEXT,
guidance_price NUMERIC,
guidance_reason TEXT,
expl JSONB
expl JSONB,
this JSONB,
partgroup TEXT
);
@ -118,7 +120,8 @@ BEGIN
ELSE bc.plevel
END
ELSE bc.plevel
END AS plevel
END AS plevel,
i.partgroup
FROM pricequote.queue q
JOIN rlarp.cust bc ON bc.code = q.bill
LEFT JOIN rlarp.cust sc ON sc.code = q.ship
@ -131,7 +134,8 @@ BEGIN
tier = src.tier,
cust = src.cust,
pltq = src.pltq,
plevel = src.plevel;
plevel = src.plevel,
partgroup = src.partgroup;
-----------------------------------------------------------------------
-- Step 3: Apply target prices and embed target metadata