From dc1f493700d4cd92e9ecfd8db319f76281cfb1fb Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Wed, 15 Nov 2023 16:57:21 -0500 Subject: [PATCH] work on simplifying and condensing logic --- apply_guidance.ts | 86 +++++++++++++++++++++------------------ sql/get_hist.pg.sql | 2 +- sql/get_hist_debug.pg.sql | 2 +- 3 files changed, 48 insertions(+), 42 deletions(-) diff --git a/apply_guidance.ts b/apply_guidance.ts index ccf150e..d066813 100644 --- a/apply_guidance.ts +++ b/apply_guidance.ts @@ -14,48 +14,54 @@ export function apply_guidance(doc: any) { return match ? match.adj : null; } - //let custPrice null; - //let custReason null; - //let cvolPrice null; - //let cvolReason null; - //let markPrice null; - //let markReason null; - //let targPrice null; - //let targReason null; + function minExcludingNull(...values) { + return values.reduce((min, val) => (val !== null && (min === null || val < min) ? val : min), null); + } - const targetPrice = doc.pricing?.v1tp ?? doc.pricing?.v0tp; - const priceBand = doc.pricing?.v1stdv ?? doc.pricing?.v0stdv; - 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; - const qty = doc.inputs?.qty; - const pltq = doc.product?.pltq; - 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; - let ltp = qty < pltq ? 1.15 : null; - let increase = 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; + // --------------------extract incoming data------------------------------------------------------ + const targetPrice = doc.pricing?.v1tp ?? doc.pricing?.v0tp; + const priceBand = doc.pricing?.v1stdv ?? doc.pricing?.v0stdv; + const earlyCustPrice = doc.hist?.cust?.early_price; + const earlyCustSeason = doc.hist?.cust?.early_season; + const earlyMarkPrice = doc.hist?.mark?.early_price; + const earlyMarkSeason = doc.hist?.mark?.early_season; + const bridgePremium = doc.pricing?.bridgePremium; + const bridgedPrice = Number((earlyCustPrice * (bridgePremium ?? 1.00)).toFixed(5)); + const altHist = doc.hist?.cust?.ds; + const iidx = doc.pricing?.iidx; + const curr = doc.customer?.curr; + const fxrate = doc.customer?.fxrate ?? 1.0; + const qty = doc.inputs?.qty; + const pltq = doc.product?.pltq; + const inflation = Math.max(...Object.keys(iidx).map(Number)); + const inflationFactor = iidx[inflation]; + const list = doc.pricing?.list && doc.product?.itemrel === "2" ? doc.pricing?.list : null; + const listUSD = list ? list / fxrate :null; - //-------set basic customer pricing-------------- - custPrice = Number((earlyPrice * bridgePremium).toFixed(5)); - anchorPrice = custPrice; - let anchor_sd = priceBand ? ((anchorPrice - targetPrice) / priceBand).toFixed(2) : 0 - let optimization = getAdjValue(anchor_sd); + // ------------------calculate price adders------------------------------------------------------ + let ltp = qty < pltq ? 0.15 : null; + let anchor_sd = priceBand ? ((bridgedPrice - targetPrice) / priceBand).toFixed(2) : 0 + let optimization = getAdjValue(anchor_sd); + let inflReason = inflationFactor !== 0 ? ` +${(inflationFactor *100).toFixed(1)}%`: ""; + let ltpReason = ltp ? ` +${(ltp * 100).toFixed(1))}%` : ""; + let optReason = optimization !== 0 ? ` +${(inflationFactor *100).toFixed(1)}%`: ""; + let custAdder = (ltp ?? 0) + optimization + inflationFactor; + let markAdder = (ltp ?? 0) + inflationFactor; + let custAddReason = `${inflReason}${ltpReason}${optReason}`; + let markAddReason = `${inflReason}${ltpReason}`; + // ------------------start building price options------------------------------------------------ + + let custPrice = bridgedPrice * (1 + custAdder); + let custSeason = earlySeason; + let custReason = bridgePremium + ? `${custSeason} (similar + ${altHist} price ${earlyPrice} x ${bridgePremium} = ${custPrice}) + ${custAddReason}` + : `${custSeason} + price ${custPrice} + ${custAddReason}`; + let markReason = `${markSeason} ASP + ${markPrice} + ${markAddReason}`; + let targPrice = targetPrice * (1 + markAdder); + let targReason = `Target price ${targetPrice} + ${markAddReason}`; + + let finalPrice = minExcludingNull(listUSD,custPrice,targPrice); // ------if there is not target price just exit--------------- if (!targetPrice) { @@ -63,7 +69,7 @@ export function apply_guidance(doc: any) { guidance.FinalReason = "No target pricing setup"; } else { // if there is no customer anchor price use target - if (earlyPrice) { + if (earlyCustPrice) { // translate alternate product history to current product quoted // --------if the price needs bridged, add the details to the description-------- if (bridgePremium === 1) { diff --git a/sql/get_hist.pg.sql b/sql/get_hist.pg.sql index 46675ba..ecdb64e 100644 --- a/sql/get_hist.pg.sql +++ b/sql/get_hist.pg.sql @@ -110,7 +110,7 @@ FROM ,('customer v0ds other',7) ,('customer v0ds vol' ,3) ,('customer v1ds other',6) - ,('customer v1ds vol' ,2) + ,('customer v1ds vol' ,2) --this will always sort to the top, v0ds will never sort to the top. you will always be getting the highest volume base price ,('market exact' ,4) ,('market v0ds other' ,9) ,('market v0ds vol' ,5) diff --git a/sql/get_hist_debug.pg.sql b/sql/get_hist_debug.pg.sql index c0e081a..e7295cf 100644 --- a/sql/get_hist_debug.pg.sql +++ b/sql/get_hist_debug.pg.sql @@ -104,7 +104,7 @@ FROM ,('customer v0ds other',7) ,('customer v0ds vol' ,3) ,('customer v1ds other',6) - ,('customer v1ds vol' ,2) + ,('customer v1ds vol' ,2) --this will always sort to the top, v0ds will never sort to the top. you will always be getting the highest volume base price ,('market exact' ,4) ,('market v0ds other' ,9) ,('market v0ds vol' ,5)