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