diff --git a/index.js b/index.js index 44380fc..bb85422 100644 --- a/index.js +++ b/index.js @@ -106,9 +106,7 @@ server.get('/scale', bodyParser.json(), function(req, res) { var args = []; var app_pincr = req.body.app_pincr; - var app_req = req.body.app_req; - var app_scenario = req.body.app_scenario; - var app_units = req.body.app_units; + var app_req = JSON.stringify(req.body); var app_vincr = req.body.app_vincr; var callback = function(arg) { @@ -125,8 +123,6 @@ server.get('/scale', bodyParser.json(), function(req, res) { //sql = sql.replace(new RegExp("where_clause", 'g'), w) sql = sql.replace(new RegExp("app_pincr", 'g'), app_pincr); sql = sql.replace(new RegExp("app_req", 'g'), app_req); - sql = sql.replace(new RegExp("app_scenario", 'g'), app_scenario); - sql = sql.replace(new RegExp("app_units", 'g'), app_units); sql = sql.replace(new RegExp("app_vincr", 'g'), app_vincr); sql = sql.replace(new RegExp("app_where", 'g'), w); //execute the sql and send the result @@ -151,7 +147,7 @@ function build_where(req, c, w, d, args) { //loop through each top level item expected to be a simple key/value list reflecting the column and the target value // "part":"XFRM500", "customer":"Sanford and Son" --> SQL --> part = 'XFRM500' // AND customer = 'Sanford and Son' - for (var i in req.body.scenario) { + for (var i in req.body.app_scenario) { //console.log(i); ///console.log(req.body[i]); //this step applies the AND seperator only @@ -160,22 +156,22 @@ function build_where(req, c, w, d, args) { ` AND `; } - if (Array.isArray(req.body.scenario[i])) { + if (Array.isArray(req.body.app_scenario[i])) { //if the scenario key has a value that is an array of items, push it into an `IN` statement //iter = [stage1, stage2] --> SQL --> iter IN ('stag1', stage2') w = w + i + " IN ("; - for (var j in req.body.scenario[i]) { + for (var j in req.body.app_scenario[i]) { if (d > 1) { w = w + ","; } - w = w + "'" + req.body.scenario[i][j] + "'"; + w = w + "'" + req.body.app_scenario[i][j] + "'"; d = d + 1; } w = w + ")"; } else { - w = w + i + " = '" + req.body.scenario[i] + "'"; + w = w + i + " = '" + req.body.app_scenario[i] + "'"; } - args.push(req.body.scenario[i]); + args.push(req.body.app_scenario[i]); c = c + 1; }; return { c, w, d }; diff --git a/routes/scale/gen_scale.sql b/routes/scale/gen_scale.sql index 3dab45a..1d9eb97 100644 --- a/routes/scale/gen_scale.sql +++ b/routes/scale/gen_scale.sql @@ -40,7 +40,7 @@ SELECT string_agg( --create the column reference 'o.'||format('%I',cname)|| - CASE WHEN appcol IN ('units', 'value', 'cost') THEN ' * vscale.factor' ELSE '' END, + CASE WHEN appcol IN ('units', 'value', 'cost') THEN ' * vscale.factor AS '||format('%I',cname) ELSE '' END, --delimiter E'\n ,' --sort column ordinal @@ -79,10 +79,10 @@ SELECT $$WITH req AS (SELECT $$||'$$app_req$$::jsonb)'||$$ -----this block is supposed to test for new products that might not be in baseline etc------- -test AS ( +,test AS ( SELECT - sum(app_units) FILTER WHERE (version <> 'ACTUALS') total - ,sum(app_units) FILTER (WHERE iter = 'baseline') base + sum($$||_units_col||$$) FILTER (WHERE version <> 'ACTUALS') total + ,sum($$||_units_col||$$) FILTER (WHERE iter = 'baseline') base FROM fc.live WHERE @@ -91,20 +91,24 @@ test AS ( ,basemix AS ( SELECT $$||_clist||$$ +FROM + fc.live o WHERE - app_scenario + app_where ), vscale AS ( SELECT app_vincr AS target_increment ,sum($$||_units_col||') AS units'||$$ ,app_vincr/sum($$||_units_col||$$) AS factor + FROM + basemix ) ,volume AS ( SELECT $$||_clist_vol||$$ FROM - baseline + basemix o CROSS JOIN vscale ) ,pscale AS ( @@ -128,6 +132,8 @@ SELECT ELSE 0 END mod_price +FROM + volume ) ,pricing AS ( SELECT diff --git a/routes/scale/req.json b/routes/scale/req.json index 25fcfeb..4164aa1 100644 --- a/routes/scale/req.json +++ b/routes/scale/req.json @@ -1,19 +1,11 @@ { - "scenario": { - "quota_rep_descr": "TINA PETTIGREW", - "substance": "Plastic", + "app_scenario": { "chan": "DIR", - "billto_group": "H&A MASTRONARDI", - "shipto_group": "H&A MASTRONARDI", - "majg_descr": "110 - INJECTION", - "part_descr": "HWA13000E21B025LZBLD - 13.00 WRAPT HP CHOCOLATE", - "version": "b21", + "account": "H&A MASTRONARDI", + "shipgrp": "H&A MASTRONARDI", + "part": "HWA13000E21B025LZBLD", "iter": [ - "copy", - "adj volume", - "adj price", - "upload volume", - "upload price" + "baseline" ] }, "stamp": "2020-02-19 17:03:38", @@ -21,7 +13,7 @@ "source": "adj", "message": "2% on volume and price", "tag": "standard price", - "version": "b21", + "version": "forecast_name", "type": "scale_vp", "app_vincr": 453.60, "app_pincr": 728.43624 diff --git a/routes/scale/scale.sql b/routes/scale/scale.sql index dc14371..59c646c 100644 --- a/routes/scale/scale.sql +++ b/routes/scale/scale.sql @@ -1,10 +1,10 @@ WITH req AS (SELECT $$app_req$$::jsonb) -----this block is supposed to test for new products that might not be in baseline etc------- -test AS ( +,test AS ( SELECT - sum(app_units) FILTER WHERE (version <> 'ACTUALS') total - ,sum(app_units) FILTER (WHERE iter = 'baseline') base + sum(fb_qty) FILTER (WHERE version <> 'ACTUALS') total + ,sum(fb_qty) FILTER (WHERE iter = 'baseline') base FROM fc.live WHERE @@ -113,14 +113,18 @@ SELECT ,o.rseas ,o.sdate ,o.sseas +FROM + fc.live o WHERE - app_scenario + app_where ), vscale AS ( SELECT app_vincr AS target_increment ,sum(fb_qty) AS units ,app_vincr/sum(fb_qty) AS factor + FROM + basemix ) ,volume AS ( SELECT @@ -208,13 +212,13 @@ SELECT ,o.r_rate ,o.c_currency ,o.c_rate - ,o.fb_qty * vscale.factor - ,o.fb_val_loc * vscale.factor + ,o.fb_qty * vscale.factor AS fb_qty + ,o.fb_val_loc * vscale.factor AS fb_val_loc ,o.fb_val_loc_dis ,o.fb_val_loc_qt ,o.fb_val_loc_pl ,o.fb_val_loc_tar - ,o.fb_cst_loc * vscale.factor + ,o.fb_cst_loc * vscale.factor AS fb_cst_loc ,o.fb_cst_loc_cur ,o.fb_cst_loc_fut ,o.calc_status @@ -226,7 +230,7 @@ SELECT ,o.sdate ,o.sseas FROM - baseline + basemix o CROSS JOIN vscale ) ,pscale AS ( @@ -250,6 +254,8 @@ SELECT ELSE 0 END mod_price +FROM + volume ) ,pricing AS ( SELECT