Fixed predefined expenses logic for comma as decimal mark.
[timetracker.git] / WEB-INF / lib / ttProjectHelper.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('ttTeamHelper');
30 import('ttUserHelper');
31
32 // Class ttProjectHelper is used to help with project related tasks.
33 class ttProjectHelper {
34         
35   // getAssignedProjects - returns an array of assigned projects.
36   static function getAssignedProjects($user_id)
37   {
38         global $user;
39         
40     $result = array();
41     $mdb2 = getConnection();
42     
43     // Do a query with inner join to get assigned projects.
44     $sql = "select p.id, p.name, p.tasks, upb.rate from tt_projects p
45       inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id and upb.status = 1)
46       where p.team_id = $user->team_id and p.status = 1 order by p.name";
47     $res = $mdb2->query($sql);
48     if (!is_a($res, 'PEAR_Error')) {
49       while ($val = $res->fetchRow()) {
50         $result[] = $val;
51       }
52     }
53     return $result;
54   }
55
56   // getRates - returns an array of project rates for user, including deassigned and deactivated projects.
57   static function getRates($user_id)
58   {
59     global $user;
60         
61     $result = array();
62     $mdb2 = getConnection();
63     
64     $sql = "select p.id, upb.rate from tt_projects p
65       inner join tt_user_project_binds upb on (upb.user_id = $user_id and upb.project_id = p.id)
66       where team_id = $user->team_id";
67     $res = $mdb2->query($sql);
68     if (!is_a($res, 'PEAR_Error')) {
69       while ($val = $res->fetchRow()) {
70         $val['rate'] = str_replace('.', $user->decimal_mark, $val['rate']);
71         $result[] = $val;
72       }
73     }
74     return $result;
75   }
76   
77   // getProjects - returns an array of active and inactive projects in a team.
78   static function getProjects()
79   {
80     global $user;
81
82     $result = array();
83     $mdb2 = getConnection();
84     
85     $sql = "select id, name, tasks from tt_projects
86       where team_id = $user->team_id and (status = 0 or status = 1) order by name";     
87         
88     $res = $mdb2->query($sql);
89     if (!is_a($res, 'PEAR_Error')) {
90       while ($val = $res->fetchRow()) {
91         $result[] = $val;
92       }
93     }
94     return $result;
95   }
96
97   // getProjectsForClient - returns an array of active and inactive projects in a team for a client.
98   static function getProjectsForClient()
99   {
100         global $user;
101                 
102         $result = array();
103     $mdb2 = getConnection();
104     
105     $sql = "select p.id, p.name, p.tasks from tt_projects p
106           inner join tt_client_project_binds cpb on (cpb.client_id = $user->client_id and cpb.project_id = p.id)
107           where p.team_id = $user->team_id and (p.status = 0 or p.status = 1)
108       order by p.name";         
109         
110     $res = $mdb2->query($sql);
111     if (!is_a($res, 'PEAR_Error')) {
112       while ($val = $res->fetchRow()) {
113         $result[] = $val;
114       }
115     }
116     return $result;
117   }
118   
119   
120   // get - gets details of the project identified by its id. 
121   static function get($id)
122   {
123     global $user;
124  
125     $mdb2 = getConnection();
126
127     $sql = "select id, name, description, status, tasks from tt_projects where id = $id and team_id = $user->team_id and (status = 0 or status = 1)";
128     $res = $mdb2->query($sql);
129     if (!is_a($res, 'PEAR_Error')) {
130       $val = $res->fetchRow();
131           if ($val && $val['id'])
132         return $val;
133     }
134     return false;
135   }
136   
137   // The getProjectByName looks up a project by name.
138   static function getProjectByName($name) {
139         
140     $mdb2 = getConnection();
141     global $user;
142
143     $sql = "select id from tt_projects where team_id = $user->team_id and name = ".
144       $mdb2->quote($name)." and (status = 1 or status = 0)";
145         $res = $mdb2->query($sql);
146         if (!is_a($res, 'PEAR_Error')) {
147       $val = $res->fetchRow();
148           if ($val && $val['id'])
149         return $val;
150     }
151     return false;
152   }
153   
154   
155   // delete - deletes things associated with a project and marks the project as deleted. 
156   static function delete($id) {
157     $mdb2 = getConnection();
158     
159     // Delete user binds to this project.
160     $sql = "delete from tt_user_project_binds where project_id = $id";
161     $affected = $mdb2->exec($sql);
162     if (is_a($affected, 'PEAR_Error'))
163       return false;
164     
165         // Delete task binds to this project.
166     $sql = "delete from tt_project_task_binds where project_id = $id";
167     $affected = $mdb2->exec($sql);
168     if (is_a($affected, 'PEAR_Error'))
169       return false;
170     
171     // Remove associated tasks.
172     $sql = "update tt_projects set tasks = NULL where id = $id";
173     $affected = $mdb2->exec($sql);
174     if (is_a($affected, 'PEAR_Error'))
175       return false;
176
177     // Mark project as deleted.
178     $sql = "update tt_projects set status = NULL where id = $id";
179     $affected = $mdb2->exec($sql);
180     if (is_a($affected, 'PEAR_Error'))
181       return false;
182
183         return true;
184   }
185   
186   // insert function inserts a new project into database.
187   static function insert($fields)
188   {
189     $mdb2 = getConnection();
190
191     $team_id = (int) $fields['team_id'];
192
193     $name = $fields['name'];
194     $description = $fields['description'];
195     $users = $fields['users'];
196     $tasks = $fields['tasks'];
197     $comma_separated = implode(',', $tasks); // This is a comma-separated list of associated task ids.
198     $status = $fields['status'];
199     
200     $sql = "insert into tt_projects (team_id, name, description, tasks, status)
201       values ($team_id, ".$mdb2->quote($name).", ".$mdb2->quote($description).", ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
202     $affected = $mdb2->exec($sql);
203     if (is_a($affected, 'PEAR_Error'))
204       return false;
205     
206     $last_id = 0;
207     $sql = "select last_insert_id() as last_insert_id";
208     $res = $mdb2->query($sql);
209     $val = $res->fetchRow();
210     $last_id = $val['last_insert_id'];
211
212     // Bind the project to users.
213     $active_users = ttTeamHelper::getActiveUsers(array('getAllFields'=>true));
214     foreach ($active_users as $u) {
215       if(in_array($u['id'], $users)) {
216         $sql = "insert into tt_user_project_binds (project_id, user_id, status, rate) values(
217           $last_id, ".$u['id'].", 1, ".$u['rate'].")";
218         $affected = $mdb2->exec($sql);
219         if (is_a($affected, 'PEAR_Error'))
220           return false;
221       }
222     }
223
224     // Bind the project to tasks in tt_project_task_binds table.
225     $all_tasks = ttTeamHelper::getAllTasks($team_id);
226     foreach ($all_tasks as $task) {
227       if(in_array($task['id'], $tasks)) {
228         $sql = "insert into tt_project_task_binds (project_id, task_id) values($last_id, ".$task['id'].")";
229         $affected = $mdb2->exec($sql);
230         if (is_a($affected, 'PEAR_Error'))
231           return false;
232       }
233     }
234
235     return $last_id;
236   } 
237
238   // update function - updates the project in database.
239   static function update($fields)
240   {
241     $mdb2 = getConnection();
242     
243     $project_id = $fields['id']; // Project we are updating.
244     $name = $fields['name']; // Project name.
245     $description = $fields['description']; // Project description.
246     $users_to_bind = $fields['users']; // Users to bind with project.
247     $tasks_to_bind = $fields['tasks']; // Tasks to bind with project.
248     $status = $fields['status']; // Project status.
249     
250     // Update records in tt_user_project_binds table.
251     $sql = "select user_id, status from tt_user_project_binds where project_id = $project_id";
252     $all_users = array();
253     $users_to_update = array();
254     $res2 = $mdb2->query($sql);
255     while ($row = $res2->fetchRow()) {
256       if(!in_array($row['user_id'], $users_to_bind)) { 
257         // Delete tt_user_project_binds record (safely).
258         ttUserHelper::deleteBind($row['user_id'], $project_id);
259       } elseif (!$row['status']) {
260         // If we are here, status of the bind is not active. Memorize such users to update their bind status.
261         $users_to_update[] = $row['user_id'];  // Users we need to update in tt_user_project_binds.
262       }
263       $all_users[] = $row['user_id']; // All users from tt_user_project_binds for project.
264     }
265     // Insert records.
266     $users_to_add = array_diff($users_to_bind, $all_users); // Users missing from tt_user_project_binds, that we need to insert.
267     if(count($users_to_add) > 0) {
268       $sql = "select id, rate from tt_users where id in (".join(', ', $users_to_add).")";
269       $res = $mdb2->query($sql);
270       while ($row = $res->fetchRow()) {
271         $user_rate[$row['id']] = $row['rate'];
272       }
273       foreach ($users_to_add as $id) {
274         $sql = "insert into tt_user_project_binds (user_id, project_id, rate, status) values($id, $project_id, ".$user_rate[$id].", 1)";
275         $affected = $mdb2->exec($sql);
276         if (is_a($affected, 'PEAR_Error'))
277           return false;
278       }
279     }
280     // Update status (to active) in existing tt_user_project_binds records.
281     if ($users_to_update) {
282       $sql = "update tt_user_project_binds set status = 1 where project_id = $project_id and user_id in (".join(', ', $users_to_update).")";
283       $affected = $mdb2->exec($sql);
284       if (is_a($affected, 'PEAR_Error'))
285         return false;
286     }
287     // End of updating tt_user_project_binds table.
288
289     // Update records in tt_project_task_binds table.
290     // Obtain existing task binds.
291     $existing_task_binds = array();
292     $sql = "select task_id from tt_project_task_binds where project_id = $project_id";
293     $res = $mdb2->query($sql);
294     while ($val = $res->fetchRow()) {
295       $existing_task_binds[] = $val['task_id'];
296     }
297     // Determine which task binds to delete and which ones to add.
298     $task_binds_to_delete = array_diff($existing_task_binds, $tasks_to_bind);
299     $task_binds_to_add = array_diff($tasks_to_bind, $existing_task_binds);
300     foreach ($task_binds_to_delete as $task_id) {
301       $sql = "delete from tt_project_task_binds where project_id = $project_id and task_id = $task_id";
302       $affected = $mdb2->exec($sql);
303       if (is_a($affected, 'PEAR_Error'))
304         return false;
305     }    
306     foreach ($task_binds_to_add as $task_id) {
307       $sql = "insert into tt_project_task_binds (project_id, task_id) values($project_id, $task_id)";
308       $affected = $mdb2->exec($sql);
309       if (is_a($affected, 'PEAR_Error'))
310         return false;
311     }
312     // End of updating tt_project_task_binds table.
313     
314     // Update project name, description, tasks and status in tt_projects table.
315     $comma_separated = implode(",", $tasks_to_bind); // This is a comma-separated list of associated task ids.
316     $sql = "update tt_projects set name = ".$mdb2->quote($name).", description = ".$mdb2->quote($description).", tasks = ".$mdb2->quote($comma_separated).", status = $status where id = $project_id";
317     $affected = $mdb2->exec($sql);
318     return (!is_a($affected, 'PEAR_Error'));
319   }
320   
321   /*
322   // getAssignedUsers - returns an array of user ids assigned to a project.
323   static function getAssignedUsers($project_id)
324   {
325         global $user;
326         
327     $result = array();
328     $mdb2 = getConnection();
329     
330     // Do a query with inner join to get assigned users.
331     $sql = "select id, name from tt_users u
332       inner join tt_user_project_binds ub on (ub.user_id = u.id and ub.project_id = $project_id and ub.status = 1)";
333     $res = $mdb2->query($sql);
334     if (!is_a($res, 'PEAR_Error')) {
335       while ($val = $res->fetchRow()) {
336         $result[] = $val;
337       }
338     }
339     return $result;
340   }*/
341 }