get scale route roughed in a building sql along with new where clause function

This commit is contained in:
Paul Trowbridge 2020-12-05 00:10:47 -05:00
parent 756aea1385
commit cb5a1dc6b5
8 changed files with 485 additions and 13 deletions

View File

@ -1 +0,0 @@
select distinct x.r[1] from fc.sql join lateral regexp_matches(t,'\[(.*?)\]','g') x(r)on true ORDER BY x.r[1] ASC;

107
index.js
View File

@ -54,15 +54,7 @@ server.get('/baseline', bodyParser.json(), function(req, res) {
var sql = "";
var path = './routes/baseline/baseline.sql';
var args = [];
fs.readFile(path, 'utf8', function(err, data) {
if (!err) {
callback(data);
} else {
console.log("fatal error pulling sql file")
callback(err);
}
});
var app_baseline_from_date = req.body.app_baseline_from_date;
var app_baseline_to_date = req.body.app_baseline_to_date;
@ -74,7 +66,6 @@ server.get('/baseline', bodyParser.json(), function(req, res) {
var callback = function(arg) {
sql = arg;
console.log(new Date().toISOString() + "-------------------------baseline build-----------------------------")
console.log(req.body);
//parse the where clause into the main sql statement
@ -92,4 +83,100 @@ server.get('/baseline', bodyParser.json(), function(req, res) {
//res.send(sql);
Postgres.FirstRow(sql, [], res)
};
fs.readFile(path, 'utf8', function(err, data) {
if (!err) {
callback(data);
} else {
console.log("fatal error pulling sql file")
callback(err);
}
});
})
//------------scale a selected slice by the specified amounts-----------------------
server.get('/scale', bodyParser.json(), function(req, res) {
var sql = "";
var w = ""; //holds the where
var c = 1; //flag if body is empty
var d = 1;
var path = './routes/scale/scale.sql';
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_vincr = req.body.app_vincr;
var callback = function(arg) {
sql = arg;
({ c, w, d } = build_where(req, c, w, d, args));
//if there was no body sent, return with nothing
//if (c == 1) {
// res.send("no body was sent");
// return;
//}
console.log(new Date().toISOString() + "-------------------------baseline build-----------------------------")
console.log(req.body);
//parse the where clause into the main sql statement
//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
args.push(req.body.app_baseline_from_date);
console.log(sql);
res.send(sql);
//Postgres.FirstRow(sql, [], res)
};
fs.readFile(path, 'utf8', function(err, data) {
if (!err) {
callback(data);
} else {
console.log("fatal error pulling sql file")
callback(err);
}
});
})
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) {
//console.log(i);
///console.log(req.body[i]);
//this step applies the AND seperator only
if (c > 1) {
w = w +
`
AND `;
}
if (Array.isArray(req.body.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]) {
if (d > 1) {
w = w + ",";
}
w = w + "'" + req.body.scenario[i][j] + "'";
d = d + 1;
}
w = w + ")";
} else {
w = w + i + " = '" + req.body.scenario[i] + "'";
}
args.push(req.body.scenario[i]);
c = c + 1;
};
return { c, w, d };
}

1
routes/scale/curl.sh Executable file
View File

@ -0,0 +1 @@
curl -H "Content-Type: application/json" -X GET -d@./routes/scale/req.json https://localhost:8082/scale --insecure > scale

View File

@ -0,0 +1,11 @@
SELECT DISTINCT
cmd
,x.r[1]
FROM
fc.sql
JOIN lateral regexp_matches(t,'app_[\w_]*','g') x(r) ON TRUE
WHERE
cmd = 'scale'
ORDER BY
cmd
,x.r[1] ASC;

View File

@ -0,0 +1,4 @@
# execure the sql for scale which builds the sql and inserts into a table
$PGD -f routes/scale/gen_scale.sql
# pull the sql out of the table and write it to route directory
$PGD -c "SELECT t FROM fc.sql WHERE cmd = 'scale'" -t -A -o routes/scale/scale.sql

View File

@ -23,6 +23,6 @@
"tag": "standard price",
"version": "b21",
"type": "scale_vp",
"qty": 453.60,
"amount": 728.43624
"app_vincr": 453.60,
"app_pincr": 728.43624
}

