diff --git a/sql/get.pg.sql b/sql/get.pg.sql index cd46d76..d949450 100644 --- a/sql/get.pg.sql +++ b/sql/get.pg.sql @@ -1,2 +1,2 @@ --select gg.d from rlarp.get_guidance('DIAM0004','DIAM0004','AMK06000G18B054',5000,2024) gg(d); -select gg.d doc from rlarp.get_guidance($1,$2, $3, $4,2024) gg(d); +SELECT gg.d doc FROM rlarp.get_guidance($1,$2, $3, $4,2024) gg(d); diff --git a/sql/get_guidance.pg.sql b/sql/get_guidance.pg.sql index 1c665c3..24691ae 100644 --- a/sql/get_guidance.pg.sql +++ b/sql/get_guidance.pg.sql @@ -13,6 +13,7 @@ DECLARE _mold text; _cust text; _v1ds text; + _v0ds text; _chan text; _regn text; _rslt jsonb; @@ -28,29 +29,35 @@ BEGIN ----------------base product-------------------------------- + SELECT - m.part_group, i.v1ds + m.part_group, i.v1ds,i.v0ds INTO - _mold,_v1ds + _mold,_v1ds ,_v0ds FROM "CMS.CUSLG".itemmv i INNER JOIN rlarp.molds m ON m.stlc = i.stlc WHERE i.item = _item; - RAISE NOTICE 'mold %', _mold; - RAISE NOTICE 'v1ds %', _v1ds; + --RAISE NOTICE 'mold %', _mold; + --RAISE NOTICE 'v1ds %', _v1ds; + _rslt := jsonb_build_object('mold',_mold,'v1ds',_v1ds,'v0ds',_v0ds); ----------------channel------------------------------------- + SELECT rlarp.channel_code(_bill, _ship) INTO _chan; - RAISE NOTICE '%', _chan; + _rslt := _rslt||jsonb_build_object('chan',_chan); + --RAISE NOTICE '%', _chan; ----------------customer------------------------------------ + SELECT dba INTO _cust FROM rlarp.cust WHERE code = CASE WHEN _chan = 'DRP' THEN _ship ELSE _bill END ; - RAISE NOTICE 'cust %', _cust; + _rslt = _rslt||jsonb_build_object('cust',_cust); + --RAISE NOTICE 'cust %', _cust; ----------------price history------------------------------- - SELECT jsonb_build_object('hist',rlarp.gethist(_mold, _cust)) INTO _rslt ; + SELECT _rslt||jsonb_build_object('hist',rlarp.gethist(_mold, _cust)) INTO _rslt ; --RAISE NOTICE '%', _rslt; ----------------target pricing------------------------------ @@ -70,11 +77,13 @@ BEGIN AND season = _seas AND data_segment = _v1ds AND region = 'ALL'; - RAISE NOTICE 'target: %', jsonb_pretty(_targ); + --RAISE NOTICE 'target: %', jsonb_pretty(_targ); + _rslt := _rslt||COALESCE(_targ,'{}'::jsonb); ----------------list ppricing------------------------------- - SELECT rlarp.get_list(_bill, _ship, _item, _qty) INTO _list; - RAISE NOTICE 'list: %', jsonb_pretty(_list); + SELECT coalesce(rlarp.get_list(_bill, _ship, _item, _qty),'{}'::jsonb) INTO _list; + _rslt := _rslt||COALESCE(_list,'{}'::jsonb); + --RAISE NOTICE 'list: %', jsonb_pretty(_list); RETURN _rslt; diff --git a/sql/guidance.pg.sql b/sql/guidance.pg.sql index da98219..d67e528 100644 --- a/sql/guidance.pg.sql +++ b/sql/guidance.pg.sql @@ -1,15 +1,15 @@ -CREATE OR REPLACE FUNCTION rlarp.guidancejsd(_p jsonb, cust text, prod text) +CREATE OR REPLACE FUNCTION rlarp.apply_guid(data jsonb) RETURNS jsonb LANGUAGE plv8 AS $function$ -function findMostRecentPrice(data, cust, prod) { +function findMostRecentPrice(data) { let mostRecentPrice = null; let mostRecentYear = null; // Iterate through each product - data.forEach(product => { + data.forEach( data.hist => { // Check if the product matches the customer channel and version if (product.chan === cust && (product.v1ds === prod || product.v0ds === prod)) { // Iterate through the seasons for the product @@ -30,7 +30,7 @@ function findMostRecentPrice(data, cust, prod) { } // Example usage: -const jsonData = _p; +const jsonData = data; const cust = 'W'; // or 'D', depending on the customer const prod = 'TUH10000'; // the product version you're interested in