More use of getActiveGroup as part of ongoing subgroup support.
[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
35     $mdb2 = getConnection();
36     global $user;
37
38     $sql = 'select ';
39     if ($all_fields)
40       $sql .= '* ';
41     else
42       $sql .= 'name ';
43
44     $sql .= "from tt_clients where group_id = ".$user->getActiveGroup().
45       " and id = $client_id and (status = 1 or status = 0)";
46     $res = $mdb2->query($sql);
47     if (!is_a($res, 'PEAR_Error')) {
48       $val = $res->fetchRow();
49       return $val;
50     }
51     return false;
52   }
53
54   // getClients - returns an array of active and inactive clients in a group.
55   static function getClients()
56   {
57     global $user;
58
59     $result = array();
60     $mdb2 = getConnection();
61
62     $sql = "select id, name from tt_clients where group_id = ".$user->getActiveGroup()." and (status = 0 or status = 1) order by upper(name)";
63     $res = $mdb2->query($sql);
64     if (!is_a($res, 'PEAR_Error')) {
65       while ($val = $res->fetchRow()) {
66         $result[] = $val;
67       }
68     }
69     return $result;
70   }
71
72   // The getClientByName looks up a client by name.
73   static function getClientByName($client_name) {
74
75     $mdb2 = getConnection();
76     global $user;
77
78     $sql = "select id from tt_clients where group_id = ".$user->getActiveGroup().
79       " and name = ".$mdb2->quote($client_name)." and (status = 1 or status = 0)";
80
81     $res = $mdb2->query($sql);
82     if (!is_a($res, 'PEAR_Error')) {
83       $val = $res->fetchRow();
84       if ($val['id']) {
85         return $val;
86       }
87     }
88     return false;
89   }
90
91   // The getDeletedClient looks up a deleted client by id.
92   static function getDeletedClient($client_id) {
93
94     $mdb2 = getConnection();
95     global $user;
96
97     $sql = "select name, address from tt_clients where group_id = ".$user->getActiveGroup().
98       " and id = $client_id and status is NULL";
99     $res = $mdb2->query($sql);
100     if (!is_a($res, 'PEAR_Error')) {
101       $val = $res->fetchRow();
102       return $val;
103     }
104     return false;
105   }
106
107   // The delete function marks client as deleded.
108   static function delete($id, $delete_client_entries) {
109
110     $mdb2 = getConnection();
111     global $user;
112
113     // Handle custom field log records.
114     if ($delete_client_entries) {
115       $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)";
116       $affected = $mdb2->exec($sql);
117         if (is_a($affected, 'PEAR_Error'))
118           return false;
119     }
120
121     // Handle time records.
122     $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
123     if ($delete_client_entries) {
124       $sql = 'update tt_log set status = NULL'.$modified_part." where client_id = $id";
125       $affected = $mdb2->exec($sql);
126       if (is_a($affected, 'PEAR_Error'))
127         return false;
128     }
129
130     // Handle expense items.
131     if ($delete_client_entries) {
132       $sql = 'update tt_expense_items set status = NULL'.$modified_part." where client_id = $id";
133       $affected = $mdb2->exec($sql);
134       if (is_a($affected, 'PEAR_Error'))
135         return false;
136     }
137
138     // Handle invoices.
139     if ($delete_client_entries) {
140       $sql = "update tt_invoices set status = NULL where client_id = $id and group_id = $user->group_id";
141       $affected = $mdb2->exec($sql);
142       if (is_a($affected, 'PEAR_Error'))
143         return false;
144     }
145
146     // Delete project binds to this client.
147     $sql = "delete from tt_client_project_binds where client_id = $id";
148     $affected = $mdb2->exec($sql);
149     if (is_a($affected, 'PEAR_Error'))
150       return false;
151
152     // Handle users for client.
153     $sql = 'update tt_users set status = NULL'.$modified_part." where client_id = $id and group_id = $user->group_id";
154     $affected = $mdb2->exec($sql);
155     if (is_a($affected, 'PEAR_Error'))
156       return false;
157
158     // Mark client deleted.
159     $sql = "update tt_clients set status = NULL where id = $id and group_id = $user->group_id";
160     $affected = $mdb2->exec($sql);
161     return (!is_a($affected, 'PEAR_Error'));
162   }
163
164   // The insert function inserts a new client record into the clients table.
165   static function insert($fields)
166   {
167     global $user;
168     $mdb2 = getConnection();
169
170     $group_id = (int) $fields['group_id'];
171     $name = $fields['name'];
172     $address = $fields['address'];
173     $tax = $fields['tax'];
174     $projects = $fields['projects'];
175     if ($projects)
176       $comma_separated = implode(',', $projects); // This is a comma-separated list of associated projects ids.
177     $status = $fields['status'];
178
179     $tax = str_replace(',', '.', $tax);
180     if ($tax == '') $tax = 0;
181
182     $sql = "insert into tt_clients (group_id, name, address, tax, projects, status)".
183       " values ($group_id, ".$mdb2->quote($name).", ".$mdb2->quote($address).", $tax, ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
184
185     $affected = $mdb2->exec($sql);
186     if (is_a($affected, 'PEAR_Error'))
187       return false;
188
189     $last_id = 0;
190     $sql = "select last_insert_id() as last_insert_id";
191     $res = $mdb2->query($sql);
192     $val = $res->fetchRow();
193     $last_id = $val['last_insert_id'];
194
195     if (count($projects) > 0)
196       foreach ($projects as $p_id) {
197         $sql = "insert into tt_client_project_binds (client_id, project_id) values($last_id, $p_id)";
198         $affected = $mdb2->exec($sql);
199         if (is_a($affected, 'PEAR_Error'))
200           return false;
201       }
202
203     return $last_id;
204   }
205
206   // The update function updates a client record in tt_clients table.  
207   static function update($fields)
208   {
209     $mdb2 = getConnection();
210     global $user;
211
212     $id = $fields['id'];
213     $name = $fields['name'];
214     $address = $fields['address'];
215     $tax = $fields['tax'];
216     $status = $fields['status'];
217     $projects = $fields['projects'];
218
219     $tax = str_replace(',', '.', $tax);
220     if ($tax == '') $tax = 0;
221
222     // Insert client to project binds into tt_client_project_binds table.
223     $sql = "delete from tt_client_project_binds where client_id = $id";
224     $affected = $mdb2->exec($sql);
225     if (is_a($affected, 'PEAR_Error'))
226       die($affected->getMessage());
227     if (count($projects) > 0)
228       foreach ($projects as $p_id) {
229         $sql = "insert into tt_client_project_binds (client_id, project_id) values($id, $p_id)";
230         $affected = $mdb2->exec($sql);
231         if (is_a($affected, 'PEAR_Error'))
232           return false;
233       }
234
235     // Update client properties in tt_clients table.
236     $comma_separated = implode(",", $projects); // This is a comma-separated list of associated project ids.
237     $sql = "update tt_clients set name = ".$mdb2->quote($name).", address = ".$mdb2->quote($address).
238       ", tax = $tax, projects = ".$mdb2->quote($comma_separated).", status = $status where group_id = ".$user->group_id." and id = ".$id;
239     $affected = $mdb2->exec($sql);
240     return (!is_a($affected, 'PEAR_Error'));
241   }
242
243   // The setMappedClient function is used during group import to change client_id value for tt_users to a mapped value.
244   static function setMappedClient($group_id, $imported_id, $mapped_id)
245   {
246     $mdb2 = getConnection();
247     $sql = "update tt_users set client_id = $mapped_id where client_id = $imported_id and group_id = $group_id ";
248     $affected = $mdb2->exec($sql);
249     if (is_a($affected, 'PEAR_Error'))
250       return false;
251
252     return true;
253   }
254
255   // The fillBean function fills the ActionForm object with client data.
256   static function fillBean($client_id, &$bean) {
257     $client = ttClientHelper::getClient($client_id, true);
258     $bean->setAttribute('name', $client['name']);
259     $bean->setAttribute('address', $client['address']);
260     $bean->setAttribute('tax', $client['tax']);
261   }
262
263   // getAssignedProjects - returns an array of projects associatied with a client.
264   static function getAssignedProjects($client_id)
265   {
266     global $user;
267
268     $result = array();
269     $mdb2 = getConnection();
270
271     // Do a query with inner join to get assigned projects.
272     $sql = "select p.id, p.name from tt_projects p
273       inner join tt_client_project_binds cpb on (cpb.client_id = $client_id and cpb.project_id = p.id)
274       where p.group_id = $user->group_id and p.status = 1 order by p.name";
275     $res = $mdb2->query($sql);
276     if (!is_a($res, 'PEAR_Error')) {
277       while ($val = $res->fetchRow()) {
278         $result[] = $val;
279       }
280     }
281     return $result;
282   }
283
284   // getClientsForUser - returns an array of clients that are relevant to a user via assigned projects. 
285   static function getClientsForUser()
286   {
287     global $user;
288     $user_id = $user->getActiveUser();
289
290     $result = array();
291     $mdb2 = getConnection();
292
293     $sql = "select distinct c.id, c.name, c.projects from tt_user_project_binds upb
294       inner join tt_client_project_binds cpb on (cpb.project_id = upb.project_id)
295       inner join tt_clients c on (c.id = cpb.client_id and c.status = 1)
296       where upb.user_id = $user_id and upb.status = 1 order by upper(c.name)";
297
298     $res = $mdb2->query($sql);
299     if (!is_a($res, 'PEAR_Error')) {
300       while ($val = $res->fetchRow()) {
301         $result[] = $val;
302       }
303     }
304     return $result;
305   }
306
307   // getAssignedProjectsForUser - returns an array of projects assigned to a user and associatied with a client.
308   static function getAssignedProjectsForUser($client_id)
309   {
310     global $user;
311     $user_id = $user->getActiveUser();
312
313     $result = array();
314     $mdb2 = getConnection();
315
316     // Do a query with inner join to get assigned projects.
317     $sql = "select p.id, p.name from tt_projects p
318       inner join tt_client_project_binds cpb on (cpb.client_id = $client_id and cpb.project_id = p.id)
319       inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id and upb.status = 1)
320       where p.group_id = $user->group_id and p.status = 1 order by p.name";
321     $res = $mdb2->query($sql);
322     if (!is_a($res, 'PEAR_Error')) {
323       while ($val = $res->fetchRow()) {
324         $result[] = $val;
325       }
326     }
327     return $result;
328   }
329 }