3dc60bdb7122c3defb8d15093707a935c9e28eca
[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 = '.$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 = $mdb2->lastInsertID('tt_clients', 'id');
193     if (count($projects) > 0)
194       foreach ($projects as $p_id) {
195         $sql = "insert into tt_client_project_binds (client_id, project_id, group_id, org_id) values($last_id, $p_id, $group_id, $org_id)";
196         $affected = $mdb2->exec($sql);
197         if (is_a($affected, 'PEAR_Error'))
198           return false;
199       }
200
201     return $last_id;
202   }
203
204   // The update function updates a client record in tt_clients table.  
205   static function update($fields)
206   {
207     $mdb2 = getConnection();
208     global $user;
209
210     $id = $fields['id'];
211     $name = $fields['name'];
212     $address = $fields['address'];
213     $tax = $fields['tax'];
214     $status = $fields['status'];
215     $projects = $fields['projects'];
216
217     $tax = str_replace(',', '.', $tax);
218     if ($tax == '') $tax = 0;
219
220     // Insert client to project binds into tt_client_project_binds table.
221     $sql = "delete from tt_client_project_binds where client_id = $id";
222     $affected = $mdb2->exec($sql);
223     if (is_a($affected, 'PEAR_Error'))
224       die($affected->getMessage());
225     if (count($projects) > 0)
226       foreach ($projects as $p_id) {
227         $sql = "insert into tt_client_project_binds (client_id, project_id) values($id, $p_id)";
228         $affected = $mdb2->exec($sql);
229         if (is_a($affected, 'PEAR_Error'))
230           return false;
231       }
232
233     // Update client properties in tt_clients table.
234     $comma_separated = implode(",", $projects); // This is a comma-separated list of associated project ids.
235     $sql = "update tt_clients set name = ".$mdb2->quote($name).", address = ".$mdb2->quote($address).
236       ", tax = $tax, projects = ".$mdb2->quote($comma_separated).", status = $status".
237       " where group_id = ".$user->getGroup()." and id = ".$id;
238     $affected = $mdb2->exec($sql);
239     return (!is_a($affected, 'PEAR_Error'));
240   }
241
242   // The setMappedClient function is used during group import to change client_id value for tt_users to a mapped value.
243   static function setMappedClient($group_id, $imported_id, $mapped_id)
244   {
245     $mdb2 = getConnection();
246     $sql = "update tt_users set client_id = $mapped_id where client_id = $imported_id and group_id = $group_id ";
247     $affected = $mdb2->exec($sql);
248     if (is_a($affected, 'PEAR_Error'))
249       return false;
250
251     return true;
252   }
253
254   // The fillBean function fills the ActionForm object with client data.
255   static function fillBean($client_id, &$bean) {
256     $client = ttClientHelper::getClient($client_id, true);
257     $bean->setAttribute('name', $client['name']);
258     $bean->setAttribute('address', $client['address']);
259     $bean->setAttribute('tax', $client['tax']);
260   }
261
262   // getAssignedProjects - returns an array of projects associatied with a client.
263   static function getAssignedProjects($client_id)
264   {
265     global $user;
266
267     $result = array();
268     $mdb2 = getConnection();
269
270     // Do a query with inner join to get assigned projects.
271     $sql = "select p.id, p.name from tt_projects p".
272       " inner join tt_client_project_binds cpb on (cpb.client_id = $client_id and cpb.project_id = p.id)".
273       " where p.group_id = ".$user->getGroup()." and p.status = 1 order by p.name";
274     $res = $mdb2->query($sql);
275     if (!is_a($res, 'PEAR_Error')) {
276       while ($val = $res->fetchRow()) {
277         $result[] = $val;
278       }
279     }
280     return $result;
281   }
282
283   // getClientsForUser - returns an array of clients that are relevant to a user via assigned projects. 
284   static function getClientsForUser()
285   {
286     global $user;
287     $user_id = $user->getUser();
288
289     $result = array();
290     $mdb2 = getConnection();
291
292     $sql = "select distinct c.id, c.name, c.projects from tt_user_project_binds upb
293       inner join tt_client_project_binds cpb on (cpb.project_id = upb.project_id)
294       inner join tt_clients c on (c.id = cpb.client_id and c.status = 1)
295       where upb.user_id = $user_id and upb.status = 1 order by upper(c.name)";
296
297     $res = $mdb2->query($sql);
298     if (!is_a($res, 'PEAR_Error')) {
299       while ($val = $res->fetchRow()) {
300         $result[] = $val;
301       }
302     }
303     return $result;
304   }
305 }