From 723e2a250cc019f0f95ac6f12a23d144488b781c Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Wed, 3 Apr 2019 03:26:07 -0400 Subject: [PATCH] fix add month route for volume only with base = 0 --- index.js | 2 +- route_sql/addmonth_vd.sql | 51 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 6c833b9..0289865 100644 --- a/index.js +++ b/index.js @@ -193,7 +193,7 @@ server.post('/addmonth_v', bodyParser.json(), function (req, res) { console.log(req.body); //console.log(args); sql = sql.replace(new RegExp("scenario = target_scenario",'g'),w); - sql = sql.replace(new RegExp("target_increment",'g'),req.body.amount); + sql = sql.replace(new RegExp("target_increment",'g'),req.body.qty); sql = sql.replace(new RegExp("target_month",'g'),req.body.month); sql = sql.replace(new RegExp("replace_version",'g'),req.body.scenario.version); sql = sql.replace(new RegExp("replace_source",'g'),req.body.source); diff --git a/route_sql/addmonth_vd.sql b/route_sql/addmonth_vd.sql index 0c2370a..712bd88 100644 --- a/route_sql/addmonth_vd.sql +++ b/route_sql/addmonth_vd.sql @@ -7,6 +7,42 @@ target_month WITH target AS (select target_increment incr) +,testv AS ( + SELECT + sum(fb_qty) tot + ,sum(fb_qty) FILTER (WHERE iter = 'copy') base + ,sum(fb_qty) FILTER (WHERE iterdef->>'type' = 'new basket') newpart + FROM + rlarp.osm_fcpool + WHERE + -----------------scenario---------------------------- + scenario = target_scenario + -----------------additional params------------------- + AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments + AND adj_orderdate <= adj_shipdate + +) +,flagv AS ( + SELECT + tot + ,base + ,newpart + ,CASE WHEN tot = 0 THEN + CASE WHEN base = 0 THEN + CASE WHEN newpart = 0 THEN + 'unclean data. tested -> does not exist' + ELSE + 'scale new part' + END + ELSE + 'scale copy' + END + ELSE + 'scale all' + END flag + FROM + testv +) ,GLD AS ( SELECT N1COMP COMP @@ -66,7 +102,11 @@ target AS (select target_increment incr) scenario = target_scenario -----------------additional params------------------- AND version = 'b20' - AND iter = 'copy' + AND CASE (SELECT flag FROM flagv) + WHEN 'scale all' THEN true + WHEN 'scale copy' THEN iter = 'copy' + WHEN 'scale new part' THEN iterdef->>'type' = 'new basket' + END AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments AND adj_orderdate <= adj_shipdate GROUP BY @@ -249,6 +289,11 @@ SELECT -----------------scenario---------------------------- scenario = target_scenario -----------------additional params------------------- + AND CASE (SELECT flag FROM flagv) + WHEN 'scale all' THEN true + WHEN 'scale copy' THEN iter = 'copy' + WHEN 'scale new part' THEN iterdef->>'type' = 'new basket' + END AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments AND adj_orderdate <= adj_shipdate GROUP BY @@ -315,8 +360,8 @@ SELECT ,scale AS ( SELECT (SELECT incr::numeric FROM target) incr - ,(SELECT sum(fb_val_loc *r_rate) FROM basemix) base - ,(SELECT incr::numeric FROM target)/(SELECT sum(fb_val_loc *r_rate) FROM basemix) factor + ,(SELECT sum(fb_qty) FROM basemix) base + ,(SELECT incr::numeric FROM target)/(SELECT sum(fb_qty) FROM basemix) factor ) ,final AS ( SELECT