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