This commit is contained in:
Paul Trowbridge 2023-02-24 19:28:14 -05:00
parent d7d2447be2
commit 68aa1c219a
4 changed files with 164 additions and 26 deletions

View File

@ -6,8 +6,8 @@ TRUNCATE TABLE rlarp.osmf_dev;
DROP TABLE IF EXISTS tdr; DROP TABLE IF EXISTS tdr;
CREATE TEMP TABLE tdr AS ( CREATE TEMP TABLE tdr AS (
SELECT SELECT
DATERANGE('2020-06-01','2021-06-01','[)') drange DATERANGE('2022-06-01','2023-06-01','[)') drange
,DATERANGE(('2020-06-01'::date + '1 year'::interval)::date,('2021-06-01'::date + '1 year'::interval)::date,'[)') repl ,DATERANGE(('2022-06-01'::date + '1 year'::interval)::date,('2023-06-01'::date + '1 year'::interval)::date,'[)') repl
,'1 year'::interval AS incr ,'1 year'::interval AS incr
,(SELECT jsonb_agg(x.v) FROM (VALUES('copy'),('actuals'),('actuals_plug')) AS x(v)) iter ,(SELECT jsonb_agg(x.v) FROM (VALUES('copy'),('actuals'),('actuals_plug')) AS x(v)) iter
); );

View File

