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 import('ttUserHelper');
31 // Class ttTimesheetHelper is used to help with project related tasks.
32 class ttTimesheetHelper {
34 // The getTimesheetByName looks up a project by name.
35 static function getTimesheetByName($name) {
37 $mdb2 = getConnection();
39 $user_id = $user->getUser();
40 $group_id = $user->getGroup();
41 $org_id = $user->org_id;
43 $sql = "select id from tt_timesheets".
44 " where group_id = $group_id and org_id = $org_id and user_id = $user_id and name = ".$mdb2->quote($name).
45 " and status is not null";
46 $res = $mdb2->query($sql);
47 if (!is_a($res, 'PEAR_Error')) {
48 $val = $res->fetchRow();
49 if ($val && $val['id'])
55 // createTimesheet function creates a new timesheet.
56 static function createTimesheet($fields)
58 // Create a new timesheet entry.
60 $mdb2 = getConnection();
62 $user_id = $user->getUser();
63 $group_id = $user->getGroup();
64 $org_id = $user->org_id;
66 $client_id = $fields['client_id'];
67 $project_id = $fields['project_id'];
68 $name = $fields['name'];
69 $comment = $fields['comment'];
71 $start_date = new DateAndTime($user->date_format, $fields['start_date']);
72 $start = $start_date->toString(DB_DATEFORMAT);
74 $end_date = new DateAndTime($user->date_format, $fields['end_date']);
75 $end = $end_date->toString(DB_DATEFORMAT);
77 $sql = "insert into tt_timesheets (user_id, group_id, org_id, client_id, project_id, name, comment, start_date, end_date)".
78 " values ($user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($project_id).", ".$mdb2->quote($name).
79 ", ".$mdb2->quote($comment).", ".$mdb2->quote($start).", ".$mdb2->quote($end).")";
80 $affected = $mdb2->exec($sql);
81 if (is_a($affected, 'PEAR_Error'))
84 $last_id = $mdb2->lastInsertID('tt_timesheets', 'id');
86 // Associate time items with timesheet.
87 if (isset($fields['client'])) $client_id = (int) $fields['client_id'];
88 if (isset($fields['project_id'])) $project_id = (int) $fields['project_id'];
90 if ($client_id) $client_part = " and client_id = $client_id";
91 if ($project_id) $project_part = " and project_id = $project_id";
93 $sql = "update tt_log set timesheet_id = $last_id".
94 " where status = 1 $client_part $project_part and timesheet_id is null".
95 " and date >= ".$mdb2->quote($start)." and date <= ".$mdb2->quote($end).
96 " and user_id = $user_id and group_id = $group_id and org_id = $org_id";
97 $affected = $mdb2->exec($sql);
98 if (is_a($affected, 'PEAR_Error'))
104 // The getActiveTimesheets obtains active timesheets for a user.
105 static function getActiveTimesheets($user_id)
108 $mdb2 = getConnection();
110 $group_id = $user->getGroup();
111 $org_id = $user->org_id;
113 // $addPaidStatus = $user->isPluginEnabled('ps');
116 if ($user->isClient())
117 $client_part = "and ts.client_id = $user->client_id";
119 $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name, ts.submit_status, ts.approve_status from tt_timesheets ts".
120 " left join tt_clients c on (c.id = ts.client_id)".
121 " where ts.status = 1 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
122 " $client_part order by ts.name";
123 $res = $mdb2->query($sql);
125 if (!is_a($res, 'PEAR_Error')) {
126 $dt = new DateAndTime(DB_DATEFORMAT);
127 while ($val = $res->fetchRow()) {
128 //if ($addPaidStatus)
129 // $val['paid'] = ttTimesheetHelper::isPaid($val['id']);
136 // The getInactiveTimesheets obtains inactive timesheets for a user.
137 static function getInactiveTimesheets($user_id)
140 $mdb2 = getConnection();
142 $group_id = $user->getGroup();
143 $org_id = $user->org_id;
145 // $addPaidStatus = $user->isPluginEnabled('ps');
148 if ($user->isClient())
149 $client_part = "and ts.client_id = $user->client_id";
151 $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name, ts.submit_status, ts.approve_status from tt_timesheets ts".
152 " left join tt_clients c on (c.id = ts.client_id)".
153 " where ts.status = 0 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
154 " $client_part order by ts.name";
155 $res = $mdb2->query($sql);
157 if (!is_a($res, 'PEAR_Error')) {
158 $dt = new DateAndTime(DB_DATEFORMAT);
159 while ($val = $res->fetchRow()) {
160 //if ($addPaidStatus)
161 // $val['paid'] = ttTimesheetHelper::isPaid($val['id']);
168 // getTimesheet - obtains timesheet data from the database.
169 static function getTimesheet($timesheet_id) {
171 $mdb2 = getConnection();
173 $user_id = $user->getUser();
174 $group_id = $user->getGroup();
175 $org_id = $user->org_id;
177 $sql = "select ts.*, u.name as user_name, c.name as client_name,".
178 " p.name as project_name from tt_timesheets ts".
179 " left join tt_users u on (ts.user_id = u.id)".
180 " left join tt_clients c on (ts.client_id = c.id)".
181 " left join tt_projects p on (ts.project_id = p.id)".
182 " where ts.id = $timesheet_id and ts.user_id = $user_id and ts.group_id = $group_id and ts.org_id = $org_id and ts.status is not null";
183 $res = $mdb2->query($sql);
184 if (!is_a($res, 'PEAR_Error')) {
185 if ($val = $res->fetchRow())
191 // delete - deletes timesheet from the database.
192 static function delete($timesheet_id) {
194 $mdb2 = getConnection();
196 $group_id = $user->getGroup();
197 $org_id = $user->org_id;
199 // Handle time records.
200 $sql = "update tt_log set timesheet_id = null".
201 " where timesheet_id = $timesheet_id and group_id = $group_id and org_id = $org_id";
202 $affected = $mdb2->exec($sql);
203 if (is_a($affected, 'PEAR_Error')) return false;
205 // Handle expense items.
206 $sql = "update tt_expense_items set timesheet_id = null".
207 " where timesheet_id = $timesheet_id and group_id = $group_id and org_id = $org_id";
208 $affected = $mdb2->exec($sql);
209 if (is_a($affected, 'PEAR_Error')) return false;
212 $sql = "update tt_timesheets set status = null".
213 " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
214 $affected = $mdb2->exec($sql);
215 return (!is_a($affected, 'PEAR_Error'));
218 // update function - updates the timesheet in database.
219 static function update($fields) {
221 $mdb2 = getConnection();
223 $group_id = $user->getGroup();
224 $org_id = $user->org_id;
226 $timesheet_id = $fields['id']; // Timesheet we are updating.
227 $name = $fields['name']; // Timesheet name.
228 $comment = $fields['comment'];
229 $status = $fields['status']; // Timesheet status.
231 $sql = "update tt_timesheets set name = ".$mdb2->quote($name).", comment = ".$mdb2->quote($comment).
232 ", status = ".$mdb2->quote($status).
233 " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
234 $affected = $mdb2->exec($sql);
235 return (!is_a($affected, 'PEAR_Error'));
238 // isUserValid function is used during access checks and determines whether user id, passed in post, is valid
239 // in current context.
240 static function isUserValid($user_id) {
241 // We have to cover several situations.
245 // TODO: we are currently re-designing timesheets.
246 // Clients are not supposed to view them at all.
247 // And the post will change on_behalf user, to keep things consistent.
251 // getReportOptions prepares $options array to be used with ttReportHelper
252 // to obtain items for timesheet view.
253 static function getReportOptions($timesheet) {
255 $group_by_client = $user->isPluginEnabled('cl') && !$timesheet['client_id'];
256 $trackingMode = $user->getTrackingMode();
257 $group_by_project = MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode;
259 $options['timesheet_id'] = $timesheet['id'];
260 $options['client_id'] = $timesheet['client_id'];
261 $options['users'] = $timesheet['user_id'];
262 $options['show_durarion'] = 1;
263 $options['show_cost'] = 1; // To include expenses.
264 $options['show_totals_only'] = 1;
265 $options['group_by1'] = 'date';
266 if ($group_by_client || $group_by_project) {
267 $options['group_by2'] = $group_by_client ? 'client' : 'project';
269 if ($options['group_by2'] && $options['group_by2'] != 'project' && $group_by_project) {
270 $options['group_by3'] = 'project';
275 // getApprovers obtains a list of users who can approve a timesheet for a given user
276 // and also have an email to receive a notification about it.
277 static function getApprovers($user_id) {
279 $mdb2 = getConnection();
281 $group_id = $user->getGroup();
282 $org_id = $user->org_id;
284 $approvers = array();
285 $rank = ttUserHelper::getUserRank($user_id);
286 $sql = "select u.id, u.name, u.email".
288 " left join tt_roles r on (r.id = u.role_id)".
289 " where u.status = 1 and u.email is not null and u.group_id = $group_id and u.org_id = $org_id".
290 " and (r.rank > $rank and r.rights like '%approve_timesheets%')";
291 $res = $mdb2->query($sql);
292 if (!is_a($res, 'PEAR_Error')) {
293 while ($val = $res->fetchRow()) {
300 // submitTimesheet marks a timesheet as submitted and sends an email to an approver.
301 static function submitTimesheet($fields) {
303 $mdb2 = getConnection();
305 $group_id = $user->getGroup();
306 $org_id = $user->org_id;
308 // First, mark a timesheet as submitted.
309 // Even if mail part below does not work, this will get us a functioning workflow
310 // (without email notifications).
311 $timesheet_id = $fields['timesheet_id'];
312 $sql = "update tt_timesheets set submit_status = 1".
313 " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
314 $affected = $mdb2->exec($sql);
315 if (is_a($affected, 'PEAR_Error')) return false;
317 // TODO: send email to approver here...
318 // $approver_id = $fields['approver_id'];
323 // approveTimesheet marks a timesheet as approved and sends an email to submitter.
324 static function approveTimesheet($fields) {
326 $mdb2 = getConnection();
328 $group_id = $user->getGroup();
329 $org_id = $user->org_id;
331 // First, mark a timesheet as approved.
332 // Even if mail part below does not work, this will get us a functioning workflow
333 // (without email notifications).
334 $timesheet_id = $fields['timesheet_id'];
335 $comment = $fields['comment'];
337 $sql = "update tt_timesheets set approve_status = 1, approve_comment = ".$mdb2->quote($comment).
338 " where id = $timesheet_id and submit_status = 1 and group_id = $group_id and org_id = $org_id";
339 $affected = $mdb2->exec($sql);
340 if (is_a($affected, 'PEAR_Error')) return false;
342 // TODO: send email to submitter here...
346 // disapproveTimesheet marks a timesheet as approved and sends an email to submitter.
347 static function disapproveTimesheet($fields) {
349 $mdb2 = getConnection();
351 $group_id = $user->getGroup();
352 $org_id = $user->org_id;
354 // First, mark a timesheet as disapproved.
355 // Even if mail part below does not work, this will get us a functioning workflow
356 // (without email notifications).
357 $timesheet_id = $fields['timesheet_id'];
358 $comment = $fields['comment'];
360 $sql = "update tt_timesheets set approve_status = 0, approve_comment = ".$mdb2->quote($comment).
361 " where id = $timesheet_id and submit_status = 1 and group_id = $group_id and org_id = $org_id";
362 $affected = $mdb2->exec($sql);
363 if (is_a($affected, 'PEAR_Error')) return false;
365 // TODO: send email to submitter here...
369 // The timesheetItemsExist determines whether tt_log records exist in the specified period
370 // for inclusion in a new timesheet.
371 static function timesheetItemsExist($fields) {
373 $mdb2 = getConnection();
375 $user_id = $user->getUser();
376 $group_id = $user->getGroup();
377 $org_id = $user->org_id;
379 if (isset($fields['client_id'])) $client_id = (int) $fields['client_id'];
380 if (isset($fields['project_id'])) $project_id = (int) $fields['project_id'];
382 $start_date = new DateAndTime($user->date_format, $fields['start_date']);
383 $start = $start_date->toString(DB_DATEFORMAT);
385 $end_date = new DateAndTime($user->date_format, $fields['end_date']);
386 $end = $end_date->toString(DB_DATEFORMAT);
389 if ($client_id) $client_part = " and client_id = $client_id";
390 if ($project_id) $project_part = " and project_id = $project_id";
392 $sql = "select count(*) as num from tt_log".
393 " where status = 1 $client_part $project_part and timesheet_id is null".
394 " and date >= ".$mdb2->quote($start)." and date <= ".$mdb2->quote($end).
395 " and user_id = $user_id and group_id = $group_id and org_id = $org_id";
396 $res = $mdb2->query($sql);
397 if (!is_a($res, 'PEAR_Error')) {
398 $val = $res->fetchRow();