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