2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
13 function add_audit_trail($trans_type, $trans_no, $trans_date, $descr='')
17 $date = date2sql($trans_date);
18 $sql = "INSERT INTO ".TB_PREF."audit_trail"
19 . " (type, trans_no, user, gl_date, description)
20 VALUES(".db_escape($trans_type).", ".db_escape($trans_no).","
21 . $_SESSION["wa_current_user"]->user . ","
22 . "'$date',". db_escape($descr). ")";
24 db_query($sql, "Cannot add audit info");
25 // all audit records beside just inserted one should have gl_seq set to NULL
26 // to avoid need for subqueries (not existing in MySQL 3) all over the code
27 $sql = "UPDATE ".TB_PREF."audit_trail audit LEFT JOIN ".TB_PREF."fiscal_year year ON year.begin<='$date' AND year.end>='$date'
28 SET audit.gl_seq = IF(audit.id=".db_insert_id().", 0, NULL),"
29 ."audit.fiscal_year=year.id"
30 . " WHERE type=".db_escape($trans_type)." AND trans_no="
31 . db_escape($trans_no);
33 db_query($sql, "Cannot update audit gl_seq");
37 function get_audit_trail_all($trans_type, $trans_no)
39 $sql = "SELECT * FROM ".TB_PREF."audit_trail"
40 ." WHERE type=".db_escape($trans_type)." AND trans_no="
41 .db_escape($trans_no);
43 return db_query($sql, "Cannot get all audit info for transaction");
46 function get_audit_trail_last($trans_type, $trans_no)
48 $sql = "SELECT * FROM ".TB_PREF."audit_trail"
49 ." WHERE type=".db_escape($trans_type).
50 " AND trans_no=".db_escape($trans_no)." AND NOT ISNULL(gl_seq)";
52 $res = db_query($sql, "Cannot get last audit info for transaction");
54 $row = db_fetch($res);
60 Confirm and close for edition all transactions up to date $todate,
63 function close_transactions($todate) {
68 // select only those audit trail records which produce any GL postings
69 $sql = "SELECT a.id, gl.tran_date, a.fiscal_year, a.gl_seq,
70 gl.tran_date <= '". date2sql($todate) ."' as closed"
71 ." FROM ".TB_PREF."gl_trans gl"
72 ." LEFT JOIN ". TB_PREF."audit_trail a ON
73 (gl.type=a.type AND gl.type_no=a.trans_no)"
74 . " WHERE NOT ISNULL(a.gl_seq) AND gl.amount!=0" // skip old audit records and voided transactions
75 . " GROUP BY a.id, gl.tran_date, a.fiscal_year, a.gl_seq ORDER BY a.fiscal_year, gl.tran_date, a.id";
77 $result = db_query($sql, "Cannot select transactions for closing");
79 if (db_num_rows($result)) {
82 while ($row = db_fetch($result)) {
83 if ($row['fiscal_year'] == null) {
84 $errors = 1; continue;
86 if ($last_year != $row['fiscal_year']) {
87 $last_year = $row['fiscal_year'];
88 $counter = 0; // reset counter on fiscal year change
91 $seq = $row['closed'] ? ++$counter : 0;
92 if ($row['gl_seq'] != $seq) { // update transaction status only when gl_seq has changed
93 $sql2 = "UPDATE ".TB_PREF."audit_trail SET"
95 . " WHERE id=".$row['id'];
96 db_query($sql2, "Cannot reindex journal");
102 display_warning(_("Some transactions journal GL postings were not indexed due to lack of audit trail record."));
104 update_company_prefs(array('gl_closing_date'=> date2sql($todate)));
105 commit_transaction();
110 function get_journal_number($type, $trans_no) {
112 $cdate = get_company_pref('gl_closing_date');
116 // FIXME: gl_date can be badly entered for some transactions due to bug in previous FA versions
117 $sql = "SELECT MAX(gl_seq) as gl_seq FROM ".TB_PREF."audit_trail"
118 . " WHERE type=".db_escape($type)
119 ." AND trans_no=".db_escape($trans_no)
120 ." AND gl_date<='$cdate'"; // date is stored in sql format
121 // ." AND (gl_date<='$cdate'" // some transaction can be not sequenced due to 0 amount, however after edition this could change
123 $res = db_query($sql, "Cannot check transaction");
124 if (db_num_rows($res))
126 $myrow =db_fetch($res);
127 return $myrow['gl_seq'] ? $myrow['gl_seq'] : _('None');
133 Closed transactions have gl_seq number assigned.
135 function is_closed_trans($type, $trans_no) {
137 return get_journal_number($type, $trans_no) > 0;