work on getting errors out

This commit is contained in:
Paul Trowbridge 2020-12-05 01:19:17 -05:00
parent cb5a1dc6b5
commit 3e94851aea
4 changed files with 39 additions and 39 deletions

View File

@ -106,9 +106,7 @@ server.get('/scale', bodyParser.json(), function(req, res) {
var args = []; var args = [];
var app_pincr = req.body.app_pincr; var app_pincr = req.body.app_pincr;
var app_req = req.body.app_req; var app_req = JSON.stringify(req.body);
var app_scenario = req.body.app_scenario;
var app_units = req.body.app_units;
var app_vincr = req.body.app_vincr; var app_vincr = req.body.app_vincr;
var callback = function(arg) { 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("where_clause", 'g'), w)
sql = sql.replace(new RegExp("app_pincr", 'g'), app_pincr); 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_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_vincr", 'g'), app_vincr);
sql = sql.replace(new RegExp("app_where", 'g'), w); sql = sql.replace(new RegExp("app_where", 'g'), w);
//execute the sql and send the result //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 //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' // "part":"XFRM500", "customer":"Sanford and Son" --> SQL --> part = 'XFRM500'
// AND customer = 'Sanford and Son' // 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(i);
///console.log(req.body[i]); ///console.log(req.body[i]);
//this step applies the AND seperator only //this step applies the AND seperator only
@ -160,22 +156,22 @@ function build_where(req, c, w, d, args) {
` `
AND `; 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 //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') //iter = [stage1, stage2] --> SQL --> iter IN ('stag1', stage2')
w = w + i + " IN ("; w = w + i + " IN (";
for (var j in req.body.scenario[i]) { for (var j in req.body.app_scenario[i]) {
if (d > 1) { if (d > 1) {
w = w + ","; w = w + ",";
} }
w = w + "'" + req.body.scenario[i][j] + "'"; w = w + "'" + req.body.app_scenario[i][j] + "'";
d = d + 1; d = d + 1;
} }
w = w + ")"; w = w + ")";
} else { } 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; c = c + 1;
}; };
return { c, w, d }; return { c, w, d };

View File

@ -40,7 +40,7 @@ SELECT
string_agg( string_agg(
--create the column reference --create the column reference
'o.'||format('%I',cname)|| '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 --delimiter
E'\n ,' E'\n ,'
--sort column ordinal --sort column ordinal
@ -79,10 +79,10 @@ SELECT
$$WITH $$WITH
req AS (SELECT $$||'$$app_req$$::jsonb)'||$$ req AS (SELECT $$||'$$app_req$$::jsonb)'||$$
-----this block is supposed to test for new products that might not be in baseline etc------- -----this block is supposed to test for new products that might not be in baseline etc-------
test AS ( ,test AS (
SELECT SELECT
sum(app_units) FILTER WHERE (version <> 'ACTUALS') total sum($$||_units_col||$$) FILTER (WHERE version <> 'ACTUALS') total
,sum(app_units) FILTER (WHERE iter = 'baseline') base ,sum($$||_units_col||$$) FILTER (WHERE iter = 'baseline') base
FROM FROM
fc.live fc.live
WHERE WHERE
@ -91,20 +91,24 @@ test AS (
,basemix AS ( ,basemix AS (
SELECT SELECT
$$||_clist||$$ $$||_clist||$$
FROM
fc.live o
WHERE WHERE
app_scenario app_where
), ),
vscale AS ( vscale AS (
SELECT SELECT
app_vincr AS target_increment app_vincr AS target_increment
,sum($$||_units_col||') AS units'||$$ ,sum($$||_units_col||') AS units'||$$
,app_vincr/sum($$||_units_col||$$) AS factor ,app_vincr/sum($$||_units_col||$$) AS factor
FROM
basemix
) )
,volume AS ( ,volume AS (
SELECT SELECT
$$||_clist_vol||$$ $$||_clist_vol||$$
FROM FROM
baseline basemix o
CROSS JOIN vscale CROSS JOIN vscale
) )
,pscale AS ( ,pscale AS (
@ -128,6 +132,8 @@ SELECT
ELSE ELSE
0 0
END mod_price END mod_price
FROM
volume
) )
,pricing AS ( ,pricing AS (
SELECT SELECT

View File

@ -1,19 +1,11 @@
{ {
"scenario": { "app_scenario": {
"quota_rep_descr": "TINA PETTIGREW",
"substance": "Plastic",
"chan": "DIR", "chan": "DIR",
"billto_group": "H&A MASTRONARDI", "account": "H&A MASTRONARDI",
"shipto_group": "H&A MASTRONARDI", "shipgrp": "H&A MASTRONARDI",
"majg_descr": "110 - INJECTION", "part": "HWA13000E21B025LZBLD",
"part_descr": "HWA13000E21B025LZBLD - 13.00 WRAPT HP CHOCOLATE",
"version": "b21",
"iter": [ "iter": [
"copy", "baseline"
"adj volume",
"adj price",
"upload volume",
"upload price"
] ]
}, },
"stamp": "2020-02-19 17:03:38", "stamp": "2020-02-19 17:03:38",
@ -21,7 +13,7 @@
"source": "adj", "source": "adj",
"message": "2% on volume and price", "message": "2% on volume and price",
"tag": "standard price", "tag": "standard price",
"version": "b21", "version": "forecast_name",
"type": "scale_vp", "type": "scale_vp",
"app_vincr": 453.60, "app_vincr": 453.60,
"app_pincr": 728.43624 "app_pincr": 728.43624

View File

@ -1,10 +1,10 @@
WITH WITH
req AS (SELECT $$app_req$$::jsonb) req AS (SELECT $$app_req$$::jsonb)
-----this block is supposed to test for new products that might not be in baseline etc------- -----this block is supposed to test for new products that might not be in baseline etc-------
test AS ( ,test AS (
SELECT SELECT
sum(app_units) FILTER WHERE (version <> 'ACTUALS') total sum(fb_qty) FILTER (WHERE version <> 'ACTUALS') total
,sum(app_units) FILTER (WHERE iter = 'baseline') base ,sum(fb_qty) FILTER (WHERE iter = 'baseline') base
FROM FROM
fc.live fc.live
WHERE WHERE
@ -113,14 +113,18 @@ SELECT
,o.rseas ,o.rseas
,o.sdate ,o.sdate
,o.sseas ,o.sseas
FROM
fc.live o
WHERE WHERE
app_scenario app_where
), ),
vscale AS ( vscale AS (
SELECT SELECT
app_vincr AS target_increment app_vincr AS target_increment
,sum(fb_qty) AS units ,sum(fb_qty) AS units
,app_vincr/sum(fb_qty) AS factor ,app_vincr/sum(fb_qty) AS factor
FROM
basemix
) )
,volume AS ( ,volume AS (
SELECT SELECT
@ -208,13 +212,13 @@ SELECT
,o.r_rate ,o.r_rate
,o.c_currency ,o.c_currency
,o.c_rate ,o.c_rate
,o.fb_qty * vscale.factor ,o.fb_qty * vscale.factor AS fb_qty
,o.fb_val_loc * vscale.factor ,o.fb_val_loc * vscale.factor AS fb_val_loc
,o.fb_val_loc_dis ,o.fb_val_loc_dis
,o.fb_val_loc_qt ,o.fb_val_loc_qt
,o.fb_val_loc_pl ,o.fb_val_loc_pl
,o.fb_val_loc_tar ,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_cur
,o.fb_cst_loc_fut ,o.fb_cst_loc_fut
,o.calc_status ,o.calc_status
@ -226,7 +230,7 @@ SELECT
,o.sdate ,o.sdate
,o.sseas ,o.sseas
FROM FROM
baseline basemix o
CROSS JOIN vscale CROSS JOIN vscale
) )
,pscale AS ( ,pscale AS (
@ -250,6 +254,8 @@ SELECT
ELSE ELSE
0 0
END mod_price END mod_price
FROM
volume
) )
,pricing AS ( ,pricing AS (
SELECT SELECT