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 (!ttAccessCheck(right_data_entry) || !$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->getMethod()=='POST' ? null : @$_SESSION['client']));
54 $_SESSION['client'] = $cl_client;
55 $cl_project = $request->getParameter('project', ($request->getMethod()=='POST' ? 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->getKey('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->getKey('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->getKey('dropdown.select'))));
130 $form->addInput(array('type'=>'textarea','maxlength'=>'800','name'=>'item_name','style'=>'width: 250px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_item_name));
131 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
132 $form->addInput(array('type'=>'calendar','name'=>'date','highlight'=>'expenses','value'=>$cl_date)); // calendar
133 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
134 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.submit')));
137 if ($request->isPost()) {
138 if ($request->getParameter('btn_submit')) {
139 // Validate user input.
140 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
141 $err->add($i18n->getKey('error.client'));
142 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
143 if (!$cl_project) $err->add($i18n->getKey('error.project'));
145 if (!ttValidString($cl_item_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.item'));
146 if (!ttValidFloat($cl_cost)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.cost'));
148 // Prohibit creating entries in future.
149 if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES)) {
150 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
151 if ($selected_date->after($browser_today))
152 $err->add($i18n->getKey('error.future_date'));
154 // Finished validating input data.
156 // Prohibit creating entries in locked range.
157 if ($user->isDateLocked($selected_date))
158 $err->add($i18n->getKey('error.range_locked'));
162 if (ttExpenseHelper::insert(array('date'=>$cl_date,'user_id'=>$user->getActiveUser(),
163 'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
164 header('Location: expenses.php');
167 $err->add($i18n->getKey('error.db'));
169 } elseif ($request->getParameter('onBehalfUser')) {
170 if($user->canManageTeam()) {
171 unset($_SESSION['behalf_id']);
172 unset($_SESSION['behalf_name']);
174 if($on_behalf_id != $user->id) {
175 $_SESSION['behalf_id'] = $on_behalf_id;
176 $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
178 header('Location: expenses.php');
184 $smarty->assign('day_total', ttExpenseHelper::getTotalForDay($user->getActiveUser(), $cl_date));
185 $smarty->assign('expense_items', ttExpenseHelper::getItems($user->getActiveUser(), $cl_date));
186 $smarty->assign('client_list', $client_list);
187 $smarty->assign('project_list', $project_list);
188 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
189 $smarty->assign('timestring', $selected_date->toString($user->date_format));
190 $smarty->assign('title', $i18n->getKey('title.expenses'));
191 $smarty->assign('content_page_name', 'expenses.tpl');
192 $smarty->display('index.tpl');