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