Improved population of on behalf dropdowns on week view and expenses pages.
[timetracker.git] / mobile / expenses.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 require_once('../initialize.php');
30 import('form.Form');
31 import('ttUserHelper');
32 import('ttTeamHelper');
33 import('DateAndTime');
34 import('ttExpenseHelper');
35
36 // Access checks.
37 if (!(ttAccessAllowed('track_own_expenses') || ttAccessAllowed('track_expenses'))) {
38   header('Location: access_denied.php');
39   exit();
40 }
41 if (!$user->isPluginEnabled('ex')) {
42   header('Location: feature_disabled.php');
43   exit();
44 }
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.
47   exit();
48 }
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.
51   exit();
52 }
53
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);
59 if(!$cl_date)
60   $cl_date = $selected_date->toString(DB_DATEFORMAT);
61 $_SESSION['date'] = $cl_date;
62
63 // Determine previous and next dates for simple navigation.
64 $prev_date = date('Y-m-d', strtotime('-1 day', strtotime($cl_date)));
65 $next_date = date('Y-m-d', strtotime('+1 day', strtotime($cl_date)));
66
67 // Initialize variables.
68 $on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id']) ? $_SESSION['behalf_id'] : $user->id));
69 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
70 $_SESSION['client'] = $cl_client;
71 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
72 $_SESSION['project'] = $cl_project;
73 $cl_item_name = $request->getParameter('item_name');
74 $cl_cost = $request->getParameter('cost');
75
76 // Elements of expensesForm.
77 $form = new Form('expensesForm');
78
79 if ($user->can('track_expenses')) {
80   if ($user->can('track_own_expenses'))
81     $options = array('status'=>ACTIVE,'max_rank'=>$user->rank-1,'include_self'=>true,'self_first'=>true);
82   else
83     $options = array('status'=>ACTIVE,'max_rank'=>$user->rank-1);
84   $user_list = $user->getUsers($options);
85   if (count($user_list) >= 1) {
86     $form->addInput(array('type'=>'combobox',
87       'onchange'=>'this.form.submit();',
88       'name'=>'onBehalfUser',
89       'style'=>'width: 250px;',
90       'value'=>$on_behalf_id,
91       'data'=>$user_list,
92       'datakeys'=>array('id','name')));
93     $smarty->assign('on_behalf_control', 1);
94   }
95 }
96
97 // Dropdown for clients in MODE_TIME. Use all active clients.
98 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
99     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
100     $form->addInput(array('type'=>'combobox',
101       'onchange'=>'fillProjectDropdown(this.value);',
102       'name'=>'client',
103       'style'=>'width: 250px;',
104       'value'=>$cl_client,
105       'data'=>$active_clients,
106       'datakeys'=>array('id', 'name'),
107       'empty'=>array(''=>$i18n->get('dropdown.select'))));
108   // Note: in other modes the client list is filtered to relevant clients only. See below.
109 }
110
111 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
112   // Dropdown for projects assigned to user.
113   $project_list = $user->getAssignedProjects();
114   $form->addInput(array('type'=>'combobox',
115     // 'onchange'=>'fillTaskDropdown(this.value);',
116     'name'=>'project',
117     'style'=>'width: 250px;',
118     'value'=>$cl_project,
119     'data'=>$project_list,
120     'datakeys'=>array('id','name'),
121     'empty'=>array(''=>$i18n->get('dropdown.select'))));
122
123   // Dropdown for clients if the clients plugin is enabled.
124   if ($user->isPluginEnabled('cl')) {
125     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
126     // We need an array of assigned project ids to do some trimming. 
127     foreach($project_list as $project)
128       $projects_assigned_to_user[] = $project['id'];
129
130     // Build a client list out of active clients. Use only clients that are relevant to user.
131     // Also trim their associated project list to only assigned projects (to user).
132     foreach($active_clients as $client) {
133       $projects_assigned_to_client = explode(',', $client['projects']);
134       $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
135       if ($intersection) {
136         $client['projects'] = implode(',', $intersection);
137         $client_list[] = $client;
138       }
139     }
140     $form->addInput(array('type'=>'combobox',
141       'onchange'=>'fillProjectDropdown(this.value);',
142       'name'=>'client',
143       'style'=>'width: 250px;',
144       'value'=>$cl_client,
145       'data'=>$client_list,
146       'datakeys'=>array('id', 'name'),
147       'empty'=>array(''=>$i18n->get('dropdown.select'))));
148   }
149 }
150 // If predefined expenses are configured, add controls to select an expense and quantity.
151 $predefined_expenses = ttTeamHelper::getPredefinedExpenses($user->team_id);
152 if ($predefined_expenses) {
153   $form->addInput(array('type'=>'combobox',
154     'onchange'=>'recalculateCost();',
155     'name'=>'predefined_expense',
156     'style'=>'width: 250px;',
157     'value'=>$cl_predefined_expense,
158     'data'=>$predefined_expenses,
159     'datakeys'=>array('id', 'name'),
160     'empty'=>array(''=>$i18n->get('dropdown.select'))));
161   $form->addInput(array('type'=>'text','onchange'=>'recalculateCost();','maxlength'=>'40','name'=>'quantity','style'=>'width: 100px;','value'=>$cl_quantity));
162 }
163 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'item_name','style'=>'width: 250px;','value'=>$cl_item_name));
164 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
165 $form->addInput(array('type'=>'calendar','name'=>'date','highlight'=>'expenses','value'=>$cl_date)); // calendar
166 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
167 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
168
169 // Submit.
170 if ($request->isPost()) {
171   if ($request->getParameter('btn_submit')) {
172     // Validate user input.
173     if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
174       $err->add($i18n->get('error.client'));
175     if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
176       if (!$cl_project) $err->add($i18n->get('error.project'));
177     }
178     if (!ttValidString($cl_item_name)) $err->add($i18n->get('error.field'), $i18n->get('label.item'));
179     if (!ttValidFloat($cl_cost)) $err->add($i18n->get('error.field'), $i18n->get('label.cost'));
180
181     // Prohibit creating entries in future.
182     if (!$user->future_entries) {
183       $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
184       if ($selected_date->after($browser_today))
185         $err->add($i18n->get('error.future_date'));
186     }
187     // Finished validating input data.
188
189     // Prohibit creating entries in locked range.
190     if ($user->isDateLocked($selected_date))
191       $err->add($i18n->get('error.range_locked'));
192
193     // Insert record.
194     if ($err->no()) {
195       if (ttExpenseHelper::insert(array('date'=>$cl_date,'user_id'=>$user->getActiveUser(),
196         'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
197         header('Location: expenses.php');
198         exit();
199       } else
200         $err->add($i18n->get('error.db'));
201     }
202   } elseif ($request->getParameter('onBehalfUser')) {
203     if($user->canManageTeam()) {
204       unset($_SESSION['behalf_id']);
205       unset($_SESSION['behalf_name']);
206
207       if($on_behalf_id != $user->id) {
208         $_SESSION['behalf_id'] = $on_behalf_id;
209         $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
210       }
211       header('Location: expenses.php');
212       exit();
213     }
214   }
215 }
216
217 $smarty->assign('next_date', $next_date);
218 $smarty->assign('prev_date', $prev_date);
219 $smarty->assign('day_total', ttExpenseHelper::getTotalForDay($user->getActiveUser(), $cl_date));
220 $smarty->assign('expense_items', ttExpenseHelper::getItems($user->getActiveUser(), $cl_date));
221 $smarty->assign('predefined_expenses', $predefined_expenses);
222 $smarty->assign('client_list', $client_list);
223 $smarty->assign('project_list', $project_list);
224 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
225 $smarty->assign('timestring', $selected_date->toString($user->date_format));
226 $smarty->assign('title', $i18n->get('title.expenses'));
227 $smarty->assign('content_page_name', 'mobile/expenses.tpl');
228 $smarty->display('mobile/index.tpl');