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