Merge pull request #1 from fleetside72/glcols

Glcols
This commit is contained in:
fleetside72 2018-09-20 22:24:04 -04:00 committed by GitHub
commit 4c6001fbf7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 23 deletions

View File

@ -31,21 +31,25 @@ COMMENT ON COLUMN evt.acct.prop IS 'properties';
------------------------fiscal periods------------------------ ------------------------fiscal periods------------------------
CREATE TABLE evt.fspr ( CREATE TABLE evt.fspr (
id ltree id ltree PRIMARY KEY
,dur tstzrange ,dur tstzrange
); );
COMMENT ON COLUMN evt.fspr.id IS 'fiscal period'; COMMENT ON COLUMN evt.fspr.id IS 'fiscal period';
COMMENT ON COLUMN evt.fspr.dur IS 'duration of period as timestamp range'; COMMENT ON COLUMN evt.fspr.dur IS 'duration of period as timestamp range';
CREATE INDEX id_gist ON evt.fspr USING GIST (id); CREATE INDEX id_gist ON evt.fspr USING GIST (id);
--------------------------relational ledger------------------------------------------ --------------------------relational ledger------------------------------------------
CREATE TABLE evt.gl ( CREATE TABLE evt.gl (
id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY
,bprid INT REFERENCES evt.bpr (id) ,bprid INT REFERENCES evt.bpr (id)
,acct text REFERENCES evt.acct (acct) ,acct text REFERENCES evt.acct (acct)
,pstmp timestamptz DEFAULT CURRENT_TIMESTAMP
--populates by trigger join to evt.fspr
,tstmp timestamptz
,fspr ltree REFERENCES evt.fspr (id)
,amount numeric (12,2) ,amount numeric (12,2)
,glline INT ,glline INT
,bprkeys JSONB ,bprkeys JSONB
@ -53,6 +57,9 @@ CREATE TABLE evt.gl (
COMMENT ON COLUMN evt.gl.id IS 'gl id'; COMMENT ON COLUMN evt.gl.id IS 'gl id';
COMMENT ON COLUMN evt.gl.bprid IS 'id of initial basic pecuniary record'; COMMENT ON COLUMN evt.gl.bprid IS 'id of initial basic pecuniary record';
COMMENT ON COLUMN evt.gl.acct IS 'account code'; COMMENT ON COLUMN evt.gl.acct IS 'account code';
COMMENT ON COLUMN evt.gl.pstmp IS 'post time stamp';
COMMENT ON COLUMN evt.gl.tstmp IS 'transaction time stamp';
COMMENT ON COLUMN evt.gl.fspr IS 'fiscal period';
COMMENT ON COLUMN evt.gl.amount IS 'amount'; COMMENT ON COLUMN evt.gl.amount IS 'amount';
COMMENT ON COLUMN evt.gl.glline IS 'gl line number'; COMMENT ON COLUMN evt.gl.glline IS 'gl line number';
COMMENT ON COLUMN evt.gl.bprkeys IS 'extract from initial basic pecuniary record'; COMMENT ON COLUMN evt.gl.bprkeys IS 'extract from initial basic pecuniary record';
@ -74,7 +81,7 @@ COMMENT ON COLUMN evt.bal.debits IS 'total debits';
COMMENT ON COLUMN evt.bal.credits IS 'total credits'; COMMENT ON COLUMN evt.bal.credits IS 'total credits';
COMMENT ON COLUMN evt.bal.cbal IS 'closing balance'; COMMENT ON COLUMN evt.bal.cbal IS 'closing balance';
-----------------------------------------process bpr insert trigger ----------------------------------- ---------------------------handle new logged event----------------------------------------
CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger
LANGUAGE plpgsql LANGUAGE plpgsql
@ -82,7 +89,7 @@ CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger
$func$ $func$
BEGIN BEGIN
WITH WITH
------------------------------------full extraction------------------------------------------- --full extraction
full_ex AS ( full_ex AS (
SELECT SELECT
ins.id ins.id
@ -106,7 +113,7 @@ CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger
LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS(ins.bpr#>ARRAY['gl','jpath',(a.rn - 1)::text]) WITH ORDINALITY p(i, rn) ON TRUE LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS(ins.bpr#>ARRAY['gl','jpath',(a.rn - 1)::text]) WITH ORDINALITY p(i, rn) ON TRUE
) )
--select * from full_ex --select * from full_ex
--------------------------------re-ggregate extraction to gl line level---------------------- --re-ggregate extraction to gl line level
,ex_gl_line AS ( ,ex_gl_line AS (
SELECT SELECT
id id
@ -135,15 +142,19 @@ CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger
RETURNING * RETURNING *
) )
INSERT INTO INSERT INTO
evt.gl (bprid,acct, amount,glline, bprkeys) evt.gl (bprid,acct, amount,tstmp , fspr, glline, bprkeys)
SELECT SELECT
id id
,account ,account
,amount ,amount
,(bprkeys->>'date')::timestamptz
,p.fspr
,gl_rownum ,gl_rownum
,bprkeys ,bprkeys
FROM FROM
ex_gl_line; ex_gl_line
LEFT OUTER JOIN evt.fspr p ON
p.dur @> (bprkeys->>'date')::timestamptz;
RETURN NULL; RETURN NULL;
END; END;
$func$; $func$;
@ -155,5 +166,4 @@ CREATE TRIGGER log_insert
FOR EACH STATEMENT FOR EACH STATEMENT
EXECUTE PROCEDURE evt.log_insert(); EXECUTE PROCEDURE evt.log_insert();
COMMIT; COMMIT;

View File

@ -1,10 +1,9 @@
------------------------fiscal periods------------------------ ------------------------fiscal periods------------------------
CREATE TABLE evt.fspr ( CREATE TABLE evt.fspr (
id ltree id ltree PRIMARY KEY
,dur tstzrange ,dur tstzrange
) );
COMMENT ON COLUMN evt.fspr.id IS 'fiscal period'; COMMENT ON COLUMN evt.fspr.id IS 'fiscal period';
COMMENT ON COLUMN evt.fspr.dur IS 'duration of period as timestamp range'; COMMENT ON COLUMN evt.fspr.dur IS 'duration of period as timestamp range';
CREATE INDEX id_gist ON evt.fspr USING GIST (id); CREATE INDEX id_gist ON evt.fspr USING GIST (id);

View File

@ -7,7 +7,7 @@ CREATE TABLE evt.gl (
,pstmp timestamptz DEFAULT CURRENT_TIMESTAMP ,pstmp timestamptz DEFAULT CURRENT_TIMESTAMP
--populates by trigger join to evt.fspr --populates by trigger join to evt.fspr
,tstmp timestamptz ,tstmp timestamptz
,fspr ltree REFERENCES evt.fspr (id); ,fspr ltree REFERENCES evt.fspr (id)
,amount numeric (12,2) ,amount numeric (12,2)
,glline INT ,glline INT
,bprkeys JSONB ,bprkeys JSONB

View File

@ -1,3 +1,4 @@
---------------------------handle new logged event----------------------------------------
CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger
LANGUAGE plpgsql LANGUAGE plpgsql
@ -5,7 +6,7 @@ CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger
$func$ $func$
BEGIN BEGIN
WITH WITH
------------------------------------full extraction------------------------------------------- --full extraction
full_ex AS ( full_ex AS (
SELECT SELECT
ins.id ins.id
@ -29,7 +30,7 @@ CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger
LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS(ins.bpr#>ARRAY['gl','jpath',(a.rn - 1)::text]) WITH ORDINALITY p(i, rn) ON TRUE LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS(ins.bpr#>ARRAY['gl','jpath',(a.rn - 1)::text]) WITH ORDINALITY p(i, rn) ON TRUE
) )
--select * from full_ex --select * from full_ex
--------------------------------re-ggregate extraction to gl line level---------------------- --re-ggregate extraction to gl line level
,ex_gl_line AS ( ,ex_gl_line AS (
SELECT SELECT
id id
@ -58,18 +59,22 @@ CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger
RETURNING * RETURNING *
) )
INSERT INTO INSERT INTO
evt.gl (bprid,acct, amount,glline, bprkeys) evt.gl (bprid,acct, amount,tstmp , fspr, glline, bprkeys)
SELECT SELECT
id e.id
,account ,e.account
,amount ,e.amount
,gl_rownum ,(e.bprkeys->>'date')::timestamptz
,bprkeys ,p.id
,e.gl_rownum
,e.bprkeys
FROM FROM
ex_gl_line; ex_gl_line e
LEFT OUTER JOIN evt.fspr p ON
p.dur @> (bprkeys->>'date')::timestamptz;
RETURN NULL; RETURN NULL;
END; END;
$func$ $func$;
CREATE TRIGGER log_insert CREATE TRIGGER log_insert

3
test
View File

@ -35,6 +35,7 @@ $${
"entity": "home", "entity": "home",
"module": "MHI", "module": "MHI",
"offset": "h.dcard", "offset": "h.dcard",
"transaction": "purchase" "transaction": "purchase",
"date": "2018-08-01"
} }
}$$::jsonb bpr }$$::jsonb bpr