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_)
18 $date = date2sql($date_);
19 $duedate = date2sql($due_date);
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");
27 add_comments(ST_DIMENSION, $id, $date_, $memo_);
29 $Refs->save(ST_DIMENSION, $id, $reference);
36 function update_dimension($id, $name, $type_, $date_, $due_date, $memo_)
40 $date = date2sql($date_);
41 $duedate = date2sql($due_date);
43 $sql = "UPDATE ".TB_PREF."dimensions SET name=".db_escape($name).",
44 type_ = ".db_escape($type_).",
47 WHERE id = ".db_escape($id);
49 db_query($sql, "could not update dimension");
51 update_comments(ST_DIMENSION, $id, null, $memo_);
58 function delete_dimension($id)
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");
66 delete_comments(ST_DIMENSION, $id);
71 //--------------------------------------------------------------------------------------
73 function get_dimension($id, $allow_null=false)
75 $sql = "SELECT * FROM ".TB_PREF."dimensions WHERE id=".db_escape($id);
77 $result = db_query($sql, "The dimension could not be retrieved");
79 if (!$allow_null && db_num_rows($result) == 0)
80 display_db_error("Could not find dimension $id", $sql);
82 return db_fetch($result);
85 //--------------------------------------------------------------------------------------
87 function get_dimension_string($id, $html=false, $space=' ')
98 $row = get_dimension($id, true);
99 $dim = $row['reference'] . $space . $row['name'];
105 //--------------------------------------------------------------------------------------
107 function get_dimensions()
109 $sql = "SELECT * FROM ".TB_PREF."dimensions ORDER BY date_";
111 return db_query($sql, "The dimensions could not be retrieved");
114 //--------------------------------------------------------------------------------------
116 function dimension_has_deposits($id)
118 return dimension_has_payments($id);
121 //--------------------------------------------------------------------------------------
123 function dimension_has_payments($id)
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);
132 function dimension_is_closed($id)
134 $result = get_dimension($id);
135 return ($result['closed'] == '1');
138 //--------------------------------------------------------------------------------------
140 function close_dimension($id)
142 $sql = "UPDATE ".TB_PREF."dimensions SET closed='1' WHERE id = ".db_escape($id);
143 db_query($sql, "could not close dimension");
146 //--------------------------------------------------------------------------------------
148 function reopen_dimension($id)
150 $sql = "UPDATE ".TB_PREF."dimensions SET closed='0' WHERE id = ".db_escape($id);
151 db_query($sql, "could not reopen dimension");
154 //--------------------------------------------------------------------------------------
156 function get_dimension_balance_all($id, $from, $to)
158 $from = date2sql($from);
160 $sql = "SELECT account, ".TB_PREF."chart_master.account_name, sum(amount) AS amt FROM
161 ".TB_PREF."gl_trans,".TB_PREF."chart_master WHERE
162 ".TB_PREF."gl_trans.account = ".TB_PREF."chart_master.account_code AND
163 (dimension_id = ".db_escape($id)." OR dimension2_id = ".db_escape($id).") AND
164 tran_date >= '$from' AND tran_date <= '$to' GROUP BY account";
165 return db_query($sql, "Transactions could not be calculated");
168 //--------------------------------------------------------------------------------------
170 function get_dimension_balance($id, $from, $to)
172 $id = db_escape($id);
173 $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans WHERE tran_date >= '" .
174 date2sql($from) . "' AND
175 tran_date <= '" . date2sql($to) . "' AND (dimension_id = $id OR dimension2_id = $id)";
176 $res = db_query($sql, "Sum of transactions could not be calculated");
177 $row = db_fetch_row($res);
182 //--------------------------------------------------------------------------------------
184 function get_sql_for_search_dimensions($dim)
186 $sql = "SELECT dim.id,
193 FROM ".TB_PREF."dimensions as dim WHERE id > 0";
195 if (isset($_POST['OrderNumber']) && $_POST['OrderNumber'] != "")
197 $sql .= " AND reference LIKE ".db_escape("%". $_POST['OrderNumber'] . "%");
201 $sql .= " AND type_=1";
203 if (isset($_POST['OpenOnly']))
205 $sql .= " AND closed=0";
208 if (isset($_POST['type_']) && ($_POST['type_'] > 0))
210 $sql .= " AND type_=".db_escape($_POST['type_']);
213 if (isset($_POST['OverdueOnly']))
215 $today = date2sql(Today());
217 $sql .= " AND due_date < '$today'";
220 $sql .= " AND date_ >= '" . date2sql($_POST['FromDate']) . "'
221 AND date_ <= '" . date2sql($_POST['ToDate']) . "'";