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