2023-11-08 12:54:46 -05:00
|
|
|
export function apply_guidance(doc: any) {
|
|
|
|
|
2023-11-13 16:10:42 -05:00
|
|
|
/*-----------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
price optimization lookup table
|
|
|
|
-----------------------------------------------------------------------------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
function getAdjValue(number) {
|
|
|
|
const data = [
|
|
|
|
{f: 2.001, t: 1000, snap: 3, adj: 0 },
|
|
|
|
{f: 1.001, t: 2, snap: 2, adj: 0 },
|
|
|
|
{f: 0.1, t: 1, snap: 1, adj: 0 },
|
|
|
|
{f: 0, t: 0.1, snap: 0, adj: 0 },
|
|
|
|
{f: -1, t: -0.00001, snap: -1, adj: 0.05},
|
|
|
|
{f: -2, t: -0.999999, snap: -2, adj: 0.05},
|
|
|
|
{f: -1000, t: -2.001, snap: -3, adj: 0.10},
|
|
|
|
];
|
|
|
|
const match = data.find(row => number >= row.f && number <= row.t);
|
|
|
|
return match ? match.adj : null;
|
|
|
|
}
|
|
|
|
|
2023-11-13 11:02:41 -05:00
|
|
|
const targetPrice = doc.pricing?.v1tp ?? doc.pricing?.v0tp;
|
2023-11-13 16:10:42 -05:00
|
|
|
const bridgePremium = doc.pricing?.bridgePremium ;
|
|
|
|
const earlyPriceOrig = doc.hist?.cust?.early_price;
|
|
|
|
const earlyPrice = doc.hist?.cust?.early_price * bridgePremium;
|
|
|
|
const earlyMarketPrice = doc.hist?.market?.early_price;
|
2023-11-13 11:02:41 -05:00
|
|
|
const earlySeason = doc.hist?.cust?.early_season;
|
2023-11-13 16:10:42 -05:00
|
|
|
const earlyMarketSeason = doc.hist?.market?.early_season;
|
2023-11-13 11:02:41 -05:00
|
|
|
const altHist = doc.hist?.cust?.ds;
|
|
|
|
const iidx = doc.pricing?.iidx;
|
|
|
|
const curr = doc.customer?.curr;
|
2023-11-13 16:10:42 -05:00
|
|
|
const chan = doc.customer?.chan;
|
2023-11-13 12:25:41 -05:00
|
|
|
const fxrate = doc.customer?.fxrate ?? 1.0;
|
2023-11-13 13:15:24 -05:00
|
|
|
const qty = doc.inputs?.qty;
|
|
|
|
const pltq = doc.product?.pltq;
|
2023-11-13 16:10:42 -05:00
|
|
|
const band = doc.pricing?.v1stdv ?? doc.pricing?.v0stdv ?? 0;
|
2023-11-13 11:45:47 -05:00
|
|
|
let custPrice = null;
|
|
|
|
let custSource = null;
|
2023-11-13 11:02:41 -05:00
|
|
|
let guidance = {};
|
|
|
|
let calcCeiling = null;
|
2023-11-13 12:25:41 -05:00
|
|
|
let finalReasonUSD = "";
|
|
|
|
let finalPriceUSD = null;
|
2023-11-13 11:02:41 -05:00
|
|
|
let finalReason = "";
|
|
|
|
let finalPrice = null;
|
2023-11-13 16:10:42 -05:00
|
|
|
let ltp = qty < pltq ? .15 : 0;
|
|
|
|
let ltpf = qty < pltq ? true : false;
|
2023-11-13 13:15:24 -05:00
|
|
|
let increase = null;
|
2023-11-13 16:10:42 -05:00
|
|
|
let isWarehouse = chan[0] === "W"
|
|
|
|
const inflationidx = Math.max(...Object.keys(iidx).map(Number));
|
|
|
|
const inflation = iidx[inflationidx];
|
2023-11-13 12:43:44 -05:00
|
|
|
const list = doc.pricing?.list && doc.product?.itemrel === "2" ? doc.pricing?.list : null;
|
2023-11-13 16:10:42 -05:00
|
|
|
const listUSD = (list && fxrate) ? list / fxrate : null;
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
determine anchor price
|
|
|
|
-----------------------------------------------------------------------------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
let anchorPrice =
|
|
|
|
earlyPrice ? earlyPrice :
|
|
|
|
targetPrice ? targetPrice :
|
|
|
|
earlyMarketPrice ? earlyMarketPrice :
|
|
|
|
null;
|
|
|
|
|
|
|
|
let anchorSource =
|
|
|
|
earlyPrice
|
|
|
|
? bridgePremium
|
|
|
|
? `${earlySeason} ${altHist} price ${earlyPriceOrig} x ${bridgePremium} = ${earlyPrice}`
|
|
|
|
: `${earlySeason} price ${(earlyPrice * 1000).toFixed(2)}` :
|
|
|
|
earlyPrice ? earlyReason :
|
|
|
|
targetPrice ? `Target price ${(targetPrice * 1000).toFixed(2)}` :
|
|
|
|
earlyMarketPrice ? `${earlyMarketSeason} price ${earlyMarketPrice}` :
|
|
|
|
null;
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
calculated increase percentages
|
|
|
|
-----------------------------------------------------------------------------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
let anchorSD = (band && targetPrice) ? ((anchorPrice - targetPrice) / band).toFixed(2) : 0
|
|
|
|
let optFactor = getAdjValue(anchorSD) ;
|
|
|
|
let increaseAnch = 1 + optFactor + ltp + inflation;
|
|
|
|
let increaseTarg = 1 + ltp + inflation;
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
apply increases
|
|
|
|
-----------------------------------------------------------------------------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
let anchorGoal = anchorPrice * increaseAnch;
|
|
|
|
let capTarget = targetPrice * increaseTarg;
|
|
|
|
let capMarket = earlyMarketPrice * increaseTarg;
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
calculate price caps
|
|
|
|
-----------------------------------------------------------------------------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* there are 3 price points
|
|
|
|
* anchor + increase
|
|
|
|
* target + increase
|
|
|
|
* list
|
|
|
|
goal is to demonstrate calculation of anchor + increase then give then cap if applicable
|
|
|
|
*/
|
|
|
|
|
|
|
|
//default to anchor goal
|
|
|
|
finalPriceUSD = anchorGoal;
|
|
|
|
finalReason = "";
|
|
|
|
let increaseReason = "";
|
|
|
|
let cap = "";
|
|
|
|
|
|
|
|
//if the target is less than the anchor goal, cap at target
|
|
|
|
if ((capTarget ?? anchorGoal ) < anchorGoal) {
|
|
|
|
finalPriceUSD = capTarget;
|
|
|
|
cap = ` cap at target ${(targetPrice * 1000).toFixed(2)} + ${((increaseTarg - 1) * 100).toFixed(1)}%`
|
|
|
|
}
|
|
|
|
//if list price is even less than the capped price, then cap at list
|
|
|
|
if ((listUSD ?? anchorGoal) < finalPriceUSD ) {
|
|
|
|
finalPriceUSD = listUSD;
|
|
|
|
cap = ` cap at list ${listUSD}`
|
|
|
|
}
|
|
|
|
/*-----use _p.list if available for warehouse-----------*/
|
|
|
|
if (isWarehouse) {
|
|
|
|
if (listUSD) {
|
|
|
|
finalPriceUSD = listUSD;
|
|
|
|
cap_curr = ` snap to list ${listUSD}`;
|
2023-11-10 16:04:06 -05:00
|
|
|
}
|
2023-11-10 02:22:23 -05:00
|
|
|
}
|
2023-11-13 16:10:42 -05:00
|
|
|
finalPrice = finalPriceUSD / fxrate;
|
|
|
|
|
|
|
|
/*-----------------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
build the logic text
|
|
|
|
-----------------------------------------------------------------------------------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
cap = cap ? ";" + cap : "";
|
|
|
|
let fltp = ltpf ? ` + ${(ltp * 100).toFixed(1) + '%'} LTP` : "";
|
|
|
|
let fopt = optFactor ? ` + ${(optFactor * 100).toFixed(1) + '%'} opt` : "";
|
|
|
|
let fguidance = inflation ? ` + ${(inflation * 100).toFixed(1) + '%'} guidance` : "";
|
|
|
|
finalReason = `${(finalPrice*1000).toFixed(2)} (${anchorSource}${fguidance}${fltp}${fopt}${cap}) `;
|
|
|
|
|
2023-11-13 11:02:41 -05:00
|
|
|
guidance.AnchorPrice = anchorPrice;
|
|
|
|
guidance.AnchorSource = anchorSource;
|
2023-11-13 16:10:42 -05:00
|
|
|
//guidance.CustAnchorPrice = custPrice;
|
|
|
|
//guidance.CustAnchorSource = custSource;
|
|
|
|
guidance.InflationFactor = inflation;
|
|
|
|
//guidance.Ceiling = calcCeiling;
|
2023-11-13 12:25:41 -05:00
|
|
|
guidance.FinalPriceUSD = finalPriceUSD;
|
2023-11-13 16:10:42 -05:00
|
|
|
guidance.FinalReason = finalReason;
|
2023-11-13 11:02:41 -05:00
|
|
|
guidance.FinalPrice = finalPrice;
|
|
|
|
guidance.FinalReason = finalReason;
|
|
|
|
guidance.BridgePremium = bridgePremium;
|
|
|
|
guidance.TargetPrice = targetPrice;
|
2023-11-13 12:43:44 -05:00
|
|
|
guidance.ListPrice = list;
|
2023-11-13 11:02:41 -05:00
|
|
|
doc.guidance = guidance;
|
2023-11-08 12:54:46 -05:00
|
|
|
return doc;
|
|
|
|
}
|