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