@ -1,6 +1,7 @@
--\timing --\timing
truncate table rlarp.osmf_dev; TRUNCATE TABLE rlarp.osmf;
INSERT INTO rlarp.osmf
WITH WITH
gld AS ( gld AS (
SELECT SELECT
@ -90,8 +91,9 @@ gld AS (
,null::text coltier ,null::text coltier
,null::text colstat ,null::text colstat
,null::text sizc ,null::text sizc
,null::text pckg ,null::text uomp
,null::text kit ,null::text suffix
,null::text accs_ps
,null::text brnd ,null::text brnd
,null::text majg ,null::text majg
,null::text ming ,null::text ming
@ -129,13 +131,15 @@ gld AS (
,o.oseas ,o.oseas
,o.rdate ,o.rdate
,o.rseas ,o.rseas
,o.pdate
,o.pseas
-----when null, greatest/least is just going to act like coalesce -----when null, greatest/least is just going to act like coalesce
,greatest(least(o.sdate,gld.edat),gld.sdat) sdate ,greatest(least(o.sdate,gld.edat),gld.sdat) sdate
,ss.ssyr sseas ,ss.ssyr sseas
,'15mo' "version" ,'15mo' "version"
,'actuals' iter ,'actuals' iter
FROM FROM
rlarp.osm_dev o rlarp.osm o
--snap the ship dates of the historic fiscal period --snap the ship dates of the historic fiscal period
LEFT OUTER JOIN gld ON LEFT OUTER JOIN gld ON
gld.fspr = o.fspr gld.fspr = o.fspr
@ -145,11 +149,11 @@ gld AS (
WHERE WHERE
( (
--base period orders booked.... --base period orders booked....
o.odate BETWEEN '2020-06-01' AND '2021-04-11' o.odate BETWEEN '2022-06-01' AND '2023-02-15'
--...or any open orders currently booked before cutoff.... --...or any open orders currently booked before cutoff....
OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate < '2021-04-11') OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate < '2023-02-15')
--...or anything that shipped in that period --...or anything that shipped in that period
OR o.fspr BETWEEN '2101' AND '2110' OR ((o.fspr BETWEEN '2301' AND '2309' OR o.fspr = '0000') AND o.sdate < '2023-02-15')
) )
AND fs_line = '41010' AND fs_line = '41010'
AND calc_status <> 'CANCELED' AND calc_status <> 'CANCELED'
@ -176,6 +180,8 @@ gld AS (
,o.oseas ,o.oseas
,o.rdate ,o.rdate
,o.rseas ,o.rseas
,o.pdate
,o.pseas
,greatest(least(o.sdate,gld.edat),gld.sdat) ,greatest(least(o.sdate,gld.edat),gld.sdat)
,ss.ssyr ,ss.ssyr
UNION ALL UNION ALL
@ -243,8 +249,9 @@ gld AS (
,null::text coltier ,null::text coltier
,null::text colstat ,null::text colstat
,null::text sizc ,null::text sizc
,null::text pckg ,null::text uomp
,null::text kit ,null::text suffix
,null::text accs_ps
,null::text brnd ,null::text brnd
,null::text majg ,null::text majg
,null::text ming ,null::text ming
@ -282,19 +289,21 @@ gld AS (
,o.oseas + 1 rseas ,o.oseas + 1 rseas
,o.rdate + interval '1 year' rdate ,o.rdate + interval '1 year' rdate
,o.rseas + 1 rseas ,o.rseas + 1 rseas
,o.pdate + interval '1 year' pdate
,o.pseas + 1 pseas
-----when null, greatest/least is just going to act like coalesce -----when null, greatest/least is just going to act like coalesce
,greatest(least(o.sdate,gld.edat),gld.sdat) + interval '1 year' sdate ,greatest(least(o.sdate,gld.edat),gld.sdat) + interval '1 year' sdate
,ss.ssyr sseas ,ss.ssyr sseas
,'actuals' "version" ,'actuals' "version"
,'actuals_plug' iter ,'actuals_plug' iter
FROM FROM
rlarp.osm_dev o rlarp.osm o
LEFT OUTER JOIN gld ON LEFT OUTER JOIN gld ON
gld.fspr = o.fspr gld.fspr = o.fspr
LEFT OUTER JOIN gld ss ON LEFT OUTER JOIN gld ss ON
greatest(least(o.sdate,gld.edat),gld.sdat) + interval '1 year' BETWEEN ss.sdat AND ss.edat greatest(least(o.sdate,gld.edat),gld.sdat) + interval '1 year' BETWEEN ss.sdat AND ss.edat
WHERE WHERE
o.odate BETWEEN '2020-04-12' AND '2020-05-31' o.odate BETWEEN '2022-02-15' AND '2022-05-31'
AND fs_line = '41010' AND fs_line = '41010'
AND calc_status <> 'CANCELED' AND calc_status <> 'CANCELED'
------exclude actuals for now and use forecast to get the plug for the rest of the year ------exclude actuals for now and use forecast to get the plug for the rest of the year
@ -319,6 +328,8 @@ gld AS (
,o.oseas ,o.oseas
,o.rdate ,o.rdate
,o.rseas ,o.rseas
,o.pdate
,o.pseas
,greatest(least(o.sdate,gld.edat),gld.sdat) ,greatest(least(o.sdate,gld.edat),gld.sdat)
,ss.ssyr ,ss.ssyr
UNION ALL UNION ALL
@ -386,8 +397,9 @@ gld AS (
,null::text coltier ,null::text coltier
,null::text colstat ,null::text colstat
,null::text sizc ,null::text sizc
,null::text pckg ,null::text uomp
,null::text kit ,null::text suffix
,null::text accs_ps
,null::text brnd ,null::text brnd
,null::text majg ,null::text majg
,null::text ming ,null::text ming
@ -425,13 +437,15 @@ gld AS (
,o.oseas rseas ,o.oseas rseas
,o.rdate rdate ,o.rdate rdate
,o.rseas rseas ,o.rseas rseas
,o.pdate pdate
,o.pseas pseas
-----when null, greatest/least is just going to act like coalesce -----when null, greatest/least is just going to act like coalesce
,greatest(least(o.sdate,gld.edat),gld.sdat) sdate ,greatest(least(o.sdate,gld.edat),gld.sdat) sdate
,ss.ssyr sseas ,ss.ssyr sseas
,'actuals' "version" ,'actuals' "version"
,'forecast_plug' iter ,'forecast_plug' iter
FROM FROM
rlarp.osmp_dev o rlarp.osmp o
LEFT OUTER JOIN gld ON LEFT OUTER JOIN gld ON
gld.fspr = o.fspr gld.fspr = o.fspr
LEFT OUTER JOIN gld ss ON LEFT OUTER JOIN gld ss ON
@ -459,6 +473,8 @@ gld AS (
,o.oseas ,o.oseas
,o.rdate ,o.rdate
,o.rseas ,o.rseas
,o.pdate
,o.pseas
,greatest(least(o.sdate,gld.edat),gld.sdat) ,greatest(least(o.sdate,gld.edat),gld.sdat)
,ss.ssyr ,ss.ssyr
) )
@ -524,8 +540,9 @@ SELECT
,o.coltier ,o.coltier
,o.colstat ,o.colstat
,o.sizc ,o.sizc
,o.pckg ,o.uomp
,o.kit ,o.suffix
,o.accs_ps
,o.brnd ,o.brnd
,o.majg ,o.majg
,o.ming ,o.ming
@ -563,6 +580,8 @@ SELECT
,o.oseas + 1 --incremented ,o.oseas + 1 --incremented
,o.rdate + interval '1 year' --incremented ,o.rdate + interval '1 year' --incremented
,o.rseas + 1 --incremented ,o.rseas + 1 --incremented
,o.pdate + interval '1 year' --incremented
,o.pseas + 1 --incremented
,o.sdate + interval '1 year' --incremented ,o.sdate + interval '1 year' --incremented
,o.sseas + 1 --incremented ,o.sseas + 1 --incremented
,'b22' "version" ,'b22' "version"
@ -574,10 +593,129 @@ FROM
WHERE WHERE
o.odate + interval '1 year' >= '2021-06-01' o.odate + interval '1 year' >= '2021-06-01'
) )
INSERT INTO rlarp.osmf_dev --INSERT INTO rlarp.osmf
SELECT * FROM incr SELECT
-----------documents-------------
"ddord#"
,"dditm#"
,"fgbol#"
,"fgent#"
,"diinv#"
,"dilin#"
,quoten
,quotel
----------dates/status------------------
,dcodat
,ddqdat
,dcmdat
,fesdat
,dhidat
------------document flags-------------------
,fesind
,dhpost
,fspr
-----------measures--------------------
,ddqtoi
,ddqtsi
,fgqshp
,diqtsh
,diext
,ditdis
,discj
------------document attributes--------------
,dhincr
,plnt
,promo
,return_reas
,terms
,custpo
,remit_to
------------customer info---------------------
,bill_class
,bill_cust
,bill_rep
,bill_terr
,ship_class
,ship_cust
,ship_rep
,ship_terr
,dsm
,account
,shipgrp
,geo
,chan
,chansub
,orig_ctry
,orig_prov
,orig_post
,bill_ctry
,bill_prov
,bill_post
,dest_ctry
,dest_prov
,dest_post
------------product info----------------------
,part
,styc
,colc
,colgrp
,coltier
,colstat
,sizc
,uomp
,suffix
,accs_ps
,brnd
,majg
,ming
,majs
,mins
,gldco
,gldc
,glec
,harm
,clss
,brand
,assc
,ddunit
,unti
,lbs
,plt
------------fiscal info-----------------------
,plcd
,fs_line
,r_currency
,r_rate
,c_currency
,c_rate
,fb_qty
,fb_val_loc
,fb_val_loc_dis
,fb_val_loc_qt
,fb_val_loc_pl
,fb_val_loc_tar
,fb_cst_loc
,fb_cst_loc_cur
,fb_cst_loc_fut
------------status info-----------------------
,calc_status
,flag
,odate
,oseas
,rdate
,rseas
,pdate
,pseas
,sdate
,sseas
,"version"
,iter
FROM
(
SELECT * FROM baseline
UNION ALL UNION ALL
SELECT * FROM baseline; SELECT * FROM incr
) x
---identify short ships: causes disconnect with actual sales------------------------------------------------------------------- ---identify short ships: causes disconnect with actual sales-------------------------------------------------------------------
--UPDATE rlarp.osmfs SET iter = 'short ship' WHERE calc_status = 'CLOSED' AND flag = 'REMAINDER'; --UPDATE rlarp.osmfs SET iter = 'short ship' WHERE calc_status = 'CLOSED' AND flag = 'REMAINDER';

View File

@ -3,9 +3,9 @@ Description=forecast_api
After=network.target After=network.target
[Service] [Service]
ExecStart=/usr/bin/node //opt/forecast_api/index.js ExecStart=/home/fc/.nvm/versions/node/v18.14.2/bin/node //opt/forecast_api/index.js
Restart=always Restart=always
User=fc_api User=fc
Environemnt=NODE_ENV=production Environemnt=NODE_ENV=production
WorkingDirectory=//opt/forecast_api/ WorkingDirectory=//opt/forecast_api/

View File

@ -696,7 +696,7 @@ function build_where(req, c, w, d, args) {
if (req.body.stamp) { if (req.body.stamp) {
w = w + w = w +
` `
AND order_date >= '` + req.body.stamp + "'::date"; AND order_date >= least('` + req.body.stamp + "'::date,'2021-06-01')";
} }
return { c, w, d }; return { c, w, d };
} }