price_api/apply_guidance.ts

55 lines
2.6 KiB
TypeScript
Raw Normal View History

export function apply_guidance(doc: any) {
2023-11-10 10:51:46 -05:00
const targetPrice = doc.v1tp ?? doc.v0tp;
2023-11-10 11:14:12 -05:00
const earlyPrice = doc.hist?.cust?.early_price;
2023-11-10 16:32:05 -05:00
let anchorPrice = null;
let anchorSource = null;
let bridgePremium = doc.bridgePremium ?? 1.00000;
// ------if there is not target price just exit---------------
2023-11-10 16:04:06 -05:00
if (!targetPrice) {
anchorSource = "No target pricing setup";
doc.finalReason = "No target pricing setup";
} 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) {
anchorSource = doc.hist.cust.early_season + ' Customer Price ' + earlyPrice;
} else {
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-------
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---------------------------------
2023-11-10 16:04:06 -05:00
const inflation = Math.max(...Object.keys(doc.iidx).map(Number));
2023-11-10 16:32:05 -05:00
//------extract the inflation factor using the relevance key--------------------------
2023-11-10 16:04:06 -05:00
const inflationFactor = doc.iidx[inflation] + 1;
2023-11-10 16:32:05 -05:00
let calcPrice = parseFloat((anchorPrice * inflationFactor).toFixed(5));
let finalReason = "";
2023-11-10 16:04:06 -05:00
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 {
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;
2023-11-10 02:22:23 -05:00
}
return doc;
}