diff --git a/apply_guidance.ts b/apply_guidance.ts index e538c2c..afd00d9 100644 --- a/apply_guidance.ts +++ b/apply_guidance.ts @@ -123,40 +123,45 @@ export function apply_guidance(doc: any) { let anchorPrice = null; let anchorSource = null; let bridgePremium = doc.bridgePremium ?? 1.00000; - // Determine the anchor price and source - if (earlyPrice) { - // translate alternate product history to current product quoted - anchorPrice = Number((earlyPrice * bridgePremium).toFixed(5)); - // after the early price is translated see if target is still less - if (targetPrice < anchorPrice) { - anchorSource = 'Target Price'; - anchorPrice = targetPrice; + if (!targetPrice) { + anchorSource = "No target pricing setup"; + doc.finalReason = "No target pricing setup"; + } else { + // Determine the anchor price and source + if (earlyPrice) { + // translate alternate product history to current product quoted + anchorPrice = Number((earlyPrice * bridgePremium).toFixed(5)); + // after the early price is translated see if target is still less + if (targetPrice < anchorPrice) { + anchorSource = `Target Price ${targetPrice}`; + anchorPrice = targetPrice; + } else { + anchorSource = doc.hist.cust.early_season + ' Similar (' + doc.hist.cust.ds + ') Customer Price ' + earlyPrice + ' x ' + doc.bridgePremium + ' = ' + anchorPrice; + } } else { - anchorSource = doc.hist.cust.early_season + ' Similar (' + doc.hist.cust.ds + ') Customer Price ' + earlyPrice + ' x ' + doc.bridgePremium + ' = ' + anchorPrice; + anchorPrice = targetPrice; + anchorSource = `Target Price ${targetPrice}`; } - } else { - anchorPrice = targetPrice; - anchorSource = `Target Price ${targetPrice}`; - } - const inflation = Math.max(...Object.keys(doc.iidx).map(Number)); - const inflationFactor = doc.iidx[inflation] + 1; - var calcPrice = parseFloat((anchorPrice * inflationFactor).toFixed(5)); - let finalReason = ""; - if (calcPrice >= doc.list && doc.list) { - doc.calcCeiling = "Cap At List"; - doc.finalPrice = doc.list; - finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice} but cap at list ${doc.list}`; - } else { - doc.finalPrice = calcPrice; - finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice}`; + const inflation = Math.max(...Object.keys(doc.iidx).map(Number)); + const inflationFactor = doc.iidx[inflation] + 1; + var calcPrice = parseFloat((anchorPrice * inflationFactor).toFixed(5)); + let finalReason = ""; + if (calcPrice >= doc.list && doc.list) { + doc.calcCeiling = "Cap At List"; + doc.finalPrice = doc.list; + finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice} but cap at list ${doc.list}`; + } else { + doc.finalPrice = calcPrice; + finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice}`; + } + doc.anchorPrice = anchorPrice; + doc.anchorSource = anchorSource; + doc.inflationFactor = inflationFactor; + doc.finalReason = finalReason; + doc.bridgePremium = bridgePremium; + doc.targetPrice = targetPrice; } - doc.anchorPrice = anchorPrice; - doc.anchorSource = anchorSource; - doc.inflationFactor = inflationFactor; - doc.finalReason = finalReason; - doc.bridgePremium = bridgePremium; - doc.targetPrice = targetPrice; return doc; } diff --git a/sql/get_guidance.pg.sql b/sql/get_guidance.pg.sql index 554763b..fb19bc2 100644 --- a/sql/get_guidance.pg.sql +++ b/sql/get_guidance.pg.sql @@ -14,6 +14,7 @@ DECLARE _stlc text; _cust text; _curr text; + _rate numeric; _v1ds text; _v0ds text; _v1tp jsonb; @@ -57,10 +58,29 @@ BEGIN --RAISE NOTICE '%', _chan; ----------------customer------------------------------------ - + ----------------customer------------------------------------ SELECT dba INTO _cust FROM rlarp.cust WHERE code = CASE WHEN _chan = 'DRP' THEN _ship ELSE _bill END ; - SELECT currency INTO _curr FROM rlarp.cust WHERE code = _bill; - _rslt = _rslt||jsonb_build_object('cust',_cust,'curr',_curr); + SELECT + currency, + (SELECT + x.rate + FROM + rlarp.ffcret x + WHERE + x.perd = (select fspr from rlarp.gld where drange @> current_date) + AND x.rtyp = 'MA' + and x.fcur = currency + AND x.tcur = 'US' + ) + INTO + _curr + ,_rate + FROM + rlarp.cust + WHERE + code = _bill; + + _rslt = _rslt||jsonb_build_object('cust',_cust,'curr',_curr,'fxrate',_rate); --RAISE NOTICE 'cust %', _cust; ----------------price history------------------------------- diff --git a/sql/get_guidance_dseg.pg.sql b/sql/get_guidance_dseg.pg.sql index 2cf552d..52fb53e 100644 --- a/sql/get_guidance_dseg.pg.sql +++ b/sql/get_guidance_dseg.pg.sql @@ -15,6 +15,7 @@ DECLARE _item text; _cust text; _curr text; + _rate numeric; _v1ds text; _v0ds text; _v1tp jsonb; @@ -25,6 +26,7 @@ DECLARE _targ jsonb; _list jsonb; _iidx jsonb; + _itemr text; BEGIN --_item := 'AMK06000G18B054'; @@ -33,24 +35,22 @@ BEGIN --_qty := 5000; --_seas := 2024; + _v0ds := CASE split_part(substring(_dseg,4,100), '.',1) WHEN 'B' THEN 'BASE' ELSE 'COLOR' END || CASE split_part(substring(_dseg,4,100), '.',2) WHEN 'L' THEN ' LABELED' WHEN 'P' THEN ' PRINTED' ELSE '' END; + _v1ds := _dseg; ----------------base product-------------------------------- - - SELECT part_group ,item ,stlc - ,v1ds - ,v0ds ,idxk + ,prefer INTO _mold ,_item ,_stlc - ,_v1ds - ,_v0ds ,_iidx + ,_itemr FROM ( SELECT @@ -60,7 +60,7 @@ BEGIN ,i.v1ds ,i.v0ds ,jsonb_strip_nulls(jsonb_build_object('assc',CASE WHEN i.assc <> '' THEN i.assc ELSE null::text END,'majg',i.majg::int,'coltier',i.coltier)) idxk - ,CASE WHEN i.v1ds = _v1ds THEN 1 ELSE 0 END prefer + ,CASE WHEN i.v1ds = _v1ds THEN 2 ELSE CASE WHEN i.v0ds = _v0ds THEN 1 ELSE 0 END END prefer FROM "CMS.CUSLG".itemmv i INNER JOIN rlarp.molds m ON @@ -73,12 +73,12 @@ BEGIN ,i.v1ds ,i.v0ds ,jsonb_strip_nulls(jsonb_build_object('assc',CASE WHEN i.assc <> '' THEN i.assc ELSE null::text END,'majg',i.majg::int,'coltier',i.coltier)) - ,CASE WHEN i.v1ds = _v1ds THEN 1 ELSE 0 END + ,CASE WHEN i.v1ds = _v1ds THEN 2 ELSE CASE WHEN i.v0ds = _v0ds THEN 1 ELSE 0 END END ) best ORDER BY prefer DESC LIMIT 1; - _rslt := jsonb_build_object('mold',_mold,'v1ds',_v1ds,'v0ds',_v0ds,'stlc',_stlc,'item',_item,'desg',_dseg)||_iidx; + _rslt := jsonb_build_object('mold',_mold,'v1ds',_v1ds,'v0ds',_v0ds,'stlc',_stlc,'item',_item,'item rel',_itemr,'desg',_dseg)||_iidx; RAISE NOTICE 'item data %', _iidx; ----------------channel------------------------------------- @@ -88,10 +88,28 @@ BEGIN RAISE NOTICE 'chan %', _chan; ----------------customer------------------------------------ - SELECT dba INTO _cust FROM rlarp.cust WHERE code = CASE WHEN _chan = 'DRP' THEN _ship ELSE _bill END ; - SELECT currency INTO _curr FROM rlarp.cust WHERE code = _bill; - _rslt = _rslt||jsonb_build_object('cust',_cust,'curr',_curr); + SELECT + currency, + (SELECT + x.rate + FROM + rlarp.ffcret x + WHERE + x.perd = (select fspr from rlarp.gld where drange @> current_date) + AND x.rtyp = 'MA' + and x.fcur = currency + AND x.tcur = 'US' + ) + INTO + _curr + ,_rate + FROM + rlarp.cust + WHERE + code = _bill; + + _rslt = _rslt||jsonb_build_object('cust',_cust,'curr',_curr,'fxrate',_rate); RAISE NOTICE 'cust %', _cust; ----------------price history------------------------------- @@ -131,7 +149,7 @@ BEGIN WHERE mold = _stlc AND season = _seas - AND data_segment = _v1ds + AND data_segment = _dseg AND region = 'ALL'; --RAISE NOTICE 'target: %', jsonb_pretty(_targ); _rslt := _rslt||COALESCE(_v1tp,'{}'::jsonb);