Compare commits

..

22 Commits

Author SHA1 Message Date
PhilRunninger
bd8aa18208 Merge branch 'dev' of github.com:The-HC-Companies/forecast_api into dev 2023-03-01 18:00:18 -05:00
PhilRunninger
bfcf8ff4b4 Keeping the workbook in sync with other code. 2023-03-01 17:56:36 -05:00
PhilRunninger
ed5975daf7 Switch source table from rlarp.itemmv to "CMS.CUSLG".itemm 2023-03-01 17:55:30 -05:00
PhilRunninger
59cb926d9c Make the log statements unique for each route. 2023-03-01 17:51:01 -05:00
5dc5a11489 increment year 2023-03-01 17:07:44 -05:00
fd4079cb17 clean up 2023-03-01 16:32:52 -05:00
e5db7372a1 repoint snaps 2023-03-01 16:07:36 -05:00
7d901e7f42 new forecast baseline dates 2023-03-01 16:01:32 -05:00
PhilRunninger
bc9aa4a456 Merge branch 'new_parser' into dev 2023-02-28 15:10:21 -05:00
PhilRunninger
3b72871cc6 Renamed the pivot tables, and simplified some code.
I made the "did I double click in the pivot table?" code work more
efficiently.
2023-02-28 15:05:54 -05:00
PhilRunninger
8ba18476d8 Include new JSON parser for evaluation. Is it better? 2023-02-27 16:26:12 -05:00
PhilRunninger
5b507d3d65 Updates to the newest season in scenario_package.sql 2023-02-27 16:15:35 -05:00
68aa1c219a updates 2023-02-24 19:28:14 -05:00
PhilRunninger
21e72a42cc Update the Excel workbook to the latest version from Teams. 2023-02-22 14:24:39 -05:00
PhilRunninger
70392a6829 Renamed the Excel file. 2023-02-22 14:23:59 -05:00
PhilRunninger
d7d2447be2 Merge branch 'dev' of github.com:The-HC-Companies/forecast_api into dev 2023-02-17 14:24:00 -05:00
PhilRunninger
e1359714cc Upgrade packages for use in node version 19.5.0. 2023-02-17 14:22:05 -05:00
e73d69ad65 include pool ddl 2023-02-15 14:21:49 -05:00
a66ec61aaf increment iteration name to reflect period 2021-08-03 13:33:37 -04:00
2253d65047 group by percentage of row to keep, and filter out -0- 2021-08-03 08:26:05 -04:00
752aeffa06 add notes and create an adjustment based on a percentage for lines where not all is pullforward 2021-08-02 15:46:43 -04:00
fc8f9592b8 incrememnt periods 2021-08-02 15:45:27 -04:00
23 changed files with 1032 additions and 546 deletions

1
.gitignore vendored
View File

@ -11,3 +11,4 @@ changes.html
/.dbeaver /.dbeaver
.project .project
/.settings /.settings
/Scripts

Binary file not shown.

BIN
Master Template.xlsm Normal file

Binary file not shown.

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,8 @@
-- Connection: usmidsap01.ubm
--\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 +92,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 +132,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 +150,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-03-01'
--...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-03-01')
--...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-03-01')
) )
AND fs_line = '41010' AND fs_line = '41010'
AND calc_status <> 'CANCELED' AND calc_status <> 'CANCELED'
@ -176,6 +181,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 +250,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 +290,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-03-01' 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 +329,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 +398,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 +438,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 +474,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 +541,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,21 +581,142 @@ 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" ,'b23' "version"
,'copy' iter ,'copy' iter
FROM FROM
baseline o baseline o
LEFT OUTER JOIN gld ON LEFT OUTER JOIN gld ON
o.sdate + interval '1 year' BETWEEN gld.sdat and gld.edat o.sdate + interval '1 year' BETWEEN gld.sdat and gld.edat
WHERE WHERE
o.odate + interval '1 year' >= '2021-06-01' o.odate + interval '1 year' >= '2023-06-01'
) )
INSERT INTO rlarp.osmf_dev --INSERT INTO rlarp.osmf
SELECT * FROM incr SELECT
UNION ALL -----------documents-------------
SELECT * FROM baseline; "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
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

