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