Refactoring: renamed getActiveUser() to getUser().
[timetracker.git] / WEB-INF / lib / ttClientHelper.class.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 // Class ttClientHelper is used to help with client related tasks.
30 class ttClientHelper {
31
32   // The getClient looks up a client by id.
33   static function getClient($client_id, $all_fields = false) {
34     global $user;
35     $mdb2 = getConnection();
36
37     $group_id = $user->getGroup();
38     $org_id = $user->org_id;
39
40     $sql = 'select ';
41     if ($all_fields)
42       $sql .= '* ';
43     else
44       $sql .= 'name ';
45
46     $sql .= "from tt_clients where group_id = $group_id and org_id = $org_id".
47       " and id = $client_id and (status = 1 or status = 0)";
48     $res = $mdb2->query($sql);
49     if (!is_a($res, 'PEAR_Error')) {
50       $val = $res->fetchRow();
51       return $val;
52     }
53     return false;
54   }
55
56   // getClients - returns an array of active and inactive clients in a group.
57   static function getClients()
58   {
59     global $user;
60
61     $result = array();
62     $mdb2 = getConnection();
63
64     $sql = "select id, name from tt_clients where group_id = ".$user->getGroup()." and (status = 0 or status = 1) order by upper(name)";
65     $res = $mdb2->query($sql);
66     if (!is_a($res, 'PEAR_Error')) {
67       while ($val = $res->fetchRow()) {
68         $result[] = $val;
69       }
70     }
71     return $result;
72   }
73
74   // The getClientByName looks up a client by name.
75   static function getClientByName($client_name) {
76
77     $mdb2 = getConnection();
78     global $user;
79
80     $sql = "select id from tt_clients where group_id = ".$user->getGroup().
81       " and name = ".$mdb2->quote($client_name)." and (status = 1 or status = 0)";
82
83     $res = $mdb2->query($sql);
84     if (!is_a($res, 'PEAR_Error')) {
85       $val = $res->fetchRow();
86       if ($val['id']) {
87         return $val;
88       }
89     }
90     return false;
91   }
92
93   // The getDeletedClient looks up a deleted client by id.
94   static function getDeletedClient($client_id) {
95
96     $mdb2 = getConnection();
97     global $user;
98
99     $sql = "select name, address from tt_clients where group_id = ".$user->getGroup().
100       " and id = $client_id and status is NULL";
101     $res = $mdb2->query($sql);
102     if (!is_a($res, 'PEAR_Error')) {
103       $val = $res->fetchRow();
104       return $val;
105     }
106     return false;
107   }
108
109   // The delete function marks client as deleded.
110   static function delete($id, $delete_client_entries) {
111
112     $mdb2 = getConnection();
113     global $user;
114
115     // Handle custom field log records.
116     if ($delete_client_entries) {
117       $sql = "update tt_custom_field_log set status = NULL where log_id in (select id from tt_log where client_id = $id and status = 1)";
118       $affected = $mdb2->exec($sql);
119         if (is_a($affected, 'PEAR_Error'))
120           return false;
121     }
122
123     // Handle time records.
124     $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
125     if ($delete_client_entries) {
126       $sql = 'update tt_log set status = NULL'.$modified_part." where client_id = $id";
127       $affected = $mdb2->exec($sql);
128       if (is_a($affected, 'PEAR_Error'))
129         return false;
130     }
131
132     // Handle expense items.
133     if ($delete_client_entries) {
134       $sql = 'update tt_expense_items set status = NULL'.$modified_part." where client_id = $id";
135       $affected = $mdb2->exec($sql);
136       if (is_a($affected, 'PEAR_Error'))
137         return false;
138     }
139
140     // Handle invoices.
141     if ($delete_client_entries) {
142       $sql = "update tt_invoices set status = NULL where client_id = $id and group_id = ".$user->getGroup();
143       $affected = $mdb2->exec($sql);
144       if (is_a($affected, 'PEAR_Error'))
145         return false;
146     }
147
148     // Delete project binds to this client.
149     $sql = "delete from tt_client_project_binds where client_id = $id";
150     $affected = $mdb2->exec($sql);
151     if (is_a($affected, 'PEAR_Error'))
152       return false;
153
154     // Handle users for client.
155     $sql = 'update tt_users set status = NULL'.$modified_part." where client_id = $id and group_id = ".$user->getGroup();
156     $affected = $mdb2->exec($sql);
157     if (is_a($affected, 'PEAR_Error'))
158       return false;
159
160     // Mark client deleted.
161     $sql = "update tt_clients set status = NULL where id = $id and group_id = ".$user->getGroup();
162     $affected = $mdb2->exec($sql);
163     return (!is_a($affected, 'PEAR_Error'));
164   }
165
166   // The insert function inserts a new client record into the clients table.
167   static function insert($fields)
168   {
169     global $user;
170     $mdb2 = getConnection();
171
172     $group_id = (int) $fields['group_id'];
173     $org_id = (int) $fields['org_id'];
174     $name = $fields['name'];
175     $address = $fields['address'];
176     $tax = $fields['tax'];
177     $projects = $fields['projects'];
178     if ($projects)
179       $comma_separated = implode(',', $projects); // This is a comma-separated list of associated projects ids.
180     $status = $fields['status'];
181
182     $tax = str_replace(',', '.', $tax);
183     if ($tax == '') $tax = 0;
184
185     $sql = "insert into tt_clients (group_id, org_id, name, address, tax, projects, status)".
186       " values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($address).", $tax, ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
187
188     $affected = $mdb2->exec($sql);
189     if (is_a($affected, 'PEAR_Error'))
190       return false;
191
192     $last_id = 0;
193     $sql = "select last_insert_id() as last_insert_id";
194     $res = $mdb2->query($sql);
195     $val = $res->fetchRow();
196     $last_id = $val['last_insert_id'];
197
198     if (count($projects) > 0)
199       foreach ($projects as $p_id) {
200         $sql = "insert into tt_client_project_binds (client_id, project_id, group_id, org_id) values($last_id, $p_id, $group_id, $org_id)";
201         $affected = $mdb2->exec($sql);
202         if (is_a($affected, 'PEAR_Error'))
203           return false;
204       }
205
206     return $last_id;
207   }
208
209   // The update function updates a client record in tt_clients table.  
210   static function update($fields)
211   {
212     $mdb2 = getConnection();
213     global $user;
214
215     $id = $fields['id'];
216     $name = $fields['name'];
217     $address = $fields['address'];
218     $tax = $fields['tax'];
219     $status = $fields['status'];
220     $projects = $fields['projects'];
221
222     $tax = str_replace(',', '.', $tax);
223     if ($tax == '') $tax = 0;
224
225     // Insert client to project binds into tt_client_project_binds table.
226     $sql = "delete from tt_client_project_binds where client_id = $id";
227     $affected = $mdb2->exec($sql);
228     if (is_a($affected, 'PEAR_Error'))
229       die($affected->getMessage());
230     if (count($projects) > 0)
231       foreach ($projects as $p_id) {
232         $sql = "insert into tt_client_project_binds (client_id, project_id) values($id, $p_id)";
233         $affected = $mdb2->exec($sql);
234         if (is_a($affected, 'PEAR_Error'))
235           return false;
236       }
237
238     // Update client properties in tt_clients table.
239     $comma_separated = implode(",", $projects); // This is a comma-separated list of associated project ids.
240     $sql = "update tt_clients set name = ".$mdb2->quote($name).", address = ".$mdb2->quote($address).
241       ", tax = $tax, projects = ".$mdb2->quote($comma_separated).", status = $status".
242       " where group_id = ".$user->getGroup()." and id = ".$id;
243     $affected = $mdb2->exec($sql);
244     return (!is_a($affected, 'PEAR_Error'));
245   }
246
247   // The setMappedClient function is used during group import to change client_id value for tt_users to a mapped value.
248   static function setMappedClient($group_id, $imported_id, $mapped_id)
249   {
250     $mdb2 = getConnection();
251     $sql = "update tt_users set client_id = $mapped_id where client_id = $imported_id and group_id = $group_id ";
252     $affected = $mdb2->exec($sql);
253     if (is_a($affected, 'PEAR_Error'))
254       return false;
255
256     return true;
257   }
258
259   // The fillBean function fills the ActionForm object with client data.
260   static function fillBean($client_id, &$bean) {
261     $client = ttClientHelper::getClient($client_id, true);
262     $bean->setAttribute('name', $client['name']);
263     $bean->setAttribute('address', $client['address']);
264     $bean->setAttribute('tax', $client['tax']);
265   }
266
267   // getAssignedProjects - returns an array of projects associatied with a client.
268   static function getAssignedProjects($client_id)
269   {
270     global $user;
271
272     $result = array();
273     $mdb2 = getConnection();
274
275     // Do a query with inner join to get assigned projects.
276     $sql = "select p.id, p.name from tt_projects p".
277       " inner join tt_client_project_binds cpb on (cpb.client_id = $client_id and cpb.project_id = p.id)".
278       " where p.group_id = ".$user->getGroup()." and p.status = 1 order by p.name";
279     $res = $mdb2->query($sql);
280     if (!is_a($res, 'PEAR_Error')) {
281       while ($val = $res->fetchRow()) {
282         $result[] = $val;
283       }
284     }
285     return $result;
286   }
287
288   // getClientsForUser - returns an array of clients that are relevant to a user via assigned projects. 
289   static function getClientsForUser()
290   {
291     global $user;
292     $user_id = $user->getUser();
293
294     $result = array();
295     $mdb2 = getConnection();
296
297     $sql = "select distinct c.id, c.name, c.projects from tt_user_project_binds upb
298       inner join tt_client_project_binds cpb on (cpb.project_id = upb.project_id)
299       inner join tt_clients c on (c.id = cpb.client_id and c.status = 1)
300       where upb.user_id = $user_id and upb.status = 1 order by upper(c.name)";
301
302     $res = $mdb2->query($sql);
303     if (!is_a($res, 'PEAR_Error')) {
304       while ($val = $res->fetchRow()) {
305         $result[] = $val;
306       }
307     }
308     return $result;
309   }
310 }