For a start, if you have a large amount of transactions in each year you wish to close/open, the function open_transactions can be replaced with:
function open_transactions($fromdate) {
$sql = "UPDATE ".TB_PREF."audit_trail SET gl_seq=0 WHERE gl_date>='". date2sql($fromdate) ."' AND !ISNULL(gl_seq) AND !ISNULL(fiscal_year)";
db_query($sql, "Cannot open transactions");
}
The above has been committed in my GitHub Repo - 1, 2.
The function close_transactions called in function close_year in admin/db/fiscalyears_db.inc be commented out and it's functionality be manually executed from the sql obtained from:
$sql = "
DROP TABLE IF EXISTS ".TB_PREF."audit_tmp;
CREATE TEMPORARY TABLE ".TB_PREF."audit_tmp SELECT id, Counter AS gl_seq FROM (
SELECT id, gl_date, fiscal_year
, (@counter:=IF(fiscal_year <> @lastyr, 1, @counter+1)) AS Counter
, (@lastyr:=IF(fiscal_year <> @lastyr, fiscal_year, @lastyr)) AS LastYr FROM
( SELECT DISTINCT a.id, a.gl_date, a.fiscal_year
FROM ".TB_PREF."gl_trans gl INNER JOIN ".TB_PREF."audit_trail a ON (gl.type=a.type AND gl.type_no=a.trans_no)
WHERE gl_date<='". date2sql($todate) ."' AND NOT ISNULL(gl_seq)
ORDER BY a.fiscal_year, a.gl_date, a.id) b, (SELECT (@lastyr:=0), (@counter:=1)) w
) c;
UPDATE ".TB_PREF."audit_tmp a LEFT JOIN ".TB_PREF."audit_trail b USING (id) SET b.gl_seq=a.gl_seq;
DROP TABLE ".TB_PREF."audit_tmp;
";
We also need to see if there is any performance gain by indexing gl_seq in the audit_trail table.
Only closed fiscal years will have the audit_table.gl_seq chronologically sequentially numbered (some will be NULL and some others 0 as well). All open fiscal years will have the said field as 0 or NULL only.
At the end of successful closure of a fiscal year, on it's last date as the last 2 gl_trans.counter entries we get the memo_ field set to Closing Year with the amount values in contra. When the fiscal year is then re-opened, these entries still remain and will need to be manually deleted to be re-created when it is subsequently closed.
The Audit Trail ERD is available in the Wiki.