price_api/sql/get_guidance.pg.sql

116 lines
3.1 KiB
MySQL
Raw Normal View History

2023-11-03 14:30:43 -04:00
DROP FUNCTION rlarp.get_guidance;
CREATE OR REPLACE FUNCTION rlarp.get_guidance(_bill text, _ship text, _item text, _qty numeric, _seas int)
RETURNS jsonb
LANGUAGE plpgsql AS
--DO
$func$
DECLARE
--_item text;
--_bill text;
--_ship text;
--_qty numeric;
--_seas int;
_mold text;
_cust text;
_v1ds text;
2023-11-03 21:16:15 -04:00
_v0ds text;
2023-11-03 14:30:43 -04:00
_chan text;
_regn text;
_rslt jsonb;
_targ jsonb;
_list jsonb;
2023-11-07 00:48:22 -05:00
_iidx jsonb;
2023-11-03 14:30:43 -04:00
BEGIN
--_item := 'AMK06000G18B054';
--_bill := 'DIAM0004';
--_ship := 'DIAM0004';
--_qty := 5000;
--_seas := 2024;
----------------base product--------------------------------
2023-11-03 21:16:15 -04:00
2023-11-03 14:30:43 -04:00
SELECT
2023-11-07 00:48:22 -05:00
m.part_group, i.v1ds,i.v0ds,jsonb_build_object('assc',i.assc,'majg',i.majg::int,'coltier',i.coltier) idxk
2023-11-03 14:30:43 -04:00
INTO
2023-11-07 00:48:22 -05:00
_mold,_v1ds ,_v0ds, _iidx
2023-11-03 14:30:43 -04:00
FROM
"CMS.CUSLG".itemmv i
INNER JOIN rlarp.molds m ON
m.stlc = i.stlc
WHERE
i.item = _item;
2023-11-03 21:16:15 -04:00
--RAISE NOTICE 'mold %', _mold;
--RAISE NOTICE 'v1ds %', _v1ds;
2023-11-07 00:48:22 -05:00
_rslt := jsonb_build_object('mold',_mold,'v1ds',_v1ds,'v0ds',_v0ds)||_iidx;
2023-11-03 14:30:43 -04:00
----------------channel-------------------------------------
2023-11-03 21:16:15 -04:00
2023-11-03 14:30:43 -04:00
SELECT rlarp.channel_code(_bill, _ship) INTO _chan;
2023-11-03 21:16:15 -04:00
_rslt := _rslt||jsonb_build_object('chan',_chan);
--RAISE NOTICE '%', _chan;
2023-11-03 14:30:43 -04:00
----------------customer------------------------------------
2023-11-03 21:16:15 -04:00
2023-11-03 14:30:43 -04:00
SELECT dba INTO _cust FROM rlarp.cust WHERE code = CASE WHEN _chan = 'DRP' THEN _ship ELSE _bill END ;
2023-11-03 21:16:15 -04:00
_rslt = _rslt||jsonb_build_object('cust',_cust);
--RAISE NOTICE 'cust %', _cust;
2023-11-03 14:30:43 -04:00
----------------price history-------------------------------
2023-11-03 21:16:15 -04:00
SELECT _rslt||jsonb_build_object('hist',rlarp.gethist(_mold, _cust)) INTO _rslt ;
2023-11-03 15:59:50 -04:00
--RAISE NOTICE '%', _rslt;
2023-11-03 14:30:43 -04:00
----------------target pricing------------------------------
SELECT
jsonb_build_object(
'v1tp',
target_price,
'stdv',
stdev_price
)
INTO
_targ
FROM
pricequote.market_setavgprice
WHERE
mold = _mold
AND season = _seas
AND data_segment = _v1ds
AND region = 'ALL';
2023-11-03 21:16:15 -04:00
--RAISE NOTICE 'target: %', jsonb_pretty(_targ);
_rslt := _rslt||COALESCE(_targ,'{}'::jsonb);
2023-11-03 14:30:43 -04:00
2023-11-07 00:48:22 -05:00
----------------inflation index-----------------------------
SELECT
jsonb_build_object(
'iidx'
,jsonb_build_object(
priority
,min(factor)
)
)
INTO
_iidx
FROM
rlarp.costindex
WHERE
timeframe @> current_date
AND (
attr @> _iidx
OR attr @> jsonb_build_object('stlc',_mold)
)
GROUP BY
priority;
--RAISE NOTICE 'target: %', jsonb_pretty(_targ);
_rslt := _rslt||COALESCE(_iidx,'{}'::jsonb);
2023-11-03 14:30:43 -04:00
----------------list ppricing-------------------------------
2023-11-03 21:16:15 -04:00
SELECT coalesce(rlarp.get_list(_bill, _ship, _item, _qty),'{}'::jsonb) INTO _list;
2023-11-06 23:02:55 -05:00
_rslt := _rslt||_list;
2023-11-03 21:16:15 -04:00
--RAISE NOTICE 'list: %', jsonb_pretty(_list);
2023-11-03 14:30:43 -04:00
RETURN _rslt;
END;
$func$;