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 $user_id = $user->getUser();
37 $group_id = $user->getGroup();
38 $org_id = $user->org_id;
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;
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'));
58 // update - updates a record in tt_expense_items table.
59 static function update($fields) {
61 $mdb2 = getConnection();
63 $user_id = $user->getUser();
64 $group_id = $user->getGroup();
65 $org_id = $user->org_id;
67 $id = (int) $fields['id'];
68 $date = $fields['date'];
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'];
76 if ($user->can('manage_invoices') && $user->isPluginEnabled('ps')) {
77 $paid_part = $fields['paid'] ? ', paid = 1' : ', paid = 0';
79 $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
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'));
89 // markDeleted - marks an item as deleted in tt_expense_items table.
90 static function markDeleted($id) {
92 $mdb2 = getConnection();
94 $user_id = $user->getUser();
95 $group_id = $user->getGroup();
96 $org_id = $user->org_id;
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'));
104 // getTotalForDay - gets total expenses for a user for a specific date.
105 static function getTotalForDay($date) {
107 $mdb2 = getConnection();
109 $user_id = $user->getUser();
110 $group_id = $user->getGroup();
111 $org_id = $user->org_id;
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'];
125 // getItem - retrieves an entry from tt_expense_items table.
126 static function getItem($id) {
128 $mdb2 = getConnection();
130 $user_id = $user->getUser();
131 $group_id = $user->getGroup();
132 $org_id = $user->org_id;
134 $client_field = null;
135 if ($user->isPluginEnabled('cl'))
136 $client_field = ", c.name as client_name";
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)";
143 $sql = "select ei.id, ei.date, ei.client_id, ei.project_id, ei.name, ei.cost, ei.invoice_id, ei.approved,".
144 " ei.paid $client_field, p.name as project_name".
145 " from tt_expense_items ei $left_joins".
146 " 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";
147 $res = $mdb2->query($sql);
148 if (!is_a($res, 'PEAR_Error')) {
149 if (!$res->numRows()) {
152 if ($val = $res->fetchRow()) {
153 $val['cost'] = str_replace('.', $user->getDecimalMark(), $val['cost']);
160 // getItems - returns expense items for a user for a given date.
161 static function getItems($date) {
163 $mdb2 = getConnection();
165 $user_id = $user->getUser();
166 $group_id = $user->getGroup();
167 $org_id = $user->org_id;
171 $client_field = null;
172 if ($user->isPluginEnabled('cl'))
173 $client_field = ", c.name as client";
176 $left_joins = " left join tt_projects p on (ei.project_id = p.id)";
177 if ($user->isPluginEnabled('cl'))
178 $left_joins .= " left join tt_clients c on (ei.client_id = c.id)";
180 $sql = "select ei.id as id $client_field, p.name as project, ei.name as item, ei.cost as cost,".
181 " ei.invoice_id, ei.approved from tt_expense_items ei $left_joins".
182 " where ei.date = ".$mdb2->quote($date)." and ei.user_id = $user_id".
183 " and ei.group_id = $group_id and ei.org_id = $org_id and ei.status = 1 order by ei.id";
185 $res = $mdb2->query($sql);
186 if (!is_a($res, 'PEAR_Error')) {
187 while ($val = $res->fetchRow()) {
188 $val['cost'] = str_replace('.', $user->getDecimalMark(), $val['cost']);