361
routes/scale/scale.sql Normal file
View File

@ -0,0 +1,361 @@
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 (
SELECT
sum(app_units) FILTER WHERE (version <> 'ACTUALS') total
,sum(app_units) FILTER (WHERE iter = 'baseline') base
FROM
fc.live
WHERE
app_where
)
,basemix AS (
SELECT
o."ddord#"
,o."dditm#"
,o."fgbol#"
,o."fgent#"
,o."diinv#"
,o."dilin#"
,o.quoten
,o.quotel
,o.dcodat
,o.ddqdat
,o.dcmdat
,o.fesdat
,o.dhidat
,o.fesind
,o.dhpost
,o.fspr
,o.ddqtoi
,o.ddqtsi
,o.fgqshp
,o.diqtsh
,o.diext
,o.ditdis
,o.discj
,o.dhincr
,o.plnt
,o.promo
,o.return_reas
,o.terms
,o.custpo
,o.remit_to
,o.bill_class
,o.bill_cust
,o.bill_rep
,o.bill_terr
,o.ship_class
,o.ship_cust
,o.ship_rep
,o.ship_terr
,o.dsm
,o.account
,o.shipgrp
,o.geo
,o.chan
,o.chansub
,o.orig_ctry
,o.orig_prov
,o.orig_post
,o.bill_ctry
,o.bill_prov
,o.bill_post
,o.dest_ctry
,o.dest_prov
,o.dest_post
,o.part
,o.styc
,o.colc
,o.colgrp
,o.coltier
,o.colstat
,o.sizc
,o.pckg
,o.kit
,o.brnd
,o.majg
,o.ming
,o.majs
,o.mins
,o.gldco
,o.gldc
,o.glec
,o.harm
,o.clss
,o.brand
,o.assc
,o.ddunit
,o.unti
,o.lbs
,o.plt
,o.plcd
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,o.fb_qty
,o.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
,o.fb_cst_loc_cur
,o.fb_cst_loc_fut
,o.calc_status
,o.flag
,o.odate
,o.oseas
,o.rdate
,o.rseas
,o.sdate
,o.sseas
WHERE
app_scenario
),
vscale AS (
SELECT
app_vincr AS target_increment
,sum(fb_qty) AS units
,app_vincr/sum(fb_qty) AS factor
)
,volume AS (
SELECT
o."ddord#"
,o."dditm#"
,o."fgbol#"
,o."fgent#"
,o."diinv#"
,o."dilin#"
,o.quoten
,o.quotel
,o.dcodat
,o.ddqdat
,o.dcmdat
,o.fesdat
,o.dhidat
,o.fesind
,o.dhpost
,o.fspr
,o.ddqtoi
,o.ddqtsi
,o.fgqshp
,o.diqtsh
,o.diext
,o.ditdis
,o.discj
,o.dhincr
,o.plnt
,o.promo
,o.return_reas
,o.terms
,o.custpo
,o.remit_to
,o.bill_class
,o.bill_cust
,o.bill_rep
,o.bill_terr
,o.ship_class
,o.ship_cust
,o.ship_rep
,o.ship_terr
,o.dsm
,o.account
,o.shipgrp
,o.geo
,o.chan
,o.chansub
,o.orig_ctry
,o.orig_prov
,o.orig_post
,o.bill_ctry
,o.bill_prov
,o.bill_post
,o.dest_ctry
,o.dest_prov
,o.dest_post
,o.part
,o.styc
,o.colc
,o.colgrp
,o.coltier
,o.colstat
,o.sizc
,o.pckg
,o.kit
,o.brnd
,o.majg
,o.ming
,o.majs
,o.mins
,o.gldco
,o.gldc
,o.glec
,o.harm
,o.clss
,o.brand
,o.assc
,o.ddunit
,o.unti
,o.lbs
,o.plt
,o.plcd
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,o.fb_qty * vscale.factor
,o.fb_val_loc * vscale.factor
,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_cur
,o.fb_cst_loc_fut
,o.calc_status
,o.flag
,o.odate
,o.oseas
,o.rdate
,o.rseas
,o.sdate
,o.sseas
FROM
baseline
CROSS JOIN vscale
)
,pscale AS (
SELECT
app_pincr AS target_increment
,sum(fb_val_loc) AS value
,CASE WHEN (SELECT sum(fb_val_loc) FROM volume) = 0 THEN
--if the base value is -0- scaling will not work, need to generate price, factor goes to -0-
0
ELSE
--if the target dollar value still does not match the target increment, make this adjustment
((SELECT pincr::numeric FROM target)-(SELECT sum(fb_val_loc) FROM volume))/(SELECT sum(fb_val_loc) FROM volume)
END factor
,CASE WHEN (SELECT sum(fb_val_loc) FROM volume) = 0 THEN
CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc) FROM volume)) <> 0 THEN
--if the base value is -0- but the target value hasn't been achieved, derive a price to apply
((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc) FROM volume))/(SELECT sum(units) FROM volume)
ELSE
0
END
ELSE
0
END mod_price
)
,pricing AS (
SELECT
o."ddord#"
,o."dditm#"
,o."fgbol#"
,o."fgent#"
,o."diinv#"
,o."dilin#"
,o.quoten
,o.quotel
,o.dcodat
,o.ddqdat
,o.dcmdat
,o.fesdat
,o.dhidat
,o.fesind
,o.dhpost
,o.fspr
,o.ddqtoi
,o.ddqtsi
,o.fgqshp
,o.diqtsh
,o.diext
,o.ditdis
,o.discj
,o.dhincr
,o.plnt
,o.promo
,o.return_reas
,o.terms
,o.custpo
,o.remit_to
,o.bill_class
,o.bill_cust
,o.bill_rep
,o.bill_terr
,o.ship_class
,o.ship_cust
,o.ship_rep
,o.ship_terr
,o.dsm
,o.account
,o.shipgrp
,o.geo
,o.chan
,o.chansub
,o.orig_ctry
,o.orig_prov
,o.orig_post
,o.bill_ctry
,o.bill_prov
,o.bill_post
,o.dest_ctry
,o.dest_prov
,o.dest_post
,o.part
,o.styc
,o.colc
,o.colgrp
,o.coltier
,o.colstat
,o.sizc
,o.pckg
,o.kit
,o.brnd
,o.majg
,o.ming
,o.majs
,o.mins
,o.gldco
,o.gldc
,o.glec
,o.harm
,o.clss
,o.brand
,o.assc
,o.ddunit
,o.unti
,o.lbs
,o.plt
,o.plcd
,o.fs_line
,o.r_currency
,o.r_rate
,o.c_currency
,o.c_rate
,0::numeric
,(CASE WHEN pscale.factor = 0 THEN o.fb_qty * pscale.mod_price ELSE o.fb_val_loc * pscale.factor END)::numeric AS fb_val_loc
,o.fb_val_loc_dis
,o.fb_val_loc_qt
,o.fb_val_loc_pl
,o.fb_val_loc_tar
,0::numeric
,o.fb_cst_loc_cur
,o.fb_cst_loc_fut
,o.calc_status
,o.flag
,o.odate
,o.oseas
,o.rdate
,o.rseas
,o.sdate
,o.sseas
)
INSERT INTO
fc.live
SELECT * FROM volume UNION ALL SELECT * FROM pricing

View File

@ -0,0 +1,9 @@
select x.* from fc.live x
where
(row_to_json(x)::jsonb) = $$
{
"chan": "DIR",
"account": "H&A MASTRONARDI",
"shipgrp": "H&A MASTRONARDI"
}$$::jsonb