Added validation of checkbox group input on project config.
[timetracker.git] / mobile / expenses.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 require_once('../initialize.php');
30 import('form.Form');
31 import('ttUserHelper');
32 import('ttGroupHelper');
33 import('DateAndTime');
34 import('ttExpenseHelper');
35
36 // Access checks.
37 if (!(ttAccessAllowed('track_own_expenses') || ttAccessAllowed('track_expenses'))) {
38   header('Location: access_denied.php');
39   exit();
40 }
41 if (!$user->isPluginEnabled('ex')) {
42   header('Location: feature_disabled.php');
43   exit();
44 }
45 if (!$user->exists()) {
46   header('Location: access_denied.php'); // Nobody to enter expenses for.
47   exit();
48 }
49 if ($user->behalf_id && (!$user->can('track_expenses') || !$user->checkBehalfId())) {
50   header('Location: access_denied.php'); // Trying on behalf, but no right or wrong user.
51   exit();
52 }
53 if (!$user->behalf_id && !$user->can('track_own_expenses') && !$user->adjustBehalfId()) {
54   header('Location: access_denied.php'); // Trying as self, but no right for self, and noone to work on behalf.
55   exit();
56 }
57 if ($request->isPost() && $request->getParameter('user')) {
58   if (!$user->isUserValid($request->getParameter('user'))) {
59     header('Location: access_denied.php'); // Wrong user id on post.
60     exit();
61   }
62 }
63 // End of access checks.
64
65 // Determine user for which we display this page.
66 $userChanged = $request->getParameter('user_changed');
67 if ($request->isPost() && $userChanged) {
68   $user_id = $request->getParameter('user');
69   $user->setOnBehalfUser($user_id);
70 } else {
71   $user_id = $user->getUser();
72 }
73
74 // Initialize and store date in session.
75 $cl_date = $request->getParameter('date', @$_SESSION['date']);
76 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
77 if($selected_date->isError())
78   $selected_date = new DateAndTime(DB_DATEFORMAT);
79 if(!$cl_date)
80   $cl_date = $selected_date->toString(DB_DATEFORMAT);
81 $_SESSION['date'] = $cl_date;
82
83 // Determine previous and next dates for simple navigation.
84 $prev_date = date('Y-m-d', strtotime('-1 day', strtotime($cl_date)));
85 $next_date = date('Y-m-d', strtotime('+1 day', strtotime($cl_date)));
86
87 $tracking_mode = $user->getTrackingMode();
88 $show_project = MODE_PROJECTS == $tracking_mode || MODE_PROJECTS_AND_TASKS == $tracking_mode;
89
90 // Initialize variables.
91 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
92 $_SESSION['client'] = $cl_client;
93 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
94 $_SESSION['project'] = $cl_project;
95 $cl_item_name = $request->getParameter('item_name');
96 $cl_cost = $request->getParameter('cost');
97
98 // Elements of expensesForm.
99 $form = new Form('expensesForm');
100
101 if ($user->can('track_expenses')) {
102   $rank = $user->getMaxRankForGroup($user->getGroup());
103   if ($user->can('track_own_expenses'))
104     $options = array('status'=>ACTIVE,'max_rank'=>$rank,'include_self'=>true,'self_first'=>true);
105   else
106     $options = array('status'=>ACTIVE,'max_rank'=>$rank);
107   $user_list = $user->getUsers($options);
108   if (count($user_list) >= 1) {
109     $form->addInput(array('type'=>'combobox',
110       'onchange'=>'this.form.user_changed.value=1;this.form.submit();',
111       'name'=>'user',
112       'style'=>'width: 250px;',
113       'value'=>$user_id,
114       'data'=>$user_list,
115       'datakeys'=>array('id','name')));
116     $form->addInput(array('type'=>'hidden','name'=>'user_changed'));
117     $smarty->assign('user_dropdown', 1);
118   }
119 }
120
121 // Dropdown for clients in MODE_TIME. Use all active clients.
122 if (MODE_TIME == $tracking_mode && $user->isPluginEnabled('cl')) {
123     $active_clients = ttGroupHelper::getActiveClients(true);
124     $form->addInput(array('type'=>'combobox',
125       'onchange'=>'fillProjectDropdown(this.value);',
126       'name'=>'client',
127       'style'=>'width: 250px;',
128       'value'=>$cl_client,
129       'data'=>$active_clients,
130       'datakeys'=>array('id', 'name'),
131       'empty'=>array(''=>$i18n->get('dropdown.select'))));
132   // Note: in other modes the client list is filtered to relevant clients only. See below.
133 }
134
135 if ($show_project) {
136   // Dropdown for projects assigned to user.
137   $project_list = $user->getAssignedProjects();
138   $form->addInput(array('type'=>'combobox',
139     // 'onchange'=>'fillTaskDropdown(this.value);',
140     'name'=>'project',
141     'style'=>'width: 250px;',
142     'value'=>$cl_project,
143     'data'=>$project_list,
144     'datakeys'=>array('id','name'),
145     'empty'=>array(''=>$i18n->get('dropdown.select'))));
146
147   // Dropdown for clients if the clients plugin is enabled.
148   if ($user->isPluginEnabled('cl')) {
149     $active_clients = ttGroupHelper::getActiveClients(true);
150     // We need an array of assigned project ids to do some trimming. 
151     foreach($project_list as $project)
152       $projects_assigned_to_user[] = $project['id'];
153
154     // Build a client list out of active clients. Use only clients that are relevant to user.
155     // Also trim their associated project list to only assigned projects (to user).
156     foreach($active_clients as $client) {
157       $projects_assigned_to_client = explode(',', $client['projects']);
158       $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
159       if ($intersection) {
160         $client['projects'] = implode(',', $intersection);
161         $client_list[] = $client;
162       }
163     }
164     $form->addInput(array('type'=>'combobox',
165       'onchange'=>'fillProjectDropdown(this.value);',
166       'name'=>'client',
167       'style'=>'width: 250px;',
168       'value'=>$cl_client,
169       'data'=>$client_list,
170       'datakeys'=>array('id', 'name'),
171       'empty'=>array(''=>$i18n->get('dropdown.select'))));
172   }
173 }
174 // If predefined expenses are configured, add controls to select an expense and quantity.
175 $predefined_expenses = ttGroupHelper::getPredefinedExpenses();
176 if ($predefined_expenses) {
177   $form->addInput(array('type'=>'combobox',
178     'onchange'=>'recalculateCost();',
179     'name'=>'predefined_expense',
180     'style'=>'width: 250px;',
181     'value'=>$cl_predefined_expense,
182     'data'=>$predefined_expenses,
183     'datakeys'=>array('id', 'name'),
184     'empty'=>array(''=>$i18n->get('dropdown.select'))));
185   $form->addInput(array('type'=>'text','onchange'=>'recalculateCost();','maxlength'=>'40','name'=>'quantity','style'=>'width: 100px;','value'=>$cl_quantity));
186 }
187 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'item_name','style'=>'width: 250px;','value'=>$cl_item_name));
188 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
189 $form->addInput(array('type'=>'calendar','name'=>'date','highlight'=>'expenses','value'=>$cl_date)); // calendar
190 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
191 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
192
193 // Submit.
194 if ($request->isPost()) {
195   if ($request->getParameter('btn_submit')) {
196     // Validate user input.
197     if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
198       $err->add($i18n->get('error.client'));
199     if ($show_project && !$cl_project)
200       $err->add($i18n->get('error.project'));
201     if (!ttValidString($cl_item_name)) $err->add($i18n->get('error.field'), $i18n->get('label.item'));
202     if (!ttValidFloat($cl_cost)) $err->add($i18n->get('error.field'), $i18n->get('label.cost'));
203
204     // Prohibit creating entries in future.
205     if (!$user->getConfigOption('future_entries')) {
206       $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
207       if ($selected_date->after($browser_today))
208         $err->add($i18n->get('error.future_date'));
209     }
210     // Finished validating input data.
211
212     // Prohibit creating entries in locked range.
213     if ($user->isDateLocked($selected_date))
214       $err->add($i18n->get('error.range_locked'));
215
216     // Insert record.
217     if ($err->no()) {
218       if (ttExpenseHelper::insert(array('date'=>$cl_date,'client_id'=>$cl_client,
219           'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
220         header('Location: expenses.php');
221         exit();
222       } else
223         $err->add($i18n->get('error.db'));
224     }
225   } elseif ($request->getParameter('onBehalfUser')) {
226     if($user->can('track_expenses')) {
227       unset($_SESSION['behalf_id']);
228       unset($_SESSION['behalf_name']);
229
230       if($on_behalf_id != $user->id) {
231         $_SESSION['behalf_id'] = $on_behalf_id;
232         $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
233       }
234       header('Location: expenses.php');
235       exit();
236     }
237   }
238 }
239
240 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
241 $smarty->assign('show_project', $show_project);
242 $smarty->assign('next_date', $next_date);
243 $smarty->assign('prev_date', $prev_date);
244 $smarty->assign('day_total', ttExpenseHelper::getTotalForDay($cl_date));
245 $smarty->assign('expense_items', ttExpenseHelper::getItems($cl_date));
246 $smarty->assign('predefined_expenses', $predefined_expenses);
247 $smarty->assign('client_list', $client_list);
248 $smarty->assign('project_list', $project_list);
249 $smarty->assign('timestring', $selected_date->toString($user->getDateFormat()));
250 $smarty->assign('title', $i18n->get('title.expenses'));
251 $smarty->assign('content_page_name', 'mobile/expenses.tpl');
252 $smarty->display('mobile/index.tpl');