78e7901654eef5dbba68cfb98176f3a557c6c9ad
[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     $project_id = $fields['project_id'];
70     $name = $fields['name'];
71     $comment = $fields['comment'];
72
73     $start_date = new DateAndTime($user->date_format, $fields['start_date']);
74     $start = $start_date->toString(DB_DATEFORMAT);
75
76     $end_date = new DateAndTime($user->date_format, $fields['end_date']);
77     $end = $end_date->toString(DB_DATEFORMAT);
78
79     $sql = "insert into tt_timesheets (user_id, group_id, org_id, client_id, project_id, name, comment, start_date, end_date)".
80       " values ($user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($project_id).", ".$mdb2->quote($name).
81       ", ".$mdb2->quote($comment).", ".$mdb2->quote($start).", ".$mdb2->quote($end).")";
82     $affected = $mdb2->exec($sql);
83     if (is_a($affected, 'PEAR_Error'))
84       return false;
85
86     $last_id = $mdb2->lastInsertID('tt_timesheets', 'id');
87
88     // Associate time items with timesheet.
89     if (isset($fields['client'])) $client_id = (int) $fields['client_id'];
90     if (isset($fields['project_id'])) $project_id = (int) $fields['project_id'];
91     // sql parts.
92     if ($client_id) $client_part = " and client_id = $client_id";
93     if ($project_id) $project_part = " and project_id = $project_id";
94
95     $sql = "update tt_log set timesheet_id = $last_id".
96       " where status = 1 $client_part $project_part and timesheet_id is null".
97       " and date >= ".$mdb2->quote($start)." and date <= ".$mdb2->quote($end).
98       " and user_id = $user_id and group_id = $group_id and org_id = $org_id";
99     $affected = $mdb2->exec($sql);
100     if (is_a($affected, 'PEAR_Error'))
101       return false;
102
103     return $last_id;
104   }
105
106   // The getActiveTimesheets obtains active timesheets for a user.
107   static function getActiveTimesheets($user_id)
108   {
109     global $user;
110     $mdb2 = getConnection();
111
112     $group_id = $user->getGroup();
113     $org_id = $user->org_id;
114
115     // $addPaidStatus = $user->isPluginEnabled('ps');
116     $result = array();
117
118     if ($user->isClient())
119       $client_part = "and ts.client_id = $user->client_id";
120
121     $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name, ts.submit_status, ts.approve_status from tt_timesheets ts".
122       " left join tt_clients c on (c.id = ts.client_id)".
123       " where ts.status = 1 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
124       " $client_part order by ts.name";
125     $res = $mdb2->query($sql);
126     $result = array();
127     if (!is_a($res, 'PEAR_Error')) {
128       $dt = new DateAndTime(DB_DATEFORMAT);
129       while ($val = $res->fetchRow()) {
130         //if ($addPaidStatus)
131         //  $val['paid'] = ttTimesheetHelper::isPaid($val['id']);
132         $result[] = $val;
133       }
134     }
135     return $result;
136   }
137
138   // The getInactiveTimesheets obtains inactive timesheets for a user.
139   static function getInactiveTimesheets($user_id)
140   {
141     global $user;
142     $mdb2 = getConnection();
143
144     $group_id = $user->getGroup();
145     $org_id = $user->org_id;
146
147     // $addPaidStatus = $user->isPluginEnabled('ps');
148     $result = array();
149
150     if ($user->isClient())
151       $client_part = "and ts.client_id = $user->client_id";
152
153     $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name, ts.submit_status, ts.approval_status from tt_timesheets ts".
154       " left join tt_clients c on (c.id = ts.client_id)".
155       " where ts.status = 0 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
156       " $client_part order by ts.name";
157     $res = $mdb2->query($sql);
158     $result = array();
159     if (!is_a($res, 'PEAR_Error')) {
160       $dt = new DateAndTime(DB_DATEFORMAT);
161       while ($val = $res->fetchRow()) {
162         //if ($addPaidStatus)
163         //  $val['paid'] = ttTimesheetHelper::isPaid($val['id']);
164         $result[] = $val;
165       }
166     }
167     return $result;
168   }
169
170   // getTimesheet - obtains timesheet data from the database.
171   static function getTimesheet($timesheet_id) {
172     global $user;
173     $mdb2 = getConnection();
174
175     $user_id = $user->getUser();
176     $group_id = $user->getGroup();
177     $org_id = $user->org_id;
178
179     $sql = "select ts.*, u.name as user_name, c.name as client_name,".
180       " p.name as project_name from tt_timesheets ts".
181       " left join tt_users u on (ts.user_id = u.id)".
182       " left join tt_clients c on (ts.client_id = c.id)".
183       " left join tt_projects p on (ts.project_id = p.id)".
184       " 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";
185     $res = $mdb2->query($sql);
186     if (!is_a($res, 'PEAR_Error')) {
187       if ($val = $res->fetchRow())
188         return $val;
189     }
190     return false;
191   }
192
193   // delete - deletes timesheet from the database.
194   static function delete($timesheet_id) {
195     global $user;
196     $mdb2 = getConnection();
197
198     $group_id = $user->getGroup();
199     $org_id = $user->org_id;
200
201     // Handle time records.
202     $sql = "update tt_log set timesheet_id = null".
203       " where timesheet_id = $timesheet_id and group_id = $group_id and org_id = $org_id";
204     $affected = $mdb2->exec($sql);
205     if (is_a($affected, 'PEAR_Error')) return false;
206
207     // Handle expense items.
208     $sql = "update tt_expense_items set timesheet_id = null".
209       " where timesheet_id = $timesheet_id and group_id = $group_id and org_id = $org_id";
210     $affected = $mdb2->exec($sql);
211     if (is_a($affected, 'PEAR_Error')) return false;
212
213     // Delete timesheet.
214     $sql = "update tt_timesheets set status = null".
215       " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
216     $affected = $mdb2->exec($sql);
217     return (!is_a($affected, 'PEAR_Error'));
218   }
219
220   // update function - updates the timesheet in database.
221   static function update($fields) {
222     global $user;
223     $mdb2 = getConnection();
224
225     $group_id = $user->getGroup();
226     $org_id = $user->org_id;
227
228     $timesheet_id = $fields['id']; // Timesheet we are updating.
229     $name = $fields['name']; // Timesheet name.
230     $submitter_comment = $fields['submitter_comment'];
231     $status = $fields['status']; // Project status.
232
233     $sql = "update tt_timesheets set name = ".$mdb2->quote($name).", submitter_comment = ".$mdb2->quote($submitter_comment).
234       ", status = ".$mdb2->quote($status).
235       " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
236     $affected = $mdb2->exec($sql);
237     return (!is_a($affected, 'PEAR_Error'));
238   }
239
240   // isUserValid function is used during access checks and determines whether user id, passed in post, is valid
241   // in current context.
242   static function isUserValid($user_id) {
243     // We have to cover several situations.
244
245     global $user;
246
247     // TODO: we are currently re-designing timesheets.
248     // Clients are not supposed to view them at all.
249     // And the post will change on_behalf user, to keep things consistent.
250     return false;
251   }
252
253   // getReportOptions prepares $options array to be used with ttReportHelper
254   // to obtain items for timesheet view.
255   static function getReportOptions($timesheet) {
256     global $user;
257     $group_by_client = $user->isPluginEnabled('cl') && !$timesheet['client_id'];
258     $trackingMode = $user->getTrackingMode();
259     $group_by_project = MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode;
260
261     $options['timesheet_id'] = $timesheet['id'];
262     $options['client_id'] = $timesheet['client_id'];
263     $options['users'] = $timesheet['user_id'];
264     $options['show_durarion'] = 1;
265     $options['show_cost'] = 1; // To include expenses.
266     $options['show_totals_only'] = 1;
267     $options['group_by1'] = 'date';
268     if ($group_by_client || $group_by_project) {
269       $options['group_by2'] = $group_by_client ? 'client' : 'project';
270     }
271     if ($options['group_by2'] && $options['group_by2'] != 'project' && $group_by_project) {
272       $options['group_by3'] = 'project';
273     }
274     return $options;
275   }
276
277   // getApprovers obtains a list of users who can approve a timesheet for a given user
278   // and also have an email to receive a notification about it.
279   static function getApprovers($user_id) {
280     global $user;
281     $mdb2 = getConnection();
282
283     $group_id = $user->getGroup();
284     $org_id = $user->org_id;
285
286     $approvers = array();
287     $rank = ttUserHelper::getUserRank($user_id);
288     $sql = "select u.id, u.name, u.email".
289       " from tt_users u".
290       " left join tt_roles r on (r.id = u.role_id)".
291       " where u.status = 1 and u.email is not null and u.group_id = $group_id and u.org_id = $org_id".
292       " and (r.rank > $rank and r.rights like '%approve_timesheets%')";
293     $res = $mdb2->query($sql);
294     if (!is_a($res, 'PEAR_Error')) {
295       while ($val = $res->fetchRow()) {
296         $approvers[] = $val;
297       }
298     }
299     return $approvers;
300   }
301
302   // submitTimesheet marks a timesheet as submitted and sends an email to an approver.
303   static function submitTimesheet($fields) {
304     global $user;
305     $mdb2 = getConnection();
306
307     $group_id = $user->getGroup();
308     $org_id = $user->org_id;
309
310     // First, mark a timesheet as submitted.
311     // Even if mail part below does not work, this will get us a functioning workflow
312     // (without email notifications).
313     $timesheet_id = $fields['timesheet_id'];
314     $sql = "update tt_timesheets set submit_status = 1".
315       " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
316     $affected = $mdb2->exec($sql);
317     if (is_a($affected, 'PEAR_Error')) return false;
318
319     // TODO: send email to approver here...
320     // $approver_id = $fields['approver_id'];
321
322     return true;
323   }
324
325   // approveTimesheet marks a timesheet as approved and sends an email to submitter.
326   static function approveTimesheet($fields) {
327     global $user;
328     $mdb2 = getConnection();
329
330     $group_id = $user->getGroup();
331     $org_id = $user->org_id;
332
333     // First, mark a timesheet as approved.
334     // Even if mail part below does not work, this will get us a functioning workflow
335     // (without email notifications).
336     $timesheet_id = $fields['timesheet_id'];
337     $comment = $fields['comment'];
338
339     $sql = "update tt_timesheets set approve_status = 1, approve_comment = ".$mdb2->quote($comment).
340       " where id = $timesheet_id and submit_status = 1 and group_id = $group_id and org_id = $org_id";
341     $affected = $mdb2->exec($sql);
342     if (is_a($affected, 'PEAR_Error')) return false;
343
344     // TODO: send email to submitter here...
345     return true;
346   }
347
348   // disapproveTimesheet marks a timesheet as approved and sends an email to submitter.
349   static function disapproveTimesheet($fields) {
350     global $user;
351     $mdb2 = getConnection();
352
353     $group_id = $user->getGroup();
354     $org_id = $user->org_id;
355
356     // First, mark a timesheet as disapproved.
357     // Even if mail part below does not work, this will get us a functioning workflow
358     // (without email notifications).
359     $timesheet_id = $fields['timesheet_id'];
360     $comment = $fields['comment'];
361
362     $sql = "update tt_timesheets set approve_status = 0, approve_comment = ".$mdb2->quote($comment).
363       " where id = $timesheet_id and submit_status = 1 and group_id = $group_id and org_id = $org_id";
364     $affected = $mdb2->exec($sql);
365     if (is_a($affected, 'PEAR_Error')) return false;
366
367     // TODO: send email to submitter here...
368     return true;
369   }
370
371   // The timesheetItemsExist determines whether tt_log records exist in the specified period
372   // for inclusion in a new timesheet.
373   static function timesheetItemsExist($fields) {
374     global $user;
375     $mdb2 = getConnection();
376
377     $user_id = $user->getUser();
378     $group_id = $user->getGroup();
379     $org_id = $user->org_id;
380
381     if (isset($fields['client_id'])) $client_id = (int) $fields['client_id'];
382     if (isset($fields['project_id'])) $project_id = (int) $fields['project_id'];
383
384     $start_date = new DateAndTime($user->date_format, $fields['start_date']);
385     $start = $start_date->toString(DB_DATEFORMAT);
386
387     $end_date = new DateAndTime($user->date_format, $fields['end_date']);
388     $end = $end_date->toString(DB_DATEFORMAT);
389
390     // sql parts.
391     if ($client_id) $client_part = " and client_id = $client_id";
392     if ($project_id) $project_part = " and project_id = $project_id";
393
394     $sql = "select count(*) as num from tt_log".
395       " where status = 1 $client_part $project_part and timesheet_id is null".
396       " and date >= ".$mdb2->quote($start)." and date <= ".$mdb2->quote($end).
397       " and user_id = $user_id and group_id = $group_id and org_id = $org_id";
398     $res = $mdb2->query($sql);
399     if (!is_a($res, 'PEAR_Error')) {
400       $val = $res->fetchRow();
401       if ($val['num']) {
402         return true;
403       }
404     }
405
406     return false;
407   }
408 }