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