@ -59,7 +59,7 @@ repc AS (
) )
--select * from si --select * from si
,logload AS ( ,logload AS (
INSERT INTO rlarp.osm_log (doc) SELECT jsonb_build_object('user',current_user,'tag','1+11','type','build_pool','stamp',current_timestamp,'source','top level','message','don''t undo') RETURNING * INSERT INTO rlarp.osm_log (doc) SELECT jsonb_build_object('user',current_user,'tag','baseline','type','build_pool','stamp',current_timestamp,'source','top level','message','don''t undo') RETURNING *
) )
--select * from logload --select * from logload
,loadset AS ( ,loadset AS (
@ -156,7 +156,7 @@ SELECT
,logload.doc->>'message' "comment" ,logload.doc->>'message' "comment"
,logload.doc->>'type' module ,logload.doc->>'type' module
FROM FROM
rlarp.osmf_dev o rlarp.osmf o
CROSS JOIN logload CROSS JOIN logload
LEFT OUTER JOIN lgdat.cust bc ON LEFT OUTER JOIN lgdat.cust bc ON
bc.bvcust = o.bill_cust bc.bvcust = o.bill_cust
@ -178,7 +178,7 @@ FROM
*/ */
LEFT OUTER JOIN seg ON LEFT OUTER JOIN seg ON
seg.glec = o.glec seg.glec = o.glec
LEFT OUTER JOIN rlarp.itemm i ON LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.item = o.part i.item = o.part
) )
SELECT * FROM loadset SELECT * FROM loadset

View File

