price_api/apply_guidance.ts

74 lines
3.2 KiB
TypeScript

export function apply_guidance(doc: any) {
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 list = doc.pricing?.list;
const curr = doc.customer?.curr;
const fxrate = doc.customer?.fxrate;
let anchorPrice = null;
let anchorSource = null;
let custPrice = null;
let custSource = null;
let guidance = {};
let calcCeiling = null;
let finalReason = "";
let finalPrice = null;
const inflation = Math.max(...Object.keys(iidx).map(Number));
const inflationFactor = iidx[inflation] + 1;
// ------if there is not target price just exit---------------
if (!targetPrice) {
anchorSource = "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
custPrice = Number((earlyPrice * bridgePremium).toFixed(5));
anchorPrice = custPrice;
// --------if the price needs bridged, add the details to the description--------
if (bridgePremium === 1) {
anchorSource = earlySeason + ' Customer Price ' + earlyPrice;
custSource = anchorSource;
} else {
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) {
anchorSource = `Target Price ${targetPrice}`;
anchorPrice = targetPrice;
}
} else {
anchorPrice = targetPrice;
anchorSource = `Target Price ${targetPrice}`;
}
//------get the most relevant inflation factor number---------------------------------
//------extract the inflation factor using the relevance key--------------------------
let calcPrice = parseFloat((anchorPrice * inflationFactor).toFixed(5));
if (calcPrice >= list && list) {
calcCeiling = "Cap At List";
finalPrice = doc.list;
finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice} but cap at list ${list}`;
} else {
finalPrice = calcPrice;
finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice}`;
}
}
guidance.AnchorPrice = anchorPrice;
guidance.AnchorSource = anchorSource;
guidance.CustAnchorPrice = custPrice;
guidance.CustAnchorSource = custSource;
guidance.InflationFactor = inflationFactor;
guidance.Ceiling = calcCeiling;
guidance.FinalPrice = finalPrice;
guidance.FinalReason = finalReason;
guidance.BridgePremium = bridgePremium;
guidance.TargetPrice = targetPrice;
doc.guidance = guidance;
return doc;
}