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