diff --git a/routes/baseline/baseline.sql b/routes/baseline/baseline.sql new file mode 100644 index 0000000..40b6db6 --- /dev/null +++ b/routes/baseline/baseline.sql @@ -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 diff --git a/routes/baseline/generate_route_sql.sh b/routes/baseline/generate_route_sql.sh index 5466890..f5b4231 100755 --- a/routes/baseline/generate_route_sql.sh +++ b/routes/baseline/generate_route_sql.sh @@ -1,4 +1,4 @@ # 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 -$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 diff --git a/routes/scale/curl_route.sh b/routes/scale/curl_route.sh index 18a3cb6..9700f2b 100755 --- a/routes/scale/curl_route.sh +++ b/routes/scale/curl_route.sh @@ -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 diff --git a/routes/scale/generate_route_sql.sh b/routes/scale/generate_route_sql.sh index 98d5eb1..3506ac9 100755 --- a/routes/scale/generate_route_sql.sh +++ b/routes/scale/generate_route_sql.sh @@ -1,4 +1,4 @@ # 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 -$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 diff --git a/routes/scale/req_dcard.json b/routes/scale/req_dcard.json new file mode 100644 index 0000000..b0dcb9f --- /dev/null +++ b/routes/scale/req_dcard.json @@ -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 +} diff --git a/routes/scale/scale.sql b/routes/scale/scale.sql index edfed1b..01b76ae 100644 --- a/routes/scale/scale.sql +++ b/routes/scale/scale.sql @@ -2,241 +2,67 @@ WITH req AS (SELECT $$app_req$$::jsonb j) ,target AS ( SELECT - (req.j->>'vincr')::numeric vincr --volume - ,(req.j->>'pincr')::numeric pincr --price + (req.j->>'app_vincr')::numeric vincr --volume + ,(req.j->>'app_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 + sum(app_units) FILTER (WHERE version <> 'ACTUALS') total + ,sum(app_units) FILTER (WHERE iter = 'baseline') base FROM - fc.live + tpsv.dcard o 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 + o.id + ,o.logid + ,o."Trans. Date" + ,o."Post Date" + ,o."Description" + ,o."Amount" + ,o."Category" + ,o."Party" + ,o."Reason" + ,o.app_units FROM - fc.live o + tpsv.dcard 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 + ,sum(app_units) AS units + ,CASE WHEN sum(app_units) = 0 THEN 0 ELSE (SELECT vincr FROM target)/sum(app_units) END AS factor FROM basemix ) +,ilog AS ( + INSERT INTO + fc.log (doc) + SELECT + req.j + FROM + req +) ,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 + o.id + ,o.logid + ,o."Trans. Date" + ,o."Post Date" + ,o."Description" + ,o."Amount" * vscale.factor AS "Amount" + ,o."Category" + ,o."Party" + ,o."Reason" + ,o.app_units * vscale.factor AS app_units + ,'app_forecast_name' AS version ,'scale vol' AS iter FROM basemix o @@ -245,18 +71,18 @@ FROM ,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 + ,sum("Amount") AS value + ,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- 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) + ((SELECT pincr FROM target)-(SELECT sum("Amount") FROM volume))/(SELECT sum("Amount") 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 + ,CASE WHEN (SELECT sum("Amount") 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 - ((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 0 END @@ -268,108 +94,17 @@ FROM ) ,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 + o.id + ,o.logid + ,o."Trans. Date" + ,o."Post Date" + ,o."Description" + ,(CASE WHEN pscale.factor = 0 THEN o.app_units * pscale.mod_price ELSE o."Amount" * pscale.factor END)::numeric AS Amount + ,o."Category" + ,o."Party" + ,o."Reason" ,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 + ,'app_forecast_name' AS version ,'scale price' AS iter FROM volume o @@ -378,7 +113,7 @@ WHERE pscale.factor <> 0 or pscale.mod_price <> 0 ) INSERT INTO - fc.live + tpsv.dcard o SELECT * FROM diff --git a/setup_sql/target_info.sql b/setup_sql/target_info.sql index 5a5af46..1b3f542 100644 --- a/setup_sql/target_info.sql +++ b/setup_sql/target_info.sql @@ -16,8 +16,8 @@ SELECT FROM information_schema.columns WHERE - table_name = 'dcard_mapped' - AND table_schema = 'fc' + table_name = 'dcard' + AND table_schema = 'tpsv' ON CONFLICT ON CONSTRAINT target_meta_pk DO UPDATE SET opos = EXCLUDED.opos ,dtype = EXCLUDED.dtype;