DB/Ledger.pm and Objects/Ledger.pm

The Database contains a table for ledger data. The table pre 1.5 did not contain monthly/quarterly data in the table. As of 1.5 the table contains the quarterly fields:

The text fields should be of the format MM-DD.

We Should only track the ending date for a period because the begginning date of the next period will always be the day after.  No!  February will cause problems with that. If we track the beginning of each period, then the ending of the previous period can always be calculated by subtracting 1 day.

Month Starting dates
SELECT ((ledger_tb.offset1 + 2000) ||'-'|| ledger_tb.start1)::date,
((ledger_tb.offset2 + 2000) ||'-'|| ledger_tb.start2)::date,
((ledger_tb.offset3 + 2000) ||'-'|| ledger_tb.start3)::date,
((ledger_tb.offset4 + 2000) ||'-'|| ledger_tb.start4)::date,
((ledger_tb.offset5 + 2000) ||'-'|| ledger_tb.start5)::date,
((ledger_tb.offset6 + 2000) ||'-'|| ledger_tb.start6)::date,
((ledger_tb.offset7 + 2000) ||'-'|| ledger_tb.start7)::date,
((ledger_tb.offset8 + 2000) ||'-'|| ledger_tb.start8)::date,
((ledger_tb.offset9 + 2000) ||'-'|| ledger_tb.start9)::date,
((ledger_tb.offset10 + 2000) ||'-'|| ledger_tb.start10)::date,
((ledger_tb.offset11 + 2000) ||'-'|| ledger_tb.start11)::date,
((ledger_tb.offset12 + 2000) ||'-'|| ledger_tb.start12)::date
WHERE code = '';

Month Ending dates
SELECT ((ledger_tb.offset2 + 2000) ||'-'|| ledger_tb.start2)::date - 1,
((ledger_tb.offset3 + 2000) ||'-'|| ledger_tb.start3)::date - 1,
((ledger_tb.offset4 + 2000) ||'-'|| ledger_tb.start4)::date - 1,
((ledger_tb.offset5 + 2000) ||'-'|| ledger_tb.start5)::date - 1,
((ledger_tb.offset6 + 2000) ||'-'|| ledger_tb.start6)::date - 1,
((ledger_tb.offset7 + 2000) ||'-'|| ledger_tb.start7)::date - 1,
((ledger_tb.offset8 + 2000) ||'-'|| ledger_tb.start8)::date - 1,
((ledger_tb.offset9 + 2000) ||'-'|| ledger_tb.start9)::date - 1,
((ledger_tb.offset10 + 2000) ||'-'|| ledger_tb.start10)::date - 1,
((ledger_tb.offset11 + 2000) ||'-'|| ledger_tb.start11)::date - 1,
((ledger_tb.offset12 + 2000) ||'-'|| ledger_tb.start12)::date - 1,
((ledger_tb.offset1 + 1 + 2000) ||'-'|| ledger_tb.start1)::date - 1

Quarter Ending dates
SELECT ((ledger_tb.offset4 + 2000) ||'-'|| ledger_tb.start4)::date - 1 as q1End,
((ledger_tb.offset7 + 2000) ||'-'|| ledger_tb.start7)::date - 1 as q2End,
((ledger_tb.offset10 + 2000) ||'-'|| ledger_tb.start10)::date - 1 as q3End,
((ledger_tb.offset1 + 1 + 2000) ||'-'|| ledger_tb.start1)::date - 1 as q4End
WHERE code = '';

The offset fields are used to change a fiscal year into an actual year to go with the particular date. For example, the fiscal year 2000 may start in 2000 but end in 2001.
Quarter Start End
1 2000-01-15 2000-04-14
2 2000-04-15 2000-07-14
3 2000-07-15 2000-10-14
4 2000-10-15 2001-01-14
So in this case the offset1-11 would be "0" and offset12 would be "1". The point of the offsets is to have the ability to quickly calculate an actual year from fiscal year by using something like:

SELECT (ledger_tb.offset1 + 2000) ||'-'|| ledger_tb.q1start, (ledger_tb.offset2 + 2000) ||'-'|| ledger_tb.q1end,

Where 2000 is the fiscal year that you are dealing with. A SQL stored procedure should be written to do this. Given a ledger and fiscal year the procedure will return the quarter dates.

Another example: The Fiscal year 2000 starts on 1999-6-3. So the ledgertable would contain:
Date Field Value Offset Field Value
1999-06-03 start1 06-03 offset1 -1
1999-09-03 start4 09-03 offset4 -1
1999-12-03 start7 12-03 offset7 -1
2000-03-03 start10 03-03 offset10 0

The upgrade process from 1.4 to 1.5 will require running some SQL to fill in the previous ledgers with this new data.
UPDATE ledger_tb SET start1 = (SELECT DISTINCT(SUBSTR(SELECT q1_start FROM known_years_tb WHERE ledger_code = code)));
UPDATE ledger_tb SET offset1 = (SELECT SUBSTR(q1_start, 1, 4)::int - year FROM known_years_tb WHERE ledger_code = code LIMIT 1);


$Id: Ledger.html,v 1.1 2003/06/26 22:25:17 moreejt Exp $