3e44c8c345d69d62a2b68e32ba2a5275fd2110c7
[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
32 // Class ttTimesheetHelper is used to help with project related tasks.
33 class ttTimesheetHelper {
34
35   // The getTimesheetByName looks up a project by name.
36   static function getTimesheetByName($name, $user_id) {
37     global $user;
38     $mdb2 = getConnection();
39
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 = 1 or status = 0)";
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   // insert function inserts a new timesheet into database.
56   static function insert($fields)
57   {
58     global $user;
59     $mdb2 = getConnection();
60
61     $group_id = $user->getGroup();
62     $org_id = $user->org_id;
63
64     $user_id = $fields['user_id'];
65     $client_id = $fields['client_id'];
66     $name = $fields['name'];
67     $submitter_comment = $fields['comment'];
68
69     $sql = "insert into tt_timesheets (user_id, group_id, org_id, client_id, name, submitter_comment)".
70       " values ($user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($name).", ".$mdb2->quote($submitter_comment).")";
71     $affected = $mdb2->exec($sql);
72     if (is_a($affected, 'PEAR_Error'))
73       return false;
74
75     $last_id = $mdb2->lastInsertID('tt_timesheets', 'id');
76
77     // TODO: Associate report items with new timesheet.
78
79     return $last_id;
80   }
81
82   // The getActiveTimesheets obtains active timesheets for a user.
83   static function getActiveTimesheets($user_id)
84   {
85     global $user;
86     $mdb2 = getConnection();
87
88     $group_id = $user->getGroup();
89     $org_id = $user->org_id;
90
91     // $addPaidStatus = $user->isPluginEnabled('ps');
92     $result = array();
93
94     if ($user->isClient())
95       $client_part = "and ts.client_id = $user->client_id";
96
97     $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name, ts.submit_status, ts.approval_status from tt_timesheets ts".
98       " left join tt_clients c on (c.id = ts.client_id)".
99       " where ts.status = 1 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
100       " $client_part order by ts.name";
101     $res = $mdb2->query($sql);
102     $result = array();
103     if (!is_a($res, 'PEAR_Error')) {
104       $dt = new DateAndTime(DB_DATEFORMAT);
105       while ($val = $res->fetchRow()) {
106         //if ($addPaidStatus)
107         //  $val['paid'] = ttTimesheetHelper::isPaid($val['id']);
108         $result[] = $val;
109       }
110     }
111     return $result;
112   }
113
114   // The getInactiveTimesheets obtains inactive timesheets for a user.
115   static function getInactiveTimesheets($user_id)
116   {
117     global $user;
118     $mdb2 = getConnection();
119
120     $group_id = $user->getGroup();
121     $org_id = $user->org_id;
122
123     // $addPaidStatus = $user->isPluginEnabled('ps');
124     $result = array();
125
126     if ($user->isClient())
127       $client_part = "and ts.client_id = $user->client_id";
128
129     $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name, ts.submit_status, ts.approval_status from tt_timesheets ts".
130       " left join tt_clients c on (c.id = ts.client_id)".
131       " where ts.status = 0 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
132       " $client_part order by ts.name";
133     $res = $mdb2->query($sql);
134     $result = array();
135     if (!is_a($res, 'PEAR_Error')) {
136       $dt = new DateAndTime(DB_DATEFORMAT);
137       while ($val = $res->fetchRow()) {
138         //if ($addPaidStatus)
139         //  $val['paid'] = ttTimesheetHelper::isPaid($val['id']);
140         $result[] = $val;
141       }
142     }
143     return $result;
144   }
145
146   // getTimesheet - obtains timesheet data from the database.
147   static function getTimesheet($timesheet_id) {
148     global $user;
149     $mdb2 = getConnection();
150
151     $group_id = $user->getGroup();
152     $org_id = $user->org_id;
153
154     if ($user->isClient()) $client_part = "and client_id = $user->client_id";
155
156     $sql = "select * from tt_timesheets".
157       " where id = $timesheet_id and group_id = $group_id and org_id = $org_id $client_part and status is not null";
158     $res = $mdb2->query($sql);
159     if (!is_a($res, 'PEAR_Error')) {
160       if ($val = $res->fetchRow())
161         return $val;
162     }
163     return false;
164   }
165
166   // delete - deletes timesheet from the database.
167   static function delete($timesheet_id) {
168     global $user;
169     $mdb2 = getConnection();
170
171     $group_id = $user->getGroup();
172     $org_id = $user->org_id;
173
174     // Handle time records.
175     $sql = "update tt_log set timesheet_id = null".
176       " where timesheet_id = $timesheet_id and group_id = $group_id and org_id = $org_id";
177     $affected = $mdb2->exec($sql);
178     if (is_a($affected, 'PEAR_Error')) return false;
179
180     // Handle expense items.
181     $sql = "update tt_expense_items set timesheet_id = null".
182       " where timesheet_id = $timesheet_id and group_id = $group_id and org_id = $org_id";
183     $affected = $mdb2->exec($sql);
184     if (is_a($affected, 'PEAR_Error')) return false;
185
186     // Delete timesheet.
187     $sql = "update tt_timesheets set status = null".
188       " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
189     $affected = $mdb2->exec($sql);
190     return (!is_a($affected, 'PEAR_Error'));
191   }
192
193   // update function - updates the timesheet in database.
194   static function update($fields) {
195     global $user;
196     $mdb2 = getConnection();
197
198     $group_id = $user->getGroup();
199     $org_id = $user->org_id;
200
201     $timesheet_id = $fields['id']; // Timesheet we are updating.
202     $name = $fields['name']; // Timesheet name.
203     $submitter_comment = $fields['submitter_comment'];
204     $status = $fields['status']; // Project status.
205
206     $sql = "update tt_timesheets set name = ".$mdb2->quote($name).", submitter_comment = ".$mdb2->quote($submitter_comment).
207       ", status = ".$mdb2->quote($status).
208       " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
209     $affected = $mdb2->exec($sql);
210     return (!is_a($affected, 'PEAR_Error'));
211   }
212
213   // isUserValid function is used during access checks and determines whether user id, passed in post, is valid
214   // in current context.
215   static function isUserValid($user_id) {
216     // We have to cover several situations.
217     //
218     // 1) User is a client.
219     // 2) User with view_all_timesheets rights.
220     // 3) User with view_timesheets rights.
221
222     global $user;
223
224     // Step 1.
225     // A client must have view_client_timesheets and
226     // aser must be assigned to one of client projects.
227     if ($user->isClient()) {
228       if (!$user->can('view_client_timesheets'))
229         return false;
230       $valid_users = ttGroupHelper::getUsersForClient($user->client_id);
231       $v = 2;
232     }
233
234     return true;
235   }
236 }