From c46d4c25d7371e82853c2f774e003bbd35be3112 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Thu, 14 Aug 2025 01:50:57 -0400 Subject: [PATCH] link in cost, rounding, target for last price should be based on last price qty --- procs/single_price_call.ms.sql | 2 +- procs/single_price_call.pg.sql | 45 +++++++++++++++++----------------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/procs/single_price_call.ms.sql b/procs/single_price_call.ms.sql index f20a84e..3240130 100644 --- a/procs/single_price_call.ms.sql +++ b/procs/single_price_call.ms.sql @@ -173,7 +173,7 @@ BEGIN WHEN 'DIS' THEN bc.dba ELSE sc.dba END - ELSE q.bill + ELSE bc.dba END, pltq = i.mpck, plevel = diff --git a/procs/single_price_call.pg.sql b/procs/single_price_call.pg.sql index f1f9521..c4c0307 100644 --- a/procs/single_price_call.pg.sql +++ b/procs/single_price_call.pg.sql @@ -289,32 +289,30 @@ BEGIN AND tp.ds = _last_dataseg AND tp.chan = _chan AND tp.tier = _tier - AND FLOOR(_vol / NULLIF(_pltq, 0))::int <@ tp.vol; + AND FLOOR(_last_qty / _pltq)::int <@ tp.vol; ------------------------------------------------------------------ -- Step 4: Cost data for normalization ------------------------------------------------------------------ - -- Current/future standard for requested v1ds - SELECT - curstdus, futstdus - INTO - _curstd, _futstd - FROM - "CMS.CUSLG".itemm i - WHERE - i.item = _part - AND i.v1ds = _v1ds; - -- Current/future standard for last_dataseg - SELECT - curstdus, futstdus - INTO - _curstd_last, _futstd_last - FROM - "CMS.CUSLG".itemm i - WHERE - i.item = _part - AND i.v1ds = _last_dataseg; + SELECT + ROUND(CASE WHEN COALESCE(_customized,'') = '' THEN _curstd_orig ELSE COALESCE(v1.curstdus, v0.curstdus) END,5) AS curstd, + ROUND(CASE WHEN COALESCE(_customized,'') = '' THEN _futstd_orig ELSE COALESCE(v1.futstdus, v0.futstdus) END,5) AS futstd, + ROUND(CASE WHEN COALESCE(_last_isdiff,'') = '' THEN _curstd_orig ELSE COALESCE(v1l.curstdus, v0l.curstdus) END,5) AS curstd_last, + ROUND(CASE WHEN COALESCE(_last_isdiff,'') = '' THEN _futstd_orig ELSE COALESCE(v1l.futstdus, v0l.futstdus) END,5) AS futstd_last + INTO + _curstd, _futstd, _curstd_last, _futstd_last + FROM (VALUES (1)) AS x(dummy) + LEFT JOIN rlarp.cost_v1ds v1 + ON v1.stlc = _stlc AND v1.v1ds = _v1ds + LEFT JOIN rlarp.cost_v0ds v0 + ON v0.stlc = _stlc AND v0.v0ds = _v0ds + LEFT JOIN rlarp.cost_v1ds v1l + ON v1l.stlc = _stlc AND v1l.v1ds = _last_dataseg + LEFT JOIN rlarp.cost_v0ds v0l + ON v0l.stlc = _stlc AND v0l.v0ds = _last_v0ds + LIMIT 1; + ------------------------------------------------------------------ -- Step 5: Normalize last price if needed @@ -325,7 +323,7 @@ BEGIN _last_price_norm := ROUND(_last_price * (_tprice / _tprice_last), 5); _last_premium_method := 'Target Price Ratio'; ELSIF _curstd_last IS NOT NULL AND _curstd IS NOT NULL AND _curstd_last <> 0 THEN - _last_premium := _curstd / _curstd_last; + _last_premium := ROUND(_curstd / _curstd_last, 5); _last_price_norm := ROUND(_last_price * (_curstd / _curstd_last), 5); _last_premium_method := 'Cost Ratio'; ELSE @@ -425,7 +423,8 @@ BEGIN 'targets', jsonb_build_object( 'target_price', _tprice, - 'target_math', _tmath + 'target_math', _tmath, + 'volume_range', _volume_range ), 'list', jsonb_build_object(