apply groupings to json

This commit is contained in:
Paul Trowbridge 2023-11-13 11:02:41 -05:00
parent 96eee9ff5b
commit 0922862c80
2 changed files with 100 additions and 56 deletions

View File

@ -1,14 +1,26 @@
export function apply_guidance(doc: any) { export function apply_guidance(doc: any) {
const targetPrice = doc.v1tp ?? doc.v0tp; const targetPrice = doc.pricing?.v1tp ?? doc.pricing?.v0tp;
const earlyPrice = doc.hist?.cust?.early_price; 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 list = doc.pricing?.list;
const curr = doc.customer?.curr;
const fxrate = doc.customer?.fxrate;
let anchorPrice = null; let anchorPrice = null;
let anchorSource = null; let anchorSource = null;
let bridgePremium = doc.bridgePremium ?? 1.00000; let guidance = {};
let calcCeiling = null;
let finalReason = "";
let finalPrice = null;
const inflation = Math.max(...Object.keys(iidx).map(Number));
const inflationFactor = iidx[inflation] + 1;
// ------if there is not target price just exit--------------- // ------if there is not target price just exit---------------
if (!targetPrice) { if (!targetPrice) {
anchorSource = "No target pricing setup"; anchorSource = "No target pricing setup";
doc.finalReason = "No target pricing setup"; guidance.FinalReason = "No target pricing setup";
} else { } else {
// if there is no customer anchor price use target // if there is no customer anchor price use target
if (earlyPrice) { if (earlyPrice) {
@ -16,9 +28,9 @@ export function apply_guidance(doc: any) {
anchorPrice = Number((earlyPrice * bridgePremium).toFixed(5)); anchorPrice = Number((earlyPrice * bridgePremium).toFixed(5));
// --------if the price needs bridged, add the details to the description-------- // --------if the price needs bridged, add the details to the description--------
if (bridgePremium === 1) { if (bridgePremium === 1) {
anchorSource = doc.hist.cust.early_season + ' Customer Price ' + earlyPrice; anchorSource = earlySeason + ' Customer Price ' + earlyPrice;
} else { } else {
anchorSource = doc.hist.cust.early_season + ' Similar (' + doc.hist.cust.ds + ') Customer Price ' + earlyPrice + ' x ' + doc.bridgePremium + ' = ' + anchorPrice; anchorSource = earlySeason + ' Similar (' + altHist + ') Customer Price ' + earlyPrice + ' x ' + bridgePremium + ' = ' + anchorPrice;
} }
// --------after the early price is translated see if target is still less------- // --------after the early price is translated see if target is still less-------
if (targetPrice < anchorPrice) { if (targetPrice < anchorPrice) {
@ -30,25 +42,25 @@ export function apply_guidance(doc: any) {
anchorSource = `Target Price ${targetPrice}`; anchorSource = `Target Price ${targetPrice}`;
} }
//------get the most relevant inflation factor number--------------------------------- //------get the most relevant inflation factor number---------------------------------
const inflation = Math.max(...Object.keys(doc.iidx).map(Number));
//------extract the inflation factor using the relevance key-------------------------- //------extract the inflation factor using the relevance key--------------------------
const inflationFactor = doc.iidx[inflation] + 1;
let calcPrice = parseFloat((anchorPrice * inflationFactor).toFixed(5)); let calcPrice = parseFloat((anchorPrice * inflationFactor).toFixed(5));
let finalReason = ""; if (calcPrice >= list && list) {
if (calcPrice >= doc.list && doc.list) { calcCeiling = "Cap At List";
doc.calcCeiling = "Cap At List"; finalPrice = doc.list;
doc.finalPrice = doc.list; finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice} but cap at list ${list}`;
finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice} but cap at list ${doc.list}`;
} else { } else {
doc.finalPrice = calcPrice; finalPrice = calcPrice;
finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice}`; finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice}`;
} }
doc.anchorPrice = anchorPrice;
doc.anchorSource = anchorSource;
doc.inflationFactor = inflationFactor;
doc.finalReason = finalReason;
doc.bridgePremium = bridgePremium;
doc.targetPrice = targetPrice;
} }
guidance.AnchorPrice = anchorPrice;
guidance.AnchorSource = anchorSource;
guidance.InflationFactor = inflationFactor;
guidance.Ceiling = calcCeiling;
guidance.FinalPrice = finalPrice;
guidance.FinalReason = finalReason;
guidance.BridgePremium = bridgePremium;
guidance.TargetPrice = targetPrice;
doc.guidance = guidance;
return doc; return doc;
} }

View File

@ -27,6 +27,11 @@ DECLARE
_list jsonb; _list jsonb;
_iidx jsonb; _iidx jsonb;
_itemr text; _itemr text;
_input jsonb;
_product jsonb;
_customer jsonb;
_hist jsonb;
_pricing jsonb;
BEGIN BEGIN
--_item := 'AMK06000G18B054'; --_item := 'AMK06000G18B054';
@ -38,17 +43,28 @@ BEGIN
_v0ds := CASE split_part(substring(_dseg,4,100), '.',1) WHEN 'B' THEN 'BASE' ELSE 'COLOR' END || CASE split_part(substring(_dseg,4,100), '.',2) WHEN 'L' THEN ' LABELED' WHEN 'P' THEN ' PRINTED' ELSE '' END; _v0ds := CASE split_part(substring(_dseg,4,100), '.',1) WHEN 'B' THEN 'BASE' ELSE 'COLOR' END || CASE split_part(substring(_dseg,4,100), '.',2) WHEN 'L' THEN ' LABELED' WHEN 'P' THEN ' PRINTED' ELSE '' END;
_v1ds := _dseg; _v1ds := _dseg;
_input := jsonb_build_object(
'inputs'
,jsonb_build_object(
'dseg',_dseg,
'v0ds',_v0ds,
'v1ds',_v1ds,
'bill',_bill,
'ship',_ship,
'stlc',_stlc,
'qty',_qty,
'season',_seas
));
----------------base product-------------------------------- ----------------base product--------------------------------
SELECT SELECT
part_group part_group
,item ,item
,stlc
,idxk ,idxk
,prefer ,prefer
INTO INTO
_mold _mold
,_item ,_item
,_stlc
,_iidx ,_iidx
,_itemr ,_itemr
FROM FROM
@ -78,14 +94,21 @@ BEGIN
ORDER BY ORDER BY
prefer DESC prefer DESC
LIMIT 1; LIMIT 1;
_rslt := jsonb_build_object('mold',_mold,'v1ds',_v1ds,'v0ds',_v0ds,'stlc',_stlc,'item',_item,'item rel',_itemr,'desg',_dseg)||_iidx; _product := jsonb_build_object(
RAISE NOTICE 'item data %', _iidx; 'product',
jsonb_build_object(
'mold',_mold
,'item',_item
,'item rel',_itemr
,'iidx',_iidx
)
);
--RAISE NOTICE 'item data %', jsonb_pretty(_product||_input);
----------------channel------------------------------------- ----------------channel-------------------------------------
SELECT rlarp.channel_code(_bill, _ship) INTO _chan; SELECT rlarp.channel_code(_bill, _ship) INTO _chan;
_rslt := _rslt||jsonb_build_object('chan',_chan); _customer := jsonb_build_object('chan',_chan);
RAISE NOTICE 'chan %', _chan;
----------------customer------------------------------------ ----------------customer------------------------------------
SELECT dba INTO _cust FROM rlarp.cust WHERE code = CASE WHEN _chan = 'DRP' THEN _ship ELSE _bill END ; SELECT dba INTO _cust FROM rlarp.cust WHERE code = CASE WHEN _chan = 'DRP' THEN _ship ELSE _bill END ;
@ -109,19 +132,26 @@ BEGIN
WHERE WHERE
code = _bill; code = _bill;
_rslt = _rslt||jsonb_build_object('cust',_cust,'curr',_curr,'fxrate',_rate); _customer := jsonb_build_object(
RAISE NOTICE 'cust %', _cust; 'customer',
_customer||jsonb_build_object(
'cust',_cust
,'curr',_curr
,'fxrate',_rate
)
);
--RAISE NOTICE 'cust %', jsonb_pretty(_customer);
----------------price history------------------------------- ----------------price history-------------------------------
SELECT _rslt||jsonb_build_object('hist',rlarp.get_hist(_mold, _v1ds, _cust, substring(_chan,1,1))) INTO _rslt ; SELECT jsonb_build_object('hist',rlarp.get_hist(_mold, _v1ds, _cust, substring(_chan,1,1))) INTO _hist;
RAISE NOTICE 'result %', _rslt; --RAISE NOTICE 'history %', _hist;
----------------target pricing------------------------------ ----------------target pricing------------------------------
SELECT SELECT
jsonb_build_object( jsonb_build_object(
'v0tp', 'v0tp',
target_price, target_price,
'stdv', 'v0stdv',
stdev_price stdev_price
) )
INTO INTO
@ -133,13 +163,12 @@ BEGIN
AND season = _seas AND season = _seas
AND data_segment = _v0ds AND data_segment = _v0ds
AND region = 'ALL'; AND region = 'ALL';
_rslt := _rslt||COALESCE(_v0tp,'{}'::jsonb);
----------------target pricing------------------------------ ----------------target pricing------------------------------
SELECT SELECT
jsonb_build_object( jsonb_build_object(
'v1tp', 'v1tp',
target_price, target_price,
'stdv', 'v1stdv',
stdev_price stdev_price
) )
INTO INTO
@ -152,10 +181,9 @@ BEGIN
AND data_segment = _dseg AND data_segment = _dseg
AND region = 'ALL'; AND region = 'ALL';
--RAISE NOTICE 'target: %', jsonb_pretty(_targ); --RAISE NOTICE 'target: %', jsonb_pretty(_targ);
_rslt := _rslt||COALESCE(_v1tp,'{}'::jsonb); _pricing := (COALESCE(_v0tp,'{}'::jsonb)||COALESCE(_v1tp,'{}'::jsonb));
----------------inflation index----------------------------- ----------------inflation index-----------------------------
RAISE NOTICE 'infaltion : %', jsonb_pretty(_iidx);
SELECT SELECT
jsonb_build_object( jsonb_build_object(
'iidx' 'iidx'
@ -176,17 +204,21 @@ BEGIN
) )
GROUP BY GROUP BY
priority; priority;
_rslt := _rslt||COALESCE(_iidx,'{}'::jsonb); _pricing := _pricing||COALESCE(_iidx,'{}'::jsonb);
--RAISE NOTICE 'add targets: %', jsonb_pretty(_pricing);
----------------list pricing--------------------------------- ----------------list pricing---------------------------------
SELECT coalesce(rlarp.get_list(_bill, _ship, _item, _qty),'{}'::jsonb) INTO _list; SELECT coalesce(rlarp.get_list(_bill, _ship, _item, _qty),'{}'::jsonb) INTO _list;
_rslt := _rslt||_list; _pricing := _pricing||_list;
--RAISE NOTICE 'list: %', jsonb_pretty(_list); --RAISE NOTICE 'add list: %', jsonb_pretty(_pricing);
----------------get premium for quote hist gap-------------- ----------------get premium for quote hist gap--------------
SELECT coalesce(rlarp.get_premium(_stlc, _seas, (SELECT xchan FROM _chx WHERE chan = _chan),_rslt->'hist'->'cust'->>'ds', _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; _pricing := jsonb_build_object('pricing',_pricing||_prem);
--RAISE NOTICE 'list: %', jsonb_pretty(_list); --RAISE NOTICE 'add bridge: %', jsonb_pretty(_pricing);
_rslt := _input||_product||_customer||_pricing||_hist;
RETURN _rslt; RETURN _rslt;