Further streamlining of access rights.
[timetracker.git] / WEB-INF / lib / ttTimesheetHelper.class.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 import('ttUserHelper');
30 import('ttGroupHelper');
31 import('form.ActionForm');
32 import('ttReportHelper');
33
34 // Class ttTimesheetHelper is used to help with project related tasks.
35 class ttTimesheetHelper {
36
37   // The getTimesheetByName looks up a project by name.
38   static function getTimesheetByName($name, $user_id) {
39     global $user;
40     $mdb2 = getConnection();
41
42     $group_id = $user->getGroup();
43     $org_id = $user->org_id;
44
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'])
52         return $val;
53     }
54     return false;
55   }
56
57   // createTimesheet function creates a new timesheet.
58   static function createTimesheet($fields)
59   {
60     // Create a new timesheet entry.
61     global $user;
62     $mdb2 = getConnection();
63
64     $user_id = $user->getUser();
65     $group_id = $user->getGroup();
66     $org_id = $user->org_id;
67
68     $client_id = $fields['client_id'];
69     $name = $fields['name'];
70     $comment = $fields['comment'];
71
72     $start_date = new DateAndTime($user->date_format, $fields['start_date']);
73     $start = $start_date->toString(DB_DATEFORMAT);
74
75     $end_date = new DateAndTime($user->date_format, $fields['end_date']);
76     $end = $end_date->toString(DB_DATEFORMAT);
77
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'))
83       return false;
84
85     $last_id = $mdb2->lastInsertID('tt_timesheets', 'id');
86
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'];
90     // sql parts.
91     if ($client_id) $client_part = " and client_id = $client_id";
92     if ($project_id) $project_part = " and project_id = $project_id";
93
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'))
100       return false;
101
102     return $last_id;
103   }
104
105   // The getActiveTimesheets obtains active timesheets for a user.
106   static function getActiveTimesheets($user_id)
107   {
108     global $user;
109     $mdb2 = getConnection();
110
111     $group_id = $user->getGroup();
112     $org_id = $user->org_id;
113
114     // $addPaidStatus = $user->isPluginEnabled('ps');
115     $result = array();
116
117     if ($user->isClient())
118       $client_part = "and ts.client_id = $user->client_id";
119
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);
125     $result = array();
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']);
131         $result[] = $val;
132       }
133     }
134     return $result;
135   }
136
137   // The getInactiveTimesheets obtains inactive timesheets for a user.
138   static function getInactiveTimesheets($user_id)
139   {
140     global $user;
141     $mdb2 = getConnection();
142
143     $group_id = $user->getGroup();
144     $org_id = $user->org_id;
145
146     // $addPaidStatus = $user->isPluginEnabled('ps');
147     $result = array();
148
149     if ($user->isClient())
150       $client_part = "and ts.client_id = $user->client_id";
151
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);
157     $result = array();
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']);
163         $result[] = $val;
164       }
165     }
166     return $result;
167   }
168
169   // getTimesheet - obtains timesheet data from the database.
170   static function getTimesheet($timesheet_id) {
171     global $user;
172     $mdb2 = getConnection();
173
174     $user_id = $user->getUser();
175     $group_id = $user->getGroup();
176     $org_id = $user->org_id;
177
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())
183         return $val;
184     }
185     return false;
186   }
187
188   // delete - deletes timesheet from the database.
189   static function delete($timesheet_id) {
190     global $user;
191     $mdb2 = getConnection();
192
193     $group_id = $user->getGroup();
194     $org_id = $user->org_id;
195
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;
201
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;
207
208     // Delete timesheet.
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'));
213   }
214
215   // update function - updates the timesheet in database.
216   static function update($fields) {
217     global $user;
218     $mdb2 = getConnection();
219
220     $group_id = $user->getGroup();
221     $org_id = $user->org_id;
222
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.
227
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'));
233   }
234
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.
239
240     global $user;
241
242     // TODO: we are currently re-designing timesheets.
243     // Clients are not supposed to view them at all.
244     // And the post will change on_behalf user, to keep things consistent.
245     return false;
246   }
247
248   // getReportOptions prepares $options array to be used with ttReportHelper
249   // to obtain items for timesheet view.
250   static function getReportOptions($timesheet) {
251     global $user;
252     $group_by_client = $user->isPluginEnabled('cl') && !$timesheet['client_id'];
253     $trackingMode = $user->getTrackingMode();
254     $group_by_project = MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode;
255
256     $options['timesheet_id'] = $timesheet['id'];
257     $options['client_id'] = $timesheet['client_id'];
258     $options['users'] = $timesheet['user_id'];
259     $options['show_durarion'] = 1;
260     $options['show_cost'] = 1; // To include expenses.
261     $options['show_totals_only'] = 1;
262     $options['group_by1'] = 'date';
263     if ($group_by_client || $group_by_project) {
264       $options['group_by2'] = $group_by_client ? 'client' : 'project';
265     }
266     if ($options['group_by2'] && $options['group_by2'] != 'project' && $group_by_project) {
267       $options['group_by3'] = 'project';
268     }
269     return $options;
270   }
271
272   // getApprovers obtains a list of users who can approve a timesheet for a given user
273   // and also have an email to receive a notification about it.
274   static function getApprovers($user_id) {
275     global $user;
276     $mdb2 = getConnection();
277
278     $group_id = $user->getGroup();
279     $org_id = $user->org_id;
280
281     $approvers = array();
282     $rank = ttUserHelper::getUserRank($user_id);
283     $sql = "select u.id, u.name, u.email".
284       " from tt_users u".
285       " left join tt_roles r on (r.id = u.role_id)".
286       " where u.status = 1 and u.email is not null and u.group_id = $group_id and u.org_id = $org_id".
287       " and (r.rank > $rank and r.rights like '%approve_timesheets%')";
288     $res = $mdb2->query($sql);
289     if (!is_a($res, 'PEAR_Error')) {
290       while ($val = $res->fetchRow()) {
291         $approvers[] = $val;
292       }
293     }
294     return $approvers;
295   }
296
297   // submitTimesheet marks a timesheet as submitted and sends an email to an approver.
298   static function submitTimesheet($fields) {
299     global $user;
300     $mdb2 = getConnection();
301
302     $group_id = $user->getGroup();
303     $org_id = $user->org_id;
304
305     // First, mark a timesheet as submitted.
306     // Even if mail part below does not work, this will get us a functioning workflow
307     // (without email notifications).
308     $timesheet_id = $fields['timesheet_id'];
309     $sql = "update tt_timesheets set submit_status = 1".
310       " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
311     $affected = $mdb2->exec($sql);
312     if (is_a($affected, 'PEAR_Error')) return false;
313
314     // TODO: send email to approver here...
315     // $approver_id = $fields['approver_id'];
316
317     return true;
318   }
319
320   // approveTimesheet marks a timesheet as approved and sends an email to submitter.
321   static function approveTimesheet($fields) {
322     global $user;
323     $mdb2 = getConnection();
324
325     $group_id = $user->getGroup();
326     $org_id = $user->org_id;
327
328     // First, mark a timesheet as approved.
329     // Even if mail part below does not work, this will get us a functioning workflow
330     // (without email notifications).
331     $timesheet_id = $fields['timesheet_id'];
332     $manager_comment = $fields['comment'];
333
334     $sql = "update tt_timesheets set approval_status = 1, manager_comment = ".$mdb2->quote($manager_comment).
335       " where id = $timesheet_id and submit_status = 1 and group_id = $group_id and org_id = $org_id";
336     $affected = $mdb2->exec($sql);
337     if (is_a($affected, 'PEAR_Error')) return false;
338
339     // TODO: send email to submitter here...
340     return true;
341   }
342
343   // disapproveTimesheet marks a timesheet as approved and sends an email to submitter.
344   static function disapproveTimesheet($fields) {
345     global $user;
346     $mdb2 = getConnection();
347
348     $group_id = $user->getGroup();
349     $org_id = $user->org_id;
350
351     // First, mark a timesheet as disapproved.
352     // Even if mail part below does not work, this will get us a functioning workflow
353     // (without email notifications).
354     $timesheet_id = $fields['timesheet_id'];
355     $manager_comment = $fields['comment'];
356
357     $sql = "update tt_timesheets set approval_status = 0, manager_comment = ".$mdb2->quote($manager_comment).
358       " where id = $timesheet_id and submit_status = 1 and group_id = $group_id and org_id = $org_id";
359     $affected = $mdb2->exec($sql);
360     if (is_a($affected, 'PEAR_Error')) return false;
361
362     // TODO: send email to submitter here...
363     return true;
364   }
365
366   // The timesheetItemsExist determines whether tt_log records exist in the specified period
367   // for inclusion in a new timesheet.
368   static function timesheetItemsExist($fields) {
369     global $user;
370     $mdb2 = getConnection();
371
372     $user_id = $user->getUser();
373     $group_id = $user->getGroup();
374     $org_id = $user->org_id;
375
376     if (isset($fields['client_id'])) $client_id = (int) $fields['client_id'];
377     if (isset($fields['project_id'])) $project_id = (int) $fields['project_id'];
378
379     $start_date = new DateAndTime($user->date_format, $fields['start_date']);
380     $start = $start_date->toString(DB_DATEFORMAT);
381
382     $end_date = new DateAndTime($user->date_format, $fields['end_date']);
383     $end = $end_date->toString(DB_DATEFORMAT);
384
385     // sql parts.
386     if ($client_id) $client_part = " and client_id = $client_id";
387     if ($project_id) $project_part = " and project_id = $project_id";
388
389     $sql = "select count(*) as num from tt_log".
390       " where status = 1 $client_part $project_part and timesheet_id is null".
391       " and date >= ".$mdb2->quote($start)." and date <= ".$mdb2->quote($end).
392       " and user_id = $user_id and group_id = $group_id and org_id = $org_id";
393     $res = $mdb2->query($sql);
394     if (!is_a($res, 'PEAR_Error')) {
395       $val = $res->fetchRow();
396       if ($val['num']) {
397         return true;
398       }
399     }
400
401     return false;
402   }
403 }