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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 require_once('../initialize.php');
31 import('ttUserHelper');
32 import('ttGroupHelper');
33 import('DateAndTime');
34 import('ttExpenseHelper');
37 if (!(ttAccessAllowed('track_own_expenses') || ttAccessAllowed('track_expenses'))) {
38 header('Location: access_denied.php');
41 if (!$user->isPluginEnabled('ex')) {
42 header('Location: feature_disabled.php');
45 if ($user->behalf_id && (!$user->can('track_expenses') || !$user->checkBehalfId())) {
46 header('Location: access_denied.php'); // Trying on behalf, but no right or wrong user.
49 if (!$user->behalf_id && !$user->can('track_own_expenses') && !$user->adjustBehalfId()) {
50 header('Location: access_denied.php'); // Trying as self, but no right for self, and noone to work on behalf.
53 if ($request->isPost() && $request->getParameter('user')) {
54 if (!$user->isUserValid($request->getParameter('user'))) {
55 header('Location: access_denied.php'); // Wrong user id on post.
59 // End of access checks.
61 // Determine user for which we display this page.
62 $userChanged = $request->getParameter('user_changed');
63 if ($request->isPost() && $userChanged) {
64 $user_id = $request->getParameter('user');
65 $user->setOnBehalfUser($user_id);
67 $user_id = $user->getUser();
68 // Handle a situation for no users in on behalf group.
69 if ($user->behalfGroup && $user_id == $user->id)
73 // Initialize and store date in session.
74 $cl_date = $request->getParameter('date', @$_SESSION['date']);
75 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
76 if($selected_date->isError())
77 $selected_date = new DateAndTime(DB_DATEFORMAT);
79 $cl_date = $selected_date->toString(DB_DATEFORMAT);
80 $_SESSION['date'] = $cl_date;
82 // Determine previous and next dates for simple navigation.
83 $prev_date = date('Y-m-d', strtotime('-1 day', strtotime($cl_date)));
84 $next_date = date('Y-m-d', strtotime('+1 day', strtotime($cl_date)));
86 $tracking_mode = $user->getTrackingMode();
87 $show_project = MODE_PROJECTS == $tracking_mode || MODE_PROJECTS_AND_TASKS == $tracking_mode;
89 // Initialize variables.
90 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
91 $_SESSION['client'] = $cl_client;
92 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
93 $_SESSION['project'] = $cl_project;
94 $cl_item_name = $request->getParameter('item_name');
95 $cl_cost = $request->getParameter('cost');
97 // Elements of expensesForm.
98 $form = new Form('expensesForm');
100 if ($user->can('track_expenses')) {
101 $rank = $user->getMaxRankForGroup($user->getGroup());
102 if ($user->can('track_own_expenses'))
103 $options = array('status'=>ACTIVE,'max_rank'=>$rank,'include_self'=>true,'self_first'=>true);
105 $options = array('status'=>ACTIVE,'max_rank'=>$rank);
106 $user_list = $user->getUsers($options);
107 if (count($user_list) >= 1) {
108 $form->addInput(array('type'=>'combobox',
109 'onchange'=>'this.form.user_changed.value=1;this.form.submit();',
111 'style'=>'width: 250px;',
114 'datakeys'=>array('id','name')));
115 $form->addInput(array('type'=>'hidden','name'=>'user_changed'));
116 $smarty->assign('user_dropdown', 1);
120 // Dropdown for clients in MODE_TIME. Use all active clients.
121 if (MODE_TIME == $tracking_mode && $user->isPluginEnabled('cl')) {
122 $active_clients = ttGroupHelper::getActiveClients(true);
123 $form->addInput(array('type'=>'combobox',
124 'onchange'=>'fillProjectDropdown(this.value);',
126 'style'=>'width: 250px;',
128 'data'=>$active_clients,
129 'datakeys'=>array('id', 'name'),
130 'empty'=>array(''=>$i18n->get('dropdown.select'))));
131 // Note: in other modes the client list is filtered to relevant clients only. See below.
135 // Dropdown for projects assigned to user.
136 $project_list = $user->getAssignedProjects();
137 $form->addInput(array('type'=>'combobox',
138 // 'onchange'=>'fillTaskDropdown(this.value);',
140 'style'=>'width: 250px;',
141 'value'=>$cl_project,
142 'data'=>$project_list,
143 'datakeys'=>array('id','name'),
144 'empty'=>array(''=>$i18n->get('dropdown.select'))));
146 // Dropdown for clients if the clients plugin is enabled.
147 if ($user->isPluginEnabled('cl')) {
148 $active_clients = ttGroupHelper::getActiveClients(true);
149 // We need an array of assigned project ids to do some trimming.
150 foreach($project_list as $project)
151 $projects_assigned_to_user[] = $project['id'];
153 // Build a client list out of active clients. Use only clients that are relevant to user.
154 // Also trim their associated project list to only assigned projects (to user).
155 foreach($active_clients as $client) {
156 $projects_assigned_to_client = explode(',', $client['projects']);
157 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
159 $client['projects'] = implode(',', $intersection);
160 $client_list[] = $client;
163 $form->addInput(array('type'=>'combobox',
164 'onchange'=>'fillProjectDropdown(this.value);',
166 'style'=>'width: 250px;',
168 'data'=>$client_list,
169 'datakeys'=>array('id', 'name'),
170 'empty'=>array(''=>$i18n->get('dropdown.select'))));
173 // If predefined expenses are configured, add controls to select an expense and quantity.
174 $predefined_expenses = ttGroupHelper::getPredefinedExpenses();
175 if ($predefined_expenses) {
176 $form->addInput(array('type'=>'combobox',
177 'onchange'=>'recalculateCost();',
178 'name'=>'predefined_expense',
179 'style'=>'width: 250px;',
180 'value'=>$cl_predefined_expense,
181 'data'=>$predefined_expenses,
182 'datakeys'=>array('id', 'name'),
183 'empty'=>array(''=>$i18n->get('dropdown.select'))));
184 $form->addInput(array('type'=>'text','onchange'=>'recalculateCost();','maxlength'=>'40','name'=>'quantity','style'=>'width: 100px;','value'=>$cl_quantity));
186 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'item_name','style'=>'width: 250px;','value'=>$cl_item_name));
187 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
188 $form->addInput(array('type'=>'calendar','name'=>'date','highlight'=>'expenses','value'=>$cl_date)); // calendar
189 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
190 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
193 if ($request->isPost()) {
194 if ($request->getParameter('btn_submit')) {
195 // Validate user input.
196 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
197 $err->add($i18n->get('error.client'));
198 if ($show_project && !$cl_project)
199 $err->add($i18n->get('error.project'));
200 if (!ttValidString($cl_item_name)) $err->add($i18n->get('error.field'), $i18n->get('label.item'));
201 if (!ttValidFloat($cl_cost)) $err->add($i18n->get('error.field'), $i18n->get('label.cost'));
203 // Prohibit creating entries in future.
204 if (!$user->getConfigOption('future_entries')) {
205 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
206 if ($selected_date->after($browser_today))
207 $err->add($i18n->get('error.future_date'));
209 // Finished validating input data.
211 // Prohibit creating entries in locked range.
212 if ($user->isDateLocked($selected_date))
213 $err->add($i18n->get('error.range_locked'));
217 if (ttExpenseHelper::insert(array('date'=>$cl_date,'client_id'=>$cl_client,
218 'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
219 header('Location: expenses.php');
222 $err->add($i18n->get('error.db'));
224 } elseif ($request->getParameter('onBehalfUser')) {
225 if($user->can('track_expenses')) {
226 unset($_SESSION['behalf_id']);
227 unset($_SESSION['behalf_name']);
229 if($on_behalf_id != $user->id) {
230 $_SESSION['behalf_id'] = $on_behalf_id;
231 $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
233 header('Location: expenses.php');
239 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
240 $smarty->assign('show_project', $show_project);
241 $smarty->assign('next_date', $next_date);
242 $smarty->assign('prev_date', $prev_date);
243 $smarty->assign('day_total', ttExpenseHelper::getTotalForDay($cl_date));
244 $smarty->assign('expense_items', ttExpenseHelper::getItems($cl_date));
245 $smarty->assign('predefined_expenses', $predefined_expenses);
246 $smarty->assign('client_list', $client_list);
247 $smarty->assign('project_list', $project_list);
248 $smarty->assign('timestring', $selected_date->toString($user->getDateFormat()));
249 $smarty->assign('title', $i18n->get('title.expenses'));
250 $smarty->assign('content_page_name', 'mobile/expenses.tpl');
251 $smarty->display('mobile/index.tpl');