Compare commits
No commits in common. "959d858b100fab9dcb2e149a15a80067abab43dd" and "8f2a7431bcff0af3beba4c516ca4b6383d6e5d7e" have entirely different histories.
959d858b10
...
8f2a7431bc
2
index.js
2
index.js
@ -63,7 +63,6 @@ server.get('/baseline', bodyParser.json(), function(req, res) {
|
|||||||
var app_plug_fromdate = req.body.app_plug_fromdate;
|
var app_plug_fromdate = req.body.app_plug_fromdate;
|
||||||
var app_plug_todate = req.body.app_plug_todate;
|
var app_plug_todate = req.body.app_plug_todate;
|
||||||
var app_openstatus_code = req.body.app_openstatus_code;
|
var app_openstatus_code = req.body.app_openstatus_code;
|
||||||
var app_forecast_name = req.body.app_forecast_name;
|
|
||||||
|
|
||||||
var callback = function(arg) {
|
var callback = function(arg) {
|
||||||
sql = arg;
|
sql = arg;
|
||||||
@ -78,7 +77,6 @@ server.get('/baseline', bodyParser.json(), function(req, res) {
|
|||||||
sql = sql.replace(new RegExp("app_openstatus_code", 'g'), app_openstatus_code);
|
sql = sql.replace(new RegExp("app_openstatus_code", 'g'), app_openstatus_code);
|
||||||
sql = sql.replace(new RegExp("app_plug_fromdate", 'g'), app_plug_fromdate);
|
sql = sql.replace(new RegExp("app_plug_fromdate", 'g'), app_plug_fromdate);
|
||||||
sql = sql.replace(new RegExp("app_plug_todate", 'g'), app_plug_todate);
|
sql = sql.replace(new RegExp("app_plug_todate", 'g'), app_plug_todate);
|
||||||
sql = sql.replace(new RegExp("app_forecast_name", 'g'), app_forecast_name);
|
|
||||||
//execute the sql and send the result
|
//execute the sql and send the result
|
||||||
args.push(req.body.app_baseline_from_date);
|
args.push(req.body.app_baseline_from_date);
|
||||||
console.log(sql);
|
console.log(sql);
|
||||||
|
@ -1,86 +0,0 @@
|
|||||||
DELETE FROM tpsv.dcard WHERE app_version = 'app_forecast_name';
|
|
||||||
WITH
|
|
||||||
baseline AS (
|
|
||||||
SELECT
|
|
||||||
o.id
|
|
||||||
,o.logid
|
|
||||||
,o."Trans. Date"
|
|
||||||
,o."Post Date"
|
|
||||||
,o."Description"
|
|
||||||
,o."Amount"
|
|
||||||
,o."Category"
|
|
||||||
,o."Party"
|
|
||||||
,o."Reason"
|
|
||||||
,'app_forecast_name' "version"
|
|
||||||
,'actuals' iter
|
|
||||||
,null::bigint app_logid
|
|
||||||
FROM
|
|
||||||
tpsv.dcard o
|
|
||||||
WHERE
|
|
||||||
(
|
|
||||||
--base period orders booked....
|
|
||||||
"Trans. Date" BETWEEN 'app_baseline_from_date'::date AND 'app_baseline_to_date'::date
|
|
||||||
--...or any open orders currently booked before cutoff....
|
|
||||||
OR ("Category" IN (app_openstatus_code) and "Trans. Date" <= 'app_openorder_cutoff'::date)
|
|
||||||
--...or anything that shipped in that period
|
|
||||||
OR ("Post Date" 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.id
|
|
||||||
,o.logid
|
|
||||||
,o."Trans. Date" + interval '1 year' AS "Trans. Date"
|
|
||||||
,o."Post Date" + interval '1 year' AS "Post Date"
|
|
||||||
,o."Description"
|
|
||||||
,o."Amount"
|
|
||||||
,o."Category"
|
|
||||||
,o."Party"
|
|
||||||
,o."Reason"
|
|
||||||
,'app_forecast_name' "version"
|
|
||||||
,'plug' iter
|
|
||||||
,null::bigint app_logid
|
|
||||||
FROM
|
|
||||||
tpsv.dcard o
|
|
||||||
LEFT OUTER JOIN fc.perd tdate ON
|
|
||||||
(o."Trans. Date" + interval '1 year' )::date <@ tdate.drange
|
|
||||||
LEFT OUTER JOIN fc.perd pdate ON
|
|
||||||
(o."Post Date" + interval '1 year' )::date <@ pdate.drange
|
|
||||||
WHERE
|
|
||||||
"Trans. Date" 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.id
|
|
||||||
,o.logid
|
|
||||||
,o."Trans. Date" + interval '1 year' AS "Trans. Date"
|
|
||||||
,o."Post Date" + interval '1 year' AS "Post Date"
|
|
||||||
,o."Description"
|
|
||||||
,o."Amount"
|
|
||||||
,o."Category"
|
|
||||||
,o."Party"
|
|
||||||
,o."Reason"
|
|
||||||
,'app_forecast_name' "version"
|
|
||||||
,'baseline' iter
|
|
||||||
,null::bigint app_logid
|
|
||||||
FROM
|
|
||||||
baseline o
|
|
||||||
LEFT OUTER JOIN fc.perd tdate ON
|
|
||||||
(o."Trans. Date" + interval '1 year' )::date <@ tdate.drange
|
|
||||||
LEFT OUTER JOIN fc.perd pdate ON
|
|
||||||
(o."Post Date" + interval '1 year' )::date <@ pdate.drange
|
|
||||||
)
|
|
||||||
,ins AS (
|
|
||||||
INSERT INTO
|
|
||||||
tpsv.dcard
|
|
||||||
SELECT
|
|
||||||
*
|
|
||||||
FROM
|
|
||||||
incr i
|
|
||||||
WHERE
|
|
||||||
i."Trans. Date" >= 'app_first_forecast_date'::date
|
|
||||||
OR i."Post Date" >= 'app_first_forecast_date'::date
|
|
||||||
RETURNING *
|
|
||||||
)
|
|
||||||
SELECT COUNT(*) num_rows FROM ins
|
|
@ -105,7 +105,7 @@ $$SELECT
|
|||||||
$$::text||
|
$$::text||
|
||||||
_clist||
|
_clist||
|
||||||
$$
|
$$
|
||||||
,'app_forecast_name' "version"
|
,'forecast_name' "version"
|
||||||
,'actuals' iter
|
,'actuals' iter
|
||||||
,null::bigint app_logid
|
,null::bigint app_logid
|
||||||
FROM
|
FROM
|
||||||
@ -133,7 +133,7 @@ SELECT
|
|||||||
$$SELECT
|
$$SELECT
|
||||||
$$||_clist_inc||
|
$$||_clist_inc||
|
||||||
$$
|
$$
|
||||||
,'app_forecast_name' "version"
|
,'forecast_name' "version"
|
||||||
,'plug' iter
|
,'plug' iter
|
||||||
,null::bigint app_logid
|
,null::bigint app_logid
|
||||||
FROM
|
FROM
|
||||||
@ -155,7 +155,7 @@ $$,incr AS (
|
|||||||
SELECT
|
SELECT
|
||||||
$$||_clist_inc||
|
$$||_clist_inc||
|
||||||
$$
|
$$
|
||||||
,'app_forecast_name' "version"
|
,'forecast_name' "version"
|
||||||
,'baseline' iter
|
,'baseline' iter
|
||||||
,null::bigint app_logid
|
,null::bigint app_logid
|
||||||
FROM
|
FROM
|
||||||
@ -183,7 +183,7 @@ INTO
|
|||||||
------------------------------stack the sql into the final format------------------------------------------------
|
------------------------------stack the sql into the final format------------------------------------------------
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
$$DELETE FROM $$||_target_table||$$ WHERE $$||_version_col||$$ = 'app_forecast_name';
|
$$DELETE FROM $$||_target_table||$$ WHERE $$||_version_col||$$ = 'forecast_name';
|
||||||
WITH
|
WITH
|
||||||
baseline AS (
|
baseline AS (
|
||||||
$$||_ytdbody||
|
$$||_ytdbody||
|
||||||
|
@ -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
|
||||||
$PG -f routes/baseline/gen_baseline.sql
|
$PGD -f routes/baseline/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
|
||||||
$PG -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 routes/baseline/baseline.sql
|
||||||
|
@ -5,6 +5,5 @@
|
|||||||
"app_openorder_cutoff":"2020-09-30",
|
"app_openorder_cutoff":"2020-09-30",
|
||||||
"app_openstatus_code":"'OPEN','BACKORDERD'",
|
"app_openstatus_code":"'OPEN','BACKORDERD'",
|
||||||
"app_plug_fromdate":"2019-10-01",
|
"app_plug_fromdate":"2019-10-01",
|
||||||
"app_plug_todate":"2020-05-31",
|
"app_plug_todate":"2020-05-31"
|
||||||
"app_forecast_name":"dev"
|
|
||||||
}
|
}
|
||||||
|
@ -1 +1 @@
|
|||||||
curl -H "Content-Type: application/json" -X GET -d@req_dcard.json https://localhost:8082/scale --insecure > scale_temp.sql
|
curl -H "Content-Type: application/json" -X GET -d@req.json https://localhost:8082/scale --insecure > scale_temp.sql
|
||||||
|
@ -11,8 +11,6 @@ DECLARE
|
|||||||
_order_status text;
|
_order_status text;
|
||||||
_actpy text;
|
_actpy text;
|
||||||
_sql text;
|
_sql text;
|
||||||
_target_table text;
|
|
||||||
_version_col text;
|
|
||||||
_baseline text;
|
_baseline text;
|
||||||
_date_funcs jsonb;
|
_date_funcs jsonb;
|
||||||
_perd_joins text;
|
_perd_joins text;
|
||||||
@ -29,18 +27,7 @@ SELECT (SELECT cname FROM fc.target_meta WHERE appcol = 'ship_date') INTO _ship_
|
|||||||
SELECT (SELECT cname FROM fc.target_meta WHERE appcol = 'order_status') INTO _order_status;
|
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 = 'units') INTO _units_col;
|
||||||
SELECT (SELECT cname FROM fc.target_meta WHERE appcol = 'value') INTO _value_col;
|
SELECT (SELECT cname FROM fc.target_meta WHERE appcol = 'value') INTO _value_col;
|
||||||
SELECT format('%I',max(schema))||'.'||format('%I',max(tname)) INTO _target_table FROM fc.target_meta;
|
|
||||||
SELECT cname INTO _version_col FROM fc.target_meta WHERE appcol = 'version';
|
|
||||||
|
|
||||||
RAISE NOTICE '_order_date; %', _order_date;
|
|
||||||
RAISE NOTICE '_ship_date; %', _ship_date;
|
|
||||||
RAISE NOTICE '_order_status;%', _order_status;
|
|
||||||
RAISE NOTICE '_units_col; %', _units_col;
|
|
||||||
RAISE NOTICE '_value_col; %', _value_col;
|
|
||||||
RAISE NOTICE '_target_table;%', _target_table;
|
|
||||||
RAISE NOTICE '_version_col; %', _version_col;
|
|
||||||
-------------------------all columns ------------------------------------------------------
|
-------------------------all columns ------------------------------------------------------
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
string_agg('o.'||format('%I',cname),E'\n ,' ORDER BY opos ASC)
|
string_agg('o.'||format('%I',cname),E'\n ,' ORDER BY opos ASC)
|
||||||
INTO
|
INTO
|
||||||
@ -48,10 +35,7 @@ INTO
|
|||||||
FROM
|
FROM
|
||||||
fc.target_meta
|
fc.target_meta
|
||||||
WHERE
|
WHERE
|
||||||
COALESCE(appcol,'') NOT IN ('version','iter','logid');
|
func NOT IN ('version');
|
||||||
|
|
||||||
RAISE NOTICE 'all columns %', _clist;
|
|
||||||
|
|
||||||
-------------------------all columns except volume scale-----------------------------------
|
-------------------------all columns except volume scale-----------------------------------
|
||||||
SELECT
|
SELECT
|
||||||
string_agg(
|
string_agg(
|
||||||
@ -68,9 +52,7 @@ INTO
|
|||||||
FROM
|
FROM
|
||||||
fc.target_meta
|
fc.target_meta
|
||||||
WHERE
|
WHERE
|
||||||
COALESCE(appcol,'') NOT IN ('version','iter','logid');
|
func NOT IN ('version');
|
||||||
|
|
||||||
RAISE NOTICE ' all columns plus scale volume columns %', _clist_vol;
|
|
||||||
|
|
||||||
-------------------------all columns except volume scale-----------------------------------
|
-------------------------all columns except volume scale-----------------------------------
|
||||||
SELECT
|
SELECT
|
||||||
@ -91,9 +73,7 @@ INTO
|
|||||||
FROM
|
FROM
|
||||||
fc.target_meta
|
fc.target_meta
|
||||||
WHERE
|
WHERE
|
||||||
COALESCE(appcol,'') NOT IN ('version','iter','logid');
|
func NOT IN ('version');
|
||||||
|
|
||||||
RAISE NOTICE 'all columns plus scale price %', _clist_prc;
|
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
---------$$app_req$$ will hold the request body--------------------
|
---------$$app_req$$ will hold the request body--------------------
|
||||||
@ -101,8 +81,8 @@ $$WITH
|
|||||||
req AS (SELECT $$||'$$app_req$$::jsonb j)'||$$
|
req AS (SELECT $$||'$$app_req$$::jsonb j)'||$$
|
||||||
,target AS (
|
,target AS (
|
||||||
SELECT
|
SELECT
|
||||||
(req.j->>'app_vincr')::numeric vincr --volume
|
(req.j->>'vincr')::numeric vincr --volume
|
||||||
,(req.j->>'app_pincr')::numeric pincr --price
|
,(req.j->>'pincr')::numeric pincr --price
|
||||||
FROM
|
FROM
|
||||||
req
|
req
|
||||||
)
|
)
|
||||||
@ -112,7 +92,7 @@ req AS (SELECT $$||'$$app_req$$::jsonb j)'||$$
|
|||||||
sum($$||_units_col||$$) FILTER (WHERE version <> 'ACTUALS') total
|
sum($$||_units_col||$$) FILTER (WHERE version <> 'ACTUALS') total
|
||||||
,sum($$||_units_col||$$) FILTER (WHERE iter = 'baseline') base
|
,sum($$||_units_col||$$) FILTER (WHERE iter = 'baseline') base
|
||||||
FROM
|
FROM
|
||||||
$$||_target_table||$$ o
|
fc.live
|
||||||
WHERE
|
WHERE
|
||||||
app_where
|
app_where
|
||||||
)
|
)
|
||||||
@ -120,7 +100,7 @@ req AS (SELECT $$||'$$app_req$$::jsonb j)'||$$
|
|||||||
SELECT
|
SELECT
|
||||||
$$||_clist||$$
|
$$||_clist||$$
|
||||||
FROM
|
FROM
|
||||||
$$||_target_table||$$ o
|
fc.live o
|
||||||
WHERE
|
WHERE
|
||||||
app_where
|
app_where
|
||||||
),
|
),
|
||||||
@ -128,7 +108,7 @@ vscale AS (
|
|||||||
SELECT
|
SELECT
|
||||||
(SELECT vincr FROM target) AS target_increment
|
(SELECT vincr FROM target) AS target_increment
|
||||||
,sum($$||_units_col||') AS units'||$$
|
,sum($$||_units_col||') AS units'||$$
|
||||||
,CASE WHEN sum($$||_units_col||$$) = 0 THEN 0 ELSE (SELECT vincr FROM target)/sum($$||_units_col||$$) END AS factor
|
,(SELECT vincr FROM target)/sum($$||_units_col||$$) AS factor
|
||||||
FROM
|
FROM
|
||||||
basemix
|
basemix
|
||||||
)
|
)
|
||||||
@ -143,7 +123,7 @@ vscale AS (
|
|||||||
,volume AS (
|
,volume AS (
|
||||||
SELECT
|
SELECT
|
||||||
$$||_clist_vol||$$
|
$$||_clist_vol||$$
|
||||||
,'app_forecast_name' AS version
|
,'forecast name' AS version
|
||||||
,'scale vol' AS iter
|
,'scale vol' AS iter
|
||||||
FROM
|
FROM
|
||||||
basemix o
|
basemix o
|
||||||
@ -152,18 +132,18 @@ FROM
|
|||||||
,pscale AS (
|
,pscale AS (
|
||||||
SELECT
|
SELECT
|
||||||
(SELECT pincr FROM target) AS target_increment
|
(SELECT pincr FROM target) AS target_increment
|
||||||
,sum($$||format('%I',_value_col)||') AS value'||$$
|
,sum($$||_value_col||') AS value'||$$
|
||||||
,CASE WHEN (SELECT sum($$||format('%I',_value_col)||$$) FROM volume) = 0 THEN
|
,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-
|
--if the base value is -0- scaling will not work, need to generate price, factor goes to -0-
|
||||||
0
|
0
|
||||||
ELSE
|
ELSE
|
||||||
--if the target dollar value still does not match the target increment, make this adjustment
|
--if the target dollar value still does not match the target increment, make this adjustment
|
||||||
((SELECT pincr FROM target)-(SELECT sum($$||format('%I',_value_col)||$$) FROM volume))/(SELECT sum($$||format('%I',_value_col)||$$) FROM volume)
|
((SELECT pincr FROM target)-(SELECT sum($$||_value_col||$$) FROM volume))/(SELECT sum($$||_value_col||$$) FROM volume)
|
||||||
END factor
|
END factor
|
||||||
,CASE WHEN (SELECT sum($$||format('%I',_value_col)||$$) FROM volume) = 0 THEN
|
,CASE WHEN (SELECT sum($$||_value_col||$$) FROM volume) = 0 THEN
|
||||||
CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum($$||format('%I',_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
|
--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($$||format('%I',_value_col)||$$) FROM volume))/(SELECT sum($$||format('%I',_units_col)||$$) FROM volume)
|
((SELECT pincr::numeric FROM target) - (SELECT sum($$||_value_col||$$) FROM volume))/(SELECT sum($$||_units_col||$$) FROM volume)
|
||||||
ELSE
|
ELSE
|
||||||
0
|
0
|
||||||
END
|
END
|
||||||
@ -176,7 +156,7 @@ FROM
|
|||||||
,pricing AS (
|
,pricing AS (
|
||||||
SELECT
|
SELECT
|
||||||
$$||_clist_prc||$$
|
$$||_clist_prc||$$
|
||||||
,'app_forecast_name' AS version
|
,'forecast name' AS version
|
||||||
,'scale price' AS iter
|
,'scale price' AS iter
|
||||||
FROM
|
FROM
|
||||||
volume o
|
volume o
|
||||||
@ -185,7 +165,7 @@ WHERE
|
|||||||
pscale.factor <> 0 or pscale.mod_price <> 0
|
pscale.factor <> 0 or pscale.mod_price <> 0
|
||||||
)
|
)
|
||||||
INSERT INTO
|
INSERT INTO
|
||||||
$$||_target_table||$$ o
|
fc.live
|
||||||
SELECT
|
SELECT
|
||||||
*
|
*
|
||||||
FROM
|
FROM
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
# execure the sql for scale which builds the sql and inserts into a table
|
# execure the sql for scale which builds the sql and inserts into a table
|
||||||
$PG -f ./routes/scale/gen_scale.sql
|
$PG -f gen_scale.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
|
||||||
$PG -c "SELECT t FROM fc.sql WHERE cmd = 'scale'" -t -A -o ./routes/scale/scale.sql
|
$PG -c "SELECT t FROM fc.sql WHERE cmd = 'scale'" -t -A -o scale.sql
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"app_scenario": {
|
|
||||||
"\"Description\"": "TARGET STOW OH",
|
|
||||||
"app_iter": [
|
|
||||||
"baseline"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"stamp": "2020-02-19 17:03:38",
|
|
||||||
"user": "Trowbridge, Paul",
|
|
||||||
"source": "adj",
|
|
||||||
"message": "test json",
|
|
||||||
"tag": "standard price",
|
|
||||||
"version": "dev",
|
|
||||||
"type": "scale_vp",
|
|
||||||
"app_vincr": 0,
|
|
||||||
"app_pincr": 2000
|
|
||||||
}
|
|
@ -2,67 +2,241 @@ WITH
|
|||||||
req AS (SELECT $$app_req$$::jsonb j)
|
req AS (SELECT $$app_req$$::jsonb j)
|
||||||
,target AS (
|
,target AS (
|
||||||
SELECT
|
SELECT
|
||||||
(req.j->>'app_vincr')::numeric vincr --volume
|
(req.j->>'vincr')::numeric vincr --volume
|
||||||
,(req.j->>'app_pincr')::numeric pincr --price
|
,(req.j->>'pincr')::numeric pincr --price
|
||||||
FROM
|
FROM
|
||||||
req
|
req
|
||||||
)
|
)
|
||||||
-----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
|
||||||
tpsv.dcard o
|
fc.live
|
||||||
WHERE
|
WHERE
|
||||||
app_where
|
app_where
|
||||||
)
|
)
|
||||||
,basemix AS (
|
,basemix AS (
|
||||||
SELECT
|
SELECT
|
||||||
o.id
|
o."ddord#"
|
||||||
,o.logid
|
,o."dditm#"
|
||||||
,o."Trans. Date"
|
,o."fgbol#"
|
||||||
,o."Post Date"
|
,o."fgent#"
|
||||||
,o."Description"
|
,o."diinv#"
|
||||||
,o."Amount"
|
,o."dilin#"
|
||||||
,o."Category"
|
,o.quoten
|
||||||
,o."Party"
|
,o.quotel
|
||||||
,o."Reason"
|
,o.dcodat
|
||||||
,o.app_units
|
,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
|
FROM
|
||||||
tpsv.dcard o
|
fc.live o
|
||||||
WHERE
|
WHERE
|
||||||
app_where
|
app_where
|
||||||
),
|
),
|
||||||
vscale AS (
|
vscale AS (
|
||||||
SELECT
|
SELECT
|
||||||
(SELECT vincr FROM target) AS target_increment
|
(SELECT vincr FROM target) AS target_increment
|
||||||
,sum(app_units) AS units
|
,sum(fb_qty) AS units
|
||||||
,CASE WHEN sum(app_units) = 0 THEN 0 ELSE (SELECT vincr FROM target)/sum(app_units) END AS factor
|
,(SELECT vincr FROM target)/sum(fb_qty) AS factor
|
||||||
FROM
|
FROM
|
||||||
basemix
|
basemix
|
||||||
)
|
)
|
||||||
,ilog AS (
|
|
||||||
INSERT INTO
|
|
||||||
fc.log (doc)
|
|
||||||
SELECT
|
|
||||||
req.j
|
|
||||||
FROM
|
|
||||||
req
|
|
||||||
)
|
|
||||||
,volume AS (
|
,volume AS (
|
||||||
SELECT
|
SELECT
|
||||||
o.id
|
o."ddord#"
|
||||||
,o.logid
|
,o."dditm#"
|
||||||
,o."Trans. Date"
|
,o."fgbol#"
|
||||||
,o."Post Date"
|
,o."fgent#"
|
||||||
,o."Description"
|
,o."diinv#"
|
||||||
,o."Amount" * vscale.factor AS "Amount"
|
,o."dilin#"
|
||||||
,o."Category"
|
,o.quoten
|
||||||
,o."Party"
|
,o.quotel
|
||||||
,o."Reason"
|
,o.dcodat
|
||||||
,o.app_units * vscale.factor AS app_units
|
,o.ddqdat
|
||||||
,'app_forecast_name' AS version
|
,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
|
,'scale vol' AS iter
|
||||||
FROM
|
FROM
|
||||||
basemix o
|
basemix o
|
||||||
@ -71,18 +245,18 @@ FROM
|
|||||||
,pscale AS (
|
,pscale AS (
|
||||||
SELECT
|
SELECT
|
||||||
(SELECT pincr FROM target) AS target_increment
|
(SELECT pincr FROM target) AS target_increment
|
||||||
,sum("Amount") AS value
|
,sum(fb_val_loc) AS value
|
||||||
,CASE WHEN (SELECT sum("Amount") FROM volume) = 0 THEN
|
,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-
|
--if the base value is -0- scaling will not work, need to generate price, factor goes to -0-
|
||||||
0
|
0
|
||||||
ELSE
|
ELSE
|
||||||
--if the target dollar value still does not match the target increment, make this adjustment
|
--if the target dollar value still does not match the target increment, make this adjustment
|
||||||
((SELECT pincr FROM target)-(SELECT sum("Amount") FROM volume))/(SELECT sum("Amount") FROM volume)
|
((SELECT pincr FROM target)-(SELECT sum(fb_val_loc) FROM volume))/(SELECT sum(fb_val_loc) FROM volume)
|
||||||
END factor
|
END factor
|
||||||
,CASE WHEN (SELECT sum("Amount") FROM volume) = 0 THEN
|
,CASE WHEN (SELECT sum(fb_val_loc) FROM volume) = 0 THEN
|
||||||
CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum("Amount") 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
|
--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("Amount") FROM volume))/(SELECT sum(app_units) FROM volume)
|
((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc) FROM volume))/(SELECT sum(fb_qty) FROM volume)
|
||||||
ELSE
|
ELSE
|
||||||
0
|
0
|
||||||
END
|
END
|
||||||
@ -94,17 +268,108 @@ FROM
|
|||||||
)
|
)
|
||||||
,pricing AS (
|
,pricing AS (
|
||||||
SELECT
|
SELECT
|
||||||
o.id
|
o."ddord#"
|
||||||
,o.logid
|
,o."dditm#"
|
||||||
,o."Trans. Date"
|
,o."fgbol#"
|
||||||
,o."Post Date"
|
,o."fgent#"
|
||||||
,o."Description"
|
,o."diinv#"
|
||||||
,(CASE WHEN pscale.factor = 0 THEN o.app_units * pscale.mod_price ELSE o."Amount" * pscale.factor END)::numeric AS Amount
|
,o."dilin#"
|
||||||
,o."Category"
|
,o.quoten
|
||||||
,o."Party"
|
,o.quotel
|
||||||
,o."Reason"
|
,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
|
,0::numeric
|
||||||
,'app_forecast_name' AS version
|
,(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
|
,'scale price' AS iter
|
||||||
FROM
|
FROM
|
||||||
volume o
|
volume o
|
||||||
@ -113,7 +378,7 @@ WHERE
|
|||||||
pscale.factor <> 0 or pscale.mod_price <> 0
|
pscale.factor <> 0 or pscale.mod_price <> 0
|
||||||
)
|
)
|
||||||
INSERT INTO
|
INSERT INTO
|
||||||
tpsv.dcard o
|
fc.live
|
||||||
SELECT
|
SELECT
|
||||||
*
|
*
|
||||||
FROM
|
FROM
|
||||||
|
@ -40,21 +40,6 @@ BEGIN
|
|||||||
EXECUTE format('%s',f.pop);
|
EXECUTE format('%s',f.pop);
|
||||||
EXECUTE format('%s',f.fk);
|
EXECUTE format('%s',f.fk);
|
||||||
END LOOP;
|
END LOOP;
|
||||||
-------add a units column if one doesn't exist--------------------------------
|
|
||||||
IF (SELECT COUNT(*) FROM fc.target_meta WHERE appcol = 'units') = 0 THEN
|
|
||||||
SELECT 'ALTER TABLE '||_target_table||' ADD COLUMN app_units numeric DEFAULT 0' INTO _sql;
|
|
||||||
EXECUTE format('%s',_sql);
|
|
||||||
--insert the newly created row meta into target_meta
|
|
||||||
INSERT INTO
|
|
||||||
fc.target_meta
|
|
||||||
SELECT
|
|
||||||
table_schema, table_name, column_name, ordinal_position, null::text, null::text, null::text, data_type, 'units', null::text
|
|
||||||
FROM
|
|
||||||
information_schema.columns
|
|
||||||
WHERE
|
|
||||||
format('%I',table_schema)||'.'||format('%I',table_name) = _target_table
|
|
||||||
AND column_name = 'app_units';
|
|
||||||
END IF;
|
|
||||||
-------add a version column if one doesn't exist--------------------------------
|
-------add a version column if one doesn't exist--------------------------------
|
||||||
IF (SELECT COUNT(*) FROM fc.target_meta WHERE appcol = 'version') = 0 THEN
|
IF (SELECT COUNT(*) FROM fc.target_meta WHERE appcol = 'version') = 0 THEN
|
||||||
SELECT 'ALTER TABLE '||_target_table||' ADD COLUMN app_version text' INTO _sql;
|
SELECT 'ALTER TABLE '||_target_table||' ADD COLUMN app_version text' INTO _sql;
|
||||||
|
@ -20,15 +20,4 @@
|
|||||||
* fcol: associated field from the master data table if it is different (oseas would refer to ssyr in fc.perd)
|
* fcol: associated field from the master data table if it is different (oseas would refer to ssyr in fc.perd)
|
||||||
* pretty: display column name (user friendly)
|
* pretty: display column name (user friendly)
|
||||||
* appcol: parameters that will have to be supplied but the application
|
* appcol: parameters that will have to be supplied but the application
|
||||||
* order_date
|
|
||||||
* ship_date
|
|
||||||
* customer
|
|
||||||
* item
|
|
||||||
* order_stats
|
|
||||||
* units
|
|
||||||
* cost
|
|
||||||
* value
|
|
||||||
* version (added if missing)
|
|
||||||
* iter (added if missing)
|
|
||||||
* logid (added if missing)
|
|
||||||
* dateref:
|
* dateref:
|
||||||
|
@ -16,8 +16,8 @@ SELECT
|
|||||||
FROM
|
FROM
|
||||||
information_schema.columns
|
information_schema.columns
|
||||||
WHERE
|
WHERE
|
||||||
table_name = 'dcard'
|
table_name = 'dcard_mapped'
|
||||||
AND table_schema = 'tpsv'
|
AND table_schema = 'fc'
|
||||||
ON CONFLICT ON CONSTRAINT target_meta_pk DO UPDATE SET
|
ON CONFLICT ON CONSTRAINT target_meta_pk DO UPDATE SET
|
||||||
opos = EXCLUDED.opos
|
opos = EXCLUDED.opos
|
||||||
,dtype = EXCLUDED.dtype;
|
,dtype = EXCLUDED.dtype;
|
||||||
|
Loading…
Reference in New Issue
Block a user