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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 // Class ttClientHelper is used to help with client related tasks.
30 class ttClientHelper {
32 // The getClient looks up a client by id.
33 static function getClient($client_id, $all_fields = false) {
35 $mdb2 = getConnection();
44 $sql .= "from tt_clients where group_id = $user->group_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();
54 // getClients - returns an array of active and inactive clients in a group.
55 static function getClients()
60 $mdb2 = getConnection();
62 $sql = "select id, name from tt_clients
63 where group_id = $user->group_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()) {
73 // The getClientByName looks up a client by name.
74 static function getClientByName($client_name) {
76 $mdb2 = getConnection();
79 $sql = "select id from tt_clients where group_id = $user->group_id and name = ".
80 $mdb2->quote($client_name)." and (status = 1 or status = 0)";
82 $res = $mdb2->query($sql);
83 if (!is_a($res, 'PEAR_Error')) {
84 $val = $res->fetchRow();
92 // The getDeletedClient looks up a deleted client by id.
93 static function getDeletedClient($client_id) {
95 $mdb2 = getConnection();
98 $sql = "select name, address from tt_clients where group_id = $user->group_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();
108 // The delete function marks client as deleded.
109 static function delete($id, $delete_client_entries) {
111 $mdb2 = getConnection();
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'))
122 // Handle time records.
123 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$mdb2->quote($user->id);
124 if ($delete_client_entries) {
125 $sql = 'update tt_log set status = NULL'.$modified_part." where client_id = $id";
126 $affected = $mdb2->exec($sql);
127 if (is_a($affected, 'PEAR_Error'))
131 // Handle expense items.
132 if ($delete_client_entries) {
133 $sql = 'update tt_expense_items set status = NULL'.$modified_part." where client_id = $id";
134 $affected = $mdb2->exec($sql);
135 if (is_a($affected, 'PEAR_Error'))
140 if ($delete_client_entries) {
141 $sql = "update tt_invoices set status = NULL where client_id = $id and group_id = $user->group_id";
142 $affected = $mdb2->exec($sql);
143 if (is_a($affected, 'PEAR_Error'))
147 // Delete project binds to this client.
148 $sql = "delete from tt_client_project_binds where client_id = $id";
149 $affected = $mdb2->exec($sql);
150 if (is_a($affected, 'PEAR_Error'))
153 // Handle users for client.
154 $sql = 'update tt_users set status = NULL'.$modified_part." where client_id = $id and group_id = $user->group_id";
155 $affected = $mdb2->exec($sql);
156 if (is_a($affected, 'PEAR_Error'))
159 // Mark client deleted.
160 $sql = "update tt_clients set status = NULL where id = $id and group_id = $user->group_id";
161 $affected = $mdb2->exec($sql);
162 return (!is_a($affected, 'PEAR_Error'));
165 // The insert function inserts a new client record into the clients table.
166 static function insert($fields)
169 $mdb2 = getConnection();
171 $group_id = (int) $fields['group_id'];
172 $name = $fields['name'];
173 $address = $fields['address'];
174 $tax = $fields['tax'];
175 $projects = $fields['projects'];
177 $comma_separated = implode(',', $projects); // This is a comma-separated list of associated projects ids.
178 $status = $fields['status'];
180 $tax = str_replace(',', '.', $tax);
181 if ($tax == '') $tax = 0;
183 $sql = "insert into tt_clients (group_id, name, address, tax, projects, status)".
184 " values ($group_id, ".$mdb2->quote($name).", ".$mdb2->quote($address).", $tax, ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
186 $affected = $mdb2->exec($sql);
187 if (is_a($affected, 'PEAR_Error'))
191 $sql = "select last_insert_id() as last_insert_id";
192 $res = $mdb2->query($sql);
193 $val = $res->fetchRow();
194 $last_id = $val['last_insert_id'];
196 if (count($projects) > 0)
197 foreach ($projects as $p_id) {
198 $sql = "insert into tt_client_project_binds (client_id, project_id) values($last_id, $p_id)";
199 $affected = $mdb2->exec($sql);
200 if (is_a($affected, 'PEAR_Error'))
207 // The update function updates a client record in tt_clients table.
208 static function update($fields)
210 $mdb2 = getConnection();
214 $name = $fields['name'];
215 $address = $fields['address'];
216 $tax = $fields['tax'];
217 $status = $fields['status'];
218 $projects = $fields['projects'];
220 $tax = str_replace(',', '.', $tax);
221 if ($tax == '') $tax = 0;
223 // Insert client to project binds into tt_client_project_binds table.
224 $sql = "delete from tt_client_project_binds where client_id = $id";
225 $affected = $mdb2->exec($sql);
226 if (is_a($affected, 'PEAR_Error'))
227 die($affected->getMessage());
228 if (count($projects) > 0)
229 foreach ($projects as $p_id) {
230 $sql = "insert into tt_client_project_binds (client_id, project_id) values($id, $p_id)";
231 $affected = $mdb2->exec($sql);
232 if (is_a($affected, 'PEAR_Error'))
236 // Update client properties in tt_clients table.
237 $comma_separated = implode(",", $projects); // This is a comma-separated list of associated project ids.
238 $sql = "update tt_clients set name = ".$mdb2->quote($name).", address = ".$mdb2->quote($address).
239 ", tax = $tax, projects = ".$mdb2->quote($comma_separated).", status = $status where group_id = ".$user->group_id." and id = ".$id;
240 $affected = $mdb2->exec($sql);
241 return (!is_a($affected, 'PEAR_Error'));
244 // The setMappedClient function is used during group import to change client_id value for tt_users to a mapped value.
245 static function setMappedClient($group_id, $imported_id, $mapped_id)
247 $mdb2 = getConnection();
248 $sql = "update tt_users set client_id = $mapped_id where client_id = $imported_id and group_id = $group_id ";
249 $affected = $mdb2->exec($sql);
250 if (is_a($affected, 'PEAR_Error'))
256 // The fillBean function fills the ActionForm object with client data.
257 static function fillBean($client_id, &$bean) {
258 $client = ttClientHelper::getClient($client_id, true);
259 $bean->setAttribute('name', $client['name']);
260 $bean->setAttribute('address', $client['address']);
261 $bean->setAttribute('tax', $client['tax']);
264 // getAssignedProjects - returns an array of projects associatied with a client.
265 static function getAssignedProjects($client_id)
270 $mdb2 = getConnection();
272 // Do a query with inner join to get assigned projects.
273 $sql = "select p.id, p.name from tt_projects p
274 inner join tt_client_project_binds cpb on (cpb.client_id = $client_id and cpb.project_id = p.id)
275 where p.group_id = $user->group_id and p.status = 1 order by p.name";
276 $res = $mdb2->query($sql);
277 if (!is_a($res, 'PEAR_Error')) {
278 while ($val = $res->fetchRow()) {
285 // getClientsForUser - returns an array of clients that are relevant to a user via assigned projects.
286 static function getClientsForUser()
289 $user_id = $user->getActiveUser();
292 $mdb2 = getConnection();
294 $sql = "select distinct c.id, c.name, c.projects from tt_user_project_binds upb
295 inner join tt_client_project_binds cpb on (cpb.project_id = upb.project_id)
296 inner join tt_clients c on (c.id = cpb.client_id and c.status = 1)
297 where upb.user_id = $user_id and upb.status = 1 order by upper(c.name)";
299 $res = $mdb2->query($sql);
300 if (!is_a($res, 'PEAR_Error')) {
301 while ($val = $res->fetchRow()) {
308 // getAssignedProjectsForUser - returns an array of projects assigned to a user and associatied with a client.
309 static function getAssignedProjectsForUser($client_id)
312 $user_id = $user->getActiveUser();
315 $mdb2 = getConnection();
317 // Do a query with inner join to get assigned projects.
318 $sql = "select p.id, p.name from tt_projects p
319 inner join tt_client_project_binds cpb on (cpb.client_id = $client_id and cpb.project_id = p.id)
320 inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id and upb.status = 1)
321 where p.group_id = $user->group_id and p.status = 1 order by p.name";
322 $res = $mdb2->query($sql);
323 if (!is_a($res, 'PEAR_Error')) {
324 while ($val = $res->fetchRow()) {