Old ineffective sql_trail superseded by new improved db_trail logging only calls...
[fa-stable.git] / dimensions / includes / dimensions_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 function add_dimension($reference, $name, $type_, $date_, $due_date, $memo_)
13 {
14         global $Refs;
15
16         begin_transaction(__FUNCTION__, func_get_args());
17
18         $date = date2sql($date_);
19         $duedate = date2sql($due_date);
20
21         $sql = "INSERT INTO ".TB_PREF."dimensions (reference, name, type_, date_, due_date)
22                 VALUES (".db_escape($reference).", ".db_escape($name).", ".db_escape($type_)
23                 .", '$date', '$duedate')";
24         db_query($sql, "could not add dimension");
25         $id = db_insert_id();
26
27         add_comments(ST_DIMENSION, $id, $date_, $memo_);
28
29         $Refs->save(ST_DIMENSION, $id, $reference);
30
31         commit_transaction();
32
33         return $id;
34 }
35
36 function update_dimension($id, $name, $type_, $date_, $due_date, $memo_)
37 {
38         begin_transaction(__FUNCTION__, func_get_args());
39
40         $date = date2sql($date_);
41         $duedate = date2sql($due_date);
42
43         $sql = "UPDATE ".TB_PREF."dimensions SET name=".db_escape($name).",
44                 type_ = ".db_escape($type_).",
45                 date_='$date',
46                 due_date='$duedate'
47                 WHERE id = ".db_escape($id);
48
49         db_query($sql, "could not update dimension");
50
51         update_comments(ST_DIMENSION, $id, null, $memo_);
52
53         commit_transaction();
54
55         return $id;
56 }
57
58 function delete_dimension($id)
59 {
60         begin_transaction(__FUNCTION__, func_get_args());
61
62         // delete the actual dimension
63         $sql="DELETE FROM ".TB_PREF."dimensions WHERE id=".db_escape($id);
64         db_query($sql,"The dimension could not be deleted");
65
66         delete_comments(ST_DIMENSION, $id);
67
68         commit_transaction();
69 }
70
71 //--------------------------------------------------------------------------------------
72
73 function get_dimension($id, $allow_null=false)
74 {
75     $sql = "SELECT * FROM ".TB_PREF."dimensions WHERE id=".db_escape($id);
76
77         $result = db_query($sql, "The dimension could not be retrieved");
78
79         if (!$allow_null && db_num_rows($result) == 0)
80                 display_db_error("Could not find dimension $id", $sql);
81
82         return db_fetch($result);
83 }
84
85 //--------------------------------------------------------------------------------------
86
87 function get_dimension_string($id, $html=false, $space=' ')
88 {
89         if ($id <= 0)
90         {
91                 if ($html)
92                         $dim = "&nbsp;";
93                 else
94                         $dim = "";
95         }
96         else
97         {
98                 $row = get_dimension($id, true);
99                 $dim = $row['reference'] . $space . $row['name'];
100         }
101
102         return $dim;
103 }
104
105 //--------------------------------------------------------------------------------------
106
107 function get_dimensions()
108 {
109         $sql = "SELECT * FROM ".TB_PREF."dimensions ORDER BY date_";
110
111         return db_query($sql, "The dimensions could not be retrieved");
112 }
113
114 //--------------------------------------------------------------------------------------
115
116 function dimension_has_deposits($id)
117 {
118         return dimension_has_payments($id);
119 }
120
121 //--------------------------------------------------------------------------------------
122
123 function dimension_has_payments($id)
124 {
125         $sql = "SELECT COUNT(*) FROM ".TB_PREF."gl_trans WHERE dimension_id = ".db_escape($id)
126          . " OR dimension2_id = ".db_escape($id);
127         $res = db_query($sql, "Transactions could not be calculated");
128         $row = db_fetch_row($res);
129         return ($row[0] > 0);
130 }
131
132 function dimension_is_closed($id)
133 {
134         $result = get_dimension($id);
135         return ($result['closed'] == '1');
136 }
137
138 //--------------------------------------------------------------------------------------
139
140 function close_dimension($id)
141 {
142         begin_transaction(__FUNCTION__, func_get_args());
143         $sql = "UPDATE ".TB_PREF."dimensions SET closed='1' WHERE id = ".db_escape($id);
144         db_query($sql, "could not close dimension");
145         commit_transaction();
146 }
147
148 //--------------------------------------------------------------------------------------
149
150 function reopen_dimension($id)
151 {
152         begin_transaction(__FUNCTION__, func_get_args());
153         $sql = "UPDATE ".TB_PREF."dimensions SET closed='0' WHERE id = ".db_escape($id);
154         db_query($sql, "could not reopen dimension");
155         commit_transaction();
156 }
157
158 //--------------------------------------------------------------------------------------
159
160 function get_dimension_balance_all($id, $from, $to) 
161 {
162         $from = date2sql($from);
163         $to = date2sql($to);
164         $sql = "SELECT account, coa.account_name, sum(amount) AS amt 
165                 FROM "
166                 .TB_PREF."gl_trans trans,"
167                 .TB_PREF."chart_master coa
168                 WHERE
169                         trans.account = coa.account_code
170                 AND     (dimension_id = ".db_escape($id)." OR dimension2_id = ".db_escape($id).")
171                 AND     tran_date >= '$from' AND tran_date <= '$to' GROUP BY account";
172         return db_query($sql, "Transactions could not be calculated");
173 }
174
175 //--------------------------------------------------------------------------------------
176
177 function get_dimension_balance($id, $from, $to) 
178 {
179         $id = db_escape($id);
180         $sql = "SELECT SUM(amount)
181                         FROM ".TB_PREF."gl_trans 
182                         WHERE
183                                 tran_date >= '" .       date2sql($from) . "' 
184                         AND     tran_date <= '" . date2sql($to) . "' 
185                         AND (dimension_id = $id OR dimension2_id = $id)";
186         $res = db_query($sql, "Sum of transactions could not be calculated");
187         $row = db_fetch_row($res);
188
189         return $row[0];
190 }
191
192 //--------------------------------------------------------------------------------------
193
194 function get_sql_for_search_dimensions($dim, $from, $to, $order='', $type = -1, $open=false, $overdue = false)
195 {
196         $sql = "SELECT dim.id,
197                 dim.reference,
198                 dim.name,
199                 dim.type_,
200                 dim.date_,
201                 dim.due_date,
202                 dim.closed
203                 FROM ".TB_PREF."dimensions as dim WHERE id > 0";
204
205         if ($order)
206         {
207                 $sql .= " AND reference LIKE ".db_escape("%". $order . "%");
208         } else {
209
210                 if ($dim == 1)
211                         $sql .= " AND type_=1";
212
213                 if ($open)
214                         $sql .= " AND closed=0";
215
216                 if ($type > 0)
217                         $sql .= " AND type_=".db_escape($type);
218
219                 if ($overdue)
220                 {
221                         $today = date2sql(Today());
222
223                         $sql .= " AND due_date < '$today'";
224                 }
225
226                 $sql .= " AND date_ >= '" . date2sql($from) . "'
227                         AND date_ <= '" . date2sql($to) . "'";
228         }
229         return $sql;
230 }