9b08207ea20f3485fa1e9c4dd80101c88c318a7f
[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         begin_transaction();
15
16         $date = date2sql($date_);
17         $duedate = date2sql($due_date);
18
19         $sql = "INSERT INTO ".TB_PREF."dimensions (reference, name, type_, date_, due_date)
20                 VALUES (".db_escape($reference).", ".db_escape($name).", ".db_escape($type_)
21                 .", ".db_escape($date_).", ".db_escape($due_date).")";
22         db_query($sql, "could not add dimension");
23
24         $id = db_insert_id();
25
26         add_comments(systypes::dimension(), $id, $date_, $memo_);
27
28         references::save_last($reference, systypes::dimension());
29
30         commit_transaction();
31
32         return $id;
33 }
34
35 function update_dimension($id, $name, $type_, $date_, $due_date, $memo_)
36 {
37         begin_transaction();
38
39         $date = date2sql($date_);
40         $duedate = date2sql($due_date);
41
42         $sql = "UPDATE ".TB_PREF."dimensions SET name=".db_escape($name).",
43                 type_ = ".db_escape($type_).",
44                 date_=".db_escape($date_).",
45                 due_date=".db_escape($due_date)."
46                 WHERE id = ".db_escape($id);
47
48         db_query($sql, "could not update dimension");
49
50         update_comments(systypes::dimension(), $id, null, $memo_);
51
52         commit_transaction();
53
54         return $id;
55 }
56
57 function delete_dimension($id)
58 {
59         begin_transaction();
60
61         // delete the actual dimension
62         $sql="DELETE FROM ".TB_PREF."dimensions WHERE id=".db_escape($id);
63         db_query($sql,"The dimension could not be deleted");
64
65         delete_comments(systypes::dimension(), $id);
66
67         commit_transaction();
68 }
69
70 //--------------------------------------------------------------------------------------
71
72 function get_dimension($id, $allow_null=false)
73 {
74     $sql = "SELECT * FROM ".TB_PREF."dimensions WHERE id=".db_escape($id);
75
76         $result = db_query($sql, "The dimension could not be retrieved");
77
78         if (!$allow_null && db_num_rows($result) == 0)
79                 display_db_error("Could not find dimension $id", $sql);
80
81         return db_fetch($result);
82 }
83
84 //--------------------------------------------------------------------------------------
85
86 function get_dimension_string($id, $html=false, $space=' ')
87 {
88         if ($id <= 0)
89         {
90                 if ($html)
91                         $dim = "&nbsp;";
92                 else
93                         $dim = "";
94         }
95         else
96         {
97                 $row = get_dimension($id, true);
98                 $dim = $row['reference'] . $space . $row['name'];
99         }
100
101         return $dim;
102 }
103
104 //--------------------------------------------------------------------------------------
105
106 function get_dimensions()
107 {
108         $sql = "SELECT * FROM ".TB_PREF."dimensions ORDER BY date_";
109
110         return db_query($sql, "The dimensions could not be retrieved");
111 }
112
113 //--------------------------------------------------------------------------------------
114
115 function dimension_has_deposits($id)
116 {
117         return dimension_has_payments($id);
118 }
119
120 //--------------------------------------------------------------------------------------
121
122 function dimension_has_payments($id)
123 {
124         $sql = "SELECT SUM(amount) FROM ".TB_PREF."gl_trans WHERE dimension_id = ".db_escape($id);
125         $res = db_query($sql, "Transactions could not be calculated");
126         $row = db_fetch_row($res);
127         return ($row[0] != 0.0);
128 }
129
130 function dimension_is_closed($id)
131 {
132         $result = get_dimension($id);
133         return ($result['closed'] == '1');
134 }
135
136 //--------------------------------------------------------------------------------------
137
138 function close_dimension($id)
139 {
140         $sql = "UPDATE ".TB_PREF."dimensions SET closed='1' WHERE id = ".db_escape($id);
141         db_query($sql, "could not close dimension");
142 }
143
144 //--------------------------------------------------------------------------------------
145
146 ?>