@ -21,9 +21,10 @@ tdr AS (
DATERANGE('2021-06-01','2022-06-01','[)') prange DATERANGE('2021-06-01','2022-06-01','[)') prange
-----------explicitly call out overlap period------------------------ -----------explicitly call out overlap period------------------------
-----------anythign in this period has to net out to match actuals--- -----------anythign in this period has to net out to match actuals---
,DATERANGE('2021-06-01','2021-07-29','[]') overlap ,DATERANGE('2021-06-01','2021-07-31','[]') overlap
,jsonb_build_array('plan','diff') iter ,jsonb_build_array('plan','diff') iter
) )
------actual orders according to whatever is in the forecast, has to be defined as 'plan' and 'diff'--------
,booked AS materialized( ,booked AS materialized(
SELECT SELECT
billto_group billto_group
@ -41,6 +42,8 @@ tdr AS (
,shipto_group ,shipto_group
,part_group ,part_group
) )
--SELECT * FROM booked
------------plan units according to the 'plan' iteration of the current forecast----------------------------
,planned AS ( ,planned AS (
SELECT SELECT
p.billto_group p.billto_group
@ -71,6 +74,8 @@ tdr AS (
,p.ship_date ,p.ship_date
,b.units ,b.units
) )
--SELECT * FROM planned LIMIT 100
-------flag rows by preparing a rolling total in this step------------------
,plan_ranked AS ( ,plan_ranked AS (
SELECT SELECT
p.billto_group p.billto_group
@ -88,7 +93,8 @@ tdr AS (
FROM FROM
planned p planned p
) )
---flag any rows where --SELECT * FROM plan_ranked
---flag future budget where up to the extent that actuals are larger than budget, but not more------
,eval AS ( ,eval AS (
SELECT SELECT
p.billto_group p.billto_group
@ -103,12 +109,20 @@ tdr AS (
,p.net_units_tot ,p.net_units_tot
,p.net_units ,p.net_units
--,p.booked --,p.booked
,plan_rolling <= (p.net_units_tot - p.plan_units_tot + plan_units) flag -----total current units less plan total units is the excess
-----if the current row contributes to the excess, flag it
-----the last row will likely pull out too much, it will need split probably
,(plan_rolling - plan_units) <= (p.net_units_tot - p.plan_units_tot) flag
,-(p.net_units_tot - p.plan_units_tot) max_possible_reduction
,-CASE
WHEN plan_rolling <= (p.net_units_tot - p.plan_units_tot) THEN plan_units
WHEN plan_rolling > (p.net_units_tot - p.plan_units_tot) AND (plan_rolling - plan_units) <= (p.net_units_tot - p.plan_units_tot) THEN (p.net_units_tot - p.plan_units_tot) - (plan_rolling - plan_units)
END qty
--,CASE WHEN (p.plan_units_agg - p.plan_units) > p.booked AND p.plan_units_agg <= p.booked * 2 THEN true ELSE false END remove --,CASE WHEN (p.plan_units_agg - p.plan_units) > p.booked AND p.plan_units_agg <= p.booked * 2 THEN true ELSE false END remove
FROM FROM
plan_ranked p plan_ranked p
) )
--select * from eval limit 10000 --SELECT * FROM eval LIMIT 10000
----------------create a log entry-------------------- ----------------create a log entry--------------------
,log AS ( ,log AS (
INSERT INTO INSERT INTO
@ -152,11 +166,12 @@ tdr AS (
,o.r_rate ,o.r_rate
,o.c_currency ,o.c_currency
,o.c_rate ,o.c_rate
,round(-sum(o.units ),2) units -----only keep the percentage of the current row determined by the last step--------
,round(-sum(o.value_loc),2) value_loc ,round(-sum(o.units ),2)*(-e.qty/e.plan_units) units
,round(-sum(o.value_usd),2) value_usd ,round(-sum(o.value_loc),2)*(-e.qty/e.plan_units) value_loc
,round(-sum(o.cost_loc ),2) cost_loc ,round(-sum(o.value_usd),2)*(-e.qty/e.plan_units) value_usd
,round(-sum(o.cost_usd ),2) cost_usd ,round(-sum(o.cost_loc ),2)*(-e.qty/e.plan_units) cost_loc
,round(-sum(o.cost_usd ),2)*(-e.qty/e.plan_units) cost_usd
,o.calc_status ,o.calc_status
,o.flag ,o.flag
,o.order_date ,o.order_date
@ -185,6 +200,7 @@ tdr AS (
CROSS JOIN log CROSS JOIN log
WHERE WHERE
e.flag e.flag
AND e.plan_units <> 0
GROUP BY GROUP BY
o.fspr o.fspr
,o.plnt ,o.plnt
@ -215,6 +231,7 @@ tdr AS (
,o.r_rate ,o.r_rate
,o.c_currency ,o.c_currency
,o.c_rate ,o.c_rate
,(-e.qty/e.plan_units) --removal_rate
,o.calc_status ,o.calc_status
,o.flag ,o.flag
,o.order_date ,o.order_date

View File

@ -16,19 +16,19 @@ DROP TABLE IF EXISTS tdr;
CREATE TEMP TABLE tdr AS ( CREATE TEMP TABLE tdr AS (
SELECT SELECT
-----------actuals into baseline------------------------------------- -----------actuals into baseline-------------------------------------
DATERANGE('2020-06-01','2021-07-29','[]') arange DATERANGE('2020-06-01','2021-07-31','[]') arange
-----------plan into baseline---------------------------------------- -----------plan into baseline----------------------------------------
,DATERANGE('2020-06-01','2022-06-01','[)') prange ,DATERANGE('2020-06-01','2022-06-01','[)') prange
-----------forecast into baseline------------------------------------ -----------forecast into baseline------------------------------------
,DATERANGE('2022-06-01','2022-06-01','[)') frange -- this range effectively excludes ,DATERANGE('2022-06-01','2022-06-01','[)') frange -- this range effectively excludes
-----------baseline selection for increment-------------------------- -----------baseline selection for increment--------------------------
,DATERANGE('2021-07-29','2021-07-29','[)') selection ,DATERANGE('2021-07-31','2021-07-31','[)') selection
-----------selection increment size---------------------------------- -----------selection increment size----------------------------------
,'0 year'::interval AS incr ,'0 year'::interval AS incr
-----------iterations to merge with---------------------------------- -----------iterations to merge with----------------------------------
,(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
-----------existing baseline overlap--------------------------------- -----------existing baseline overlap---------------------------------
,DATERANGE('2000-06-01','2021-07-29') overlap ,DATERANGE('2000-06-01','2021-07-31') overlap
); );
--select * from tdr --select * from tdr
@ -169,7 +169,7 @@ gld AS (
-----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
,'1+11' AS "version" ,'2+10' AS "version"
,'plan' iter ,'plan' iter
FROM FROM
rlarp.osmp_dev o rlarp.osmp_dev o
@ -332,7 +332,7 @@ gld AS (
-----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
,'1+11' AS "version" ,'2+10' AS "version"
,'fcst' iter ,'fcst' iter
FROM FROM
rlarp.osmf_dev o rlarp.osmf_dev o
@ -496,7 +496,7 @@ gld AS (
-----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
,'1+11' "version" ,'2+10' "version"
,'actuals' iter ,'actuals' iter
FROM FROM
rlarp.osm_dev o rlarp.osm_dev o
@ -660,7 +660,7 @@ gld AS (
-----when null, greatest/least is just going to act like coalesce -----when null, greatest/least is just going to act like coalesce
,o.sdate ,o.sdate
,o.sseas ,o.sseas
,'1+11' AS version ,'2+10' AS version
,'diff' AS iter ,'diff' AS iter
FROM FROM
forecast_basis o forecast_basis o
@ -810,7 +810,7 @@ gld AS (
-----when null, greatest/least is just going to act like coalesce -----when null, greatest/least is just going to act like coalesce
,o.sdate ,o.sdate
,o.sseas ,o.sseas
,'1+11' AS version ,'2+10' AS version
,'diff' AS iter ,'diff' AS iter
FROM FROM
actuals o actuals o
@ -960,7 +960,7 @@ gld AS (
,o.sseas ,o.sseas
--,o.version --,o.version
--,o.iter --,o.iter
,'1+11' AS version ,'2+10' AS version
,'diff' iter ,'diff' iter
FROM FROM
diff o diff o

78
build/osm_pool.sql Normal file
View File

@ -0,0 +1,78 @@
-- rlarp.osm_pool definition
-- Drop table
-- DROP TABLE rlarp.osm_pool;
CREATE TABLE rlarp.osm_pool (
fspr text NULL,
plnt text NULL,
promo text NULL,
terms text NULL,
bill_cust_descr text NULL,
ship_cust_descr text NULL,
dsm text NULL,
quota_rep_descr text NULL,
director text NULL,
billto_group text NULL,
shipto_group text NULL,
chan text NULL,
chansub text NULL,
chan_retail text NULL,
part text NULL,
part_descr text NULL,
part_group text NULL,
branding text NULL,
majg_descr text NULL,
ming_descr text NULL,
majs_descr text NULL,
mins_descr text NULL,
segm text NULL,
substance text NULL,
fs_line text NULL,
r_currency text NULL,
r_rate numeric(31, 10) NULL,
c_currency text NULL,
c_rate numeric(31, 10) NULL,
units numeric(31, 10) NULL,
value_loc numeric(31, 10) NULL,
value_usd numeric NULL,
cost_loc numeric(31, 10) NULL,
cost_usd numeric NULL,
calc_status text NULL,
flag text NULL,
order_date date NULL,
order_month text NULL,
order_season int4 NULL,
request_date date NULL,
request_month text NULL,
request_season int4 NULL,
ship_date date NULL,
ship_month text NULL,
ship_season int4 NULL,
"version" text NULL,
iter text NULL,
logid int4 NULL,
tag text NULL,
"comment" text NULL,
"module" text NULL
);
CREATE INDEX osm_pool_logid ON rlarp.osm_pool USING btree (logid);
CREATE INDEX osm_qr ON rlarp.osm_pool USING btree (quota_rep_descr, bill_cust_descr, ship_cust_descr);
-- rlarp.osm_pool foreign keys
ALTER TABLE rlarp.osm_pool ADD CONSTRAINT logfk FOREIGN KEY (logid) REFERENCES rlarp.osm_log(id);
-- rlarp.osm_log definition
-- Drop table
-- DROP TABLE rlarp.osm_log;
CREATE TABLE rlarp.osm_log (
id int4 NOT NULL GENERATED BY DEFAULT AS IDENTITY,
doc jsonb NULL,
CONSTRAINT osm_log_pkey PRIMARY KEY (id)
);

2
build/rebuild_script.sh Normal file → Executable file
View File

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
#$PG -f ./build_stage.sql; #$PG -f ./build_stage.sql;
$PG -f ./build_rolling.sql; #$PG -f ./build_rolling.sql;
$PG -f ./snap_itemm.sql; $PG -f ./snap_itemm.sql;
$PG -f ./snap_cost_current.sql; $PG -f ./snap_cost_current.sql;
$PG -f ./snap_customer.sql; $PG -f ./snap_customer.sql;

View File

@ -6,7 +6,7 @@ plist AS (
part part
,plnt ,plnt
FROM FROM
rlarp.osmf_dev rlarp.osmf
-----prevent a list of fake parts -----prevent a list of fake parts
INNER JOIN lgdat.stka ON INNER JOIN lgdat.stka ON
v6part = part v6part = part
@ -30,7 +30,7 @@ plist AS (
AND ir.y0plnt = p.plnt AND ir.y0plnt = p.plnt
) )
UPDATE UPDATE
rlarp.osmf_dev o rlarp.osmf o
SET SET
fb_cst_loc_cur = c.stdcost * o.fb_qty fb_cst_loc_cur = c.stdcost * o.fb_qty
FROM FROM

View File

@ -1,64 +1,64 @@
----------------------------SET BILL-TO REP------------------------------------ ----------------------------SET BILL-TO REP------------------------------------
UPDATE UPDATE
RLARP.OSMF_DEV S rlarp.osmf s
SET SET
BILL_REP = C.BVSALM bill_rep = c.bvsalm
,BILL_CLASS = C.BVCLAS ,bill_class = c.bvclas
,BILL_TERR = C.BVTERR ,bill_terr = c.bvterr
,BILL_CTRY = C.bvctry ,bill_ctry = c.bvctry
,bill_prov = C.bvprcd ,bill_prov = c.bvprcd
,bill_post = C.bvpost ,bill_post = c.bvpost
,remit_to = c.bvcomp ,remit_to = c.bvcomp
,ACCOUNT = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END ,account = CASE bvadr6 WHEN '' THEN bvname ELSE bvadr6 END
FROM FROM
LGDAT.CUST C lgdat.cust c
WHERE WHERE
C.BVCUST = S.BILL_CUST c.bvcust = s.bill_cust
AND ( AND (
COALESCE(S.BILL_REP,'') <> C.BVSALM COALESCE(s.bill_rep,'') <> c.bvsalm
OR COALESCE(S.BILL_CLASS,'') <> C.BVCLAS OR COALESCE(s.bill_class,'') <> c.bvclas
OR COALESCE(S.BILL_TERR,'') <> C.BVTERR OR COALESCE(s.bill_terr,'') <> c.bvterr
OR COALESCE(BILL_CTRY,'') <> C.bvctry OR COALESCE(bill_ctry,'') <> c.bvctry
OR COALESCE(bill_prov,'') <> C.bvprcd OR COALESCE(bill_prov,'') <> c.bvprcd
OR COALESCE(bill_post,'') <> C.bvpost OR COALESCE(bill_post,'') <> c.bvpost
OR COALESCE(remit_to,'') <> c.bvcomp::text OR COALESCE(remit_to,'') <> c.bvcomp::text
); );
----------------------------SET SHIP-TO REP------------------------------------ ----------------------------SET SHIP-TO REP------------------------------------
UPDATE UPDATE
RLARP.OSMF_DEV S rlarp.osmf s
SET SET
SHIP_REP = C.BVSALM ship_rep = c.bvsalm
,SHIP_CLASS = C.BVCLAS ,ship_class = c.bvclas
,SHIP_TERR = C.BVTERR ,ship_terr = c.bvterr
,dest_CTRY = C.bvctry ,dest_ctry = c.bvctry
,dest_prov = C.bvprcd ,dest_prov = c.bvprcd
,dest_post = C.bvpost ,dest_post = c.bvpost
FROM FROM
LGDAT.CUST C lgdat.cust c
WHERE WHERE
C.BVCUST = S.SHIP_CUST c.bvcust = s.ship_cust
AND ( AND (
COALESCE(S.SHIP_REP,'') <> C.BVSALM COALESCE(s.ship_rep,'') <> c.bvsalm
OR COALESCE(S.SHIP_CLASS,'') <> C.BVCLAS OR COALESCE(s.ship_class,'') <> c.bvclas
OR COALESCE(S.SHIP_TERR,'') <> C.BVTERR OR COALESCE(s.ship_terr,'') <> c.bvterr
OR COALESCE(dest_CTRY,'') <> C.bvctry OR COALESCE(dest_ctry,'') <> c.bvctry
OR COALESCE(dest_prov,'') <> C.bvprcd OR COALESCE(dest_prov,'') <> c.bvprcd
OR COALESCE(dest_post,'') <> C.bvpost OR COALESCE(dest_post,'') <> c.bvpost
); );
----------------------------SET BILLTO GROUP------------------------------------ ----------------------------SET BILLTO GROUP------------------------------------
UPDATE UPDATE
RLARP.OSMF_DEV O rlarp.osmf o
SET SET
ACCOUNT = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END ACCOUNT = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END
FROM FROM
LGDAT.CUST C lgdat.cust c
WHERE WHERE
C.BVCUST = O.BILL_CUST c.bvcust = o.bill_cust
AND coalesce(account,'') <> CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END; AND coalesce(account,'') <> CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END;
@ -66,13 +66,13 @@ WHERE
----------------------------SET SHIPTO GROUP------------------------------------ ----------------------------SET SHIPTO GROUP------------------------------------
UPDATE UPDATE
RLARP.OSMF_DEV O rlarp.osmf o
SET SET
SHIPGRP = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END SHIPGRP = CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END
FROM FROM
LGDAT.CUST C lgdat.cust c
WHERE WHERE
C.BVCUST = O.SHIP_CUST c.bvcust = o.ship_cust
AND CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END <> COALESCE(O.SHIPGRP,''); AND CASE BVADR6 WHEN '' THEN BVNAME ELSE BVADR6 END <> COALESCE(O.SHIPGRP,'');
@ -80,7 +80,7 @@ WHERE
UPDATE UPDATE
rlarp.osmF_dev rlarp.osmf
SET SET
CHAN = CASE SUBSTRING(BILL_CLASS,2,3) CHAN = CASE SUBSTRING(BILL_CLASS,2,3)
--if the bill to class is ditsributor, then it's either warehouse or drop --if the bill to class is ditsributor, then it's either warehouse or drop
@ -136,17 +136,17 @@ WHERE
UPDATE UPDATE
RLARP.OSMF_DEV S rlarp.osmf s
SET SET
DSM = CR.QUOTA_REP dsm = cr.quota_rep
FROM FROM
( (
SELECT DISTINCT SELECT DISTINCT
VERSION, version,
COALESCE(GLEC,'') GLEC, COALESCE(glec,'') glec,
MING, ming,
BILL_CUST, bill_cust,
SHIP_CUST, ship_cust,
------------quota rep column-------------- ------------quota rep column--------------
CASE WHEN COALESCE(ming,'') = 'B52' THEN 'PW' ELSE CASE WHEN COALESCE(ming,'') = 'B52' THEN 'PW' ELSE
--if the gl expense code is 1RE use the retail rep assigned to the bill-to customer if available --if the gl expense code is 1RE use the retail rep assigned to the bill-to customer if available
@ -163,18 +163,18 @@ FROM
END END
END QUOTA_REP END QUOTA_REP
FROM FROM
RLARP.OSMF_DEV S rlarp.osmf s
LEFT OUTER JOIN LGDAT.CUST ON LEFT OUTER JOIN lgdat.cust ON
BVCUST = BILL_CUST bvcust = bill_cust
LEFT OUTER JOIN lgpgm.usrcust cu ON LEFT OUTER JOIN lgpgm.usrcust cu ON
cu.cucust = s.bill_cust cu.cucust = s.bill_cust
WHERE WHERE
version = 'ACTUALS' version = 'ACTUALS'
) CR ) CR
WHERE WHERE
CR.VERSION = S.VERSION cr.version = s.version
AND CR.GLEC = COALESCE(S.GLEC,'') AND cr.glec = COALESCE(s.glec,'')
AND CR.MING = S.MING AND cr.ming = s.ming
AND CR.BILL_CUST = S.BILL_CUST AND cr.bill_cust = s.bill_cust
AND CR.SHIP_CUST = S.SHIP_CUST AND cr.ship_cust = s.ship_cust
AND COALESCE(S.DSM,'') <> CR.QUOTA_REP; AND COALESCE(s.dsm,'') <> cr.quota_rep;

View File

@ -1,28 +1,29 @@
UPDATE UPDATE
RLARP.OSMF_DEV O rlarp.osmf o
SET SET
STYC = M.STLC stlc = m.stlc
,COLC = M.COLC ,colc = m.colc
,COLGRP = M.COLGRP ,colgrp = m.colgrp
,COLTIER = M.COLTIER ,coltier = m.coltier
,COLSTAT = M.COLSTAT ,colstat = m.colstat
,SIZC = M.SIZC ,sizc = m.sizc
,PCKG = M.PACKAGE ,uomp = m.uomp
,KIT = M.KIT ,suffix = m.suffix
,BRND = M.BRANDING ,accs_ps = m.accs_ps
,MAJG = M.MAJG ,brnd = m.branding
,MING = M.MING ,majg = m.majg
,MAJS = M.MAJS ,ming = m.ming
,MINS = M.MINS ,majs = m.majs
,GLDC = M.GLCD ,mins = m.mins
,GLEC = M.GLEC ,gldc = m.glcd
,HARM = M.HARM ,glec = m.glec
,CLSS = M.CLSS ,harm = m.harm
,BRAND = M.BRAND ,clss = m.clss
,ASSC = M.ASSC ,brand = m.brand
,LBS = CASE M.NWUN WHEN 'KG' THEN 2.2046 ELSE 1 END*M.NWHT ,assc = m.assc
,UNTI = M.UNTI ,lbs = CASE m.nwun WHEN 'KG' THEN 2.2046 ELSE 1 END*m.nwht
,unti = m.unti
FROM FROM
RLARP.ITEMM M "CMS.CUSLG".itemm m
WHERE WHERE
M.ITEM = O.PART m.item = o.part

View File

@ -39,7 +39,7 @@ SELECT DISTINCT
,CASE WHEN i.majg = '610' THEN 'Fiber' ELSE 'Plastic' END substance ,CASE WHEN i.majg = '610' THEN 'Fiber' ELSE 'Plastic' END substance
FROM FROM
rlarp.osm_pool o rlarp.osm_pool o
LEFT OUTER JOIN rlarp.itemmv i ON LEFT OUTER JOIN "CMS.CUSLG".itemmv i ON
i.item = o.part i.item = o.part
LEFT OUTER JOIN seg ON LEFT OUTER JOIN seg ON
seg.glec = i.glec seg.glec = i.glec

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

@ -203,7 +203,7 @@ server.post('/swap', bodyParser.json(), function(req, res) {
res.send("no body was sent"); res.send("no body was sent");
return; return;
} }
console.log(new Date().toISOString() + "-------------------------get swap fit:------------------------------") console.log(new Date().toISOString() + "-------------------------post swap:------------------------------")
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
sql = sql.replace(new RegExp("where_clause", 'g'), w); sql = sql.replace(new RegExp("where_clause", 'g'), w);
@ -246,7 +246,7 @@ server.post('/cust_swap', bodyParser.json(), function(req, res) {
res.send("no body was sent"); res.send("no body was sent");
return; return;
} }
console.log(new Date().toISOString() + "-------------------------get swap fit:------------------------------") console.log(new Date().toISOString() + "-------------------------cust swap:------------------------------")
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
sql = sql.replace(new RegExp("where_clause", 'g'), w); sql = sql.replace(new RegExp("where_clause", 'g'), w);
@ -553,7 +553,7 @@ server.post('/scale_vp_sales', bodyParser.json(), function(req, res) {
res.send("no body was sent"); res.send("no body was sent");
return; return;
} }
console.log(new Date().toISOString() + "--------------------scale volume & price:-------------------"); console.log(new Date().toISOString() + "--------------------scale volume & price sales:-------------------");
req.body.stamp = new Date().toISOString() req.body.stamp = new Date().toISOString()
console.log(req.body); console.log(req.body);
//console.log(args); //console.log(args);
@ -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 };
} }

View File

@ -154,7 +154,7 @@ incr AS (
,row_number() OVER (PARTITION BY o.styc||'.'||o.colgrp||substring(o.sizc,1,3),o.account, o.shipgrp ORDER BY o.odate DESC) rn ,row_number() OVER (PARTITION BY o.styc||'.'||o.colgrp||substring(o.sizc,1,3),o.account, o.shipgrp ORDER BY o.odate DESC) rn
FROM FROM
rlarp.osm_dev o rlarp.osm_dev o
INNER JOIN rlarp.itemmv i ON INNER JOIN "CMS.CUSLG".itemm i ON
i.item = o.part i.item = o.part
WHERE WHERE
---exclude R&A's ---exclude R&A's
@ -226,7 +226,7 @@ SELECT
,jsonb_agg(DISTINCT iter) iters ,jsonb_agg(DISTINCT iter) iters
FROM FROM
rlarp.osm_pool o rlarp.osm_pool o
,rlarp.itemmv i ,"CMS.CUSLG".itemm i
WHERE WHERE
i.item = o.part i.item = o.part
--AND o.units <> 0 --AND o.units <> 0
@ -415,7 +415,7 @@ GROUP BY
FROM FROM
rlarp.osm_pool o rlarp.osm_pool o
--need to join to itemm to get the product from osm_pool --need to join to itemm to get the product from osm_pool
,rlarp.itemmv i ,"CMS.CUSLG".itemm i
WHERE WHERE
i.item = o.part i.item = o.part
GROUP BY GROUP BY

848
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -9,8 +9,8 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"dotenv": "^6.2.0", "dotenv": "^16.0.3",
"express": "^4.17.1", "express": "^4.17.1",
"pg": "^7.18.1" "pg": "^8.9.0"
} }
} }

View File

@ -354,7 +354,7 @@ SELECT
FROM FROM
basemix b basemix b
CROSS JOIN log CROSS JOIN log
LEFT OUTER JOIN rlarp.itemmv i ON LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.item = b.part i.item = b.part
LEFT OUTER JOIN SEG ON LEFT OUTER JOIN SEG ON
SEG.GLEC = i.glec SEG.GLEC = i.glec

View File

@ -36,7 +36,7 @@ FROM
rlarp.osm_pool rlarp.osm_pool
WHERE WHERE
where_clause where_clause
AND order_season IN (2021,2022) AND order_season IN (2023,2024)
GROUP BY GROUP BY
order_season order_season
,order_month ,order_month
@ -94,14 +94,14 @@ GROUP BY
SELECT SELECT
order_month order_month
,seq ,seq
,SUM(units) FILTER (WHERE order_season = 2021) "2021 qty" ,SUM(units) FILTER (WHERE order_season = 2023) "2023 qty"
,SUM(units) FILTER (WHERE order_season = 2022 AND iter IN ('copy','short ship','bad_ship','plan')) "2022 base qty" ,SUM(units) FILTER (WHERE order_season = 2024 AND iter IN ('copy','short ship','bad_ship','plan')) "2024 base qty"
,SUM(units) FILTER (WHERE order_season = 2022 AND iter NOT IN ('copy','short ship','bad_ship','plan')) "2022 adj qty" ,SUM(units) FILTER (WHERE order_season = 2024 AND iter NOT IN ('copy','short ship','bad_ship','plan')) "2024 adj qty"
,SUM(units) FILTER (WHERE order_season = 2022) "2022 tot qty" ,SUM(units) FILTER (WHERE order_season = 2024) "2024 tot qty"
,SUM(value_usd) FILTER (WHERE order_season = 2021) "2021 value_usd" ,SUM(value_usd) FILTER (WHERE order_season = 2023) "2023 value_usd"
,SUM(value_usd) FILTER (WHERE order_season = 2022 AND iter IN ('copy','short ship','bad_ship','plan')) "2022 base value_usd" ,SUM(value_usd) FILTER (WHERE order_season = 2024 AND iter IN ('copy','short ship','bad_ship','plan')) "2024 base value_usd"
,SUM(value_usd) FILTER (WHERE order_season = 2022 AND iter NOT IN ('copy','short ship','bad_ship','plan')) "2022 adj value_usd" ,SUM(value_usd) FILTER (WHERE order_season = 2024 AND iter NOT IN ('copy','short ship','bad_ship','plan')) "2024 adj value_usd"
,SUM(value_usd) FILTER (WHERE order_season = 2022) "2022 tot value_usd" ,SUM(value_usd) FILTER (WHERE order_season = 2024) "2024 tot value_usd"
FROM FROM
months months
GROUP BY GROUP BY
@ -113,14 +113,14 @@ GROUP BY
,mlist AS ( ,mlist AS (
SELECT SELECT
mseq.m order_month mseq.m order_month
,"2021 qty" ,"2023 qty"
,"2022 base qty" ,"2024 base qty"
,"2022 adj qty" ,"2024 adj qty"
,"2022 tot qty" ,"2024 tot qty"
,"2021 value_usd" ,"2023 value_usd"
,"2022 base value_usd" ,"2024 base value_usd"
,"2022 adj value_usd" ,"2024 adj value_usd"
,"2022 tot value_usd" ,"2024 tot value_usd"
FROM FROM
mseq mseq
LEFT OUTER JOIN mpvt ON LEFT OUTER JOIN mpvt ON

View File

@ -11,7 +11,7 @@ target AS (select 'replace_new_mold' new_mold)
SUM(value_usd) value_usd SUM(value_usd) value_usd
FROM FROM
rlarp.osm_pool o rlarp.osm_pool o
LEFT OUTER JOIN rlarp.itemmv i ON LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.item = o.part i.item = o.part
WHERE WHERE
-----------------scenario---------------------------- -----------------scenario----------------------------
@ -46,7 +46,7 @@ target AS (select 'replace_new_mold' new_mold)
CASE WHEN substring(b.part,9,12) = substring(i.item,9,12) THEN '1' ELSE 0 END fit CASE WHEN substring(b.part,9,12) = substring(i.item,9,12) THEN '1' ELSE 0 END fit
FROM FROM
basemix b basemix b
LEFT OUTER JOIN rlarp.itemmv i ON LEFT OUTER JOIN "CMS.CUSLG".itemm i ON
i.stlc = (SELECT new_mold FROM target WHERE new_mold <> '') i.stlc = (SELECT new_mold FROM target WHERE new_mold <> '')
AND CASE WHEN b.sizc = i.sizc THEN '1' ELSE '0' END|| AND CASE WHEN b.sizc = i.sizc THEN '1' ELSE '0' END||
CASE WHEN i.aplnt = 'I' THEN '0' ELSE '1' END|| CASE WHEN i.aplnt = 'I' THEN '0' ELSE '1' END||

View File

@ -160,13 +160,13 @@ target AS (SELECT $$swap_doc$$::jsonb swap)
remove o remove o
INNER JOIN pl ON INNER JOIN pl ON
pl.original = o.part pl.original = o.part
INNER JOIN rlarp.itemmv m ON INNER JOIN "CMS.CUSLG".itemm m ON
m.item = pl.replace m.item = pl.replace
LEFT OUTER JOIN rlarp.icstx c ON LEFT OUTER JOIN rlarp.icstx c ON
c.part = pl.replace c.part = pl.replace
AND c.plnt = m.dplt AND c.plnt = m.dplt
LEFT OUTER JOIN rlarp.plpr r ON LEFT OUTER JOIN rlarp.plpr r ON
yaplnt = m.dplt r.plnt = m.dplt
LEFT OUTER JOIN rlarp.ffcret x ON LEFT OUTER JOIN rlarp.ffcret x ON
x.fcur = r.curr x.fcur = r.curr
AND x.tcur = 'US' AND x.tcur = 'US'