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)) {
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'=>'text','maxlength'=>'100','name'=>'item_name','style'=>'width: 250px;','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')));
136 // Determine lock date. Time entries earlier than lock date cannot be created or modified.
137 $lock_interval = $user->lock_interval;
139 if ($lock_interval > 0) {
140 $lockdate = new DateAndTime();
141 $lockdate->decDay($lock_interval);
145 if ($request->isPost()) {
146 if ($request->getParameter('btn_submit')) {
147 // Validate user input.
148 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
149 $err->add($i18n->getKey('error.client'));
150 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
151 if (!$cl_project) $err->add($i18n->getKey('error.project'));
153 if (!ttValidString($cl_item_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.item'));
154 if (!ttValidFloat($cl_cost)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.cost'));
156 // Prohibit creating entries in future.
157 if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES)) {
158 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
159 if ($selected_date->after($browser_today))
160 $err->add($i18n->getKey('error.future_date'));
162 // Finished validating input data.
164 // Prohibit creating time entries in locked interval.
165 if($lockdate && $selected_date->before($lockdate))
166 $err->add($i18n->getKey('error.period_locked'));
170 if (ttExpenseHelper::insert(array('date'=>$cl_date,'user_id'=>$user->getActiveUser(),
171 'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
172 header('Location: expenses.php');
175 $err->add($i18n->getKey('error.db'));
177 } elseif ($request->getParameter('onBehalfUser')) {
178 if($user->canManageTeam()) {
179 unset($_SESSION['behalf_id']);
180 unset($_SESSION['behalf_name']);
182 if($on_behalf_id != $user->id) {
183 $_SESSION['behalf_id'] = $on_behalf_id;
184 $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
186 header('Location: expenses.php');
192 $smarty->assign('day_total', ttExpenseHelper::getTotalForDay($user->getActiveUser(), $cl_date));
193 $smarty->assign('expense_items', ttExpenseHelper::getItems($user->getActiveUser(), $cl_date));
194 $smarty->assign('client_list', $client_list);
195 $smarty->assign('project_list', $project_list);
196 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
197 $smarty->assign('timestring', $selected_date->toString($user->date_format));
198 $smarty->assign('title', $i18n->getKey('title.expenses'));
199 $smarty->assign('content_page_name', 'expenses.tpl');
200 $smarty->display('index.tpl');