Added is_closed_trans()
[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         $sql = "INSERT INTO ".TB_PREF."audit_trail"
16                 . " (type, trans_no, user, fiscal_year, gl_date, description, gl_seq)
17                         VALUES($trans_type, $trans_no,"
18                         . $_SESSION["wa_current_user"]->user. ","
19                         . get_company_pref('f_year') .","
20                         . "'". date2sql($trans_date) ."',"
21                         . db_escape($descr). ", 0)";
22
23         db_query($sql, "Cannot add audit info");
24         
25         // all audit records beside latest 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 SET gl_seq = NULL"
28                 . " WHERE type=$trans_type AND trans_no=$trans_no AND id!=".db_insert_id();
29
30         db_query($sql, "Cannot update audit gl_seq");
31 }
32
33 function get_audit_trail_all($trans_type, $trans_no)
34 {
35         $sql = "SELECT * FROM ".TB_PREF."audit_trail"
36                 ." WHERE type=$trans_type AND trans_no=$trans_no";
37
38         return db_query($sql, "Cannot get all audit info for transaction");
39 }
40
41 function get_audit_trail_last($trans_type, $trans_no)
42 {
43         $sql = "SELECT * FROM ".TB_PREF."audit_trail"
44                 ." WHERE type=$trans_type AND trans_no=$trans_no AND NOT ISNULL(gl_seq)";
45
46         $res = db_query($sql, "Cannot get last audit info for transaction");
47         if ($res)
48                 $row = db_fetch($res);
49
50         return $row;
51 }
52
53 /*
54         Confirm and close for edition all transactions up to date $todate, 
55         and reindex     journal.
56 */
57 function close_transactions($todate) {
58         $errors = 0;
59         $sql = "SELECT DISTINCT a.id, a.gl_date, a.fiscal_year"
60                 ." FROM ".TB_PREF."gl_trans gl"
61                 ." LEFT JOIN ". TB_PREF."audit_trail a ON 
62                         (gl.type=a.type AND gl.type_no=a.trans_no)"
63                 . " WHERE gl_date<='". date2sql($todate) ."'"
64                 . " AND NOT ISNULL(gl_seq)"
65                 . " ORDER BY a.fiscal_year, a.gl_date, a.id";
66
67         $result = db_query($sql, "Cannot select transactions for closing");
68
69         if (db_num_rows($result)) {
70                 $last_year = 0;
71
72                 while ($row = db_fetch($result)) {
73                         if ($row['fiscal_year'] == null) {
74                                 $errors = 1; continue;
75                         }
76                         if ($last_year != $row['fiscal_year']) {
77                                 $last_year = $row['fiscal_year'];
78                                 $counter = 1; // reset counter on fiscal year change
79                         } else
80                                 $counter++;
81                         $sql2 = "UPDATE ".TB_PREF."audit_trail SET"
82                                 . " gl_seq=$counter"
83                                 . " WHERE id=".$row['id'];
84                                                                                         
85                         db_query($sql2, "Cannot reindex journal");
86                 }
87         }
88         
89         if ($errors) 
90                 display_warning(_("Some transactions journal GL postings were not indexed due to lack of audit trail record."));
91 }
92
93 /*
94         Reopen all transactions for edition up from date $fromdate
95 */
96 function open_transactions($fromdate) {
97
98         $sql = "SELECT a.id, a.gl_date, a.fiscal_year"
99                 ." FROM ".TB_PREF."gl_trans gl"
100                 ." LEFT JOIN ". TB_PREF."audit_trail a ON 
101                         (gl.type=a.type AND gl.type_no=a.trans_no)"
102                 . " WHERE gl_date>='". date2sql($fromdate) ."'"
103                 . " AND !ISNULL(gl_seq)"
104                 . " ORDER BY a.fiscal_year, a.gl_date, a.id";
105
106         $result = db_query($sql, "Cannot select transactions for openning");
107
108         if (db_num_rows($result)) {
109                 $last_year = 0;
110
111                 while ($row = db_fetch($result)) {
112                         if ($row['fiscal_year'] == null) {
113                                 continue;
114                         }
115                         $sql2 = "UPDATE ".TB_PREF."audit_trail SET"
116                                 . " gl_seq=0"
117                                 . " WHERE id=".$row['id'];
118                                                                                         
119                         db_query($sql2, "Cannot clear journal order");
120                 }
121         }
122 }
123 /*
124         Closed transactions have gl_seq number assigned.
125 */
126 function is_closed_trans($type, $trans_no) {
127         $sql = "SELECT  gl_seq  FROM ".TB_PREF."audit_trail"
128                 . " WHERE type=$type AND trans_no=$trans_no AND gl_seq>0";
129
130         $res = db_query($sql, "Cannot check transaction");
131
132         return db_num_rows($res);
133 }
134
135 ?>