Compare commits

...

5 Commits

13 changed files with 235 additions and 348 deletions

View File

@ -63,6 +63,7 @@ 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;
@ -77,6 +78,7 @@ 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);

View File

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

View File

@ -105,7 +105,7 @@ $$SELECT
$$::text|| $$::text||
_clist|| _clist||
$$ $$
,'forecast_name' "version" ,'app_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||
$$ $$
,'forecast_name' "version" ,'app_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||
$$ $$
,'forecast_name' "version" ,'app_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||$$ = 'forecast_name'; $$DELETE FROM $$||_target_table||$$ WHERE $$||_version_col||$$ = 'app_forecast_name';
WITH WITH
baseline AS ( baseline AS (
$$||_ytdbody|| $$||_ytdbody||

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 $PG -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
$PGD -c "SELECT t FROM fc.sql WHERE cmd = 'baseline'" -t -A -o routes/baseline/baseline.sql $PG -c "SELECT t FROM fc.sql WHERE cmd = 'baseline'" -t -A -o routes/baseline/baseline.sql

View File

@ -5,5 +5,6 @@
"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"
} }

View File

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

View File

@ -11,6 +11,8 @@ 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;
@ -27,7 +29,18 @@ 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
@ -35,7 +48,10 @@ INTO
FROM FROM
fc.target_meta fc.target_meta
WHERE WHERE
func NOT IN ('version'); COALESCE(appcol,'') NOT IN ('version','iter','logid');
RAISE NOTICE 'all columns %', _clist;
-------------------------all columns except volume scale----------------------------------- -------------------------all columns except volume scale-----------------------------------
SELECT SELECT
string_agg( string_agg(
@ -52,7 +68,9 @@ INTO
FROM FROM
fc.target_meta fc.target_meta
WHERE WHERE
func NOT IN ('version'); COALESCE(appcol,'') NOT IN ('version','iter','logid');
RAISE NOTICE ' all columns plus scale volume columns %', _clist_vol;
-------------------------all columns except volume scale----------------------------------- -------------------------all columns except volume scale-----------------------------------
SELECT SELECT
@ -73,7 +91,9 @@ INTO
FROM FROM
fc.target_meta fc.target_meta
WHERE WHERE
func NOT IN ('version'); COALESCE(appcol,'') NOT IN ('version','iter','logid');
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--------------------
@ -81,8 +101,8 @@ $$WITH
req AS (SELECT $$||'$$app_req$$::jsonb j)'||$$ req AS (SELECT $$||'$$app_req$$::jsonb j)'||$$
,target AS ( ,target AS (
SELECT SELECT
(req.j->>'vincr')::numeric vincr --volume (req.j->>'app_vincr')::numeric vincr --volume
,(req.j->>'pincr')::numeric pincr --price ,(req.j->>'app_pincr')::numeric pincr --price
FROM FROM
req req
) )
@ -92,7 +112,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
fc.live $$||_target_table||$$ o
WHERE WHERE
app_where app_where
) )
@ -100,7 +120,7 @@ req AS (SELECT $$||'$$app_req$$::jsonb j)'||$$
SELECT SELECT
$$||_clist||$$ $$||_clist||$$
FROM FROM
fc.live o $$||_target_table||$$ o
WHERE WHERE
app_where app_where
), ),
@ -108,7 +128,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'||$$
,(SELECT vincr FROM target)/sum($$||_units_col||$$) AS factor ,CASE WHEN sum($$||_units_col||$$) = 0 THEN 0 ELSE (SELECT vincr FROM target)/sum($$||_units_col||$$) END AS factor
FROM FROM
basemix basemix
) )
@ -123,7 +143,7 @@ vscale AS (
,volume AS ( ,volume AS (
SELECT SELECT
$$||_clist_vol||$$ $$||_clist_vol||$$
,'forecast name' AS version ,'app_forecast_name' AS version
,'scale vol' AS iter ,'scale vol' AS iter
FROM FROM
basemix o basemix o
@ -132,18 +152,18 @@ FROM
,pscale AS ( ,pscale AS (
SELECT SELECT
(SELECT pincr FROM target) AS target_increment (SELECT pincr FROM target) AS target_increment
,sum($$||_value_col||') AS value'||$$ ,sum($$||format('%I',_value_col)||') AS value'||$$
,CASE WHEN (SELECT sum($$||_value_col||$$) FROM volume) = 0 THEN ,CASE WHEN (SELECT sum($$||format('%I',_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($$||_value_col||$$) FROM volume))/(SELECT sum($$||_value_col||$$) FROM volume) ((SELECT pincr FROM target)-(SELECT sum($$||format('%I',_value_col)||$$) FROM volume))/(SELECT sum($$||format('%I',_value_col)||$$) FROM volume)
END factor END factor
,CASE WHEN (SELECT sum($$||_value_col||$$) FROM volume) = 0 THEN ,CASE WHEN (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 CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum($$||format('%I',_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($$||_value_col||$$) FROM volume))/(SELECT sum($$||_units_col||$$) FROM volume) ((SELECT pincr::numeric FROM target) - (SELECT sum($$||format('%I',_value_col)||$$) FROM volume))/(SELECT sum($$||format('%I',_units_col)||$$) FROM volume)
ELSE ELSE
0 0
END END
@ -156,7 +176,7 @@ FROM
,pricing AS ( ,pricing AS (
SELECT SELECT
$$||_clist_prc||$$ $$||_clist_prc||$$
,'forecast name' AS version ,'app_forecast_name' AS version
,'scale price' AS iter ,'scale price' AS iter
FROM FROM
volume o volume o
@ -165,7 +185,7 @@ WHERE
pscale.factor <> 0 or pscale.mod_price <> 0 pscale.factor <> 0 or pscale.mod_price <> 0
) )
INSERT INTO INSERT INTO
fc.live $$||_target_table||$$ o
SELECT SELECT
* *
FROM FROM

View File

@ -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 gen_scale.sql $PG -f ./routes/scale/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 scale.sql $PG -c "SELECT t FROM fc.sql WHERE cmd = 'scale'" -t -A -o ./routes/scale/scale.sql

View File

@ -0,0 +1,17 @@
{
"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
}

View File

@ -2,241 +2,67 @@ WITH
req AS (SELECT $$app_req$$::jsonb j) req AS (SELECT $$app_req$$::jsonb j)
,target AS ( ,target AS (
SELECT SELECT
(req.j->>'vincr')::numeric vincr --volume (req.j->>'app_vincr')::numeric vincr --volume
,(req.j->>'pincr')::numeric pincr --price ,(req.j->>'app_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(fb_qty) FILTER (WHERE version <> 'ACTUALS') total sum(app_units) FILTER (WHERE version <> 'ACTUALS') total
,sum(fb_qty) FILTER (WHERE iter = 'baseline') base ,sum(app_units) FILTER (WHERE iter = 'baseline') base
FROM FROM
fc.live tpsv.dcard o
WHERE WHERE
app_where app_where
) )
,basemix AS ( ,basemix AS (
SELECT SELECT
o."ddord#" o.id
,o."dditm#" ,o.logid
,o."fgbol#" ,o."Trans. Date"
,o."fgent#" ,o."Post Date"
,o."diinv#" ,o."Description"
,o."dilin#" ,o."Amount"
,o.quoten ,o."Category"
,o.quotel ,o."Party"
,o.dcodat ,o."Reason"
,o.ddqdat ,o.app_units
,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
fc.live o tpsv.dcard 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(fb_qty) AS units ,sum(app_units) AS units
,(SELECT vincr FROM target)/sum(fb_qty) AS factor ,CASE WHEN sum(app_units) = 0 THEN 0 ELSE (SELECT vincr FROM target)/sum(app_units) END 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."ddord#" o.id
,o."dditm#" ,o.logid
,o."fgbol#" ,o."Trans. Date"
,o."fgent#" ,o."Post Date"
,o."diinv#" ,o."Description"
,o."dilin#" ,o."Amount" * vscale.factor AS "Amount"
,o.quoten ,o."Category"
,o.quotel ,o."Party"
,o.dcodat ,o."Reason"
,o.ddqdat ,o.app_units * vscale.factor AS app_units
,o.dcmdat ,'app_forecast_name' AS version
,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
@ -245,18 +71,18 @@ FROM
,pscale AS ( ,pscale AS (
SELECT SELECT
(SELECT pincr FROM target) AS target_increment (SELECT pincr FROM target) AS target_increment
,sum(fb_val_loc) AS value ,sum("Amount") AS value
,CASE WHEN (SELECT sum(fb_val_loc) FROM volume) = 0 THEN ,CASE WHEN (SELECT sum("Amount") 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(fb_val_loc) FROM volume))/(SELECT sum(fb_val_loc) FROM volume) ((SELECT pincr FROM target)-(SELECT sum("Amount") FROM volume))/(SELECT sum("Amount") FROM volume)
END factor END factor
,CASE WHEN (SELECT sum(fb_val_loc) FROM volume) = 0 THEN ,CASE WHEN (SELECT sum("Amount") FROM volume) = 0 THEN
CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum(fb_val_loc) FROM volume)) <> 0 THEN CASE WHEN ((SELECT pincr::numeric FROM target) - (SELECT sum("Amount") 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(fb_val_loc) FROM volume))/(SELECT sum(fb_qty) FROM volume) ((SELECT pincr::numeric FROM target) - (SELECT sum("Amount") FROM volume))/(SELECT sum(app_units) FROM volume)
ELSE ELSE
0 0
END END
@ -268,108 +94,17 @@ FROM
) )
,pricing AS ( ,pricing AS (
SELECT SELECT
o."ddord#" o.id
,o."dditm#" ,o.logid
,o."fgbol#" ,o."Trans. Date"
,o."fgent#" ,o."Post Date"
,o."diinv#" ,o."Description"
,o."dilin#" ,(CASE WHEN pscale.factor = 0 THEN o.app_units * pscale.mod_price ELSE o."Amount" * pscale.factor END)::numeric AS Amount
,o.quoten ,o."Category"
,o.quotel ,o."Party"
,o.dcodat ,o."Reason"
,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
,(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 ,'app_forecast_name' AS version
,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
@ -378,7 +113,7 @@ WHERE
pscale.factor <> 0 or pscale.mod_price <> 0 pscale.factor <> 0 or pscale.mod_price <> 0
) )
INSERT INTO INSERT INTO
fc.live tpsv.dcard o
SELECT SELECT
* *
FROM FROM

View File

@ -40,6 +40,21 @@ 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;

View File

@ -20,4 +20,15 @@
* 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:

View File

@ -16,8 +16,8 @@ SELECT
FROM FROM
information_schema.columns information_schema.columns
WHERE WHERE
table_name = 'dcard_mapped' table_name = 'dcard'
AND table_schema = 'fc' AND table_schema = 'tpsv'
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;