add comments

This commit is contained in:
Paul Trowbridge 2018-10-05 14:29:04 -04:00
parent 664b18cdce
commit 85922e0141
2 changed files with 62 additions and 14 deletions

View File

@ -188,6 +188,11 @@ DECLARE
_mind timestamptz;
_maxd timestamptz;
BEGIN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--find min and max applicable periods to roll
--min: earliest period involved in current gl posting
--max: latest period involved in any posting, or if none, the current posting
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SELECT
(SELECT min(lower(f.dur)) FROM ins INNER JOIN evt.fspr f ON f.id = ins.fspr)
,GREATEST(
@ -201,12 +206,16 @@ BEGIN
_mind
,_maxd;
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--aggregate all inserted gl transactions
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
WITH
agg AS (
SELECT
acct
,fspr
,dur
put a negative in front to negate the initial debit/credit assignment
,coalesce(sum(amount) FILTER (WHERE amount > 0),0) debits
,coalesce(sum(amount) FILTER (WHERE amount < 0),0) credits
FROM
@ -218,14 +227,19 @@ BEGIN
,fspr
,dur
)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--get every account involved in target range
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
,arng AS (
SELECT DISTINCT
acct
FROM
agg b
)
,seq AS (
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--roll the balances forward
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
,bld AS (
WITH RECURSIVE rf (acct, id, dur, obal, debits, credits, cbal) AS
(
SELECT
@ -272,7 +286,10 @@ BEGIN
)
SELECT * FROM rf WHERE lower(dur) >= _mind
)
,bali AS (
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--insert the rolled balances
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
,ins AS (
INSERT INTO
evt.bal (acct, fspr, obal, debits, credits, cbal)
SELECT
@ -283,7 +300,7 @@ BEGIN
,credits
,cbal
FROM
seq
bld
ON CONFLICT ON CONSTRAINT bal_pk DO UPDATE SET
obal = EXCLUDED.obal
,debits = EXCLUDED.debits
@ -292,21 +309,30 @@ BEGIN
,prop = evt.bal.prop || EXCLUDED.prop
RETURNING *
)
,n as (
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--determine all fiscal periods invovled in the insert
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
,touched as (
SELECT DISTINCT
fspr
FROM
bali
ins
)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
--update evt.fspr to reflect touched by gl
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
UPDATE
evt.fspr f
SET
prop = COALESCE(f.prop,'{}'::JSONB) || '{"gltouch":"yes"}'::jsonb
FROM
n
touched t
WHERE
f.id = n.fspr;
f.id = t.fspr;
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--this is to catch up all the other accounts if actually necessary
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PERFORM evt.balrf();
RETURN NULL;

View File

@ -8,9 +8,11 @@ DECLARE
_mind timestamptz;
_maxd timestamptz;
BEGIN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--find min and max applicable periods to roll
--min: earliest period involved in current gl posting
--max: latest period involved in any posting, or if none, the current posting
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SELECT
(SELECT min(lower(f.dur)) FROM ins INNER JOIN evt.fspr f ON f.id = ins.fspr)
,GREATEST(
@ -24,6 +26,9 @@ BEGIN
_mind
,_maxd;
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--aggregate all inserted gl transactions
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
WITH
agg AS (
SELECT
@ -42,14 +47,19 @@ BEGIN
,fspr
,dur
)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--get every account involved in target range
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
,arng AS (
SELECT DISTINCT
acct
FROM
agg b
)
,seq AS (
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--roll the balances forward
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
,bld AS (
WITH RECURSIVE rf (acct, id, dur, obal, debits, credits, cbal) AS
(
SELECT
@ -96,7 +106,10 @@ BEGIN
)
SELECT * FROM rf WHERE lower(dur) >= _mind
)
,bali AS (
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--insert the rolled balances
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
,ins AS (
INSERT INTO
evt.bal (acct, fspr, obal, debits, credits, cbal)
SELECT
@ -107,7 +120,7 @@ BEGIN
,credits
,cbal
FROM
seq
bld
ON CONFLICT ON CONSTRAINT bal_pk DO UPDATE SET
obal = EXCLUDED.obal
,debits = EXCLUDED.debits
@ -116,21 +129,30 @@ BEGIN
,prop = evt.bal.prop || EXCLUDED.prop
RETURNING *
)
,n as (
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--determine all fiscal periods invovled in the insert
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
,touched as (
SELECT DISTINCT
fspr
FROM
bali
ins
)
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
--update evt.fspr to reflect touched by gl
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
UPDATE
evt.fspr f
SET
prop = COALESCE(f.prop,'{}'::JSONB) || '{"gltouch":"yes"}'::jsonb
FROM
n
touched t
WHERE
f.id = n.fspr;
f.id = t.fspr;
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--this is to catch up all the other accounts if actually necessary
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PERFORM evt.balrf();
RETURN NULL;