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->exists()) {
46 header('Location: access_denied.php'); // Nobody to enter expenses for.
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.
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.
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.
63 // End of access checks.
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);
71 $user_id = $user->getUser();
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);
80 $cl_date = $selected_date->toString(DB_DATEFORMAT);
81 $_SESSION['date'] = $cl_date;
83 $tracking_mode = $user->getTrackingMode();
84 $show_project = MODE_PROJECTS == $tracking_mode || MODE_PROJECTS_AND_TASKS == $tracking_mode;
86 // Initialize variables.
87 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
88 $_SESSION['client'] = $cl_client;
89 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
90 $_SESSION['project'] = $cl_project;
91 $cl_item_name = $request->getParameter('item_name');
92 $cl_cost = $request->getParameter('cost');
94 // Elements of expensesForm.
95 $form = new Form('expensesForm');
97 if ($user->can('track_expenses')) {
98 $rank = $user->getMaxRankForGroup($user->getGroup());
99 if ($user->can('track_own_expenses'))
100 $options = array('status'=>ACTIVE,'max_rank'=>$rank,'include_self'=>true,'self_first'=>true);
102 $options = array('status'=>ACTIVE,'max_rank'=>$rank);
103 $user_list = $user->getUsers($options);
104 if (count($user_list) >= 1) {
105 $form->addInput(array('type'=>'combobox',
106 'onchange'=>'this.form.user_changed.value=1;this.form.submit();',
108 'style'=>'width: 250px;',
111 'datakeys'=>array('id','name')));
112 $form->addInput(array('type'=>'hidden','name'=>'user_changed'));
113 $smarty->assign('user_dropdown', 1);
117 // Dropdown for clients in MODE_TIME. Use all active clients.
118 if (MODE_TIME == $tracking_mode && $user->isPluginEnabled('cl')) {
119 $active_clients = ttGroupHelper::getActiveClients(true);
120 $form->addInput(array('type'=>'combobox',
121 'onchange'=>'fillProjectDropdown(this.value);',
123 'style'=>'width: 250px;',
125 'data'=>$active_clients,
126 'datakeys'=>array('id', 'name'),
127 'empty'=>array(''=>$i18n->get('dropdown.select'))));
128 // Note: in other modes the client list is filtered to relevant clients only. See below.
132 // Dropdown for projects assigned to user.
133 $project_list = $user->getAssignedProjects();
134 $form->addInput(array('type'=>'combobox',
136 'style'=>'width: 250px;',
137 'value'=>$cl_project,
138 'data'=>$project_list,
139 'datakeys'=>array('id','name'),
140 'empty'=>array(''=>$i18n->get('dropdown.select'))));
142 // Dropdown for clients if the clients plugin is enabled.
143 if ($user->isPluginEnabled('cl')) {
144 $active_clients = ttGroupHelper::getActiveClients(true);
145 // We need an array of assigned project ids to do some trimming.
146 foreach($project_list as $project)
147 $projects_assigned_to_user[] = $project['id'];
149 // Build a client list out of active clients. Use only clients that are relevant to user.
150 // Also trim their associated project list to only assigned projects (to user).
151 foreach($active_clients as $client) {
152 $projects_assigned_to_client = explode(',', $client['projects']);
153 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
155 $client['projects'] = implode(',', $intersection);
156 $client_list[] = $client;
159 $form->addInput(array('type'=>'combobox',
160 'onchange'=>'fillProjectDropdown(this.value);',
162 'style'=>'width: 250px;',
164 'data'=>$client_list,
165 'datakeys'=>array('id', 'name'),
166 'empty'=>array(''=>$i18n->get('dropdown.select'))));
169 // If predefined expenses are configured, add controls to select an expense and quantity.
170 $predefined_expenses = ttGroupHelper::getPredefinedExpenses();
171 if ($predefined_expenses) {
172 $form->addInput(array('type'=>'combobox',
173 'onchange'=>'recalculateCost();',
174 'name'=>'predefined_expense',
175 'style'=>'width: 250px;',
176 'value'=>$cl_predefined_expense,
177 'data'=>$predefined_expenses,
178 'datakeys'=>array('id', 'name'),
179 'empty'=>array(''=>$i18n->get('dropdown.select'))));
180 $form->addInput(array('type'=>'text','onchange'=>'recalculateCost();','maxlength'=>'40','name'=>'quantity','style'=>'width: 100px;','value'=>$cl_quantity));
182 $form->addInput(array('type'=>'textarea','maxlength'=>'800','name'=>'item_name','style'=>'width: 250px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_item_name));
183 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
184 $form->addInput(array('type'=>'calendar','name'=>'date','highlight'=>'expenses','value'=>$cl_date)); // calendar
185 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
186 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
189 if ($request->isPost()) {
190 if ($request->getParameter('btn_submit')) {
191 // Validate user input.
192 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
193 $err->add($i18n->get('error.client'));
194 if ($show_project && !$cl_project)
195 $err->add($i18n->get('error.project'));
196 if (!ttValidString($cl_item_name)) $err->add($i18n->get('error.field'), $i18n->get('label.comment'));
197 if (!ttValidFloat($cl_cost)) $err->add($i18n->get('error.field'), $i18n->get('label.cost'));
199 // Prohibit creating entries in future.
200 if (!$user->getConfigOption('future_entries')) {
201 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
202 if ($selected_date->after($browser_today))
203 $err->add($i18n->get('error.future_date'));
205 // Finished validating input data.
207 // Prohibit creating entries in locked range.
208 if ($user->isDateLocked($selected_date))
209 $err->add($i18n->get('error.range_locked'));
213 if (ttExpenseHelper::insert(array('date'=>$cl_date,'client_id'=>$cl_client,
214 'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
215 header('Location: expenses.php');
218 $err->add($i18n->get('error.db'));
223 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
224 $smarty->assign('show_project', $show_project);
225 $smarty->assign('day_total', ttExpenseHelper::getTotalForDay($cl_date));
226 $smarty->assign('expense_items', ttExpenseHelper::getItems($cl_date));
227 $smarty->assign('predefined_expenses', $predefined_expenses);
228 $smarty->assign('client_list', $client_list);
229 $smarty->assign('project_list', $project_list);
230 $smarty->assign('timestring', $selected_date->toString($user->getDateFormat()));
231 $smarty->assign('title', $i18n->get('title.expenses'));
232 $smarty->assign('content_page_name', 'expenses.tpl');
233 $smarty->display('index.tpl');