Compare commits

..

No commits in common. "7919160ecc7329532589d804e3f51e133564e86c" and "96eee9ff5b440bc3b256eaa6a0c68e4123e84138" have entirely different histories.

2 changed files with 61 additions and 128 deletions

View File

@ -1,43 +1,24 @@
export function apply_guidance(doc: any) { export function apply_guidance(doc: any) {
const targetPrice = doc.pricing?.v1tp ?? doc.pricing?.v0tp; const targetPrice = doc.v1tp ?? doc.v0tp;
const earlyPrice = doc.hist?.cust?.early_price; const earlyPrice = doc.hist?.cust?.early_price;
const earlySeason = doc.hist?.cust?.early_season;
const bridgePremium = doc.pricing?.bridgePremium ?? 1.00000;
const altHist = doc.hist?.cust?.ds;
const iidx = doc.pricing?.iidx;
const curr = doc.customer?.curr;
const fxrate = doc.customer?.fxrate ?? 1.0;
let anchorPrice = null; let anchorPrice = null;
let anchorSource = null; let anchorSource = null;
let custPrice = null; let bridgePremium = doc.bridgePremium ?? 1.00000;
let custSource = null;
let guidance = {};
let calcCeiling = null;
let finalReasonUSD = "";
let finalPriceUSD = null;
let finalReason = "";
let finalPrice = null;
const inflation = Math.max(...Object.keys(iidx).map(Number));
const inflationFactor = iidx[inflation] + 1;
const list = doc.pricing?.list && doc.product?.itemrel === "2" ? doc.pricing?.list : null;
// ------if there is not target price just exit--------------- // ------if there is not target price just exit---------------
if (!targetPrice) { if (!targetPrice) {
anchorSource = "No target pricing setup"; anchorSource = "No target pricing setup";
guidance.FinalReason = "No target pricing setup"; doc.finalReason = "No target pricing setup";
} else { } else {
// if there is no customer anchor price use target // if there is no customer anchor price use target
if (earlyPrice) { if (earlyPrice) {
// translate alternate product history to current product quoted // translate alternate product history to current product quoted
custPrice = Number((earlyPrice * bridgePremium).toFixed(5)); anchorPrice = Number((earlyPrice * bridgePremium).toFixed(5));
anchorPrice = custPrice;
// --------if the price needs bridged, add the details to the description-------- // --------if the price needs bridged, add the details to the description--------
if (bridgePremium === 1) { if (bridgePremium === 1) {
anchorSource = earlySeason + ' Customer Price ' + earlyPrice; anchorSource = doc.hist.cust.early_season + ' Customer Price ' + earlyPrice;
custSource = anchorSource;
} else { } else {
anchorSource = earlySeason + ' Similar (' + altHist + ') Customer Price ' + earlyPrice + ' x ' + bridgePremium + ' = ' + anchorPrice; anchorSource = doc.hist.cust.early_season + ' Similar (' + doc.hist.cust.ds + ') Customer Price ' + earlyPrice + ' x ' + doc.bridgePremium + ' = ' + anchorPrice;
custSource = anchorSource;
} }
// --------after the early price is translated see if target is still less------- // --------after the early price is translated see if target is still less-------
if (targetPrice < anchorPrice) { if (targetPrice < anchorPrice) {
@ -49,41 +30,25 @@ export function apply_guidance(doc: any) {
anchorSource = `Target Price ${targetPrice}`; anchorSource = `Target Price ${targetPrice}`;
} }
//------get the most relevant inflation factor number--------------------------------- //------get the most relevant inflation factor number---------------------------------
//------anchor x inflation / fxrate--------------------------------------------------- const inflation = Math.max(...Object.keys(doc.iidx).map(Number));
let calcPriceUSD = parseFloat((anchorPrice * inflationFactor).toFixed(5)); //------extract the inflation factor using the relevance key--------------------------
let calcPrice = parseFloat((calcPriceUSD / fxrate).toFixed(5)); const inflationFactor = doc.iidx[inflation] + 1;
if (calcPrice >= list && list) { let calcPrice = parseFloat((anchorPrice * inflationFactor).toFixed(5));
calcCeiling = "Cap At List"; let finalReason = "";
//multiply list by FX to get to USD if in CAD if (calcPrice >= doc.list && doc.list) {
finalPrice = list; doc.calcCeiling = "Cap At List";
if (curr === "CA") { doc.finalPrice = doc.list;
finalReason = `${anchorSource} x ${inflationFactor} / ${fxrate} FX = ${calcPrice} CAD, cap at list ${list} CAD`; finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice} but cap at list ${doc.list}`;
} else {
finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice}, cap at list ${list}`;
}
} else {
finalPrice = calcPrice;
finalPriceUSD = calcPriceUSD;
if (curr === "CA") {
finalReason = `${anchorSource} x ${inflationFactor} / ${fxrate} FX = ${calcPrice} CAD`;
} else { } else {
doc.finalPrice = calcPrice;
finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice}`; finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice}`;
} }
doc.anchorPrice = anchorPrice;
doc.anchorSource = anchorSource;
doc.inflationFactor = inflationFactor;
doc.finalReason = finalReason;
doc.bridgePremium = bridgePremium;
doc.targetPrice = targetPrice;
} }
}
guidance.AnchorPrice = anchorPrice;
guidance.AnchorSource = anchorSource;
guidance.CustAnchorPrice = custPrice;
guidance.CustAnchorSource = custSource;
guidance.InflationFactor = inflationFactor;
guidance.Ceiling = calcCeiling;
guidance.FinalPriceUSD = finalPriceUSD;
guidance.FinalReasonUSD = finalReasonUSD;
guidance.FinalPrice = finalPrice;
guidance.FinalReason = finalReason;
guidance.BridgePremium = bridgePremium;
guidance.TargetPrice = targetPrice;
guidance.ListPrice = list;
doc.guidance = guidance;
return doc; return doc;
} }

