price_api/apply_guidance.ts

67 lines
2.9 KiB
TypeScript
Raw Normal View History

export function apply_guidance(doc: any) {
2023-11-13 11:02:41 -05:00
const targetPrice = doc.pricing?.v1tp ?? doc.pricing?.v0tp;
2023-11-10 11:14:12 -05:00
const earlyPrice = doc.hist?.cust?.early_price;
2023-11-13 11:02:41 -05:00
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;
2023-11-10 16:32:05 -05:00
let anchorPrice = null;
let anchorSource = null;
2023-11-13 11:02:41 -05:00
let guidance = {};
let calcCeiling = null;
let finalReason = "";
let finalPrice = null;
const inflation = Math.max(...Object.keys(iidx).map(Number));
const inflationFactor = iidx[inflation] + 1;
2023-11-10 16:32:05 -05:00
// ------if there is not target price just exit---------------
2023-11-10 16:04:06 -05:00
if (!targetPrice) {
anchorSource = "No target pricing setup";
2023-11-13 11:02:41 -05:00
guidance.FinalReason = "No target pricing setup";
2023-11-10 16:04:06 -05:00
} else {
2023-11-10 16:32:05 -05:00
// if there is no customer anchor price use target
2023-11-10 16:04:06 -05:00
if (earlyPrice) {
// translate alternate product history to current product quoted
anchorPrice = Number((earlyPrice * bridgePremium).toFixed(5));
2023-11-10 16:32:05 -05:00
// --------if the price needs bridged, add the details to the description--------
if (bridgePremium === 1) {
2023-11-13 11:02:41 -05:00
anchorSource = earlySeason + ' Customer Price ' + earlyPrice;
2023-11-10 16:32:05 -05:00
} else {
2023-11-13 11:02:41 -05:00
anchorSource = earlySeason + ' Similar (' + altHist + ') Customer Price ' + earlyPrice + ' x ' + bridgePremium + ' = ' + anchorPrice;
2023-11-10 16:32:05 -05:00
}
// --------after the early price is translated see if target is still less-------
2023-11-10 16:04:06 -05:00
if (targetPrice < anchorPrice) {
anchorSource = `Target Price ${targetPrice}`;
anchorPrice = targetPrice;
}
2023-11-10 02:22:23 -05:00
} else {
2023-11-10 16:04:06 -05:00
anchorPrice = targetPrice;
anchorSource = `Target Price ${targetPrice}`;
2023-11-10 02:22:23 -05:00
}
2023-11-10 16:32:05 -05:00
//------get the most relevant inflation factor number---------------------------------
//------extract the inflation factor using the relevance key--------------------------
let calcPrice = parseFloat((anchorPrice * inflationFactor).toFixed(5));
2023-11-13 11:02:41 -05:00
if (calcPrice >= list && list) {
calcCeiling = "Cap At List";
finalPrice = doc.list;
finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice} but cap at list ${list}`;
2023-11-10 16:04:06 -05:00
} else {
2023-11-13 11:02:41 -05:00
finalPrice = calcPrice;
2023-11-10 16:04:06 -05:00
finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice}`;
}
2023-11-10 02:22:23 -05:00
}
2023-11-13 11:02:41 -05:00
guidance.AnchorPrice = anchorPrice;
guidance.AnchorSource = anchorSource;
guidance.InflationFactor = inflationFactor;
guidance.Ceiling = calcCeiling;
guidance.FinalPrice = finalPrice;
guidance.FinalReason = finalReason;
guidance.BridgePremium = bridgePremium;
guidance.TargetPrice = targetPrice;
doc.guidance = guidance;
return doc;
}