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