View File

@ -27,11 +27,6 @@ DECLARE
_list jsonb; _list jsonb;
_iidx jsonb; _iidx jsonb;
_itemr text; _itemr text;
_input jsonb;
_product jsonb;
_customer jsonb;
_hist jsonb;
_pricing jsonb;
BEGIN BEGIN
--_item := 'AMK06000G18B054'; --_item := 'AMK06000G18B054';
@ -43,28 +38,17 @@ BEGIN
_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; _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; _v1ds := _dseg;
_input := jsonb_build_object(
'inputs'
,jsonb_build_object(
'dseg',_dseg,
'v0ds',_v0ds,
'v1ds',_v1ds,
'bill',_bill,
'ship',_ship,
'stlc',_stlc,
'qty',_qty,
'season',_seas
));
----------------base product-------------------------------- ----------------base product--------------------------------
SELECT SELECT
part_group part_group
,item ,item
,stlc
,idxk ,idxk
,prefer ,prefer
INTO INTO
_mold _mold
,_item ,_item
,_stlc
,_iidx ,_iidx
,_itemr ,_itemr
FROM FROM
@ -94,21 +78,14 @@ BEGIN
ORDER BY ORDER BY
prefer DESC prefer DESC
LIMIT 1; LIMIT 1;
_product := jsonb_build_object( _rslt := jsonb_build_object('mold',_mold,'v1ds',_v1ds,'v0ds',_v0ds,'stlc',_stlc,'item',_item,'item rel',_itemr,'desg',_dseg)||_iidx;
'product', RAISE NOTICE 'item data %', _iidx;
jsonb_build_object(
'mold',_mold
,'item',_item
,'itemrel',_itemr
,'iidx',_iidx
)
);
--RAISE NOTICE 'item data %', jsonb_pretty(_product||_input);
----------------channel------------------------------------- ----------------channel-------------------------------------
SELECT rlarp.channel_code(_bill, _ship) INTO _chan; SELECT rlarp.channel_code(_bill, _ship) INTO _chan;
_customer := jsonb_build_object('chan',_chan); _rslt := _rslt||jsonb_build_object('chan',_chan);
RAISE NOTICE 'chan %', _chan;
----------------customer------------------------------------ ----------------customer------------------------------------
SELECT dba INTO _cust FROM rlarp.cust WHERE code = CASE WHEN _chan = 'DRP' THEN _ship ELSE _bill END ; SELECT dba INTO _cust FROM rlarp.cust WHERE code = CASE WHEN _chan = 'DRP' THEN _ship ELSE _bill END ;
@ -132,26 +109,19 @@ BEGIN
WHERE WHERE
code = _bill; code = _bill;
_customer := jsonb_build_object( _rslt = _rslt||jsonb_build_object('cust',_cust,'curr',_curr,'fxrate',_rate);
'customer', RAISE NOTICE 'cust %', _cust;
_customer||jsonb_build_object(
'cust',_cust
,'curr',_curr
,'fxrate',_rate
)
);
--RAISE NOTICE 'cust %', jsonb_pretty(_customer);
----------------price history------------------------------- ----------------price history-------------------------------
SELECT jsonb_build_object('hist',rlarp.get_hist(_mold, _v1ds, _cust, substring(_chan,1,1))) INTO _hist; SELECT _rslt||jsonb_build_object('hist',rlarp.get_hist(_mold, _v1ds, _cust, substring(_chan,1,1))) INTO _rslt ;
--RAISE NOTICE 'history %', jsonb_pretty(_hist); RAISE NOTICE 'result %', _rslt;
----------------target pricing------------------------------ ----------------target pricing------------------------------
SELECT SELECT
jsonb_build_object( jsonb_build_object(
'v0tp', 'v0tp',
target_price, target_price,
'v0stdv', 'stdv',
stdev_price stdev_price
) )
INTO INTO
@ -163,12 +133,13 @@ BEGIN
AND season = _seas AND season = _seas
AND data_segment = _v0ds AND data_segment = _v0ds
AND region = 'ALL'; AND region = 'ALL';
_rslt := _rslt||COALESCE(_v0tp,'{}'::jsonb);
----------------target pricing------------------------------ ----------------target pricing------------------------------
SELECT SELECT
jsonb_build_object( jsonb_build_object(
'v1tp', 'v1tp',
target_price, target_price,
'v1stdv', 'stdv',
stdev_price stdev_price
) )
INTO INTO
@ -181,9 +152,10 @@ BEGIN
AND data_segment = _dseg AND data_segment = _dseg
AND region = 'ALL'; AND region = 'ALL';
--RAISE NOTICE 'target: %', jsonb_pretty(_targ); --RAISE NOTICE 'target: %', jsonb_pretty(_targ);
_pricing := (COALESCE(_v0tp,'{}'::jsonb)||COALESCE(_v1tp,'{}'::jsonb)); _rslt := _rslt||COALESCE(_v1tp,'{}'::jsonb);
----------------inflation index----------------------------- ----------------inflation index-----------------------------
RAISE NOTICE 'infaltion : %', jsonb_pretty(_iidx);
SELECT SELECT
jsonb_build_object( jsonb_build_object(
'iidx' 'iidx'
@ -204,21 +176,17 @@ BEGIN
) )
GROUP BY GROUP BY
priority; priority;
_pricing := _pricing||COALESCE(_iidx,'{}'::jsonb); _rslt := _rslt||COALESCE(_iidx,'{}'::jsonb);
--RAISE NOTICE 'add targets: %', jsonb_pretty(_pricing);
----------------list pricing--------------------------------- ----------------list pricing---------------------------------
SELECT coalesce(rlarp.get_list(_bill, _ship, _item, _qty),'{}'::jsonb) INTO _list; SELECT coalesce(rlarp.get_list(_bill, _ship, _item, _qty),'{}'::jsonb) INTO _list;
_pricing := _pricing||_list; _rslt := _rslt||_list;
--RAISE NOTICE 'add list: %', jsonb_pretty(_pricing); --RAISE NOTICE 'list: %', jsonb_pretty(_list);
----------------get premium for quote hist gap-------------- ----------------get premium for quote hist gap--------------
SELECT coalesce(rlarp.get_premium(_stlc, _seas, (SELECT xchan FROM _chx WHERE chan = _chan),_hist->'hist'->'cust'->>'ds', _v1ds),'{}'::jsonb) INTO _prem; SELECT coalesce(rlarp.get_premium(_stlc, _seas, (SELECT xchan FROM _chx WHERE chan = _chan),_rslt->'hist'->'cust'->>'ds', _v1ds),'{}'::jsonb) INTO _prem;
_pricing := jsonb_build_object('pricing',_pricing||_prem); _rslt := _rslt||_prem;
--RAISE NOTICE 'add bridge: %', jsonb_pretty(_pricing); --RAISE NOTICE 'list: %', jsonb_pretty(_list);
_rslt := _input||_product||_customer||_pricing||_hist;
RETURN _rslt; RETURN _rslt;