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