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