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 // Determine previous and next dates for simple navigation.
52 $prev_date = date('Y-m-d', strtotime('-1 day', strtotime($cl_date)));
53 $next_date = date('Y-m-d', strtotime('+1 day', strtotime($cl_date)));
55 // Initialize variables.
56 $on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id']) ? $_SESSION['behalf_id'] : $user->id));
57 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
58 $_SESSION['client'] = $cl_client;
59 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
60 $_SESSION['project'] = $cl_project;
61 $cl_item_name = $request->getParameter('item_name');
62 $cl_cost = $request->getParameter('cost');
64 // Elements of expensesForm.
65 $form = new Form('expensesForm');
67 if ($user->canManageTeam()) {
68 $user_list = ttTeamHelper::getActiveUsers(array('putSelfFirst'=>true));
69 if (count($user_list) > 1) {
70 $form->addInput(array('type'=>'combobox',
71 'onchange'=>'this.form.submit();',
72 'name'=>'onBehalfUser',
73 'style'=>'width: 250px;',
74 'value'=>$on_behalf_id,
76 'datakeys'=>array('id','name')));
77 $smarty->assign('on_behalf_control', 1);
81 // Dropdown for clients in MODE_TIME. Use all active clients.
82 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
83 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
84 $form->addInput(array('type'=>'combobox',
85 'onchange'=>'fillProjectDropdown(this.value);',
87 'style'=>'width: 250px;',
89 'data'=>$active_clients,
90 'datakeys'=>array('id', 'name'),
91 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
92 // Note: in other modes the client list is filtered to relevant clients only. See below.
95 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
96 // Dropdown for projects assigned to user.
97 $project_list = $user->getAssignedProjects();
98 $form->addInput(array('type'=>'combobox',
99 // 'onchange'=>'fillTaskDropdown(this.value);',
101 'style'=>'width: 250px;',
102 'value'=>$cl_project,
103 'data'=>$project_list,
104 'datakeys'=>array('id','name'),
105 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
107 // Dropdown for clients if the clients plugin is enabled.
108 if ($user->isPluginEnabled('cl')) {
109 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
110 // We need an array of assigned project ids to do some trimming.
111 foreach($project_list as $project)
112 $projects_assigned_to_user[] = $project['id'];
114 // Build a client list out of active clients. Use only clients that are relevant to user.
115 // Also trim their associated project list to only assigned projects (to user).
116 foreach($active_clients as $client) {
117 $projects_assigned_to_client = explode(',', $client['projects']);
118 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
120 $client['projects'] = implode(',', $intersection);
121 $client_list[] = $client;
124 $form->addInput(array('type'=>'combobox',
125 'onchange'=>'fillProjectDropdown(this.value);',
127 'style'=>'width: 250px;',
129 'data'=>$client_list,
130 'datakeys'=>array('id', 'name'),
131 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
134 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'item_name','style'=>'width: 250px;','value'=>$cl_item_name));
135 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
136 $form->addInput(array('type'=>'calendar','name'=>'date','highlight'=>'expenses','value'=>$cl_date)); // calendar
137 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
138 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.submit')));
141 if ($request->isPost()) {
142 if ($request->getParameter('btn_submit')) {
143 // Validate user input.
144 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
145 $err->add($i18n->getKey('error.client'));
146 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
147 if (!$cl_project) $err->add($i18n->getKey('error.project'));
149 if (!ttValidString($cl_item_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.item'));
150 if (!ttValidFloat($cl_cost)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.cost'));
152 // Prohibit creating entries in future.
153 if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES)) {
154 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
155 if ($selected_date->after($browser_today))
156 $err->add($i18n->getKey('error.future_date'));
158 // Finished validating input data.
160 // Prohibit creating entries in locked range.
161 if ($user->isDateLocked($selected_date))
162 $err->add($i18n->getKey('error.range_locked'));
166 if (ttExpenseHelper::insert(array('date'=>$cl_date,'user_id'=>$user->getActiveUser(),
167 'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
168 header('Location: expenses.php');
171 $err->add($i18n->getKey('error.db'));
173 } elseif ($request->getParameter('onBehalfUser')) {
174 if($user->canManageTeam()) {
175 unset($_SESSION['behalf_id']);
176 unset($_SESSION['behalf_name']);
178 if($on_behalf_id != $user->id) {
179 $_SESSION['behalf_id'] = $on_behalf_id;
180 $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
182 header('Location: expenses.php');
188 $smarty->assign('next_date', $next_date);
189 $smarty->assign('prev_date', $prev_date);
190 $smarty->assign('day_total', ttExpenseHelper::getTotalForDay($user->getActiveUser(), $cl_date));
191 $smarty->assign('expense_items', ttExpenseHelper::getItems($user->getActiveUser(), $cl_date));
192 $smarty->assign('client_list', $client_list);
193 $smarty->assign('project_list', $project_list);
194 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
195 $smarty->assign('timestring', $selected_date->toString($user->date_format));
196 $smarty->assign('title', $i18n->getKey('title.expenses'));
197 $smarty->assign('content_page_name', 'mobile/expenses.tpl');
198 $smarty->display('mobile/index.tpl');