add comments

This commit is contained in:
Paul Trowbridge 2018-10-05 14:08:23 -04:00
parent 588a34e2bc
commit 6c0718e394

View File

@ -6,7 +6,9 @@ DECLARE
_maxd timestamptz; _maxd timestamptz;
BEGIN BEGIN
--get last accounts touched and last rollforward if available -----------------------------------------------------------------------------------------------------------------------------------------------------------------
--get last periods touched and last rollforward if available
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
SELECT SELECT
--get last rollforward, if none, use earliest period touched --get last rollforward, if none, use earliest period touched
COALESCE( COALESCE(
@ -21,12 +23,17 @@ BEGIN
FROM FROM
evt.fspr; evt.fspr;
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
--test if a roll forward is required
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
IF _maxd <= _mind THEN IF _maxd <= _mind THEN
RETURN; RETURN;
END IF; END IF;
WITH WITH
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
--list each period in min and max --list each period in min and max
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
prng AS ( prng AS (
SELECT SELECT
id id
@ -38,7 +45,9 @@ BEGIN
lower(f.dur) >= _mind lower(f.dur) >= _mind
AND lower(f.dur) <= _maxd AND lower(f.dur) <= _maxd
) )
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
--get every account involved in target range --get every account involved in target range
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
,arng AS ( ,arng AS (
SELECT DISTINCT SELECT DISTINCT
acct acct
@ -47,6 +56,9 @@ BEGIN
INNER JOIN prng ON INNER JOIN prng ON
prng.id = b.fspr prng.id = b.fspr
) )
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
--cascade the balances
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
,bld AS ( ,bld AS (
WITH RECURSIVE rf (acct, id, dur, obal, debits, credits, cbal) AS WITH RECURSIVE rf (acct, id, dur, obal, debits, credits, cbal) AS
( (
@ -88,6 +100,9 @@ BEGIN
) )
select * from rf select * from rf
) )
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
--upsert the cascaded balances
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
,ins AS ( ,ins AS (
INSERT INTO INSERT INTO
evt.bal evt.bal
@ -107,12 +122,18 @@ BEGIN
,cbal = EXCLUDED.cbal ,cbal = EXCLUDED.cbal
RETURNING * RETURNING *
) )
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
--determine all fiscal periods involved
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
,touched AS ( ,touched AS (
SELECT DISTINCT SELECT DISTINCT
fspr fspr
FROM FROM
ins ins
) )
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
--update evt.fsor to reflect roll status
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
UPDATE UPDATE
evt.fspr f evt.fspr f
SET SET