From 15c60c629d0b4221159f0adb2e428d10fb08898c Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Mon, 13 Nov 2023 12:25:41 -0500 Subject: [PATCH] convert to USD --- apply_guidance.ts | 29 ++++++++++++++++++++++------- sql/get_guidance_dseg.pg.sql | 2 +- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/apply_guidance.ts b/apply_guidance.ts index 72bd348..8b20060 100644 --- a/apply_guidance.ts +++ b/apply_guidance.ts @@ -6,19 +6,21 @@ export function apply_guidance(doc: any) { 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; + const fxrate = doc.customer?.fxrate ?? 1.0; 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; const inflation = Math.max(...Object.keys(iidx).map(Number)); const inflationFactor = iidx[inflation] + 1; + const list = doc.pricing?.list && doc.product?.itemrel === 1 ? doc.pricing?.list : null; // ------if there is not target price just exit--------------- if (!targetPrice) { anchorSource = "No target pricing setup"; @@ -47,15 +49,26 @@ export function apply_guidance(doc: any) { 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)); + //------anchor x inflation / fxrate--------------------------------------------------- + let calcPriceUSD = parseFloat((anchorPrice * inflationFactor).toFixed(5)); + let calcPrice = parseFloat((calcPriceUSD / fxrate).toFixed(5)); if (calcPrice >= list && list) { calcCeiling = "Cap At List"; - finalPrice = doc.list; - finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice} but cap at list ${list}`; + //multiply list by FX to get to USD if in CAD + finalPrice = list; + if (curr === "CA") { + finalReason = `${anchorSource} x ${inflationFactor} / ${fxrate} FX = ${calcPrice} CAD, cap at list ${list} CAD`; + } else { + finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice}, cap at list ${list}`; + } } else { finalPrice = calcPrice; - finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice}`; + finalPriceUSD = calcPriceUSD; + if (curr === "CA") { + finalReason = `${anchorSource} x ${inflationFactor} / ${fxrate} FX = ${calcPrice} CAD`; + } else { + finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice}`; + } } } guidance.AnchorPrice = anchorPrice; @@ -64,6 +77,8 @@ export function apply_guidance(doc: any) { guidance.CustAnchorSource = custSource; guidance.InflationFactor = inflationFactor; guidance.Ceiling = calcCeiling; + guidance.FinalPriceUSD = finalPriceUSD; + guidance.FinalReasonUSD = finalReasonUSD; guidance.FinalPrice = finalPrice; guidance.FinalReason = finalReason; guidance.BridgePremium = bridgePremium; diff --git a/sql/get_guidance_dseg.pg.sql b/sql/get_guidance_dseg.pg.sql index 53d1445..c33d535 100644 --- a/sql/get_guidance_dseg.pg.sql +++ b/sql/get_guidance_dseg.pg.sql @@ -99,7 +99,7 @@ BEGIN jsonb_build_object( 'mold',_mold ,'item',_item - ,'item rel',_itemr + ,'itemrel',_itemr ,'iidx',_iidx ) );