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