diff --git a/apply_guidance.ts b/apply_guidance.ts index ef704fc..dbd383e 100644 --- a/apply_guidance.ts +++ b/apply_guidance.ts @@ -118,40 +118,40 @@ export function apply_guidance(doc: any) { //} - doc.targetPrice = doc.v1tp ?? doc.v0tp; + const targetPrice = doc.v1tp ?? doc.v0tp; + const earlyPrice = doc.hist.cust.early_price; + let anchorPrice = null; + let anchorSource = null; + let bridgePremium = doc.bridgePremium ?? 1.00000; // Determine the anchor price and source - if (doc.hist.cust?.early_price !== undefined ) { - if (doc.hist.cust.relevance !== 'customer exact') { - // translate alternate product history to current product quoted - doc.anchorPrice = Number((doc.hist.cust.early_price * doc.bridgePremium).toFixed(5)); - if (doc.targetPrice < doc.anchorPrice) { - doc.anchorPrice = doc.targetPrice; - doc.anchorSource = 'Target Price' - } else { - doc.anchorSource = doc.hist.cust.early_season + ' Similar (' + doc.hist.cust.ds + ') Customer Price ' + doc.hist.cust.early_price + ' x ' + doc.bridgePremium + ' = ' + doc.anchorPrice; - } + if (earlyPrice) { + // translate alternate product history to current product quoted + anchorPrice = Number((earlyPrice * bridgePremium).toFixed(5)); + if (targetPrice < anchorPrice) { + anchorSource = 'Target Price'; + anchorPrice = targetPrice; } else { - doc.anchorPrice = doc.hist.cust.early_price; - doc.anchorSource = doc.hist.cust.early_season + ' Customer Price ' + doc.hist.cust.early_price; + anchorSource = doc.hist.cust.early_season + ' Similar (' + doc.hist.cust.ds + ') Customer Price ' + earlyPrice + ' x ' + doc.bridgePremium + ' = ' + anchorPrice; } } else { - doc.anchorPrice = doc.targetPrice; - doc.anchorSource = 'Target Price'; + anchorPrice = doc.targetPrice; + anchorSource = 'Target Price'; } + doc.anchorSource = anchorSource; const inflation = Math.max(...Object.keys(doc.iidx).map(Number)); const inflationFactor = doc.iidx[inflation] + 1; doc.inflationFactor = inflationFactor; - var calcPrice = doc.anchorPrice * doc.inflationFactor; + var calcPrice = anchorPrice * doc.inflationFactor; calcPrice = parseFloat(calcPrice.toFixed(5)); let finalReason = ""; if (calcPrice >= doc.list) { doc.calcCeiling = "Cap At List"; doc.finalPrice = doc.list; - finalReason = `${doc.anchorSource} x ${inflationFactor} = ${calcPrice} but cap at list ${doc.list}`; + finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice} but cap at list ${doc.list}`; } else { doc.finalPrice = calcPrice; - finalReason = `${doc.anchorSource} x ${inflationFactor} = ${calcPrice}`; + finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice}`; } doc.finalReason = finalReason; diff --git a/sql/get_guidance.pg.sql b/sql/get_guidance.pg.sql index e472ed9..812ff88 100644 --- a/sql/get_guidance.pg.sql +++ b/sql/get_guidance.pg.sql @@ -133,7 +133,7 @@ BEGIN --RAISE NOTICE 'list: %', jsonb_pretty(_list); ----------------get premium for quote hist gap-------------- - SELECT coalesce(rlarp.get_premium(_stlc, _seas, (SELECT xchan FROM _chx WHERE chan = _chan),_v1ds, ((_rslt->'mostRelevantCustomerPriceInfo')->'source')->>'v1ds'),'{}'::jsonb) INTO _prem; + SELECT coalesce(rlarp.get_premium(_stlc, _seas, (SELECT xchan FROM _chx WHERE chan = _chan),_rslt->'hist'->'cust'->>'ds', _v1ds),'{}'::jsonb) INTO _prem; _rslt := _rslt||_prem; --RAISE NOTICE 'list: %', jsonb_pretty(_list);