056fc6859ab3c44c4f2666d879526f850999c3ff
[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   // insert function inserts a new timesheet into database.
58   static function insert($fields)
59   {
60     // First, we obtain report items.
61
62     // Obtain session bean with report attributes.
63     $bean = new ActionForm('reportBean', new Form('reportForm'));
64     $options = ttReportHelper::getReportOptions($bean);
65     $report_items = ttReportHelper::getItems($options);
66
67     // Prepare ids for time and expense items, at the same time checking
68     // if we can proceed with creating a timesheet.
69     $canCreateTimesheet = true;
70     $first_user_id = null;
71
72     foreach ($report_items as $report_item) {
73       // Check user id.
74       if (!$first_user_id)
75         $first_user_id = $report_item['user_id'];
76       else {
77         if ($report_item['user_id'] != $first_user_id) {
78           // We have items for multiple users.
79           $canCreateTimesheet = false;
80           break;
81         }
82       }
83       // Check timesheet id.
84       if ($report_item['timesheet_id']) {
85         // We have an item already assigned to a timesheet.
86         $canCreateTimesheet = false;
87         break;
88       }
89       if ($report_item['type'] == 1)
90         $time_ids[] = $report_item['id'];
91       elseif ($report_item['type'] == 2)
92         $expense_ids[] = $report_item['id'];
93     }
94     if (!$canCreateTimesheet) return false;
95
96     // Make comma-seperated lists of ids for sql.
97     if ($time_ids)
98       $comma_separated_time_ids = implode(',', $time_ids);
99     if ($expense_ids)
100       $comma_separated_expense_ids = implode(',', $expense_ids);
101
102     // Create a new timesheet entry.
103     global $user;
104     $mdb2 = getConnection();
105
106     $group_id = $user->getGroup();
107     $org_id = $user->org_id;
108
109     $user_id = $fields['user_id'];
110     $client_id = $fields['client_id'];
111     $name = $fields['name'];
112     $submitter_comment = $fields['comment'];
113
114     $sql = "insert into tt_timesheets (user_id, group_id, org_id, client_id, name, submitter_comment)".
115       " values ($user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($name).", ".$mdb2->quote($submitter_comment).")";
116     $affected = $mdb2->exec($sql);
117     if (is_a($affected, 'PEAR_Error'))
118       return false;
119
120     $last_id = $mdb2->lastInsertID('tt_timesheets', 'id');
121
122     // Associate time items with timesheet.
123     if ($comma_separated_time_ids) {
124       $sql = "update tt_log set timesheet_id = $last_id".
125         " where id in ($comma_separated_time_ids) and group_id = $group_id and org_id = $org_id";
126       $affected = $mdb2->exec($sql);
127       if (is_a($affected, 'PEAR_Error'))
128         return false;
129     }
130
131     // Associate expense items with timesheet.
132     if ($comma_separated_expense_ids) {
133       $sql = "update tt_expense_items set timesheet_id = $last_id".
134         " where id in ($comma_separated_expense_ids) and group_id = $group_id and org_id = $org_id";
135       $affected = $mdb2->exec($sql);
136       if (is_a($affected, 'PEAR_Error'))
137         return false;
138     }
139
140     return $last_id;
141   }
142
143   // The getActiveTimesheets obtains active timesheets for a user.
144   static function getActiveTimesheets($user_id)
145   {
146     global $user;
147     $mdb2 = getConnection();
148
149     $group_id = $user->getGroup();
150     $org_id = $user->org_id;
151
152     // $addPaidStatus = $user->isPluginEnabled('ps');
153     $result = array();
154
155     if ($user->isClient())
156       $client_part = "and ts.client_id = $user->client_id";
157
158     $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name, ts.submit_status, ts.approval_status from tt_timesheets ts".
159       " left join tt_clients c on (c.id = ts.client_id)".
160       " where ts.status = 1 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
161       " $client_part order by ts.name";
162     $res = $mdb2->query($sql);
163     $result = array();
164     if (!is_a($res, 'PEAR_Error')) {
165       $dt = new DateAndTime(DB_DATEFORMAT);
166       while ($val = $res->fetchRow()) {
167         //if ($addPaidStatus)
168         //  $val['paid'] = ttTimesheetHelper::isPaid($val['id']);
169         $result[] = $val;
170       }
171     }
172     return $result;
173   }
174
175   // The getInactiveTimesheets obtains inactive timesheets for a user.
176   static function getInactiveTimesheets($user_id)
177   {
178     global $user;
179     $mdb2 = getConnection();
180
181     $group_id = $user->getGroup();
182     $org_id = $user->org_id;
183
184     // $addPaidStatus = $user->isPluginEnabled('ps');
185     $result = array();
186
187     if ($user->isClient())
188       $client_part = "and ts.client_id = $user->client_id";
189
190     $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name, ts.submit_status, ts.approval_status from tt_timesheets ts".
191       " left join tt_clients c on (c.id = ts.client_id)".
192       " where ts.status = 0 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
193       " $client_part order by ts.name";
194     $res = $mdb2->query($sql);
195     $result = array();
196     if (!is_a($res, 'PEAR_Error')) {
197       $dt = new DateAndTime(DB_DATEFORMAT);
198       while ($val = $res->fetchRow()) {
199         //if ($addPaidStatus)
200         //  $val['paid'] = ttTimesheetHelper::isPaid($val['id']);
201         $result[] = $val;
202       }
203     }
204     return $result;
205   }
206
207   // getTimesheet - obtains timesheet data from the database.
208   static function getTimesheet($timesheet_id) {
209     global $user;
210     $mdb2 = getConnection();
211
212     $group_id = $user->getGroup();
213     $org_id = $user->org_id;
214
215     if ($user->isClient()) $client_part = "and client_id = $user->client_id";
216
217     $sql = "select * from tt_timesheets".
218       " where id = $timesheet_id and group_id = $group_id and org_id = $org_id $client_part and status is not null";
219     $res = $mdb2->query($sql);
220     if (!is_a($res, 'PEAR_Error')) {
221       if ($val = $res->fetchRow())
222         return $val;
223     }
224     return false;
225   }
226
227   // delete - deletes timesheet from the database.
228   static function delete($timesheet_id) {
229     global $user;
230     $mdb2 = getConnection();
231
232     $group_id = $user->getGroup();
233     $org_id = $user->org_id;
234
235     // Handle time records.
236     $sql = "update tt_log set timesheet_id = null".
237       " where timesheet_id = $timesheet_id and group_id = $group_id and org_id = $org_id";
238     $affected = $mdb2->exec($sql);
239     if (is_a($affected, 'PEAR_Error')) return false;
240
241     // Handle expense items.
242     $sql = "update tt_expense_items set timesheet_id = null".
243       " where timesheet_id = $timesheet_id and group_id = $group_id and org_id = $org_id";
244     $affected = $mdb2->exec($sql);
245     if (is_a($affected, 'PEAR_Error')) return false;
246
247     // Delete timesheet.
248     $sql = "update tt_timesheets set status = null".
249       " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
250     $affected = $mdb2->exec($sql);
251     return (!is_a($affected, 'PEAR_Error'));
252   }
253
254   // update function - updates the timesheet in database.
255   static function update($fields) {
256     global $user;
257     $mdb2 = getConnection();
258
259     $group_id = $user->getGroup();
260     $org_id = $user->org_id;
261
262     $timesheet_id = $fields['id']; // Timesheet we are updating.
263     $name = $fields['name']; // Timesheet name.
264     $submitter_comment = $fields['submitter_comment'];
265     $status = $fields['status']; // Project status.
266
267     $sql = "update tt_timesheets set name = ".$mdb2->quote($name).", submitter_comment = ".$mdb2->quote($submitter_comment).
268       ", status = ".$mdb2->quote($status).
269       " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
270     $affected = $mdb2->exec($sql);
271     return (!is_a($affected, 'PEAR_Error'));
272   }
273
274   // isUserValid function is used during access checks and determines whether user id, passed in post, is valid
275   // in current context.
276   static function isUserValid($user_id) {
277     // We have to cover several situations.
278     //
279     // 1) User is a client.
280     // 2) User with view_all_timesheets rights.
281     // 3) User with view_timesheets rights.
282
283     global $user;
284
285     // Step 1.
286     // A client must have view_client_timesheets and
287     // aser must be assigned to one of client projects.
288     if ($user->isClient()) {
289       if (!$user->can('view_client_timesheets'))
290         return false;
291       $valid_users = ttGroupHelper::getUsersForClient($user->client_id);
292       $v = 2;
293     }
294
295     return true;
296   }
297 }