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 // End of access checks.
55 // Initialize and store date in session.
56 $cl_date = $request->getParameter('date', @$_SESSION['date']);
57 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
58 if($selected_date->isError())
59 $selected_date = new DateAndTime(DB_DATEFORMAT);
61 $cl_date = $selected_date->toString(DB_DATEFORMAT);
62 $_SESSION['date'] = $cl_date;
64 // Initialize variables.
65 $on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id']) ? $_SESSION['behalf_id'] : $user->id));
66 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
67 $_SESSION['client'] = $cl_client;
68 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
69 $_SESSION['project'] = $cl_project;
70 $cl_item_name = $request->getParameter('item_name');
71 $cl_cost = $request->getParameter('cost');
73 // Elements of expensesForm.
74 $form = new Form('expensesForm');
76 if ($user->can('track_expenses')) {
77 if ($user->can('track_own_expenses'))
78 $options = array('status'=>ACTIVE,'max_rank'=>$user->rank-1,'include_self'=>true,'self_first'=>true);
80 $options = array('status'=>ACTIVE,'max_rank'=>$user->rank-1);
81 $user_list = $user->getUsers($options);
82 if (count($user_list) >= 1) {
83 $form->addInput(array('type'=>'combobox',
84 'onchange'=>'this.form.submit();',
85 'name'=>'onBehalfUser',
86 'style'=>'width: 250px;',
87 'value'=>$on_behalf_id,
89 'datakeys'=>array('id','name')));
90 $smarty->assign('on_behalf_control', 1);
94 // Dropdown for clients in MODE_TIME. Use all active clients.
95 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
96 $active_clients = ttGroupHelper::getActiveClients(true);
97 $form->addInput(array('type'=>'combobox',
98 'onchange'=>'fillProjectDropdown(this.value);',
100 'style'=>'width: 250px;',
102 'data'=>$active_clients,
103 'datakeys'=>array('id', 'name'),
104 'empty'=>array(''=>$i18n->get('dropdown.select'))));
105 // Note: in other modes the client list is filtered to relevant clients only. See below.
108 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
109 // Dropdown for projects assigned to user.
110 $project_list = $user->getAssignedProjects();
111 $form->addInput(array('type'=>'combobox',
112 // 'onchange'=>'fillTaskDropdown(this.value);',
114 'style'=>'width: 250px;',
115 'value'=>$cl_project,
116 'data'=>$project_list,
117 'datakeys'=>array('id','name'),
118 'empty'=>array(''=>$i18n->get('dropdown.select'))));
120 // Dropdown for clients if the clients plugin is enabled.
121 if ($user->isPluginEnabled('cl')) {
122 $active_clients = ttGroupHelper::getActiveClients(true);
123 // We need an array of assigned project ids to do some trimming.
124 foreach($project_list as $project)
125 $projects_assigned_to_user[] = $project['id'];
127 // Build a client list out of active clients. Use only clients that are relevant to user.
128 // Also trim their associated project list to only assigned projects (to user).
129 foreach($active_clients as $client) {
130 $projects_assigned_to_client = explode(',', $client['projects']);
131 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
133 $client['projects'] = implode(',', $intersection);
134 $client_list[] = $client;
137 $form->addInput(array('type'=>'combobox',
138 'onchange'=>'fillProjectDropdown(this.value);',
140 'style'=>'width: 250px;',
142 'data'=>$client_list,
143 'datakeys'=>array('id', 'name'),
144 'empty'=>array(''=>$i18n->get('dropdown.select'))));
147 // If predefined expenses are configured, add controls to select an expense and quantity.
148 $predefined_expenses = ttGroupHelper::getPredefinedExpenses();
149 if ($predefined_expenses) {
150 $form->addInput(array('type'=>'combobox',
151 'onchange'=>'recalculateCost();',
152 'name'=>'predefined_expense',
153 'style'=>'width: 250px;',
154 'value'=>$cl_predefined_expense,
155 'data'=>$predefined_expenses,
156 'datakeys'=>array('id', 'name'),
157 'empty'=>array(''=>$i18n->get('dropdown.select'))));
158 $form->addInput(array('type'=>'text','onchange'=>'recalculateCost();','maxlength'=>'40','name'=>'quantity','style'=>'width: 100px;','value'=>$cl_quantity));
160 $form->addInput(array('type'=>'textarea','maxlength'=>'800','name'=>'item_name','style'=>'width: 250px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_item_name));
161 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
162 $form->addInput(array('type'=>'calendar','name'=>'date','highlight'=>'expenses','value'=>$cl_date)); // calendar
163 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
164 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
167 if ($request->isPost()) {
168 if ($request->getParameter('btn_submit')) {
169 // Validate user input.
170 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
171 $err->add($i18n->get('error.client'));
172 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
173 if (!$cl_project) $err->add($i18n->get('error.project'));
175 if (!ttValidString($cl_item_name)) $err->add($i18n->get('error.field'), $i18n->get('label.comment'));
176 if (!ttValidFloat($cl_cost)) $err->add($i18n->get('error.field'), $i18n->get('label.cost'));
178 // Prohibit creating entries in future.
179 if (!$user->future_entries) {
180 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
181 if ($selected_date->after($browser_today))
182 $err->add($i18n->get('error.future_date'));
184 // Finished validating input data.
186 // Prohibit creating entries in locked range.
187 if ($user->isDateLocked($selected_date))
188 $err->add($i18n->get('error.range_locked'));
192 if (ttExpenseHelper::insert(array('date'=>$cl_date,'client_id'=>$cl_client,
193 'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
194 header('Location: expenses.php');
197 $err->add($i18n->get('error.db'));
199 } elseif ($request->getParameter('onBehalfUser')) {
200 if($user->can('track_expenses')) {
201 unset($_SESSION['behalf_id']);
202 unset($_SESSION['behalf_name']);
204 if($on_behalf_id != $user->id) {
205 $_SESSION['behalf_id'] = $on_behalf_id;
206 $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
208 header('Location: expenses.php');
214 $smarty->assign('day_total', ttExpenseHelper::getTotalForDay($cl_date));
215 $smarty->assign('expense_items', ttExpenseHelper::getItems($cl_date));
216 $smarty->assign('predefined_expenses', $predefined_expenses);
217 $smarty->assign('client_list', $client_list);
218 $smarty->assign('project_list', $project_list);
219 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
220 $smarty->assign('timestring', $selected_date->toString($user->date_format));
221 $smarty->assign('title', $i18n->get('title.expenses'));
222 $smarty->assign('content_page_name', 'expenses.tpl');
223 $smarty->display('index.tpl');