More progress on timesheets.
[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 getTimesheets obtains timesheets for user.
83   static function getTimesheets($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 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 }