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