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