Compare commits

..

No commits in common. "59ffe255ccc5f94a577281adb7db4b0dc8c21ab2" and "a9b1523f93a81fa0868de2942e73822fd03ae8c8" have entirely different histories.

19 changed files with 21 additions and 1489 deletions

1
.gitignore vendored
View File

@ -5,4 +5,3 @@ old_*
node_modules/ node_modules/
.env .env
*.pem *.pem
*temp*

1
curl Executable file
View File

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

View File

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

View File

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

107
index.js
View File

@ -29,7 +29,7 @@ var Postgres = new pg.Client({
port: process.env.port, port: process.env.port,
database: process.env.database, database: process.env.database,
ssl: false, ssl: false,
application_name: "ps_api" application_name: "osm_api"
}); });
Postgres.connect(); Postgres.connect();
@ -52,9 +52,17 @@ server.get('/', (req, res) => res.send('pivotscale api is running'))
server.get('/baseline', bodyParser.json(), function(req, res) { server.get('/baseline', bodyParser.json(), function(req, res) {
var sql = ""; var sql = "";
var path = './routes/baseline/baseline.sql'; var path = './route_sql/baseline.sql';
var args = []; 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_from_date = req.body.app_baseline_from_date;
var app_baseline_to_date = req.body.app_baseline_to_date; var app_baseline_to_date = req.body.app_baseline_to_date;
@ -66,6 +74,7 @@ server.get('/baseline', bodyParser.json(), function(req, res) {
var callback = function(arg) { var callback = function(arg) {
sql = arg; sql = arg;
console.log(new Date().toISOString() + "-------------------------baseline build-----------------------------") console.log(new Date().toISOString() + "-------------------------baseline build-----------------------------")
console.log(req.body); console.log(req.body);
//parse the where clause into the main sql statement //parse the where clause into the main sql statement
@ -83,96 +92,4 @@ server.get('/baseline', bodyParser.json(), function(req, res) {
//res.send(sql); //res.send(sql);
Postgres.FirstRow(sql, [], res) 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 = JSON.stringify(req.body);
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(JSON.stringify(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_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.app_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.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.app_scenario[i]) {
if (d > 1) {
w = w + ",";
}
w = w + "'" + req.body.app_scenario[i][j] + "'";
d = d + 1;
}
w = w + ")";
} else {
w = w + i + " = '" + req.body.app_scenario[i] + "'";
}
args.push(req.body.app_scenario[i]);
c = c + 1;
};
return { c, w, d };
}

View File

@ -28,17 +28,12 @@ route baseline
* regular updates to baseline may be required to keep up with canceled/altered orders * regular updates to baseline may be required to keep up with canceled/altered orders
* copy some period of actual sales and increment all the dates to serve as a baseline forecast * copy some period of actual sales and increment all the dates to serve as a baseline forecast
TO-DO:
- [x] join to period tables to populate season; requires variance number oof table joins, based on howmany date functions there are 🙄 - [x] join to period tables to populate season; requires variance number oof table joins, based on howmany date functions there are 🙄
- [ ] some of the app parameters can be consolidated, the baseline period could be one large range potentially, instead of 2 stacked periods - [ ] some of the app parameters can be consolidated, the baseline period could be one large range potentially, instead of 2 stacked periods
- [x] setup something to fill in sql parameters to do testing on the function - [x] setup something to fill in sql parameters to do testing on the function
- [ ] update node to handle forecast name parameter - [ ] update node to handle forecast name parameter
- [ ] calc status is hard-coded right now in the json request -> probably needs to be manuall supplied up front - [ ] clean up SQL generation to prevent injection
scale
----------------------------------------------------------------------------------------------------------------------------------------------------
- [x] need to add version columns to all CTE's
- [ ] need to build log insert
- [x] Need to build where clause for scenario
running problem list running problem list
---------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------
@ -49,8 +44,3 @@ running problem list
- [x] problem: the target sales data has to map have concepts like order_date, and the application needs to know which col is order date - [x] problem: the target sales data has to map have concepts like order_date, and the application needs to know which col is order date
* add column called application hook * add column called application hook
- [ ] there is not currently any initial grouping to limit excess data from all the document# scenarios - [ ] there is not currently any initial grouping to limit excess data from all the document# scenarios
* general
- [ ] clean up SQL generation to prevent injection
- [ ] how to handle a target value adjustment, which currency is it in?
- [ ] **the sales data has to have a column for module and change ID, live sales data isn't going to work well**
- [ ] need to target the live sales data, build build a whole new table to use it plus add version columns

View File

@ -1,363 +0,0 @@
DELETE FROM fc.live WHERE version = 'forecast_name';
WITH
baseline 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
,'forecast_name' "version"
,'actuals' iter
FROM
fc.live o
WHERE
(
--base period orders booked....
odate BETWEEN 'app_baseline_from_date'::date AND 'app_baseline_to_date'::date
--...or any open orders currently booked before cutoff....
OR (calc_status IN (app_openstatus_code) and odate <= 'app_openorder_cutoff'::date)
--...or anything that shipped in that period
OR (sdate BETWEEN 'app_baseline_from_date'::date AND 'app_baseline_to_date'::date)
)
--be sure to pre-exclude unwanted items, like canceled orders, non-gross sales, and short-ships
UNION ALL
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 + interval '1 year' AS odate
,odate.ssyr
,o.rdate + interval '1 year' AS rdate
,rdate.ssyr
,o.sdate + interval '1 year' AS sdate
,sdate.ssyr
,'forecast_name' "version"
,'plug' iter
FROM
fc.live o
LEFT OUTER JOIN fc.perd rdate ON
(o.rdate + interval '1 year' )::date <@ rdate.drange
LEFT OUTER JOIN fc.perd odate ON
(o.odate + interval '1 year' )::date <@ odate.drange
LEFT OUTER JOIN fc.perd sdate ON
(o.sdate + interval '1 year' )::date <@ sdate.drange
WHERE
odate BETWEEN 'app_plug_fromdate'::date AND 'app_plug_todate'::date
--be sure to pre-exclude unwanted items, like canceled orders, non-gross sales, and short-ships
)
,incr 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 + interval '1 year' AS odate
,odate.ssyr
,o.rdate + interval '1 year' AS rdate
,rdate.ssyr
,o.sdate + interval '1 year' AS sdate
,sdate.ssyr
,'forecast_name' "version"
,'baseline' iter
FROM
baseline o
LEFT OUTER JOIN fc.perd rdate ON
(o.rdate + interval '1 year' )::date <@ rdate.drange
LEFT OUTER JOIN fc.perd odate ON
(o.odate + interval '1 year' )::date <@ odate.drange
LEFT OUTER JOIN fc.perd sdate ON
(o.sdate + interval '1 year' )::date <@ sdate.drange
)
,ins AS (
INSERT INTO
fc.live
SELECT
*
FROM
incr i
WHERE
i.odate >= 'app_first_forecast_date'::date
OR i.sdate >= 'app_first_forecast_date'::date
RETURNING *
)
SELECT COUNT(*) num_rows FROM ins

View File

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

View File

@ -1,13 +0,0 @@
SELECT
tname
,cname
,opos
,func
,fkey
,pretty
,dtype
,mastcol
,appcol
,dateref
FROM
fc.target_meta

View File

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

View File

@ -1,11 +0,0 @@
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

@ -1,188 +0,0 @@
DO
$func$
DECLARE
_clist text;
_clist_vol text;
_clist_prc text;
_clist_inc text;
_ytdbody text;
_order_date text;
_ship_date text;
_order_status text;
_actpy text;
_sql text;
_baseline text;
_date_funcs jsonb;
_perd_joins text;
_interval interval;
_units_col text;
_value_col text;
BEGIN
-----------------populate application variables--------------------------------------------
SELECT (SELECT cname FROM fc.target_meta WHERE appcol = 'order_date') INTO _order_date;
SELECT (SELECT cname FROM fc.target_meta WHERE appcol = 'ship_date') INTO _ship_date;
SELECT (SELECT cname FROM fc.target_meta WHERE appcol = 'order_status') INTO _order_status;
SELECT (SELECT cname FROM fc.target_meta WHERE appcol = 'units') INTO _units_col;
SELECT (SELECT cname FROM fc.target_meta WHERE appcol = 'value') INTO _value_col;
-------------------------all columns ------------------------------------------------------
SELECT
string_agg('o.'||format('%I',cname),E'\n ,' ORDER BY opos ASC)
INTO
_clist
FROM
fc.target_meta
WHERE
func NOT IN ('version');
-------------------------all columns except volume scale-----------------------------------
SELECT
string_agg(
--create the column reference
'o.'||format('%I',cname)||
CASE WHEN appcol IN ('units', 'value', 'cost') THEN ' * vscale.factor AS '||format('%I',cname) ELSE '' END,
--delimiter
E'\n ,'
--sort column ordinal
ORDER BY opos ASC
)
INTO
_clist_vol
FROM
fc.target_meta
WHERE
func NOT IN ('version');
-------------------------all columns except volume scale-----------------------------------
SELECT
string_agg(
--create the column reference
CASE
WHEN appcol IN ('units', 'cost') THEN '0::numeric'
WHEN appcol IN ('value') THEN $$(CASE WHEN pscale.factor = 0 THEN o.$$||_units_col||$$ * pscale.mod_price ELSE o.$$||format('%I',cname)||' * pscale.factor END)::numeric AS '||_value_col
ELSE 'o.'||format('%I',cname)
END,
--delimiter
E'\n ,'
--sort column ordinal
ORDER BY opos ASC
)
INTO
_clist_prc
FROM
fc.target_meta
WHERE
func NOT IN ('version');
SELECT
---------$$app_req$$ will hold the request body--------------------
$$WITH
req AS (SELECT $$||'$$app_req$$::jsonb j)'||$$
,target AS (
SELECT
(req.j->>'vincr')::numeric vincr --volume
,(req.j->>'pincr')::numeric pincr --price
FROM
req
)
-----this block is supposed to test for new products that might not be in baseline etc-------
,test AS (
SELECT
sum($$||_units_col||$$) FILTER (WHERE version <> 'ACTUALS') total
,sum($$||_units_col||$$) FILTER (WHERE iter = 'baseline') base
FROM
fc.live
WHERE
app_where
)
,basemix AS (
SELECT
$$||_clist||$$
FROM
fc.live o
WHERE
app_where
),
vscale AS (
SELECT
(SELECT vincr FROM target) AS target_increment
,sum($$||_units_col||') AS units'||$$
,(SELECT vincr FROM target)/sum($$||_units_col||$$) AS factor
FROM
basemix
)
,ilog AS (
INSERT INTO
fc.log (doc)
SELECT
req.j
FROM
req
)
,volume AS (
SELECT
$$||_clist_vol||$$
,'forecast name' AS version
,'scale vol' AS iter
FROM
basemix o
CROSS JOIN vscale
)
,pscale AS (
SELECT
(SELECT pincr FROM target) AS target_increment
,sum($$||_value_col||') AS value'||$$
,CASE WHEN (SELECT sum($$||_value_col||$$) 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 FROM target)-(SELECT sum($$||_value_col||$$) FROM volume))/(SELECT sum($$||_value_col||$$) FROM volume)
END factor
,CASE WHEN (SELECT sum($$||_value_col||$$) FROM volume) = 0 THEN
CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum($$||_value_col||$$) 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($$||_value_col||$$) FROM volume))/(SELECT sum($$||_units_col||$$) FROM volume)
ELSE
0
END
ELSE
0
END mod_price
FROM
volume
)
,pricing AS (
SELECT
$$||_clist_prc||$$
,'forecast name' AS version
,'scale price' AS iter
FROM
volume o
CROSS JOIN pscale
WHERE
pscale.factor <> 0 or pscale.mod_price <> 0
)
INSERT INTO
fc.live
SELECT
*
FROM
volume
UNION ALL
SELECT
*
FROM
pricing
$$
INTO
_sql;
RAISE NOTICE '%', _sql;
INSERT INTO fc.sql SELECT 'scale', _sql ON CONFLICT ON CONSTRAINT sql_pkey DO UPDATE SET t = EXCLUDED.t;
END
$func$;
---SELECT * FROM fc.sql;

View File

@ -1,4 +0,0 @@
# execure the sql for scale which builds the sql and inserts into a table
$PGD -f 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 scale.sql

View File

@ -1,19 +0,0 @@
{
"app_scenario": {
"chan": "WHS",
"account": "GRIFFIN",
"part": "XNS0T1G3G18B096",
"iter": [
"baseline"
]
},
"stamp": "2020-02-19 17:03:38",
"user": "Trowbridge, Paul",
"source": "adj",
"message": "2% on volume and price",
"tag": "standard price",
"version": "forecast_name",
"type": "scale_vp",
"app_vincr": 20000,
"app_pincr": 10000
}

View File

@ -1,391 +0,0 @@
WITH
req AS (SELECT $$app_req$$::jsonb j)
,target AS (
SELECT
(req.j->>'vincr')::numeric vincr --volume
,(req.j->>'pincr')::numeric pincr --price
FROM
req
)
-----this block is supposed to test for new products that might not be in baseline etc-------
,test AS (
SELECT
sum(fb_qty) FILTER (WHERE version <> 'ACTUALS') total
,sum(fb_qty) 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
FROM
fc.live o
WHERE
app_where
),
vscale AS (
SELECT
(SELECT vincr FROM target) AS target_increment
,sum(fb_qty) AS units
,(SELECT vincr FROM target)/sum(fb_qty) AS factor
FROM
basemix
)
,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 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 AS 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
,'forecast name' AS version
,'scale vol' AS iter
FROM
basemix o
CROSS JOIN vscale
)
,pscale AS (
SELECT
(SELECT pincr FROM target) 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 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(fb_qty) FROM volume)
ELSE
0
END
ELSE
0
END mod_price
FROM
volume
)
,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
,'forecast name' AS version
,'scale price' AS iter
FROM
volume o
CROSS JOIN pscale
WHERE
pscale.factor <> 0 or pscale.mod_price <> 0
)
INSERT INTO
fc.live
SELECT
*
FROM
volume
UNION ALL
SELECT
*
FROM
pricing

View File

@ -1,378 +0,0 @@
WITH
target AS (select target_vol vincr, target_prc pincr)
,testv AS (
SELECT
sum(units) tot
,sum(units) FILTER (WHERE iter = 'copy') base
,sum(units) FILTER (WHERE module = 'new basket') newpart
FROM
rlarp.osm_pool
WHERE
-----------------scenario----------------------------
where_clause
-----------------additional params-------------------
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
AND order_date <= ship_date
)
,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
)
,basemix AS (
SELECT
fspr
,plnt ---master data
,promo --history date mix
,terms
,bill_cust_descr --history cust mix
,ship_cust_descr --history cust mix
,dsm
,quota_rep_descr --master data
,director
,billto_group --master data
,shipto_group
,chan --master data
,chansub
,chan_retail
,part
,part_descr
,part_group
,branding
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,segm
,substance
,fs_line --master data
,r_currency --history cust mix
,r_rate --master data
,c_currency --master data
,c_rate --master data
,sum(coalesce(units,0)) units --history value
,sum(coalesce(value_loc,0)) value_loc --history value
,sum(coalesce(value_usd,0)) value_usd --0
,sum(coalesce(cost_loc,0)) cost_loc --history part mix
,sum(coalesce(cost_usd,0)) cost_usd
,calc_status --0
,flag --0
,order_date --history date mix
,order_month
,order_season
,request_date --history date mix
,request_month
,request_season
,ship_date --history date mix
,ship_month
,ship_season
FROM
rlarp.osm_pool
WHERE
-----------------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 module = 'new basket'
END
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
AND order_date <= ship_date
GROUP BY
fspr
,plnt ---master data
,promo --history date mix
,terms
,bill_cust_descr --history cust mix
,ship_cust_descr --history cust mix
,dsm
,quota_rep_descr --master data
,director
,billto_group --master data
,shipto_group
,chan --master data
,chansub
,chan_retail
,part
,part_descr
,part_group
,branding
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,segm
,substance
,fs_line --master data
,r_currency --history cust mix
,r_rate --master data
,c_currency --master data
,c_rate --master data
,calc_status --0
,flag --0
,order_date --history date mix
,order_month
,order_season
,request_date --history date mix
,request_month
,request_season
,ship_date --history date mix
,ship_month
,ship_season
)
,vscale AS (
SELECT
(SELECT vincr::numeric FROM target) incr
,(SELECT sum(units)::numeric FROM basemix) base
,(SELECT vincr::numeric FROM target)/(SELECT sum(units)::numeric FROM basemix) factor
)
--select * from vscale
,log AS (
INSERT INTO rlarp.osm_log(doc) SELECT $$replace_iterdef$$::jsonb doc RETURNING *
)
,volume AS (
SELECT
fspr
,plnt ---master data
,promo --history date mix
,terms
,bill_cust_descr --history cust mix
,ship_cust_descr --history cust mix
,dsm
,quota_rep_descr --master data
,director
,billto_group --master data
,shipto_group
,chan --master data
,chansub
,chan_retail
,part
,part_descr
,part_group
,branding
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,segm
,substance
,fs_line --master data
,r_currency --history cust mix
,r_rate --master data
,c_currency --master data
,c_rate --master data
,units*s.factor units
,value_loc*s.factor value_loc
,value_usd*s.factor value_usd
,cost_loc*s.factor cost_loc
,cost_usd*s.factor cost_usd
,calc_status --0
,flag --0
,order_date --history date mix
,order_month
,order_season
,request_date --history date mix
,request_month
,request_season
,ship_date --history date mix
,ship_month
,ship_season
,'replace_version' "version"
,'replace_source'||' volume' iter
,log.id
,COALESCE(log.doc->>'tag','') "tag"
,log.doc->>'message' "comment"
,log.doc->>'type' module
FROM
basemix b
CROSS JOIN vscale s
CROSS JOIN log
)
,pscale AS (
SELECT
(SELECT pincr::numeric FROM target) incr
,(SELECT sum(value_loc * r_rate) FROM volume) base
,CASE WHEN (SELECT sum(value_loc * r_rate) 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 $amount is not achieved, adjust further
((SELECT pincr::numeric FROM target)-(SELECT sum(value_loc * r_rate) FROM volume))/(SELECT sum(value_loc * r_rate) FROM volume)
END factor
,CASE WHEN (SELECT sum(value_loc * r_rate) FROM volume) = 0 THEN
CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum(value_loc * r_rate) 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(value_loc * r_rate) FROM volume))/(SELECT sum(units) FROM volume)
ELSE
0
END
ELSE
0
END mod_price
)
--select * from pscale
,pricing AS (
SELECT
fspr
,plnt ---master data
,promo --history date mix
,terms
,bill_cust_descr --history cust mix
,ship_cust_descr --history cust mix
,dsm
,quota_rep_descr --master data
,director
,billto_group --master data
,shipto_group
,chan --master data
,chansub
,chan_retail
,part
,part_descr
,part_group
,branding
,majg_descr
,ming_descr
,majs_descr
,mins_descr
,segm
,substance
,fs_line --master data
,r_currency --history cust mix
,r_rate --master data
,c_currency --master data
,c_rate --master data
,0::numeric units
,(CASE WHEN s.factor = 0 THEN b.units * s.mod_price/b.r_rate ELSE b.value_loc*s.factor END)::numeric value_loc
,(CASE WHEN s.factor = 0 THEN b.units * s.mod_price ELSE b.value_usd*s.factor END)::numeric value_usd
,0::numeric cost_loc
,0::numeric cost_usd
,calc_status --0
,flag --0
,order_date --history date mix
,order_month
,order_season
,request_date --history date mix
,request_month
,request_season
,ship_date --history date mix
,ship_month
,ship_season
,'replace_version' "version"
,'replace_source'||' price' iter
,log.id
,COALESCE(log.doc->>'tag','') "tag"
,log.doc->>'message' "comment"
,log.doc->>'type' module
FROM
volume b
CROSS JOIN pscale s
CROSS JOIN log
WHERE
s.factor <> 0 or s.mod_price <> 0
)
--select sum(value_usd), sum(units) from pricing
, ins AS (
INSERT INTO rlarp.osm_pool (SELECT * FROM pricing UNION ALL SELECT * FROM volume) RETURNING *
)
,insagg AS (
SELECT
---------customer info-----------------
bill_cust_descr
,billto_group
,ship_cust_descr
,shipto_group
,quota_rep_descr
,director
,segm
,substance
,chan
,chansub
---------product info------------------
,majg_descr
,ming_descr
,majs_descr
,mins_descr
--,brand
--,part_family
,part_group
,branding
--,color
,part_descr
---------dates-------------------------
,order_season
,order_month
,ship_season
,ship_month
,request_season
,request_month
,promo
,version
,iter
,logid
,tag
,comment
--------values-------------------------
,sum(value_loc) value_loc
,sum(value_usd) value_usd
,sum(cost_loc) cost_loc
,sum(cost_usd) cost_usd
,sum(units) units
FROM
ins
GROUP BY
---------customer info-----------------
bill_cust_descr
,billto_group
,ship_cust_descr
,shipto_group
,quota_rep_descr
,director
,segm
,substance
,chan
,chansub
---------product info------------------
,majg_descr
,ming_descr
,majs_descr
,mins_descr
--,brand
--,part_family
,part_group
,branding
--,color
,part_descr
---------dates-------------------------
,order_season
,order_month
,ship_season
,ship_month
,request_season
,request_month
,promo
,version
,iter
,logid
,tag
,comment
)
SELECT json_agg(row_to_json(insagg)) x from insagg

View File

@ -1,7 +1,7 @@
--assumes schema fc already exists --assumes schema fc already exists
--DROP TABLE IF EXISTS fc.target_meta; DROP TABLE IF EXISTS fc.target_meta;
CREATE TABLE IF NOT EXISTS fc.target_meta ( CREATE TABLE fc.target_meta (
tname text tname text
,cname text ,cname text
,opos int ,opos int
@ -15,7 +15,7 @@ CREATE TABLE IF NOT EXISTS fc.target_meta (
); );
--ALTER TABLE fc.target_meta DROP CONSTRAINT IF EXISTS target_meta_pk; --ALTER TABLE fc.target_meta DROP CONSTRAINT IF EXISTS target_meta_pk;
ALTER TABLE fc.target_meta ADD CONSTRAINT IF NOT EXISTS target_meta_pk PRIMARY KEY (tname, cname); ALTER TABLE fc.target_meta ADD CONSTRAINT target_meta_pk PRIMARY KEY (tname, cname);
COMMENT ON TABLE fc.target_meta IS 'target table layout info'; COMMENT ON TABLE fc.target_meta IS 'target table layout info';
COMMENT ON COLUMN fc.target_meta.tname IS 'schema.table_name of target sales data table'; COMMENT ON COLUMN fc.target_meta.tname IS 'schema.table_name of target sales data table';
@ -28,10 +28,3 @@ COMMENT ON COLUMN fc.target_meta.dtype IS 'data type of the sales table column';
COMMENT ON COLUMN fc.target_meta.mastcol IS 'associated field from the master data table if it is different (oseas would refer to ssyr in fc.perd)'; COMMENT ON COLUMN fc.target_meta.mastcol IS 'associated field from the master data table if it is different (oseas would refer to ssyr in fc.perd)';
COMMENT ON COLUMN fc.target_meta.appcol IS 'supply column name to be used for application variables - (specifcy the order date column)'; COMMENT ON COLUMN fc.target_meta.appcol IS 'supply column name to be used for application variables - (specifcy the order date column)';
COMMENT ON COLUMN fc.target_meta.dateref IS 'reference to the relevant hard coded perd table column for dates'; COMMENT ON COLUMN fc.target_meta.dateref IS 'reference to the relevant hard coded perd table column for dates';
CREATE TABLE IF NOT EXISTS fc.log (
id int GENERATED ALWAYS AS IDENTITY
,doc jsonb
);
COMMENT ON TABLE fc.log IS 'forecast change log';