As suggested in PR#33, allowed entry of decimal values with a comma for teams with...
[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 upper(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
82     $res = $mdb2->query($sql);
83     if (!is_a($res, 'PEAR_Error')) {
84       $val = $res->fetchRow();
85       if ($val['id']) {
86         return $val;
87       }
88     }
89     return false;
90   }
91
92   // The getDeletedClient looks up a deleted client by id.
93   static function getDeletedClient($client_id) {
94
95     $mdb2 = getConnection();
96     global $user;
97
98     $sql = "select name, address from tt_clients where team_id = $user->team_id
99       and id = $client_id and status is NULL";
100     $res = $mdb2->query($sql);
101     if (!is_a($res, 'PEAR_Error')) {
102       $val = $res->fetchRow();
103       return $val;
104     }
105     return false;
106   }
107
108   // The delete function marks client as deleded.
109   static function delete($id, $delete_client_entries) {
110
111     $mdb2 = getConnection();
112     global $user;
113
114     // Handle custom field log records.
115     if ($delete_client_entries) {
116       $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)";
117       $affected = $mdb2->exec($sql);
118         if (is_a($affected, 'PEAR_Error'))
119           return false;
120     }
121
122     // Handle time records.
123     if ($delete_client_entries) {
124       $sql = "update tt_log set status = NULL 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 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";
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     $sql = "update tt_clients set status = NULL where id = $id and team_id = ".$user->team_id;
153     $affected = $mdb2->exec($sql);
154     return (!is_a($affected, 'PEAR_Error'));
155   }
156
157   // The insert function inserts a new client record into the clients table.
158   static function insert($fields)
159   {
160     global $user;
161     $mdb2 = getConnection();
162
163     $team_id = (int) $fields['team_id'];
164     $name = $fields['name'];
165     $address = $fields['address'];
166     $tax = $fields['tax'];
167     $projects = $fields['projects'];
168     if ($projects)
169       $comma_separated = implode(',', $projects); // This is a comma-separated list of associated projects ids.
170     $status = $fields['status'];
171
172     $tax = str_replace(',', '.', $tax);
173     if ($tax == '') $tax = 0;
174
175     $sql = "insert into tt_clients (team_id, name, address, tax, projects, status) 
176       values ($team_id, ".$mdb2->quote($name).", ".$mdb2->quote($address).", $tax, ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
177
178     $affected = $mdb2->exec($sql);
179     if (is_a($affected, 'PEAR_Error'))
180       return false;
181
182     $last_id = 0;
183     $sql = "select last_insert_id() as last_insert_id";
184     $res = $mdb2->query($sql);
185     $val = $res->fetchRow();
186     $last_id = $val['last_insert_id'];
187
188     if (count($projects) > 0)
189       foreach ($projects as $p_id) {
190         $sql = "insert into tt_client_project_binds (client_id, project_id) values($last_id, $p_id)";
191         $affected = $mdb2->exec($sql);
192         if (is_a($affected, 'PEAR_Error'))
193           return false;
194       }
195
196     return $last_id;
197   }
198
199   // The update function updates a client record in tt_clients table.  
200   static function update($fields)
201   {
202     $mdb2 = getConnection();
203     global $user;
204
205     $id = $fields['id'];
206     $name = $fields['name'];
207     $address = $fields['address'];
208     $tax = $fields['tax'];
209     $status = $fields['status'];
210     $projects = $fields['projects'];
211
212     $tax = str_replace(',', '.', $tax);
213     if ($tax == '') $tax = 0;
214
215     // Insert client to project binds into tt_client_project_binds table.
216     $sql = "delete from tt_client_project_binds where client_id = $id";
217     $affected = $mdb2->exec($sql);
218     if (is_a($affected, 'PEAR_Error'))
219       die($affected->getMessage());
220     if (count($projects) > 0)
221       foreach ($projects as $p_id) {
222         $sql = "insert into tt_client_project_binds (client_id, project_id) values($id, $p_id)";
223         $affected = $mdb2->exec($sql);
224         if (is_a($affected, 'PEAR_Error'))
225           return false;
226       }
227
228     // Update client properties in tt_clients table.
229     $comma_separated = implode(",", $projects); // This is a comma-separated list of associated project ids.
230     $sql = "update tt_clients set name = ".$mdb2->quote($name).", address = ".$mdb2->quote($address).
231       ", tax = $tax, projects = ".$mdb2->quote($comma_separated).", status = $status where team_id = ".$user->team_id." and id = ".$id;
232     $affected = $mdb2->exec($sql);
233     return (!is_a($affected, 'PEAR_Error'));
234   }
235
236   // The setMappedClient function is used during team import to change client_id value for tt_users to a mapped value.
237   static function setMappedClient($team_id, $imported_id, $mapped_id)
238   {
239     $mdb2 = getConnection();
240     $sql = "update tt_users set client_id = $mapped_id where client_id = $imported_id and team_id = $team_id ";
241     $affected = $mdb2->exec($sql);
242     if (is_a($affected, 'PEAR_Error'))
243       return false;
244
245     return true;
246   }
247
248   // The fillBean function fills the ActionForm object with client data.
249   static function fillBean($client_id, &$bean) {
250     $client = ttClientHelper::getClient($client_id, true);
251     $bean->setAttribute('name', $client['name']);
252     $bean->setAttribute('address', $client['address']);
253     $bean->setAttribute('tax', $client['tax']);
254   }
255
256   // getAssignedProjects - returns an array of projects associatied with a client.
257   static function getAssignedProjects($client_id)
258   {
259     global $user;
260
261     $result = array();
262     $mdb2 = getConnection();
263
264     // Do a query with inner join to get assigned projects.
265     $sql = "select p.id, p.name from tt_projects p
266       inner join tt_client_project_binds cpb on (cpb.client_id = $client_id and cpb.project_id = p.id)
267       where p.team_id = $user->team_id and p.status = 1 order by p.name";
268     $res = $mdb2->query($sql);
269     if (!is_a($res, 'PEAR_Error')) {
270       while ($val = $res->fetchRow()) {
271         $result[] = $val;
272       }
273     }
274     return $result;
275   }
276
277   // getClientsForUser - returns an array of clients that are relevant to a user via assigned projects. 
278   static function getClientsForUser()
279   {
280     global $user;
281     $user_id = $user->getActiveUser();
282
283     $result = array();
284     $mdb2 = getConnection();
285
286     $sql = "select distinct c.id, c.name, c.projects from tt_user_project_binds upb
287       inner join tt_client_project_binds cpb on (cpb.project_id = upb.project_id)
288       inner join tt_clients c on (c.id = cpb.client_id and c.status = 1)
289       where upb.user_id = $user_id and upb.status = 1 order by upper(c.name)";
290
291     $res = $mdb2->query($sql);
292     if (!is_a($res, 'PEAR_Error')) {
293       while ($val = $res->fetchRow()) {
294         $result[] = $val;
295       }
296     }
297     return $result;
298   }
299
300   // getAssignedProjectsForUser - returns an array of projects assigned to a user and associatied with a client.
301   static function getAssignedProjectsForUser($client_id)
302   {
303     global $user;
304     $user_id = $user->getActiveUser();
305
306     $result = array();
307     $mdb2 = getConnection();
308
309     // Do a query with inner join to get assigned projects.
310     $sql = "select p.id, p.name from tt_projects p
311       inner join tt_client_project_binds cpb on (cpb.client_id = $client_id and cpb.project_id = p.id)
312       inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id and upb.status = 1)
313       where p.team_id = $user->team_id and p.status = 1 order by p.name";
314     $res = $mdb2->query($sql);
315     if (!is_a($res, 'PEAR_Error')) {
316       while ($val = $res->fetchRow()) {
317         $result[] = $val;
318       }
319     }
320     return $result;
321   }
322 }