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();
37 $group_id = $user->getGroup();
38 $org_id = $user->org_id;
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();
56 // getClients - returns an array of active and inactive clients in a group.
57 static function getClients()
62 $mdb2 = getConnection();
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()) {
74 // The getClientByName looks up a client by name.
75 static function getClientByName($client_name) {
77 $mdb2 = getConnection();
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)";
83 $res = $mdb2->query($sql);
84 if (!is_a($res, 'PEAR_Error')) {
85 $val = $res->fetchRow();
93 // The getDeletedClient looks up a deleted client by id.
94 static function getDeletedClient($client_id) {
96 $mdb2 = getConnection();
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();
109 // The delete function marks client as deleded.
110 static function delete($id, $delete_client_entries) {
112 $mdb2 = getConnection();
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'))
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'))
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'))
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'))
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'))
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'))
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'));
166 // The insert function inserts a new client record into the clients table.
167 static function insert($fields)
170 $mdb2 = getConnection();
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'];
179 $comma_separated = implode(',', $projects); // This is a comma-separated list of associated projects ids.
180 $status = $fields['status'];
182 $tax = str_replace(',', '.', $tax);
183 if ($tax == '') $tax = 0;
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).")";
188 $affected = $mdb2->exec($sql);
189 if (is_a($affected, 'PEAR_Error'))
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'))
204 // The update function updates a client record in tt_clients table.
205 static function update($fields)
207 $mdb2 = getConnection();
211 $name = $fields['name'];
212 $address = $fields['address'];
213 $tax = $fields['tax'];
214 $status = $fields['status'];
215 $projects = $fields['projects'];
217 $tax = str_replace(',', '.', $tax);
218 if ($tax == '') $tax = 0;
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'))
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'));
242 // The fillBean function fills the ActionForm object with client data.
243 static function fillBean($client_id, &$bean) {
244 $client = ttClientHelper::getClient($client_id, true);
245 $bean->setAttribute('name', $client['name']);
246 $bean->setAttribute('address', $client['address']);
247 $bean->setAttribute('tax', $client['tax']);
250 // getAssignedProjects - returns an array of projects associatied with a client.
251 static function getAssignedProjects($client_id)
256 $mdb2 = getConnection();
258 // Do a query with inner join to get assigned projects.
259 $sql = "select p.id, p.name from tt_projects p".
260 " inner join tt_client_project_binds cpb on (cpb.client_id = $client_id and cpb.project_id = p.id)".
261 " where p.group_id = ".$user->getGroup()." and p.status = 1 order by p.name";
262 $res = $mdb2->query($sql);
263 if (!is_a($res, 'PEAR_Error')) {
264 while ($val = $res->fetchRow()) {
271 // getClientsForUser - returns an array of clients that are relevant to a user via assigned projects.
272 static function getClientsForUser()
275 $user_id = $user->getUser();
278 $mdb2 = getConnection();
280 $sql = "select distinct c.id, c.name, c.projects from tt_user_project_binds upb
281 inner join tt_client_project_binds cpb on (cpb.project_id = upb.project_id)
282 inner join tt_clients c on (c.id = cpb.client_id and c.status = 1)
283 where upb.user_id = $user_id and upb.status = 1 order by upper(c.name)";
285 $res = $mdb2->query($sql);
286 if (!is_a($res, 'PEAR_Error')) {
287 while ($val = $res->fetchRow()) {