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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
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)
34 $mdb2 = getConnection();
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'];
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'))
53 $id = $mdb2->lastInsertID('tt_expense_items', 'id');
57 // update - updates a record in tt_expense_items table.
58 static function update($fields)
61 $mdb2 = getConnection();
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'];
73 if ($user->canManageTeam() && $user->isPluginEnabled('ps')) {
74 $paid_part = $fields['paid'] ? ', paid = 1' : ', paid = 0';
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).
82 $affected = $mdb2->exec($sql);
83 if (is_a($affected, 'PEAR_Error'))
89 // markDeleted - marks an item as deleted in tt_expense_items table.
90 static function markDeleted($id, $user_id) {
91 $mdb2 = getConnection();
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'))
101 // getTotalForDay - gets total expenses for a user for a specific date.
102 static function getTotalForDay($user_id, $date) {
105 $mdb2 = getConnection();
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']);
117 // getItem - retrieves an entry from tt_expense_items table.
118 static function getItem($id, $user_id) {
121 $mdb2 = getConnection();
123 $client_field = null;
124 if ($user->isPluginEnabled('cl'))
125 $client_field = ", c.name as client_name";
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)";
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
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()) {
141 if ($val = $res->fetchRow()) {
142 $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
149 // getItems - returns expense items for a user for a given date.
150 static function getItems($user_id, $date) {
154 $mdb2 = getConnection();
156 $client_field = null;
157 if ($user->isPluginEnabled('cl'))
158 $client_field = ", c.name as client";
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)";
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
168 where ei.date = ".$mdb2->quote($date)." and ei.user_id = $user_id and ei.status = 1
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']);