Simplify parsing the (different) JSON sent to shift_ship_date.sql.

This commit is contained in:
PhilRunninger 2024-04-01 11:53:11 -04:00
parent 9933e66c77
commit ae6b21b197
2 changed files with 68 additions and 343 deletions

View File

@ -374,7 +374,7 @@ server.post('/shift_ship_dates', bodyParser.json(), function(req, res) {
req.body.stamp = new Date().toISOString()
sql = sql.replace(new RegExp("where_clause", 'g'), where);
sql = sql.replace(new RegExp("replace_request", 'g'), JSON.stringify(req.body));
sql = sql.replace(new RegExp("replace_request", 'g'), JSON.stringify(req.body.distributions));
sql = sql.replace(new RegExp("replace_version", 'g'), req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source", 'g'), req.body.source);
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));

View File

@ -1,19 +1,8 @@
-- Connection: usmidsap02.ubm
WITH
/*
the volume must be expressed in terms of units, since that is what it will be scaling
*/
input AS (
select $$replace_request$$::json spec
)
-- select 'input', * from input
--
,target as (
select
ship_season, ship_month, pct
from
input i,
json_to_recordset(i.spec->'distributions') AS x(ship_season int, ship_month text, pct numeric)
target as (
SELECT ship_season, ship_month, pct
FROM json_to_recordset('replace_request')
AS x(ship_season int, ship_month text, pct numeric)
)
-- select 'target', * from target
--
@ -39,52 +28,21 @@ input AS (
--
,basemix AS (
SELECT
fspr
,plnt
,promo
,terms
,bill_cust_descr
,ship_cust_descr
,dsm
,quota_rep_descr
,director
,billto_group
,shipto_group
,chan
,chansub
,chan_retail
,part
,part_descr
,part_group
,branding
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,segm
,substance
,fs_line
,r_currency
,coalesce(r_rate,1) as r_rate
,c_currency
,coalesce(c_rate,1) as c_rate
fspr ,plnt ,promo ,terms ,bill_cust_descr ,ship_cust_descr ,dsm ,quota_rep_descr ,director
,billto_group ,shipto_group ,chan ,chansub ,chan_retail ,part ,part_descr ,part_group
,branding ,majg_descr ,ming_descr ,majs_descr ,mins_descr ,segm ,substance ,fs_line
,r_currency ,coalesce(r_rate,1) as r_rate
,c_currency ,coalesce(c_rate,1) as c_rate
,sum(coalesce(units,0)) units
,sum(coalesce(value_loc,0)) value_loc
,sum(coalesce(value_usd,0)) value_usd
,sum(coalesce(cost_loc,0)) cost_loc
,sum(coalesce(cost_usd,0)) cost_usd
,sum(coalesce(pounds,0)) pounds
,calc_status
,flag
,order_date
,order_month
,order_season
,request_date
,request_month
,request_season
,ship_date
,ship_month
,ship_season
,calc_status ,flag
,order_date ,order_month ,order_season
,request_date ,request_month ,request_season
,ship_date ,ship_month ,ship_season
FROM
rlarp.osm_pool
WHERE
@ -93,97 +51,35 @@ input AS (
-----------------additional params-------------------
AND order_date <= ship_date
GROUP BY
fspr
,plnt
,promo
,terms
,bill_cust_descr
,ship_cust_descr
,dsm
,quota_rep_descr
,director
,billto_group
,shipto_group
,chan
,chansub
,chan_retail
,part
,part_descr
,part_group
,branding
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,segm
,substance
,fs_line
,r_currency
,r_rate
,c_currency
,c_rate
,calc_status
,flag
,order_date
,order_month
,order_season
,request_date
,request_month
,request_season
,ship_date
,ship_month
,ship_season
fspr ,plnt ,promo ,terms ,bill_cust_descr ,ship_cust_descr ,dsm ,quota_rep_descr ,director
,billto_group ,shipto_group ,chan ,chansub ,chan_retail ,part ,part_descr ,part_group
,branding ,majg_descr ,ming_descr ,majs_descr ,mins_descr ,segm ,substance ,fs_line
,r_currency ,r_rate
,c_currency ,c_rate
,calc_status ,flag
,order_date ,order_month ,order_season
,request_date ,request_month ,request_season
,ship_date ,ship_month ,ship_season
)
-- select 'basemix', * from basemix
--
,subtractions AS (
SELECT
fspr
,plnt
,promo
,terms
,bill_cust_descr
,ship_cust_descr
,dsm
,quota_rep_descr
,director
,billto_group
,shipto_group
,chan
,chansub
,chan_retail
,part
,part_descr
,part_group
,branding
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,segm
,substance
,fs_line
,r_currency
,r_rate
,c_currency
,c_rate
fspr ,plnt ,promo ,terms ,bill_cust_descr ,ship_cust_descr ,dsm ,quota_rep_descr ,director
,billto_group ,shipto_group ,chan ,chansub ,chan_retail ,part ,part_descr ,part_group
,branding ,majg_descr ,ming_descr ,majs_descr ,mins_descr ,segm ,substance ,fs_line
,r_currency ,r_rate
,c_currency ,c_rate
,-(1 - target.pct) * units as units
,-(1 - target.pct) * value_loc as value_loc
,-(1 - target.pct) * value_usd as value_usd
,-(1 - target.pct) * cost_loc as cost_loc
,-(1 - target.pct) * cost_usd as cost_usd
,-(1 - target.pct) * pounds as pounds
,calc_status
,flag
,order_date
,order_month
,order_season
,request_date
,request_month
,request_season
,ship_date
,basemix.ship_month
,basemix.ship_season
,calc_status ,flag
,order_date ,order_month ,order_season
,request_date ,request_month ,request_season
,ship_date ,basemix.ship_month ,basemix.ship_season
FROM basemix
INNER JOIN target ON
basemix.ship_season = target.ship_season AND
@ -193,52 +89,23 @@ input AS (
--
,additions AS (
SELECT
fspr
,plnt
,promo
,terms
,bill_cust_descr
,ship_cust_descr
,dsm
,quota_rep_descr
,director
,billto_group
,shipto_group
,chan
,chansub
,chan_retail
,part
,part_descr
,part_group
,branding
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,segm
,substance
,fs_line
,r_currency
,r_rate
,c_currency
,c_rate
fspr ,plnt ,promo ,terms ,bill_cust_descr ,ship_cust_descr ,dsm ,quota_rep_descr ,director
,billto_group ,shipto_group ,chan ,chansub ,chan_retail ,part ,part_descr ,part_group
,branding ,majg_descr ,ming_descr ,majs_descr ,mins_descr ,segm ,substance ,fs_line
,r_currency ,r_rate
,c_currency ,c_rate
,t.pct * coalesce(units) as units
,t.pct * coalesce(value_loc) as value_loc
,t.pct * coalesce(value_usd) as value_usd
,t.pct * coalesce(cost_loc) as cost_loc
,t.pct * coalesce(cost_usd) as cost_usd
,t.pct * coalesce(pounds) as pounds
,calc_status
,flag
,order_date
,order_month
,order_season
,request_date
,request_month
,request_season
,calc_status ,flag
,order_date ,order_month ,order_season
,request_date ,request_month ,request_season
-- These case statements fix the situation where a ship date was specified that is earlier than
-- the order date. If that happens, the new ship date becomes order date + 15 days. The case
-- statements also handle rolling over to the next month or season.
-- the order date. If that happens, the new ship date becomes order date + 15 days. This also
-- handles overflow into the next month and/or season.
,case
when t.ship_season <= b.order_season AND t.ship_month < b.order_month then b.order_date + 15
else make_date(t.ship_season, tMonths.cal, 1)
@ -273,51 +140,15 @@ input AS (
--
,final AS (
SELECT
fspr
,plnt
,promo
,terms
,bill_cust_descr
,ship_cust_descr
,dsm
,quota_rep_descr
,director
,billto_group
,shipto_group
,chan
,chansub
,chan_retail
,part
,part_descr
,part_group
,branding
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,segm
,substance
,fs_line
,r_currency
,r_rate
,c_currency
,c_rate
,units
,value_loc
,value_usd
,cost_loc
,cost_usd
,calc_status
,flag
,order_date
,order_month
,order_season
,request_date
,request_month
,request_season
,ship_date
,ship_month
,ship_season
fspr ,plnt ,promo ,terms ,bill_cust_descr ,ship_cust_descr ,dsm ,quota_rep_descr ,director
,billto_group ,shipto_group ,chan ,chansub ,chan_retail ,part ,part_descr ,part_group
,branding ,majg_descr ,ming_descr ,majs_descr ,mins_descr ,segm ,substance ,fs_line
,r_currency ,r_rate
,c_currency ,c_rate
,units ,value_loc ,value_usd ,cost_loc ,cost_usd ,calc_status ,flag
,order_date ,order_month ,order_season
,request_date ,request_month ,request_season
,ship_date ,ship_month ,ship_season
,'replace_version' "version"
,'replace_source' iter
,log.id
@ -325,63 +156,11 @@ input AS (
,log.doc->>'message' "comment"
,log.doc->>'type' module
,pounds
FROM subtractions
CROSS JOIN log
FROM
( SELECT * FROM subtractions
UNION ALL
SELECT
fspr
,plnt
,promo
,terms
,bill_cust_descr
,ship_cust_descr
,dsm
,quota_rep_descr
,director
,billto_group
,shipto_group
,chan
,chansub
,chan_retail
,part
,part_descr
,part_group
,branding
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,segm
,substance
,fs_line
,r_currency
,r_rate
,c_currency
,c_rate
,units
,value_loc
,value_usd
,cost_loc
,cost_usd
,calc_status
,flag
,order_date
,order_month
,order_season
,request_date
,request_month
,request_season
,ship_date
,ship_month
,ship_season
,'replace_version' "version"
,'replace_source' iter
,log.id
,COALESCE(log.doc->>'tag','Volume') "tag"
,log.doc->>'message' "comment"
,log.doc->>'type' module
,pounds
FROM additions
SELECT * FROM additions
) f
CROSS JOIN log
)
-- select 'final', * from final
@ -392,40 +171,13 @@ CROSS JOIN log
,insagg AS (
SELECT
---------customer info-----------------
bill_cust_descr
,billto_group
,ship_cust_descr
,shipto_group
,quota_rep_descr
,director
,segm
,substance
,chan
,chansub
bill_cust_descr ,billto_group ,ship_cust_descr ,shipto_group ,quota_rep_descr ,director
,segm ,substance ,chan ,chansub
---------product info------------------
,majg_descr
,ming_descr
,majs_descr
,mins_descr
--,brand
--,part_family
,part_group
,branding
--,color
,part_descr
,majg_descr ,ming_descr ,majs_descr ,mins_descr ,part_group ,branding ,part_descr
---------dates-------------------------
,order_season
,order_month
,ship_season
,ship_month
,request_season
,request_month
,promo
,version
,iter
,logid
,tag
,comment
,order_season ,order_month ,ship_season ,ship_month ,request_season ,request_month
,promo ,version ,iter ,logid ,tag ,comment
--------values-------------------------
,sum(value_loc) value_loc
,sum(value_usd) value_usd
@ -437,41 +189,14 @@ CROSS JOIN log
ins
GROUP BY
---------customer info-----------------
bill_cust_descr
,billto_group
,ship_cust_descr
,shipto_group
,quota_rep_descr
,director
,segm
,substance
,chan
,chansub
bill_cust_descr ,billto_group ,ship_cust_descr ,shipto_group ,quota_rep_descr ,director
,segm ,substance ,chan ,chansub
---------product info------------------
,majg_descr
,ming_descr
,majs_descr
,mins_descr
--,brand
--,part_family
,part_group
,branding
--,color
,part_descr
,majg_descr ,ming_descr ,majs_descr ,mins_descr ,part_group ,branding ,part_descr
---------dates-------------------------
,order_season
,order_month
,ship_season
,ship_month
,request_season
,request_month
,promo
,version
,iter
,logid
,tag
,comment
,order_season ,order_month ,ship_season ,ship_month ,request_season ,request_month
,promo ,version ,iter ,logid ,tag ,comment
)
-- select 'insagg', * from insagg
--
SELECT json_agg(row_to_json(insagg)) x from insagg
SELECT json_agg(row_to_json(insagg)) x FROM insagg