From b42573a8ffdf0da6a8203e6ca0cbb282a2bc0b20 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Tue, 29 Jul 2025 02:20:10 -0400 Subject: [PATCH] last price age --- new_targets/procs/guidance_logic.pg.sql | 15 ++++++++++----- new_targets/procs/single_price_call.pg.sql | 12 +++++++----- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/new_targets/procs/guidance_logic.pg.sql b/new_targets/procs/guidance_logic.pg.sql index b848648..c5e98cf 100644 --- a/new_targets/procs/guidance_logic.pg.sql +++ b/new_targets/procs/guidance_logic.pg.sql @@ -1,7 +1,8 @@ CREATE OR REPLACE FUNCTION pricequote.guidance_logic( _target_price NUMERIC(20,5), _last_price NUMERIC(20,5), - _list_price NUMERIC(20,5) + _list_price NUMERIC(20,5), + _last_date DATE ) RETURNS TABLE ( guidance_price NUMERIC(20,5), @@ -12,8 +13,9 @@ DECLARE _reason TEXT := ''; _floored NUMERIC(20,5); _capped NUMERIC(20,5); + _is_last_recent BOOLEAN := _last_date IS NOT NULL AND _last_date > CURRENT_DATE - INTERVAL '2 years'; BEGIN - IF _target_price IS NOT NULL AND _last_price IS NOT NULL THEN + IF _target_price IS NOT NULL AND _last_price IS NOT NULL AND _is_last_recent THEN _floored := GREATEST(_target_price, _last_price * 0.95); _capped := LEAST(_floored, _last_price); _price := LEAST(COALESCE(_list_price, 1e9), _capped); @@ -33,16 +35,19 @@ BEGIN END IF; END IF; - ELSIF _last_price IS NOT NULL THEN + ELSIF _last_price IS NOT NULL AND _is_last_recent THEN _price := _last_price; _reason := 'Last price - no target'; - ELSE + ELSIF _target_price IS NOT NULL THEN _price := _target_price; _reason := 'Target price - no prior sale'; + + ELSE + _price := NULL; + _reason := 'No pricing available'; END IF; RETURN QUERY SELECT _price, _reason; END; $$ LANGUAGE plpgsql; - diff --git a/new_targets/procs/single_price_call.pg.sql b/new_targets/procs/single_price_call.pg.sql index 6bdb9e5..9823536 100644 --- a/new_targets/procs/single_price_call.pg.sql +++ b/new_targets/procs/single_price_call.pg.sql @@ -1,3 +1,5 @@ +DROP FUNCTION IF EXISTS pricequote.single_price_call; + CREATE OR REPLACE FUNCTION pricequote.single_price_call( _bill TEXT, _ship TEXT, @@ -18,7 +20,7 @@ RETURNS TABLE ( tier TEXT, pltq NUMERIC, plevel TEXT, - hist JSONB, +-- hist JSONB, last_price NUMERIC, last_date DATE, last_order TEXT, @@ -127,8 +129,8 @@ BEGIN 'last_price', _last_price, 'last_date', _last_date, 'last_order', _last_order, - 'last_quote', _last_quote, - 'full_history_________', _hist + 'last_quote', _last_quote +-- 'full_history_________', _hist ); ------------------------------------------------------------------ @@ -165,7 +167,7 @@ BEGIN INTO _guidance_price, _guidance_reason FROM - pricequote.guidance_logic(_tprice, _last_price, _list_price) gl; + pricequote.guidance_logic(_tprice, _last_price, _list_price, _last_date) gl; _expl := _expl || jsonb_build_object( 'guidance_price', _guidance_price, @@ -179,7 +181,7 @@ BEGIN SELECT _bill, _ship, _part, _stlc, _v1ds, _vol, _chan, _cust, _tier, _pltq, _plevel, - _hist, + -- _hist, _last_price, _last_date, _last_order, _last_quote, _tprice, _guidance_price,