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');
30 import('ttGroupHelper');
31 import('form.ActionForm');
32 import('ttReportHelper');
34 // Class ttTimesheetHelper is used to help with project related tasks.
35 class ttTimesheetHelper {
37 // The getTimesheetByName looks up a project by name.
38 static function getTimesheetByName($name, $user_id) {
40 $mdb2 = getConnection();
42 $group_id = $user->getGroup();
43 $org_id = $user->org_id;
45 $sql = "select id from tt_timesheets".
46 " where group_id = $group_id and org_id = $org_id and user_id = $user_id and name = ".$mdb2->quote($name).
47 " and (status = 1 or status = 0)";
48 $res = $mdb2->query($sql);
49 if (!is_a($res, 'PEAR_Error')) {
50 $val = $res->fetchRow();
51 if ($val && $val['id'])
57 // createTimesheet function creates a new timesheet.
58 static function createTimesheet($fields)
60 // Create a new timesheet entry.
62 $mdb2 = getConnection();
64 $user_id = $user->getUser();
65 $group_id = $user->getGroup();
66 $org_id = $user->org_id;
68 $client_id = $fields['client_id'];
69 $name = $fields['name'];
70 $comment = $fields['comment'];
72 $start_date = new DateAndTime($user->date_format, $fields['start_date']);
73 $start = $start_date->toString(DB_DATEFORMAT);
75 $end_date = new DateAndTime($user->date_format, $fields['end_date']);
76 $end = $end_date->toString(DB_DATEFORMAT);
78 $sql = "insert into tt_timesheets (user_id, group_id, org_id, client_id, name, comment, start_date, end_date)".
79 " values ($user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($name).
80 ", ".$mdb2->quote($comment).", ".$mdb2->quote($start).", ".$mdb2->quote($end).")";
81 $affected = $mdb2->exec($sql);
82 if (is_a($affected, 'PEAR_Error'))
85 $last_id = $mdb2->lastInsertID('tt_timesheets', 'id');
87 // Associate time items with timesheet.
88 if (isset($fields['client'])) $client_id = (int) $fields['client_id'];
89 if (isset($fields['project_id'])) $project_id = (int) $fields['project_id'];
91 if ($client_id) $client_part = " and client_id = $client_id";
92 if ($project_id) $project_part = " and project_id = $project_id";
94 $sql = "update tt_log set timesheet_id = $last_id".
95 " where status = 1 $client_part $project_part and timesheet_id is null".
96 " and date >= ".$mdb2->quote($start)." and date <= ".$mdb2->quote($end).
97 " and user_id = $user_id and group_id = $group_id and org_id = $org_id";
98 $affected = $mdb2->exec($sql);
99 if (is_a($affected, 'PEAR_Error'))
105 // The getActiveTimesheets obtains active timesheets for a user.
106 static function getActiveTimesheets($user_id)
109 $mdb2 = getConnection();
111 $group_id = $user->getGroup();
112 $org_id = $user->org_id;
114 // $addPaidStatus = $user->isPluginEnabled('ps');
117 if ($user->isClient())
118 $client_part = "and ts.client_id = $user->client_id";
120 $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name, ts.submit_status, ts.approve_status from tt_timesheets ts".
121 " left join tt_clients c on (c.id = ts.client_id)".
122 " where ts.status = 1 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
123 " $client_part order by ts.name";
124 $res = $mdb2->query($sql);
126 if (!is_a($res, 'PEAR_Error')) {
127 $dt = new DateAndTime(DB_DATEFORMAT);
128 while ($val = $res->fetchRow()) {
129 //if ($addPaidStatus)
130 // $val['paid'] = ttTimesheetHelper::isPaid($val['id']);
137 // The getInactiveTimesheets obtains inactive timesheets for a user.
138 static function getInactiveTimesheets($user_id)
141 $mdb2 = getConnection();
143 $group_id = $user->getGroup();
144 $org_id = $user->org_id;
146 // $addPaidStatus = $user->isPluginEnabled('ps');
149 if ($user->isClient())
150 $client_part = "and ts.client_id = $user->client_id";
152 $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name, ts.submit_status, ts.approval_status from tt_timesheets ts".
153 " left join tt_clients c on (c.id = ts.client_id)".
154 " where ts.status = 0 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
155 " $client_part order by ts.name";
156 $res = $mdb2->query($sql);
158 if (!is_a($res, 'PEAR_Error')) {
159 $dt = new DateAndTime(DB_DATEFORMAT);
160 while ($val = $res->fetchRow()) {
161 //if ($addPaidStatus)
162 // $val['paid'] = ttTimesheetHelper::isPaid($val['id']);
169 // getTimesheet - obtains timesheet data from the database.
170 static function getTimesheet($timesheet_id) {
172 $mdb2 = getConnection();
174 $user_id = $user->getUser();
175 $group_id = $user->getGroup();
176 $org_id = $user->org_id;
178 $sql = "select * from tt_timesheets".
179 " where id = $timesheet_id and user_id = $user_id and group_id = $group_id and org_id = $org_id and status is not null";
180 $res = $mdb2->query($sql);
181 if (!is_a($res, 'PEAR_Error')) {
182 if ($val = $res->fetchRow())
188 // delete - deletes timesheet from the database.
189 static function delete($timesheet_id) {
191 $mdb2 = getConnection();
193 $group_id = $user->getGroup();
194 $org_id = $user->org_id;
196 // Handle time records.
197 $sql = "update tt_log set timesheet_id = null".
198 " where timesheet_id = $timesheet_id and group_id = $group_id and org_id = $org_id";
199 $affected = $mdb2->exec($sql);
200 if (is_a($affected, 'PEAR_Error')) return false;
202 // Handle expense items.
203 $sql = "update tt_expense_items set timesheet_id = null".
204 " where timesheet_id = $timesheet_id and group_id = $group_id and org_id = $org_id";
205 $affected = $mdb2->exec($sql);
206 if (is_a($affected, 'PEAR_Error')) return false;
209 $sql = "update tt_timesheets set status = null".
210 " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
211 $affected = $mdb2->exec($sql);
212 return (!is_a($affected, 'PEAR_Error'));
215 // update function - updates the timesheet in database.
216 static function update($fields) {
218 $mdb2 = getConnection();
220 $group_id = $user->getGroup();
221 $org_id = $user->org_id;
223 $timesheet_id = $fields['id']; // Timesheet we are updating.
224 $name = $fields['name']; // Timesheet name.
225 $submitter_comment = $fields['submitter_comment'];
226 $status = $fields['status']; // Project status.
228 $sql = "update tt_timesheets set name = ".$mdb2->quote($name).", submitter_comment = ".$mdb2->quote($submitter_comment).
229 ", status = ".$mdb2->quote($status).
230 " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
231 $affected = $mdb2->exec($sql);
232 return (!is_a($affected, 'PEAR_Error'));
235 // isUserValid function is used during access checks and determines whether user id, passed in post, is valid
236 // in current context.
237 static function isUserValid($user_id) {
238 // We have to cover several situations.
240 // 1) User is a client.
241 // 2) User with view_all_timesheets rights.
242 // 3) User with view_timesheets rights.
246 // TODO: we are currently re-designing timesheets.
247 // Clients are not supposed to view them at all.
248 // And the post will change on_behalf user, to keep things consistent.
252 // getReportOptions prepares $options array to be used with ttReportHelper
253 // to obtain items for timesheet view.
254 static function getReportOptions($timesheet) {
256 $group_by_client = $user->isPluginEnabled('cl') && !$timesheet['client_id'];
257 $trackingMode = $user->getTrackingMode();
258 $group_by_project = MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode;
260 $options['timesheet_id'] = $timesheet['id'];
261 $options['client_id'] = $timesheet['client_id'];
262 $options['users'] = $timesheet['user_id'];
263 $options['show_durarion'] = 1;
264 $options['show_cost'] = 1; // To include expenses.
265 $options['show_totals_only'] = 1;
266 $options['group_by1'] = 'date';
267 if ($group_by_client || $group_by_project) {
268 $options['group_by2'] = $group_by_client ? 'client' : 'project';
270 if ($options['group_by2'] && $options['group_by2'] != 'project' && $group_by_project) {
271 $options['group_by3'] = 'project';
276 // getApprovers obtains a list of users who can approve a timesheet for a given user
277 // and also have an email to receive a notification about it.
278 static function getApprovers($user_id) {
280 $mdb2 = getConnection();
282 $group_id = $user->getGroup();
283 $org_id = $user->org_id;
285 $approvers = array();
286 $rank = ttUserHelper::getUserRank($user_id);
287 $sql = "select u.id, u.name, u.email".
289 " left join tt_roles r on (r.id = u.role_id)".
290 " where u.status = 1 and u.email is not null and u.group_id = $group_id and u.org_id = $org_id".
291 " and (r.rights like '%approve_all_timesheets%' or (r.rank > $rank and r.rights like '%approve_timesheets%'))";
292 $res = $mdb2->query($sql);
293 if (!is_a($res, 'PEAR_Error')) {
294 while ($val = $res->fetchRow()) {
301 // submitTimesheet marks a timesheet as submitted and sends an email to an approver.
302 static function submitTimesheet($fields) {
304 $mdb2 = getConnection();
306 $group_id = $user->getGroup();
307 $org_id = $user->org_id;
309 // First, mark a timesheet as submitted.
310 // Even if mail part below does not work, this will get us a functioning workflow
311 // (without email notifications).
312 $timesheet_id = $fields['timesheet_id'];
313 $sql = "update tt_timesheets set submit_status = 1".
314 " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
315 $affected = $mdb2->exec($sql);
316 if (is_a($affected, 'PEAR_Error')) return false;
318 // TODO: send email to approver here...
319 // $approver_id = $fields['approver_id'];
324 // approveTimesheet marks a timesheet as approved and sends an email to submitter.
325 static function approveTimesheet($fields) {
327 $mdb2 = getConnection();
329 $group_id = $user->getGroup();
330 $org_id = $user->org_id;
332 // First, mark a timesheet as approved.
333 // Even if mail part below does not work, this will get us a functioning workflow
334 // (without email notifications).
335 $timesheet_id = $fields['timesheet_id'];
336 $manager_comment = $fields['comment'];
338 $sql = "update tt_timesheets set approval_status = 1, manager_comment = ".$mdb2->quote($manager_comment).
339 " where id = $timesheet_id and submit_status = 1 and group_id = $group_id and org_id = $org_id";
340 $affected = $mdb2->exec($sql);
341 if (is_a($affected, 'PEAR_Error')) return false;
343 // TODO: send email to submitter here...
347 // disapproveTimesheet marks a timesheet as approved and sends an email to submitter.
348 static function disapproveTimesheet($fields) {
350 $mdb2 = getConnection();
352 $group_id = $user->getGroup();
353 $org_id = $user->org_id;
355 // First, mark a timesheet as disapproved.
356 // Even if mail part below does not work, this will get us a functioning workflow
357 // (without email notifications).
358 $timesheet_id = $fields['timesheet_id'];
359 $manager_comment = $fields['comment'];
361 $sql = "update tt_timesheets set approval_status = 0, manager_comment = ".$mdb2->quote($manager_comment).
362 " where id = $timesheet_id and submit_status = 1 and group_id = $group_id and org_id = $org_id";
363 $affected = $mdb2->exec($sql);
364 if (is_a($affected, 'PEAR_Error')) return false;
366 // TODO: send email to submitter here...
370 // The timesheetItemsExist determines whether tt_log records exist in the specified period
371 // for inclusion in a new timesheet.
372 static function timesheetItemsExist($fields) {
374 $mdb2 = getConnection();
376 $user_id = $user->getUser();
377 $group_id = $user->getGroup();
378 $org_id = $user->org_id;
380 if (isset($fields['client_id'])) $client_id = (int) $fields['client_id'];
381 if (isset($fields['project_id'])) $project_id = (int) $fields['project_id'];
383 $start_date = new DateAndTime($user->date_format, $fields['start_date']);
384 $start = $start_date->toString(DB_DATEFORMAT);
386 $end_date = new DateAndTime($user->date_format, $fields['end_date']);
387 $end = $end_date->toString(DB_DATEFORMAT);
390 if ($client_id) $client_part = " and client_id = $client_id";
391 if ($project_id) $project_part = " and project_id = $project_id";
393 $sql = "select count(*) as num from tt_log".
394 " where status = 1 $client_part $project_part and timesheet_id is null".
395 " and date >= ".$mdb2->quote($start)." and date <= ".$mdb2->quote($end).
396 " and user_id = $user_id and group_id = $group_id and org_id = $org_id";
397 $res = $mdb2->query($sql);
398 if (!is_a($res, 'PEAR_Error')) {
399 $val = $res->fetchRow();