Merges an updated upstream from main.
[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 team_id = $user->team_id
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 team.
55   static function getClients()
56   {
57         global $user;
58                 
59         $result = array();
60     $mdb2 = getConnection();
61     
62     $sql = "select id, name from tt_clients
63       where team_id = $user->team_id and (status = 0 or status = 1) order by name";     
64     $res = $mdb2->query($sql);
65     if (!is_a($res, 'PEAR_Error')) {
66       while ($val = $res->fetchRow()) {
67         $result[] = $val;
68       }
69     }
70     return $result;
71   }
72         
73   // The getClientByName looks up a client by name.
74   static function getClientByName($client_name) {
75         
76     $mdb2 = getConnection();
77     global $user;
78
79     $sql = "select id from tt_clients where team_id = $user->team_id and name = ".
80       $mdb2->quote($client_name)." and (status = 1 or status = 0)";
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 team_id = $user->team_id
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     if ($delete_client_entries) {
123       $sql = "update tt_log set status = NULL where client_id = $id";
124       $affected = $mdb2->exec($sql);
125       if (is_a($affected, 'PEAR_Error'))
126             return false;
127     }
128     
129     // Handle expense items.
130     if ($delete_client_entries) {         
131       $sql = "update tt_expense_items set status = NULL where client_id = $id";
132       $affected = $mdb2->exec($sql);
133       if (is_a($affected, 'PEAR_Error'))
134         return false;
135     }
136     
137     // Handle invoices.
138     if ($delete_client_entries) {         
139       $sql = "update tt_invoices set status = NULL where client_id = $id";
140       $affected = $mdb2->exec($sql);
141       if (is_a($affected, 'PEAR_Error'))
142         return false;
143     }
144  
145         // Delete project binds to this client.
146     $sql = "delete from tt_client_project_binds where client_id = $id";
147     $affected = $mdb2->exec($sql);
148     if (is_a($affected, 'PEAR_Error'))
149       return false;
150         
151         $sql = "update tt_clients set status = NULL where id = $id and team_id = ".$user->team_id;
152     $affected = $mdb2->exec($sql);
153     return (!is_a($affected, 'PEAR_Error'));
154   }
155   
156   // The insert function inserts a new client record into the clients table.
157   static function insert($fields)
158   {
159         global $user;
160     $mdb2 = getConnection();
161     
162     $team_id = (int) $fields['team_id'];
163     $name = $fields['name'];
164     $address = $fields['address'];
165     $tax = $fields['tax'];
166     $projects = $fields['projects'];
167     if ($projects)
168       $comma_separated = implode(',', $projects); // This is a comma-separated list of associated projects ids.
169     $status = $fields['status'];
170
171     $tax = str_replace(',', '.', $tax);
172     if ($tax == '') $tax = 0;
173
174     $sql = "insert into tt_clients (team_id, name, address, tax, projects, status) 
175       values ($team_id, ".$mdb2->quote($name).", ".$mdb2->quote($address).", $tax, ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
176       
177     $affected = $mdb2->exec($sql);
178     if (is_a($affected, 'PEAR_Error'))
179       return false;
180       
181     $last_id = 0;
182     $sql = "select last_insert_id() as last_insert_id";
183     $res = $mdb2->query($sql);
184     $val = $res->fetchRow();
185     $last_id = $val['last_insert_id'];
186       
187     if (count($projects) > 0)
188       foreach ($projects as $p_id) {
189         $sql = "insert into tt_client_project_binds (client_id, project_id) values($last_id, $p_id)";
190         $affected = $mdb2->exec($sql);
191         if (is_a($affected, 'PEAR_Error'))
192           return false;
193       }
194
195     return $last_id;
196   }
197   
198   // The update function updates a client record in tt_clients table.  
199   static function update($fields)
200   {
201     $mdb2 = getConnection();
202     global $user;
203
204     $id = $fields['id'];
205     $name = $fields['name'];
206     $address = $fields['address'];
207     $tax = $fields['tax'];
208     $status = $fields['status'];
209     $projects = $fields['projects'];
210     
211     $tax = str_replace(',', '.', $tax);
212         if ($tax == '') $tax = 0;
213
214     // Insert client to project binds into tt_client_project_binds table.
215     $sql = "delete from tt_client_project_binds where client_id = $id";
216     $affected = $mdb2->exec($sql);
217     if (is_a($affected, 'PEAR_Error'))
218       die($affected->getMessage());
219     if (count($projects) > 0)
220       foreach ($projects as $p_id) {
221         $sql = "insert into tt_client_project_binds (client_id, project_id) values($id, $p_id)";
222         $affected = $mdb2->exec($sql);
223         if (is_a($affected, 'PEAR_Error'))
224           return false;
225       }
226
227     // Update client properties in tt_clients table.
228     $comma_separated = implode(",", $projects); // This is a comma-separated list of associated project ids.
229     $sql = "update tt_clients set name = ".$mdb2->quote($name).", address = ".$mdb2->quote($address).
230       ", tax = $tax, projects = ".$mdb2->quote($comma_separated).", status = $status where team_id = ".$user->team_id." and id = ".$id;
231     $affected = $mdb2->exec($sql);
232     return (!is_a($affected, 'PEAR_Error'));
233   }
234   
235   // The setMappedClient function is used during team import to change client_id value for tt_users to a mapped value.
236   static function setMappedClient($team_id, $imported_id, $mapped_id)
237   {
238     $mdb2 = getConnection();
239     $sql = "update tt_users set client_id = $mapped_id where client_id = $imported_id and team_id = $team_id ";
240     $affected = $mdb2->exec($sql);
241     if (is_a($affected, 'PEAR_Error'))
242       return false;
243       
244     return true;
245   }
246   
247   // The fillBean function fills the ActionForm object with client data.
248   static function fillBean($client_id, &$bean) {
249         $client = ttClientHelper::getClient($client_id, true);
250     $bean->setAttribute('name', $client['name']);
251     $bean->setAttribute('address', $client['address']);
252     $bean->setAttribute('tax', $client['tax']);
253   }
254   
255   // getAssignedProjects - returns an array of projects associatied with a client.
256   static function getAssignedProjects($client_id)
257   {
258         global $user;
259         
260     $result = array();
261     $mdb2 = getConnection();
262     
263     // Do a query with inner join to get assigned projects.
264     $sql = "select p.id, p.name from tt_projects p
265       inner join tt_client_project_binds cpb on (cpb.client_id = $client_id and cpb.project_id = p.id)
266       where p.team_id = $user->team_id and p.status = 1 order by p.name";
267     $res = $mdb2->query($sql);
268     if (!is_a($res, 'PEAR_Error')) {
269       while ($val = $res->fetchRow()) {
270         $result[] = $val;
271       }
272     }
273     return $result;
274   }
275   
276   // getClientsForUser - returns an array of clients that are relevant to a user via assigned projects. 
277   static function getClientsForUser($withProjects = false)
278   {
279         global $user;
280         $user_id = $user->getActiveUser();
281         
282         $result = array();
283         $mdb2 = getConnection();
284         
285     $sql = "select distinct c.id, c.name, c.projects from tt_user_project_binds upb
286       inner join tt_client_project_binds cpb on (cpb.project_id = upb.project_id)
287       inner join tt_clients c on (c.id = cpb.client_id and c.status = 1)
288       where upb.user_id = $user_id and upb.status = 1 order by c.name";
289     
290         $res = $mdb2->query($sql);
291         if (!is_a($res, 'PEAR_Error')) {
292       while ($val = $res->fetchRow()) {
293         // if ($withProjects) {
294          // $projects = ttClientHelper::getAssignedProjectsForUser($val['id']);
295               //$project_ids = array();
296               //foreach ($projects as $project_item)
297                 //$project_ids[] = $project_item[id];
298               //$val['projects'] = implode(',', $project_ids);
299         //}
300         $result[] = $val;
301       }
302     }
303     return $result;
304   }
305   
306   // getAssignedProjectsForUser - returns an array of projects assigned to a user and associatied with a client.
307   static function getAssignedProjectsForUser($client_id)
308   {
309         global $user;
310         $user_id = $user->getActiveUser();
311         
312         $result = array();
313     $mdb2 = getConnection();
314     
315     // Do a query with inner join to get assigned projects.
316     $sql = "select p.id, p.name from tt_projects p
317       inner join tt_client_project_binds cpb on (cpb.client_id = $client_id and cpb.project_id = p.id)
318       inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id and upb.status = 1)
319       where p.team_id = $user->team_id and p.status = 1 order by p.name";
320     $res = $mdb2->query($sql);
321     if (!is_a($res, 'PEAR_Error')) {
322       while ($val = $res->fetchRow()) {
323         $result[] = $val;
324       }
325     }
326     return $result;
327   }
328 }