apply groupings to json
This commit is contained in:
		
							parent
							
								
									96eee9ff5b
								
							
						
					
					
						commit
						0922862c80
					
				| @ -1,14 +1,26 @@ | ||||
| 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 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   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 (!targetPrice) { | ||||
|         anchorSource = "No target pricing setup"; | ||||
|         doc.finalReason = "No target pricing setup"; | ||||
|         guidance.FinalReason = "No target pricing setup"; | ||||
|     } else { | ||||
|         // if there is no customer anchor price use target
 | ||||
|         if (earlyPrice) { | ||||
| @ -16,9 +28,9 @@ export function apply_guidance(doc: any) { | ||||
|             anchorPrice = Number((earlyPrice * bridgePremium).toFixed(5)); | ||||
|             // --------if the price needs bridged, add the details to the description--------
 | ||||
|             if (bridgePremium === 1) { | ||||
|                 anchorSource = doc.hist.cust.early_season + ' Customer Price ' + earlyPrice; | ||||
|                 anchorSource = earlySeason + ' Customer Price ' + earlyPrice; | ||||
|             } 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-------
 | ||||
|             if (targetPrice < anchorPrice) { | ||||
| @ -30,25 +42,25 @@ export function apply_guidance(doc: any) { | ||||
|             anchorSource = `Target Price ${targetPrice}`; | ||||
|         } | ||||
|         //------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--------------------------
 | ||||
|         const inflationFactor = doc.iidx[inflation] + 1; | ||||
|         let   calcPrice = parseFloat((anchorPrice * inflationFactor).toFixed(5)); | ||||
|         let   finalReason = ""; | ||||
|         if (calcPrice >= doc.list && doc.list) { | ||||
|             doc.calcCeiling = "Cap At List"; | ||||
|             doc.finalPrice = doc.list; | ||||
|             finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice} but cap at list ${doc.list}`; | ||||
|         if (calcPrice >= list && list) { | ||||
|             calcCeiling = "Cap At List"; | ||||
|             finalPrice = doc.list; | ||||
|             finalReason = `${anchorSource} x ${inflationFactor} = ${calcPrice} but cap at list ${list}`; | ||||
|         } else { | ||||
|             doc.finalPrice = calcPrice; | ||||
|             finalPrice = 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; | ||||
| } | ||||
|  | ||||
| @ -27,6 +27,11 @@ DECLARE | ||||
|     _list     jsonb; | ||||
|     _iidx     jsonb; | ||||
|     _itemr    text; | ||||
|     _input    jsonb; | ||||
|     _product  jsonb; | ||||
|     _customer jsonb; | ||||
|     _hist     jsonb; | ||||
|     _pricing  jsonb; | ||||
| 
 | ||||
| BEGIN | ||||
|     --_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; | ||||
|     _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-------------------------------- | ||||
|     SELECT  | ||||
|         part_group | ||||
|         ,item | ||||
|         ,stlc | ||||
|         ,idxk | ||||
|         ,prefer | ||||
|     INTO  | ||||
|         _mold | ||||
|         ,_item | ||||
|         ,_stlc | ||||
|         ,_iidx | ||||
|         ,_itemr | ||||
|     FROM | ||||
| @ -78,14 +94,21 @@ BEGIN | ||||
|     ORDER BY | ||||
|         prefer DESC | ||||
|     LIMIT 1; | ||||
|     _rslt := jsonb_build_object('mold',_mold,'v1ds',_v1ds,'v0ds',_v0ds,'stlc',_stlc,'item',_item,'item rel',_itemr,'desg',_dseg)||_iidx; | ||||
|     RAISE NOTICE 'item data %', _iidx; | ||||
|     _product := jsonb_build_object( | ||||
|         'product', | ||||
|         jsonb_build_object( | ||||
|             'mold',_mold | ||||
|             ,'item',_item | ||||
|             ,'item rel',_itemr | ||||
|             ,'iidx',_iidx | ||||
|         ) | ||||
|     ); | ||||
|     --RAISE NOTICE 'item data %', jsonb_pretty(_product||_input); | ||||
| 
 | ||||
|     ----------------channel------------------------------------- | ||||
| 
 | ||||
|     SELECT rlarp.channel_code(_bill, _ship) INTO _chan; | ||||
|     _rslt := _rslt||jsonb_build_object('chan',_chan); | ||||
|     RAISE NOTICE 'chan %', _chan; | ||||
|     _customer := jsonb_build_object('chan',_chan); | ||||
| 
 | ||||
|     ----------------customer------------------------------------ | ||||
|     SELECT dba INTO _cust FROM rlarp.cust WHERE code = CASE WHEN _chan = 'DRP' THEN _ship ELSE _bill END ; | ||||
| @ -109,19 +132,26 @@ BEGIN | ||||
|     WHERE  | ||||
|         code = _bill; | ||||
| 
 | ||||
|     _rslt = _rslt||jsonb_build_object('cust',_cust,'curr',_curr,'fxrate',_rate); | ||||
|     RAISE NOTICE 'cust %', _cust; | ||||
|     _customer := jsonb_build_object( | ||||
|         'customer', | ||||
|         _customer||jsonb_build_object( | ||||
|             'cust',_cust | ||||
|             ,'curr',_curr | ||||
|             ,'fxrate',_rate | ||||
|         ) | ||||
|     ); | ||||
|     --RAISE NOTICE 'cust %', jsonb_pretty(_customer); | ||||
| 
 | ||||
|     ----------------price history------------------------------- | ||||
|     SELECT _rslt||jsonb_build_object('hist',rlarp.get_hist(_mold, _v1ds, _cust, substring(_chan,1,1))) INTO _rslt  ; | ||||
|     RAISE NOTICE 'result %', _rslt; | ||||
|     SELECT jsonb_build_object('hist',rlarp.get_hist(_mold, _v1ds, _cust, substring(_chan,1,1))) INTO _hist; | ||||
|     --RAISE NOTICE 'history %', _hist; | ||||
| 
 | ||||
|     ----------------target pricing------------------------------ | ||||
|     SELECT  | ||||
|         jsonb_build_object( | ||||
|             'v0tp',  | ||||
|             target_price,  | ||||
|             'stdv', | ||||
|             'v0stdv', | ||||
|             stdev_price  | ||||
|         ) | ||||
|     INTO  | ||||
| @ -133,13 +163,12 @@ BEGIN | ||||
|         AND season = _seas | ||||
|         AND data_segment = _v0ds | ||||
|         AND region = 'ALL'; | ||||
|     _rslt := _rslt||COALESCE(_v0tp,'{}'::jsonb); | ||||
|     ----------------target pricing------------------------------ | ||||
|     SELECT  | ||||
|         jsonb_build_object( | ||||
|             'v1tp',  | ||||
|             target_price,  | ||||
|             'stdv', | ||||
|             'v1stdv', | ||||
|             stdev_price  | ||||
|         ) | ||||
|     INTO  | ||||
| @ -152,10 +181,9 @@ BEGIN | ||||
|         AND data_segment = _dseg | ||||
|         AND region = 'ALL'; | ||||
|     --RAISE NOTICE 'target: %', jsonb_pretty(_targ); | ||||
|     _rslt := _rslt||COALESCE(_v1tp,'{}'::jsonb); | ||||
|     _pricing := (COALESCE(_v0tp,'{}'::jsonb)||COALESCE(_v1tp,'{}'::jsonb)); | ||||
| 
 | ||||
|     ----------------inflation index----------------------------- | ||||
|     RAISE NOTICE 'infaltion : %', jsonb_pretty(_iidx); | ||||
|     SELECT  | ||||
|         jsonb_build_object( | ||||
|             'iidx' | ||||
| @ -176,17 +204,21 @@ BEGIN | ||||
|         ) | ||||
|     GROUP BY | ||||
|         priority; | ||||
|     _rslt := _rslt||COALESCE(_iidx,'{}'::jsonb); | ||||
|     _pricing := _pricing||COALESCE(_iidx,'{}'::jsonb); | ||||
|     --RAISE NOTICE 'add targets: %', jsonb_pretty(_pricing); | ||||
| 
 | ||||
|     ----------------list pricing--------------------------------- | ||||
|     SELECT coalesce(rlarp.get_list(_bill, _ship, _item, _qty),'{}'::jsonb) INTO _list; | ||||
|     _rslt := _rslt||_list; | ||||
|     --RAISE NOTICE 'list: %', jsonb_pretty(_list); | ||||
|     _pricing := _pricing||_list; | ||||
|     --RAISE NOTICE 'add list: %', jsonb_pretty(_pricing); | ||||
| 
 | ||||
|     ----------------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; | ||||
|     _rslt := _rslt||_prem; | ||||
|     --RAISE NOTICE 'list: %', jsonb_pretty(_list); | ||||
|     _pricing := jsonb_build_object('pricing',_pricing||_prem); | ||||
|     --RAISE NOTICE 'add bridge: %', jsonb_pretty(_pricing); | ||||
| 
 | ||||
|     _rslt := _input||_product||_customer||_pricing||_hist; | ||||
|      | ||||
| 
 | ||||
|     RETURN _rslt; | ||||
| 
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user