Refactoring, moving plugin config options into config field.
[timetracker.git] / WEB-INF / lib / ttPredefinedExpenseHelper.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
30 // Class ttPredefinedExpenseHelper is used to help with predefined expense related tasks.
31 class ttPredefinedExpenseHelper {
32
33   // get - gets predefined expense details.
34   static function get($id) {
35     global $user;
36     $mdb2 = getConnection();
37
38     $group_id = $user->getGroup();
39     $org_id = $user->org_id;
40
41     $sql = "select id, name, cost from tt_predefined_expenses".
42       " where id = $id and group_id = $group_id and org_id = $org_id";
43     $res = $mdb2->query($sql);
44     if (!is_a($res, 'PEAR_Error')) {
45       $val = $res->fetchRow();
46       if ($val && $val['id']) {
47         if ('.' != $user->getDecimalMark())
48           $val['cost'] = str_replace('.', $user->getDecimalMark(), $val['cost']);
49         return $val;
50       }
51     }
52     return false;
53   }
54
55   // delete - deletes a predefined expense from tt_predefined_expenses table.
56   static function delete($id) {
57     global $user;
58     $mdb2 = getConnection();
59
60     $group_id = $user->getGroup();
61     $org_id = $user->org_id;
62
63     $sql = "delete from tt_predefined_expenses".
64       " where id = $id and group_id = $group_id and org_id = $org_id";
65     $affected = $mdb2->exec($sql);
66     if (is_a($affected, 'PEAR_Error'))
67       return false;
68
69     return true;
70   }
71
72   // insert function inserts a new predefined expense into database.
73   static function insert($fields) {
74     global $user;
75     $mdb2 = getConnection();
76
77     $group_id = $user->getGroup();
78     $org_id = $user->org_id;
79
80     $name = $fields['name'];
81     $cost = $fields['cost'];
82     if ('.' != $user->getDecimalMark())
83       $cost = str_replace($user->getDecimalMark(), '.', $cost);
84
85     $sql = "insert into tt_predefined_expenses (group_id, org_id, name, cost)".
86       " values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($cost).")";
87     $affected = $mdb2->exec($sql);
88     if (is_a($affected, 'PEAR_Error'))
89       return false;
90
91     return true;
92   }
93
94   // update function - updates a predefined expense in database.
95   static function update($fields) {
96     global $user;
97     $mdb2 = getConnection();
98
99     $group_id = $user->getGroup();
100     $org_id = $user->org_id;
101
102     $predefined_expense_id = (int) $fields['id'];
103     $name = $fields['name'];
104     $cost = $fields['cost'];
105     if ('.' != $user->getDecimalMark())
106       $cost = str_replace($user->getDecimalMark(), '.', $cost);
107
108     $sql = "update tt_predefined_expenses set name = ".$mdb2->quote($name).", cost = ".$mdb2->quote($cost).
109       " where id = $predefined_expense_id and group_id = $group_id and org_id = $org_id";
110     $affected = $mdb2->exec($sql);
111     return (!is_a($affected, 'PEAR_Error'));
112   }
113 }