Started to replace canManageTeam calls with right checks.
[timetracker.git] / WEB-INF / lib / ttExpenseHelper.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 // The ttExpenseHelper is a class to help with expense items.
30 class ttExpenseHelper {
31   // insert - inserts an entry into tt_expense_items table.
32   static function insert($fields)
33   {
34     $mdb2 = getConnection();
35
36     $date = $fields['date'];
37     $user_id = (int) $fields['user_id'];
38     $client_id = $fields['client_id'];
39     $project_id = $fields['project_id'];
40     $name = $fields['name'];
41     $cost = str_replace(',', '.', $fields['cost']);
42     $invoice_id = $fields['invoice_id'];
43     $status = $fields['status'];
44     $paid = (int) $fields['paid'];
45
46     $sql = "insert into tt_expense_items (date, user_id, client_id, project_id, name, cost, invoice_id, paid, status) ".
47       "values (".$mdb2->quote($date).", $user_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($project_id).
48       ", ".$mdb2->quote($name).", ".$mdb2->quote($cost).", ".$mdb2->quote($invoice_id).", $paid, ".$mdb2->quote($status).")";
49     $affected = $mdb2->exec($sql);
50     if (is_a($affected, 'PEAR_Error'))
51       return false;
52
53     $id = $mdb2->lastInsertID('tt_expense_items', 'id');
54     return $id;
55   }
56
57   // update - updates a record in tt_expense_items table.
58   static function update($fields)
59   {
60     global $user;
61     $mdb2 = getConnection();
62
63     $id = (int) $fields['id'];
64     $date = $fields['date'];
65     $user_id = (int) $fields['user_id'];
66     $client_id = $fields['client_id'];
67     $project_id = $fields['project_id'];
68     $name = $fields['name'];
69     $cost = str_replace(',', '.', $fields['cost']);
70     $invoice_id = $fields['invoice_id'];
71
72     $paid_part = '';
73     if ($user->can('manage_invoices') && $user->isPluginEnabled('ps')) {
74       $paid_part = $fields['paid'] ? ', paid = 1' : ', paid = 0';
75     }
76
77     $sql = "UPDATE tt_expense_items set date = ".$mdb2->quote($date).", user_id = $user_id, client_id = ".$mdb2->quote($client_id).
78       ", project_id = ".$mdb2->quote($project_id).", name = ".$mdb2->quote($name).
79       ", cost = ".$mdb2->quote($cost)."$paid_part, invoice_id = ".$mdb2->quote($invoice_id).
80       " WHERE id = $id";
81
82     $affected = $mdb2->exec($sql);
83     if (is_a($affected, 'PEAR_Error'))
84       return false;
85
86     return true;
87   }
88
89   // markDeleted - marks an item as deleted in tt_expense_items table.
90   static function markDeleted($id, $user_id) {
91     $mdb2 = getConnection();
92
93     $sql = "update tt_expense_items set status = NULL where id = $id and user_id = $user_id";
94     $affected = $mdb2->exec($sql);
95     if (is_a($affected, 'PEAR_Error'))
96       return false;
97
98     return true;
99   }
100
101   // getTotalForDay - gets total expenses for a user for a specific date.
102   static function getTotalForDay($user_id, $date) {
103     global $user;
104
105     $mdb2 = getConnection();
106
107     $sql = "select sum(cost) as sm from tt_expense_items where user_id = $user_id and date = ".$mdb2->quote($date)." and status = 1";
108     $res = $mdb2->query($sql);
109     if (!is_a($res, 'PEAR_Error')) {
110       $val = $res->fetchRow();
111       $val['sm'] = str_replace('.', $user->decimal_mark, $val['sm']);
112       return $val['sm'];
113     }
114     return false;
115   }
116
117   // getItem - retrieves an entry from tt_expense_items table.
118   static function getItem($id, $user_id) {
119     global $user;
120
121     $mdb2 = getConnection();
122
123     $client_field = null;
124     if ($user->isPluginEnabled('cl'))
125       $client_field = ", c.name as client_name";
126
127     $left_joins = "";
128     $left_joins = " left join tt_projects p on (ei.project_id = p.id)";
129     if ($user->isPluginEnabled('cl'))
130       $left_joins .= " left join tt_clients c on (ei.client_id = c.id)";
131
132     $sql = "select ei.id, ei.date, ei.client_id, ei.project_id, ei.name, ei.cost, ei.invoice_id, ei.paid $client_field, p.name as project_name
133       from tt_expense_items ei
134       $left_joins
135       where ei.id = $id and ei.user_id = $user_id and ei.status = 1";
136     $res = $mdb2->query($sql);
137     if (!is_a($res, 'PEAR_Error')) {
138       if (!$res->numRows()) {
139         return false;
140       }
141       if ($val = $res->fetchRow()) {
142         $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
143         return $val;
144       }
145     }
146     return false;
147   }
148
149   // getItems - returns expense items for a user for a given date.
150   static function getItems($user_id, $date) {
151     global $user;
152
153     $result = array();
154     $mdb2 = getConnection();
155
156     $client_field = null;
157     if ($user->isPluginEnabled('cl'))
158       $client_field = ", c.name as client";
159
160     $left_joins = "";
161     $left_joins = " left join tt_projects p on (ei.project_id = p.id)";
162     if ($user->isPluginEnabled('cl'))
163       $left_joins .= " left join tt_clients c on (ei.client_id = c.id)";
164
165     $sql = "select ei.id as id $client_field, p.name as project, ei.name as item, ei.cost as cost,
166       ei.invoice_id from tt_expense_items ei
167       $left_joins
168       where ei.date = ".$mdb2->quote($date)." and ei.user_id = $user_id and ei.status = 1
169       order by ei.id";
170
171     $res = $mdb2->query($sql);
172     if (!is_a($res, 'PEAR_Error')) {
173       while ($val = $res->fetchRow()) {
174         $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
175         $result[] = $val;
176       }
177     } else return false;
178
179     return $result;
180   }
181 }