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