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