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