forked from anuko/timetracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathttExpenseHelper.class.php
More file actions
174 lines (147 loc) · 6.07 KB
/
Copy pathttExpenseHelper.class.php
File metadata and controls
174 lines (147 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
// +----------------------------------------------------------------------+
// | Anuko Time Tracker
// +----------------------------------------------------------------------+
// | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
// +----------------------------------------------------------------------+
// | LIBERAL FREEWARE LICENSE: This source code document may be used
// | by anyone for any purpose, and freely redistributed alone or in
// | combination with other software, provided that the license is obeyed.
// |
// | There are only two ways to violate the license:
// |
// | 1. To redistribute this code in source form, with the copyright
// | notice or license removed or altered. (Distributing in compiled
// | forms without embedded copyright notices is permitted).
// |
// | 2. To redistribute modified versions of this code in *any* form
// | that bears insufficient indications that the modifications are
// | not the work of the original author(s).
// |
// | This license applies to this document only, not any other software
// | that it may be combined with.
// |
// +----------------------------------------------------------------------+
// | Contributors:
// | https://www.anuko.com/time_tracker/credits.htm
// +----------------------------------------------------------------------+
// The ttExpenseHelper is a class to help with expense items.
class ttExpenseHelper {
// insert - inserts an entry into tt_expense_items table.
static function insert($fields)
{
$mdb2 = getConnection();
$date = $fields['date'];
$user_id = (int) $fields['user_id'];
$client_id = $fields['client_id'];
$project_id = $fields['project_id'];
$name = $fields['name'];
$cost = str_replace(',', '.', $fields['cost']);
$invoice_id = $fields['invoice_id'];
$status = $fields['status'];
$sql = "insert into tt_expense_items (date, user_id, client_id, project_id, name, cost, invoice_id, status) ".
"values (".$mdb2->quote($date).", $user_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($project_id).
", ".$mdb2->quote($name).", ".$mdb2->quote($cost).", ".$mdb2->quote($invoice_id).", ".$mdb2->quote($status).")";
$affected = $mdb2->exec($sql);
if (is_a($affected, 'PEAR_Error'))
return false;
$id = $mdb2->lastInsertID('tt_expense_items', 'id');
return $id;
}
// update - updates a record in tt_expense_items table.
static function update($fields)
{
$mdb2 = getConnection();
$id = (int) $fields['id'];
$date = $fields['date'];
$user_id = (int) $fields['user_id'];
$client_id = $fields['client_id'];
$project_id = $fields['project_id'];
$name = $fields['name'];
$cost = str_replace(',', '.', $fields['cost']);
$invoice_id = $fields['invoice_id'];
$sql = "UPDATE tt_expense_items set date = ".$mdb2->quote($date).", user_id = $user_id, client_id = ".$mdb2->quote($client_id).
", project_id = ".$mdb2->quote($project_id).", name = ".$mdb2->quote($name).
", cost = ".$mdb2->quote($cost).", invoice_id = ".$mdb2->quote($invoice_id).
" WHERE id = $id";
$affected = $mdb2->exec($sql);
if (is_a($affected, 'PEAR_Error'))
return false;
return true;
}
// markDeleted - marks an item as deleted in tt_expense_items table.
static function markDeleted($id, $user_id) {
$mdb2 = getConnection();
$sql = "update tt_expense_items set status = NULL where id = $id and user_id = $user_id";
$affected = $mdb2->exec($sql);
if (is_a($affected, 'PEAR_Error'))
return false;
return true;
}
// getTotalForDay - gets total expenses for a user for a specific date.
static function getTotalForDay($user_id, $date) {
global $user;
$mdb2 = getConnection();
$sql = "select sum(cost) as sm from tt_expense_items where user_id = $user_id and date = ".$mdb2->quote($date)." and status = 1";
$res = $mdb2->query($sql);
if (!is_a($res, 'PEAR_Error')) {
$val = $res->fetchRow();
$val['sm'] = str_replace('.', $user->decimal_mark, $val['sm']);
return $val['sm'];
}
return false;
}
// getItem - retrieves an entry from tt_expense_items table.
static function getItem($id, $user_id) {
global $user;
$mdb2 = getConnection();
$client_field = null;
if ($user->isPluginEnabled('cl'))
$client_field = ", c.name as client_name";
$left_joins = "";
$left_joins = " left join tt_projects p on (ei.project_id = p.id)";
if ($user->isPluginEnabled('cl'))
$left_joins .= " left join tt_clients c on (ei.client_id = c.id)";
$sql = "select ei.id, ei.date, ei.client_id, ei.project_id, ei.name, ei.cost, ei.invoice_id $client_field, p.name as project_name
from tt_expense_items ei
$left_joins
where ei.id = $id and ei.user_id = $user_id and ei.status = 1";
$res = $mdb2->query($sql);
if (!is_a($res, 'PEAR_Error')) {
if (!$res->numRows()) {
return false;
}
if ($val = $res->fetchRow()) {
$val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
return $val;
}
}
return false;
}
// getItems - returns expense items for a user for a given date.
static function getItems($user_id, $date) {
global $user;
$result = array();
$mdb2 = getConnection();
$client_field = null;
if ($user->isPluginEnabled('cl'))
$client_field = ", c.name as client";
$left_joins = "";
$left_joins = " left join tt_projects p on (ei.project_id = p.id)";
if ($user->isPluginEnabled('cl'))
$left_joins .= " left join tt_clients c on (ei.client_id = c.id)";
$sql = "select ei.id as id $client_field, p.name as project, ei.name as item, ei.cost as cost,
ei.invoice_id from tt_expense_items ei
$left_joins
where ei.date = ".$mdb2->quote($date)." and ei.user_id = $user_id and ei.status = 1
order by ei.id";
$res = $mdb2->query($sql);
if (!is_a($res, 'PEAR_Error')) {
while ($val = $res->fetchRow()) {
$val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
$result[] = $val;
}
} else return false;
return $result;
}
}