validate forecast matches actual, build in log entries for initial build

This commit is contained in:
Trowbridge 2020-02-20 14:26:39 -05:00
parent e9a0756240
commit fcc95ecb40
6 changed files with 42 additions and 44 deletions

View File

@ -50,6 +50,9 @@ repc AS (
,SI AS (
SELECT * FROM LGDAT.MMSL WHERE COALESCE(BSMNCD,'') <> ''
)
,logload AS (
INSERT INTO rlarp.osm_log (doc) SELECT jsonb_build_object('user',current_user,'tag','Initial Build','type','build_pool','stamp',current_timestamp,'source','top level','message','don''t undo') RETURNING *
)
SELECT
fspr
,plnt
@ -106,12 +109,13 @@ SELECT
,sseas ship_season
,version
,iter
,null::int logid
,''::text tag
,''::text "comment"
,''::text module
,logload.id logid
,logload.doc->>'tag' tag
,logload.doc->>'message' "comment"
,logload.doc->>'type' module
FROM
rlarp.osmf_dev o
CROSS JOIN logload
LEFT OUTER JOIN lgdat.cust bc ON
bc.bvcust = o.bill_cust
LEFT OUTER JOIN lgdat.cust sc ON

View File

@ -144,8 +144,11 @@ gld AS (
greatest(least(o.sdate,gld.edat),gld.sdat) BETWEEN ss.sdat AND ss.edat
WHERE
(
o.odate BETWEEN '2019-06-01' AND '2019-01-31'
--base period orders booked....
o.odate BETWEEN '2019-06-01' AND '2020-01-31'
--...or any open orders currently booked before cutoff....
OR (o.calc_status IN ('OPEN','BACKORDER') and o.odate < '2020-02-01')
--...or anything that shipped in that period
OR o.fspr BETWEEN '2001' AND '2008'
)
AND fs_line = '41010'

View File

@ -9,6 +9,7 @@ SELECT
,order_month
,doc->>'stamp' stamp
,logid
,tag
,sum(value_usd) FILTER (WHERE iter = 'copy') sales
,sum(cost_usd) FILTER (WHERE iter = 'copy') scogs
,sum(value_usd) FILTER (WHERE iter <> 'copy') sales_adj
@ -28,17 +29,18 @@ GROUP BY
,order_month
,doc->>'stamp'
,logid
,tag
)
SELECT
director
,quota_rep_descr
,to_char(sum(sales),'999,999,999') sales
,to_char(sum(sales - scogs),'999,999,999') margin
,to_char(sum(sales_adj),'999,999,999') sales_adj
,to_char(sum(sales_adj - scogs_adj),'999,999,999') margin_adj
,count(DISTINCT logid) "# of changes"
,COUNT(logid) "# of rows added"
,(max(stamp))::timestamptz "last edit"
,to_char(sum(sales_adj) FILTER (WHERE tag <> 'Initial Build'),'999,999,999') sales_adj
,to_char(sum(sales_adj - scogs_adj) FILTER (WHERE tag <> 'Initial Build'),'999,999,999') margin_adj
,count(DISTINCT logid) FILTER (WHERE tag <> 'Initial Build') "# of changes"
,COUNT(logid) FILTER (WHERE tag <> 'Initial Build') "# of rows added"
,(max(stamp) FILTER (WHERE tag <> 'Initial Build'))::timestamptz "last edit"
FROM
preagg
GROUP BY

View File

@ -1,7 +1,7 @@
UPDATE
rlarp.osmf_dev
SET
r_rate = .75
r_rate = .7597
WHERE
r_currency = 'CA';
@ -15,7 +15,7 @@ WHERE
UPDATE
rlarp.osmf_dev
SET
c_rate = .75
c_rate = .7597
WHERE
c_currency = 'CA';

View File

@ -158,7 +158,7 @@ server.get('/list_changes', bodyParser.json(), function(req, res) {
console.log(new Date().toISOString() + "-------------------------list changes:------------------------------")
console.log(req.body);
//parse the where clause into the main sql statement
sql = sql.replace(new RegExp("replace_user", 'g'), JSON.stringify(req.body.quota_rep_descr))
sql = sql.replace(new RegExp("replace_user", 'g'), req.body.quota_rep_descr)
//execute the sql and send the result
console.log(sql);
Postgres.FirstRow(sql, [], res)

View File

@ -1,40 +1,29 @@
WITH
list AS materialized (
agg as (
SELECT
l.doc->>'user' "user",
jsonb_path_query_first(l.doc,'$.scenario.quota_rep_descr')->>0 quota_rep_descr,
to_char((l.doc->>'stamp')::timestamptz,'YYYY-MM-DD HH:MI:SS') "stamp",
l.doc->>'tag' "tag",
l.doc->>'message' "comment",
jsonb_pretty(l.doc) def,
l.id
FROM
rlarp.osm_log l
WHERE
l.doc @? '$.scenario.quota_rep_descr ? (@ == replace_user)'
)
,agg as (
SELECT
list.user
,list.quota_rep_descr
,list.stamp
,list.tag
,list.comment
,list.def
,list.id
ol.doc->>'user' "user"
,quota_rep_descr
,ol.doc->>'stamp' stamp
,ol.doc->>'tag' tag
,ol.doc->>'message' "comment"
,ol.doc
,ol.id
,to_char(round(sum(value_usd),2),'$999,999,999D00') sales
from
LIST
rlarp.osm_log ol
inner join rlarp.osm_pool on
id = logid
group BY
list.user
,list.quota_rep_descr
,list.stamp
,list.tag
,list.comment
,list.def
,list.id
WHERE
quota_rep_descr = 'replace_user'
AND tag <> 'Initial Build'
group BY
ol.doc->>'user'
,quota_rep_descr
,ol.doc->>'stamp'
,ol.doc->>'tag'
,ol.doc->>'comment'
,ol.doc
,ol.id
)
SELECT
jsonb_agg(row_to_json(agg)::jsonb ORDER BY agg.stamp desc) x from agg