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() req.body.stamp = new Date().toISOString()
sql = sql.replace(new RegExp("where_clause", 'g'), where); 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_version", 'g'), req.body.scenario.version);
sql = sql.replace(new RegExp("replace_source", 'g'), req.body.source); sql = sql.replace(new RegExp("replace_source", 'g'), req.body.source);
sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body)); sql = sql.replace(new RegExp("replace_iterdef", 'g'), JSON.stringify(req.body));

View File

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