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

View File

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