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 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();
54 // getClients - returns an array of active and inactive clients in a team.
55 static function getClients()
60 $mdb2 = getConnection();
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()) {
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 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();
91 // The getDeletedClient looks up a deleted client by id.
92 static function getDeletedClient($client_id) {
94 $mdb2 = getConnection();
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();
107 // The delete function marks client as deleded.
108 static function delete($id, $delete_client_entries) {
110 $mdb2 = getConnection();
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'))
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'))
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'))
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'))
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'))
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'));
156 // The insert function inserts a new client record into the clients table.
157 static function insert($fields)
160 $mdb2 = getConnection();
162 $team_id = (int) $fields['team_id'];
163 $name = $fields['name'];
164 $address = $fields['address'];
165 $tax = $fields['tax'];
166 $projects = $fields['projects'];
168 $comma_separated = implode(',', $projects); // This is a comma-separated list of associated projects ids.
169 $status = $fields['status'];
171 $tax = str_replace(',', '.', $tax);
172 if ($tax == '') $tax = 0;
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).")";
177 $affected = $mdb2->exec($sql);
178 if (is_a($affected, 'PEAR_Error'))
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'];
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'))
198 // The update function updates a client record in tt_clients table.
199 static function update($fields)
201 $mdb2 = getConnection();
205 $name = $fields['name'];
206 $address = $fields['address'];
207 $tax = $fields['tax'];
208 $status = $fields['status'];
209 $projects = $fields['projects'];
211 $tax = str_replace(',', '.', $tax);
212 if ($tax == '') $tax = 0;
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'))
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'));
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)
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'))
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']);
255 // getAssignedProjects - returns an array of projects associatied with a client.
256 static function getAssignedProjects($client_id)
261 $mdb2 = getConnection();
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()) {
276 // getClientsForUser - returns an array of clients that are relevant to a user via assigned projects.
277 static function getClientsForUser($withProjects = false)
280 $user_id = $user->getActiveUser();
283 $mdb2 = getConnection();
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";
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);
306 // getAssignedProjectsForUser - returns an array of projects assigned to a user and associatied with a client.
307 static function getAssignedProjectsForUser($client_id)
310 $user_id = $user->getActiveUser();
313 $mdb2 = getConnection();
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()) {