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') || !$user->isPluginEnabled('ex')) {
38 header('Location: access_denied.php');
42 // Initialize and store date in session.
43 $cl_date = $request->getParameter('date', @$_SESSION['date']);
44 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
45 if($selected_date->isError())
46 $selected_date = new DateAndTime(DB_DATEFORMAT);
48 $cl_date = $selected_date->toString(DB_DATEFORMAT);
49 $_SESSION['date'] = $cl_date;
51 // Initialize variables.
52 $on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id']) ? $_SESSION['behalf_id'] : $user->id));
53 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
54 $_SESSION['client'] = $cl_client;
55 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
56 $_SESSION['project'] = $cl_project;
57 $cl_item_name = $request->getParameter('item_name');
58 $cl_cost = $request->getParameter('cost');
60 // Elements of expensesForm.
61 $form = new Form('expensesForm');
63 if ($user->canManageTeam()) {
64 $user_list = ttTeamHelper::getActiveUsers(array('putSelfFirst'=>true));
65 if (count($user_list) > 1) {
66 $form->addInput(array('type'=>'combobox',
67 'onchange'=>'this.form.submit();',
68 'name'=>'onBehalfUser',
69 'style'=>'width: 250px;',
70 'value'=>$on_behalf_id,
72 'datakeys'=>array('id','name')));
73 $smarty->assign('on_behalf_control', 1);
77 // Dropdown for clients in MODE_TIME. Use all active clients.
78 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
79 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
80 $form->addInput(array('type'=>'combobox',
81 'onchange'=>'fillProjectDropdown(this.value);',
83 'style'=>'width: 250px;',
85 'data'=>$active_clients,
86 'datakeys'=>array('id', 'name'),
87 'empty'=>array(''=>$i18n->get('dropdown.select'))));
88 // Note: in other modes the client list is filtered to relevant clients only. See below.
91 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
92 // Dropdown for projects assigned to user.
93 $project_list = $user->getAssignedProjects();
94 $form->addInput(array('type'=>'combobox',
95 // 'onchange'=>'fillTaskDropdown(this.value);',
97 'style'=>'width: 250px;',
99 'data'=>$project_list,
100 'datakeys'=>array('id','name'),
101 'empty'=>array(''=>$i18n->get('dropdown.select'))));
103 // Dropdown for clients if the clients plugin is enabled.
104 if ($user->isPluginEnabled('cl')) {
105 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
106 // We need an array of assigned project ids to do some trimming.
107 foreach($project_list as $project)
108 $projects_assigned_to_user[] = $project['id'];
110 // Build a client list out of active clients. Use only clients that are relevant to user.
111 // Also trim their associated project list to only assigned projects (to user).
112 foreach($active_clients as $client) {
113 $projects_assigned_to_client = explode(',', $client['projects']);
114 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
116 $client['projects'] = implode(',', $intersection);
117 $client_list[] = $client;
120 $form->addInput(array('type'=>'combobox',
121 'onchange'=>'fillProjectDropdown(this.value);',
123 'style'=>'width: 250px;',
125 'data'=>$client_list,
126 'datakeys'=>array('id', 'name'),
127 'empty'=>array(''=>$i18n->get('dropdown.select'))));
130 // If predefined expenses are configured, add controls to select an expense and quantity.
131 $predefined_expenses = ttTeamHelper::getPredefinedExpenses($user->team_id);
132 if ($predefined_expenses) {
133 $form->addInput(array('type'=>'combobox',
134 'onchange'=>'recalculateCost();',
135 'name'=>'predefined_expense',
136 'style'=>'width: 250px;',
137 'value'=>$cl_predefined_expense,
138 'data'=>$predefined_expenses,
139 'datakeys'=>array('id', 'name'),
140 'empty'=>array(''=>$i18n->get('dropdown.select'))));
141 $form->addInput(array('type'=>'text','onchange'=>'recalculateCost();','maxlength'=>'40','name'=>'quantity','style'=>'width: 100px;','value'=>$cl_quantity));
143 $form->addInput(array('type'=>'textarea','maxlength'=>'800','name'=>'item_name','style'=>'width: 250px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_item_name));
144 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
145 $form->addInput(array('type'=>'calendar','name'=>'date','highlight'=>'expenses','value'=>$cl_date)); // calendar
146 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
147 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
150 if ($request->isPost()) {
151 if ($request->getParameter('btn_submit')) {
152 // Validate user input.
153 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
154 $err->add($i18n->get('error.client'));
155 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
156 if (!$cl_project) $err->add($i18n->get('error.project'));
158 if (!ttValidString($cl_item_name)) $err->add($i18n->get('error.field'), $i18n->get('label.comment'));
159 if (!ttValidFloat($cl_cost)) $err->add($i18n->get('error.field'), $i18n->get('label.cost'));
161 // Prohibit creating entries in future.
162 if (!$user->future_entries) {
163 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
164 if ($selected_date->after($browser_today))
165 $err->add($i18n->get('error.future_date'));
167 // Finished validating input data.
169 // Prohibit creating entries in locked range.
170 if ($user->isDateLocked($selected_date))
171 $err->add($i18n->get('error.range_locked'));
175 if (ttExpenseHelper::insert(array('date'=>$cl_date,'user_id'=>$user->getActiveUser(),
176 'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
177 header('Location: expenses.php');
180 $err->add($i18n->get('error.db'));
182 } elseif ($request->getParameter('onBehalfUser')) {
183 if($user->canManageTeam()) {
184 unset($_SESSION['behalf_id']);
185 unset($_SESSION['behalf_name']);
187 if($on_behalf_id != $user->id) {
188 $_SESSION['behalf_id'] = $on_behalf_id;
189 $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
191 header('Location: expenses.php');
197 $smarty->assign('day_total', ttExpenseHelper::getTotalForDay($user->getActiveUser(), $cl_date));
198 $smarty->assign('expense_items', ttExpenseHelper::getItems($user->getActiveUser(), $cl_date));
199 $smarty->assign('predefined_expenses', $predefined_expenses);
200 $smarty->assign('client_list', $client_list);
201 $smarty->assign('project_list', $project_list);
202 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
203 $smarty->assign('timestring', $selected_date->toString($user->date_format));
204 $smarty->assign('title', $i18n->get('title.expenses'));
205 $smarty->assign('content_page_name', 'expenses.tpl');
206 $smarty->display('index.tpl');