From 8cb6b843ba2f40bd3914a8f0462c3316a59a8706 Mon Sep 17 00:00:00 2001 From: fleetside72 Date: Wed, 3 Apr 2019 01:42:10 -0400 Subject: [PATCH] recast scale_v to be based on qty and handle -0- qty --- index.js | 2 +- route_sql/scale_vd.sql | 54 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 1d78d7e..6c833b9 100644 --- a/index.js +++ b/index.js @@ -269,7 +269,7 @@ server.post('/scale_v', bodyParser.json(), function (req, res) { console.log(req.body); //console.log(args); sql = sql.replace(new RegExp("where_clause",'g'),w); - sql = sql.replace(new RegExp("target_increment",'g'),req.body.amount); + sql = sql.replace(new RegExp("incr_qty",'g'),req.body.qty); 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)); diff --git a/route_sql/scale_vd.sql b/route_sql/scale_vd.sql index 5e9e5d6..45b00c2 100644 --- a/route_sql/scale_vd.sql +++ b/route_sql/scale_vd.sql @@ -1,5 +1,42 @@ WITH -target AS (select target_increment incr) +target AS (select incr_qty qincr) +,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---------------------------- + where_clause + -----------------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 +) +--ever need receive a target of -0- qty but value <> 0? ,basemix AS ( SELECT plnt ---master data @@ -76,8 +113,8 @@ SELECT ,adj_orderdate --history ,adj_requestdate --history ,adj_shipdate --history - ,'b20' "version" --calculated - ,'adjustment' iter --calculated + ,null::text "version" --calculated + ,null::text iter --calculated ---------------ui columns------------------------- ,order_season ,order_month @@ -114,6 +151,11 @@ SELECT -----------------scenario---------------------------- where_clause -----------------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 @@ -190,12 +232,14 @@ SELECT ,quota_rep_descr ,director_descr ) +--SELECT * FROM basemix ,scale AS ( SELECT - (SELECT incr::numeric FROM target) incr + (SELECT qincr::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 qincr FROM target)/(SELECT SUM(fb_qty) FROM basemix) factor ) +--select * from scale ,final AS ( SELECT b.plnt --master data