Changes up to 2.3.7 merged into unstable branch.
[fa-stable.git] / includes / db / audit_trail_db.inc
1 <?php
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 ***********************************************************************/
12
13 function add_audit_trail($trans_type, $trans_no, $trans_date, $descr='')
14 {
15         $date = date2sql($trans_date);
16         $sql = "INSERT INTO ".TB_PREF."audit_trail"
17                 . " (type, trans_no, user, gl_date, description)
18                         VALUES(".db_escape($trans_type).", ".db_escape($trans_no).","
19                         . $_SESSION["wa_current_user"]->user . ","
20                         . "'$date',". db_escape($descr). ")";
21
22         db_query($sql, "Cannot add audit info");
23         
24         // all audit records beside just inserted one should have gl_seq set to NULL
25         // to avoid need for subqueries (not existing in MySQL 3) all over the code
26         $sql = "UPDATE ".TB_PREF."audit_trail audit LEFT JOIN ".TB_PREF."fiscal_year year ON year.begin<='$date' AND year.end>='$date'
27                 SET audit.gl_seq = IF(audit.id=".db_insert_id().", 0, NULL),"
28                 ."audit.fiscal_year=year.id"
29                 . " WHERE type=".db_escape($trans_type)." AND trans_no="
30                 . db_escape($trans_no);
31
32         db_query($sql, "Cannot update audit gl_seq");
33 }
34
35 function get_audit_trail_all($trans_type, $trans_no)
36 {
37         $sql = "SELECT * FROM ".TB_PREF."audit_trail"
38                 ." WHERE type=".db_escape($trans_type)." AND trans_no="
39                 .db_escape($trans_no);
40
41         return db_query($sql, "Cannot get all audit info for transaction");
42 }
43
44 function get_audit_trail_last($trans_type, $trans_no)
45 {
46         $sql = "SELECT * FROM ".TB_PREF."audit_trail"
47                 ." WHERE type=".db_escape($trans_type).
48                         " AND trans_no=".db_escape($trans_no)." AND NOT ISNULL(gl_seq)";
49
50         $res = db_query($sql, "Cannot get last audit info for transaction");
51         if ($res)
52                 $row = db_fetch($res);
53
54         return $row;
55 }
56
57 /*
58         Confirm and close for edition all transactions up to date $todate, 
59         and reindex     journal.
60 */
61 function close_transactions($todate) {
62
63         begin_transaction();
64
65         $errors = 0;
66         // select only those audit trail records which produce any GL postings
67         $sql = "SELECT a.id, gl.tran_date, a.fiscal_year, a.gl_seq,
68                  gl.tran_date <= '". date2sql($todate) ."' as closed"
69                 ." FROM ".TB_PREF."gl_trans gl"
70                 ." LEFT JOIN ". TB_PREF."audit_trail a ON 
71                         (gl.type=a.type AND gl.type_no=a.trans_no)"
72                 . " WHERE NOT ISNULL(a.gl_seq) AND gl.amount!=0"        // skip old audit records and voided transactions
73                 . " GROUP BY  a.id, gl.tran_date, a.fiscal_year, a.gl_seq ORDER BY a.fiscal_year, gl.tran_date, a.id";
74
75         $result = db_query($sql, "Cannot select transactions for closing");
76
77         if (db_num_rows($result)) {
78                 $last_year = 0;
79
80                 while ($row = db_fetch($result)) {
81                         if ($row['fiscal_year'] == null) {
82                                 $errors = 1; continue;
83                         }
84                         if ($last_year != $row['fiscal_year']) {
85                                 $last_year = $row['fiscal_year'];
86                                 $counter = 0; // reset counter on fiscal year change
87                         }
88                         
89                         $seq = $row['closed'] ? ++$counter : 0;
90                         if ($row['gl_seq'] != $seq)     { // update transaction status only when gl_seq has changed
91                                 $sql2 = "UPDATE ".TB_PREF."audit_trail SET"
92                                         . " gl_seq=$seq"
93                                         . " WHERE id=".$row['id'];
94                                 db_query($sql2, "Cannot reindex journal");
95                         }
96                 }
97         }
98         if ($errors) 
99                 display_warning(_("Some transactions journal GL postings were not indexed due to lack of audit trail record."));
100         else
101                 update_company_prefs(array('gl_closing_date'=> date2sql($todate)));
102         commit_transaction();
103
104         return $errors;
105 }
106
107 /*
108         Closed transactions have gl_seq number assigned.
109 */
110 function is_closed_trans($type, $trans_no) {
111
112         $cdate = get_company_pref('gl_closing_date');
113         if (!$cdate)
114                 return false;
115
116 // FIXME: gl_date can be badly entered for some transactions due to bug in previous FA versions 
117         $sql = "SELECT  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
122 //              ." OR gl_seq>0)";
123
124         $res = db_query($sql, "Cannot check transaction");
125
126         return db_num_rows($res);
127
128 }
129
130 ?>