price_api/sql/get_guidance_dseg.pg.sql

251 lines
7.2 KiB
MySQL
Raw Normal View History

2023-11-20 15:46:59 -05:00
DROP FUNCTION rlarp.get_guidance_dseg CASCADE;
2023-11-08 13:20:53 -05:00
CREATE OR REPLACE FUNCTION rlarp.get_guidance_dseg(_bill text, _ship text, _stlc text, _dseg text, _qty numeric, _seas int)
RETURNS jsonb
LANGUAGE plpgsql AS
--DO
$func$
DECLARE
--_item text;
--_bill text;
--_ship text;
--_qty numeric;
--_seas int;
2023-11-13 11:02:41 -05:00
_prem jsonb;
_mold text;
_item text;
_unti text;
_pltq numeric;
2023-11-20 15:46:59 -05:00
_fstd numeric;
2023-11-29 08:16:26 -05:00
_fstdina numeric;
2023-11-13 11:02:41 -05:00
_cust text;
_curr text;
_rate numeric;
_v1ds text;
_v0ds text;
_v1tp jsonb;
_v0tp jsonb;
_chan text;
_regn text;
_rslt jsonb;
_targ jsonb;
_list jsonb;
_iidx jsonb;
_itemr text;
_input jsonb;
_product jsonb;
_customer jsonb;
_hist jsonb;
_pricing jsonb;
2023-11-08 13:20:53 -05:00
BEGIN
--_item := 'AMK06000G18B054';
--_bill := 'DIAM0004';
--_ship := 'DIAM0004';
--_qty := 5000;
--_seas := 2024;
2023-11-10 16:04:06 -05:00
_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;
2023-11-08 13:20:53 -05:00
2023-11-13 11:02:41 -05:00
_input := jsonb_build_object(
'inputs'
,jsonb_build_object(
'dseg',_dseg,
'v0ds',_v0ds,
'v1ds',_v1ds,
'bill',_bill,
'ship',_ship,
'stlc',_stlc,
'qty',_qty,
'season',_seas
));
2023-11-08 13:20:53 -05:00
----------------base product--------------------------------
SELECT
2023-11-10 11:44:21 -05:00
part_group
,item
,idxk
2023-11-10 16:04:06 -05:00
,prefer
2023-11-15 13:47:00 -05:00
,pltq
2024-02-29 13:07:03 -05:00
,futstdus
,futstdus_ina
2023-11-08 13:20:53 -05:00
INTO
_mold
,_item
,_iidx
2023-11-10 16:04:06 -05:00
,_itemr
2023-11-15 13:47:00 -05:00
,_pltq
2023-11-20 15:46:59 -05:00
,_fstd
2023-11-29 08:16:26 -05:00
,_fstdina
2023-11-10 11:44:21 -05:00
FROM
(
SELECT
2024-02-29 13:07:03 -05:00
i.partgroup part_group
2023-11-10 11:44:21 -05:00
,min(i.item) item
,i.stlc
,i.v1ds
2024-02-29 13:07:03 -05:00
,_ds.dataseg v0ds
,i.mpck pltq
,avg(i.futstdus) FILTER (WHERE aplnt <> 'I') futstdus
,avg(i.futstdus) futstdus_ina
2023-11-10 11:44:21 -05:00
,jsonb_strip_nulls(jsonb_build_object('assc',CASE WHEN i.assc <> '' THEN i.assc ELSE null::text END,'majg',i.majg::int,'coltier',i.coltier)) idxk
2024-02-29 13:07:03 -05:00
,CASE WHEN i.v1ds = _v1ds THEN 2 ELSE CASE WHEN _ds.dataseg = _v0ds THEN 1 ELSE 0 END END prefer
2023-11-10 11:44:21 -05:00
FROM
2024-02-29 13:07:03 -05:00
"CMS.CUSLG".itemm i
LEFT OUTER JOIN _ds ON
_ds.colgrp = i.colgrp
AND _ds.brand = SUBSTRING(i.branding,1,1)
--INNER JOIN rlarp.molds m ON
-- m.stlc = i.stlc
2023-11-10 11:44:21 -05:00
WHERE
i.stlc = _stlc
GROUP BY
2024-02-29 13:07:03 -05:00
i.partgroup
2023-11-10 11:44:21 -05:00
,i.stlc
,i.v1ds
2024-02-29 13:07:03 -05:00
,_ds.dataseg
,i.mpck
2023-11-10 11:44:21 -05:00
,jsonb_strip_nulls(jsonb_build_object('assc',CASE WHEN i.assc <> '' THEN i.assc ELSE null::text END,'majg',i.majg::int,'coltier',i.coltier))
2024-02-29 13:07:03 -05:00
,CASE WHEN i.v1ds = _v1ds THEN 2 ELSE CASE WHEN _ds.dataseg = _v0ds THEN 1 ELSE 0 END END
2023-11-10 11:44:21 -05:00
) best
ORDER BY
prefer DESC
LIMIT 1;
_product :=
2023-11-13 11:02:41 -05:00
jsonb_build_object(
2023-11-15 13:47:00 -05:00
'product'
,jsonb_build_object(
'mold',_mold
,'item',_item
,'itemrel',_itemr
,'iidx',_iidx
,'pltq',_pltq
2024-02-29 13:07:03 -05:00
,'futstdus',_fstd
,'futstdus_ina',_fstdina
2023-11-15 13:47:00 -05:00
)
);
2023-11-13 11:02:41 -05:00
--RAISE NOTICE 'item data %', jsonb_pretty(_product||_input);
2023-11-08 13:20:53 -05:00
----------------channel-------------------------------------
SELECT rlarp.get_cust(_bill, _ship) INTO _customer;
--_customer := jsonb_build_object('chan',_chan);
2023-11-08 13:20:53 -05:00
----------------customer------------------------------------
--SELECT dba INTO _cust FROM rlarp.cust WHERE code = CASE WHEN _chan = 'DRP' THEN _ship ELSE _bill END ;
--SELECT
-- currency,
-- (SELECT
-- x.rate
-- FROM
-- rlarp.ffcret x
-- WHERE
-- x.perd = (select fspr from rlarp.gld where drange @> current_date)
-- AND x.rtyp = 'MA'
-- and x.fcur = currency
-- AND x.tcur = 'US'
-- )
--INTO
-- _curr
-- ,_rate
--FROM
-- rlarp.cust
--WHERE
-- code = _bill;
2023-11-10 16:04:06 -05:00
--_customer := jsonb_build_object(
-- 'customer',
-- _customer||jsonb_build_object(
-- 'cust',_cust
-- ,'curr',_curr
-- ,'fxrate',_rate
-- )
--);
2023-11-13 11:02:41 -05:00
--RAISE NOTICE 'cust %', jsonb_pretty(_customer);
2023-11-08 13:20:53 -05:00
----------------price history-------------------------------
2023-11-21 15:39:36 -05:00
--RAISE NOTICE 'varb %', _customer;
2023-11-21 15:38:47 -05:00
SELECT jsonb_build_object('hist',rlarp.get_hist(_mold, _v1ds, _customer->'customer'->>'cust', substring(_customer->'customer'->>'chan',1,1))) INTO _hist;
2023-11-13 11:45:47 -05:00
--RAISE NOTICE 'history %', jsonb_pretty(_hist);
2023-11-08 13:20:53 -05:00
----------------target pricing------------------------------
SELECT
jsonb_build_object(
'v0tp',
target_price,
2023-11-13 11:02:41 -05:00
'v0stdv',
2023-11-08 13:20:53 -05:00
stdev_price
)
INTO
_v0tp
FROM
pricequote.market_setavgprice
WHERE
mold = _stlc
AND season = _seas
AND data_segment = _v0ds
2023-12-08 11:37:07 -05:00
AND region = 'ALL'
AND chan = _customer->'customer'->>'chantp';
2023-11-08 13:20:53 -05:00
----------------target pricing------------------------------
SELECT
jsonb_build_object(
'v1tp',
target_price,
2023-11-13 11:02:41 -05:00
'v1stdv',
2023-11-08 13:20:53 -05:00
stdev_price
)
INTO
_v1tp
FROM
pricequote.market_setavgprice
WHERE
mold = _stlc
AND season = _seas
2023-11-10 16:04:06 -05:00
AND data_segment = _dseg
2023-12-08 11:37:07 -05:00
AND region = 'ALL'
AND chan = _customer->'customer'->>'chantp';
2023-11-08 13:20:53 -05:00
--RAISE NOTICE 'target: %', jsonb_pretty(_targ);
2023-11-13 11:02:41 -05:00
_pricing := (COALESCE(_v0tp,'{}'::jsonb)||COALESCE(_v1tp,'{}'::jsonb));
2023-11-08 13:20:53 -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;
2023-11-13 11:02:41 -05:00
_pricing := _pricing||COALESCE(_iidx,'{}'::jsonb);
--RAISE NOTICE 'add targets: %', jsonb_pretty(_pricing);
2023-11-10 01:44:37 -05:00
----------------list pricing---------------------------------
2023-11-08 13:20:53 -05:00
SELECT coalesce(rlarp.get_list(_bill, _ship, _item, _qty),'{}'::jsonb) INTO _list;
2023-11-13 11:02:41 -05:00
_pricing := _pricing||_list;
--RAISE NOTICE 'add list: %', jsonb_pretty(_pricing);
2023-11-08 13:20:53 -05:00
2023-11-10 01:44:37 -05:00
----------------get premium for quote hist gap--------------
2023-12-08 12:32:18 -05:00
SELECT coalesce(rlarp.get_premium(_stlc, _seas, _customer->'customer'->>'chantp',_hist->'hist'->'cust'->>'ds', _v1ds),'{}'::jsonb) INTO _prem;
2023-11-13 11:02:41 -05:00
_pricing := jsonb_build_object('pricing',_pricing||_prem);
--RAISE NOTICE 'add bridge: %', jsonb_pretty(_pricing);
_rslt := _input||_product||_customer||_pricing||_hist;
2023-11-10 01:44:37 -05:00
2023-11-08 13:20:53 -05:00
RETURN _rslt;
END;
$func$;