incorporate last price logic details

This commit is contained in:
Paul Trowbridge 2025-08-06 23:47:36 -04:00
parent 4b1f6a3136
commit c6f817526f

View File

@ -1,4 +1,4 @@
DROP FUNCTION IF EXISTS pricequote.single_price_call() CASCADE; -- DROP FUNCTION IF EXISTS pricequote.single_price_call() CASCADE;
CREATE OR REPLACE FUNCTION pricequote.single_price_call( CREATE OR REPLACE FUNCTION pricequote.single_price_call(
_bill TEXT, _bill TEXT,
@ -55,8 +55,13 @@ DECLARE
_guidance_reason TEXT; _guidance_reason TEXT;
_hist JSONB := '{}'::jsonb; _hist JSONB := '{}'::jsonb;
-- Last sale/quote/volume/segment fields -- Last sale/quote/volume/segment fields
mrs JSONB; mrq JSONB; lvs JSONB; lvq JSONB; _mrs JSONB;
dss JSONB; dsq JSONB; _mrq JSONB;
_lvs JSONB;
_lvq JSONB;
_dss JSONB;
_dsq JSONB;
-- Precedence chain -- Precedence chain
_last_price NUMERIC; _last_price NUMERIC;
_last_qty NUMERIC; _last_qty NUMERIC;
@ -113,7 +118,7 @@ BEGIN
-- Step 2: Target price logic -- Step 2: Target price logic
------------------------------------------------------------------ ------------------------------------------------------------------
SELECT tp.price, SELECT tp.price,
tp.math, to_json(tp.math),
tp.vol::text tp.vol::text
INTO _tprice, _tmath, _volume_range INTO _tprice, _tmath, _volume_range
FROM pricequote.target_prices tp FROM pricequote.target_prices tp
@ -135,57 +140,57 @@ BEGIN
AND lp.partgroup = _partgroup; AND lp.partgroup = _partgroup;
-- Extract top-level keys -- Extract top-level keys
mrs := _hist -> 'mrs'; _mrs := _hist -> 'mrs';
mrq := _hist -> 'mrq'; _mrq := _hist -> 'mrq';
lvs := _hist -> 'lvs'; _lvs := _hist -> 'lvs';
lvq := _hist -> 'lvq'; _lvq := _hist -> 'lvq';
-- Extract per-datasegment block matching the input v1ds -- Extract per-datasegment block matching the input v1ds
dss := (_hist -> _v1ds) -> 'dss'; _dss := (_hist -> _v1ds) -> 'dss';
dsq := (_hist -> _v1ds) -> 'dsq'; _dsq := (_hist -> _v1ds) -> 'dsq';
-- Precedence chain for last_price, etc. -- Precedence chain for last_price, etc.
_last_price := COALESCE( _last_price := COALESCE(
(dsq->>'price')::numeric, (_dsq->>'price')::numeric,
(dss->>'price')::numeric, (_dss->>'price')::numeric,
(mrq->>'price')::numeric, (_mrq->>'price')::numeric,
(mrs->>'price')::numeric (_mrs->>'price')::numeric
); );
_last_qty := COALESCE( _last_qty := COALESCE(
(dsq->>'qty')::numeric, (_dsq->>'qty')::numeric,
(dss->>'qty')::numeric, (_dss->>'qty')::numeric,
(mrq->>'qty')::numeric, (_mrq->>'qty')::numeric,
(mrs->>'qty')::numeric (_mrs->>'qty')::numeric
); );
_last_dataseg := COALESCE( _last_dataseg := COALESCE(
dsq->>'datasegment', _dsq->>'datasegment',
dss->>'datasegment', _dss->>'datasegment',
mrq->>'datasegment', _mrq->>'datasegment',
mrs->>'datasegment' _mrs->>'datasegment'
); );
_last_date := COALESCE( _last_date := COALESCE(
(dsq->>'odate')::date, (_dsq->>'odate')::date,
(dss->>'odate')::date, (_dss->>'odate')::date,
(mrq->>'odate')::date, (_mrq->>'odate')::date,
(mrs->>'odate')::date (_mrs->>'odate')::date
); );
_last_order := COALESCE( _last_order := COALESCE(
dsq->>'ordnum', _dsq->>'ordnum',
dss->>'ordnum', _dss->>'ordnum',
mrq->>'ordnum', _mrq->>'ordnum',
mrs->>'ordnum' _mrs->>'ordnum'
); );
_last_quote := COALESCE( _last_quote := COALESCE(
dsq->>'quoten', _dsq->>'quoten',
dss->>'quoten', _dss->>'quoten',
mrq->>'quoten', _mrq->>'quoten',
mrs->>'quoten' _mrs->>'quoten'
); );
_last_source := CASE _last_source := CASE
WHEN dsq->>'price' IS NOT NULL THEN 'dsq' WHEN _dsq->>'price' IS NOT NULL THEN 'dsq'
WHEN dss->>'price' IS NOT NULL THEN 'dss' WHEN _dss->>'price' IS NOT NULL THEN 'dss'
WHEN mrq->>'price' IS NOT NULL THEN 'mrq' WHEN _mrq->>'price' IS NOT NULL THEN 'mrq'
WHEN mrs->>'price' IS NOT NULL THEN 'mrs' WHEN _mrs->>'price' IS NOT NULL THEN 'mrs'
ELSE NULL ELSE NULL
END; END;
@ -227,6 +232,14 @@ BEGIN
'last_dataseg', _last_dataseg, 'last_dataseg', _last_dataseg,
'last_source', _last_source, 'last_source', _last_source,
'last_date', _last_date, 'last_date', _last_date,
'last_order', _last_order,
'last_quote', _last_quote,
'mrs', _mrs,
'mrq', _mrq,
'lvs', _lvs,
'lvq', _lvq,
'dss', _dss,
'dsq', _dsq,
'target_price', _tprice, 'target_price', _tprice,
'target_math', _tmath, 'target_math', _tmath,
'calculated_pallets', FLOOR(_vol / NULLIF(_pltq, 0)), 'calculated_pallets', FLOOR(_vol / NULLIF(_pltq, 0)),