From 6c0718e3943c36ad8b55e47fc26087da5d6df732 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Fri, 5 Oct 2018 14:08:23 -0400 Subject: [PATCH] add comments --- schema/functions/balrf.sql | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/schema/functions/balrf.sql b/schema/functions/balrf.sql index 1ee5991..ce876d0 100644 --- a/schema/functions/balrf.sql +++ b/schema/functions/balrf.sql @@ -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