Compare commits

...

4 Commits

2 changed files with 128 additions and 61 deletions

View File

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

View File

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