Compare commits
No commits in common. "7919160ecc7329532589d804e3f51e133564e86c" and "96eee9ff5b440bc3b256eaa6a0c68e4123e84138" have entirely different histories.
7919160ecc
...
96eee9ff5b
@ -1,43 +1,24 @@
|
||||
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 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 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;
|
||||
let bridgePremium = doc.bridgePremium ?? 1.00000;
|
||||
// ------if there is not target price just exit---------------
|
||||
if (!targetPrice) {
|
||||
anchorSource = "No target pricing setup";
|
||||
guidance.FinalReason = "No target pricing setup";
|
||||
doc.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
|
||||
custPrice = Number((earlyPrice * bridgePremium).toFixed(5));
|
||||
anchorPrice = custPrice;
|
||||
anchorPrice = Number((earlyPrice * bridgePremium).toFixed(5));
|
||||
// --------if the price needs bridged, add the details to the description--------
|
||||
if (bridgePremium === 1) {
|
||||
anchorSource = earlySeason + ' Customer Price ' + earlyPrice;
|
||||
custSource = anchorSource;
|
||||
anchorSource = doc.hist.cust.early_season + ' Customer Price ' + earlyPrice;
|
||||
} else {
|
||||
anchorSource = earlySeason + ' Similar (' + altHist + ') Customer Price ' + earlyPrice + ' x ' + bridgePremium + ' = ' + anchorPrice;
|
||||
custSource = anchorSource;
|
||||
anchorSource = doc.hist.cust.early_season + ' Similar (' + doc.hist.cust.ds + ') Customer Price ' + earlyPrice + ' x ' + doc.bridgePremium + ' = ' + anchorPrice;
|
||||
}
|
||||
// --------after the early price is translated see if target is still less-------
|
||||
if (targetPrice < anchorPrice) {
|
||||
@ -49,41 +30,25 @@ export function apply_guidance(doc: any) {
|
||||
anchorSource = `Target Price ${targetPrice}`;
|
||||
}
|
||||
//------get the most relevant inflation factor number---------------------------------
|
||||
//------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}`;
|
||||
}
|
||||
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}`;
|
||||
} else {
|
||||
finalPrice = calcPrice;
|
||||
finalPriceUSD = calcPriceUSD;
|
||||
if (curr === "CA") {
|
||||
finalReason = `${anchorSource} x ${inflationFactor} / ${fxrate} FX = ${calcPrice} CAD`;
|
||||
} else {
|
||||
finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice}`;
|
||||
}
|
||||
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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
@ -10,28 +10,23 @@ 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;
|
||||
_input jsonb;
|
||||
_product jsonb;
|
||||
_customer jsonb;
|
||||
_hist jsonb;
|
||||
_pricing jsonb;
|
||||
_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;
|
||||
|
||||
BEGIN
|
||||
--_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;
|
||||
_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
|
||||
@ -94,21 +78,14 @@ BEGIN
|
||||
ORDER BY
|
||||
prefer DESC
|
||||
LIMIT 1;
|
||||
_product := jsonb_build_object(
|
||||
'product',
|
||||
jsonb_build_object(
|
||||
'mold',_mold
|
||||
,'item',_item
|
||||
,'itemrel',_itemr
|
||||
,'iidx',_iidx
|
||||
)
|
||||
);
|
||||
--RAISE NOTICE 'item data %', jsonb_pretty(_product||_input);
|
||||
_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-------------------------------------
|
||||
|
||||
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------------------------------------
|
||||
SELECT dba INTO _cust FROM rlarp.cust WHERE code = CASE WHEN _chan = 'DRP' THEN _ship ELSE _bill END ;
|
||||
@ -132,26 +109,19 @@ BEGIN
|
||||
WHERE
|
||||
code = _bill;
|
||||
|
||||
_customer := jsonb_build_object(
|
||||
'customer',
|
||||
_customer||jsonb_build_object(
|
||||
'cust',_cust
|
||||
,'curr',_curr
|
||||
,'fxrate',_rate
|
||||
)
|
||||
);
|
||||
--RAISE NOTICE 'cust %', jsonb_pretty(_customer);
|
||||
_rslt = _rslt||jsonb_build_object('cust',_cust,'curr',_curr,'fxrate',_rate);
|
||||
RAISE NOTICE 'cust %', _cust;
|
||||
|
||||
----------------price history-------------------------------
|
||||
SELECT jsonb_build_object('hist',rlarp.get_hist(_mold, _v1ds, _cust, substring(_chan,1,1))) INTO _hist;
|
||||
--RAISE NOTICE 'history %', jsonb_pretty(_hist);
|
||||
SELECT _rslt||jsonb_build_object('hist',rlarp.get_hist(_mold, _v1ds, _cust, substring(_chan,1,1))) INTO _rslt ;
|
||||
RAISE NOTICE 'result %', _rslt;
|
||||
|
||||
----------------target pricing------------------------------
|
||||
SELECT
|
||||
jsonb_build_object(
|
||||
'v0tp',
|
||||
target_price,
|
||||
'v0stdv',
|
||||
'stdv',
|
||||
stdev_price
|
||||
)
|
||||
INTO
|
||||
@ -163,12 +133,13 @@ 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,
|
||||
'v1stdv',
|
||||
'stdv',
|
||||
stdev_price
|
||||
)
|
||||
INTO
|
||||
@ -181,9 +152,10 @@ BEGIN
|
||||
AND data_segment = _dseg
|
||||
AND region = 'ALL';
|
||||
--RAISE NOTICE 'target: %', jsonb_pretty(_targ);
|
||||
_pricing := (COALESCE(_v0tp,'{}'::jsonb)||COALESCE(_v1tp,'{}'::jsonb));
|
||||
_rslt := _rslt||COALESCE(_v1tp,'{}'::jsonb);
|
||||
|
||||
----------------inflation index-----------------------------
|
||||
RAISE NOTICE 'infaltion : %', jsonb_pretty(_iidx);
|
||||
SELECT
|
||||
jsonb_build_object(
|
||||
'iidx'
|
||||
@ -204,21 +176,17 @@ BEGIN
|
||||
)
|
||||
GROUP BY
|
||||
priority;
|
||||
_pricing := _pricing||COALESCE(_iidx,'{}'::jsonb);
|
||||
--RAISE NOTICE 'add targets: %', jsonb_pretty(_pricing);
|
||||
_rslt := _rslt||COALESCE(_iidx,'{}'::jsonb);
|
||||
|
||||
----------------list pricing---------------------------------
|
||||
SELECT coalesce(rlarp.get_list(_bill, _ship, _item, _qty),'{}'::jsonb) INTO _list;
|
||||
_pricing := _pricing||_list;
|
||||
--RAISE NOTICE 'add list: %', jsonb_pretty(_pricing);
|
||||
_rslt := _rslt||_list;
|
||||
--RAISE NOTICE 'list: %', jsonb_pretty(_list);
|
||||
|
||||
----------------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;
|
||||
_pricing := jsonb_build_object('pricing',_pricing||_prem);
|
||||
--RAISE NOTICE 'add bridge: %', jsonb_pretty(_pricing);
|
||||
|
||||
_rslt := _input||_product||_customer||_pricing||_hist;
|
||||
|
||||
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);
|
||||
|
||||
RETURN _rslt;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user