Some refactoring of access checks - in progress.
[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
46 // Initialize and store date in session.
47 $cl_date = $request->getParameter('date', @$_SESSION['date']);
48 $selected_date = new DateAndTime(DB_DATEFORMAT, $cl_date);
49 if($selected_date->isError())
50   $selected_date = new DateAndTime(DB_DATEFORMAT);
51 if(!$cl_date)
52   $cl_date = $selected_date->toString(DB_DATEFORMAT);
53 $_SESSION['date'] = $cl_date;
54
55 // Determine previous and next dates for simple navigation.
56 $prev_date = date('Y-m-d', strtotime('-1 day', strtotime($cl_date)));
57 $next_date = date('Y-m-d', strtotime('+1 day', strtotime($cl_date)));
58
59 // Initialize variables.
60 $on_behalf_id = $request->getParameter('onBehalfUser', (isset($_SESSION['behalf_id']) ? $_SESSION['behalf_id'] : $user->id));
61 $cl_client = $request->getParameter('client', ($request->isPost() ? null : @$_SESSION['client']));
62 $_SESSION['client'] = $cl_client;
63 $cl_project = $request->getParameter('project', ($request->isPost() ? null : @$_SESSION['project']));
64 $_SESSION['project'] = $cl_project;
65 $cl_item_name = $request->getParameter('item_name');
66 $cl_cost = $request->getParameter('cost');
67
68 // Elements of expensesForm.
69 $form = new Form('expensesForm');
70
71 if ($user->canManageTeam()) {
72   $user_list = ttTeamHelper::getActiveUsers(array('putSelfFirst'=>true));
73   if (count($user_list) > 1) {
74     $form->addInput(array('type'=>'combobox',
75       'onchange'=>'this.form.submit();',
76       'name'=>'onBehalfUser',
77       'style'=>'width: 250px;',
78       'value'=>$on_behalf_id,
79       'data'=>$user_list,
80       'datakeys'=>array('id','name')));
81     $smarty->assign('on_behalf_control', 1);
82   }
83 }
84
85 // Dropdown for clients in MODE_TIME. Use all active clients.
86 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
87     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
88     $form->addInput(array('type'=>'combobox',
89       'onchange'=>'fillProjectDropdown(this.value);',
90       'name'=>'client',
91       'style'=>'width: 250px;',
92       'value'=>$cl_client,
93       'data'=>$active_clients,
94       'datakeys'=>array('id', 'name'),
95       'empty'=>array(''=>$i18n->get('dropdown.select'))));
96   // Note: in other modes the client list is filtered to relevant clients only. See below.
97 }
98
99 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
100   // Dropdown for projects assigned to user.
101   $project_list = $user->getAssignedProjects();
102   $form->addInput(array('type'=>'combobox',
103     // 'onchange'=>'fillTaskDropdown(this.value);',
104     'name'=>'project',
105     'style'=>'width: 250px;',
106     'value'=>$cl_project,
107     'data'=>$project_list,
108     'datakeys'=>array('id','name'),
109     'empty'=>array(''=>$i18n->get('dropdown.select'))));
110
111   // Dropdown for clients if the clients plugin is enabled.
112   if ($user->isPluginEnabled('cl')) {
113     $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
114     // We need an array of assigned project ids to do some trimming. 
115     foreach($project_list as $project)
116       $projects_assigned_to_user[] = $project['id'];
117
118     // Build a client list out of active clients. Use only clients that are relevant to user.
119     // Also trim their associated project list to only assigned projects (to user).
120     foreach($active_clients as $client) {
121       $projects_assigned_to_client = explode(',', $client['projects']);
122       $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
123       if ($intersection) {
124         $client['projects'] = implode(',', $intersection);
125         $client_list[] = $client;
126       }
127     }
128     $form->addInput(array('type'=>'combobox',
129       'onchange'=>'fillProjectDropdown(this.value);',
130       'name'=>'client',
131       'style'=>'width: 250px;',
132       'value'=>$cl_client,
133       'data'=>$client_list,
134       'datakeys'=>array('id', 'name'),
135       'empty'=>array(''=>$i18n->get('dropdown.select'))));
136   }
137 }
138 // If predefined expenses are configured, add controls to select an expense and quantity.
139 $predefined_expenses = ttTeamHelper::getPredefinedExpenses($user->team_id);
140 if ($predefined_expenses) {
141   $form->addInput(array('type'=>'combobox',
142     'onchange'=>'recalculateCost();',
143     'name'=>'predefined_expense',
144     'style'=>'width: 250px;',
145     'value'=>$cl_predefined_expense,
146     'data'=>$predefined_expenses,
147     'datakeys'=>array('id', 'name'),
148     'empty'=>array(''=>$i18n->get('dropdown.select'))));
149   $form->addInput(array('type'=>'text','onchange'=>'recalculateCost();','maxlength'=>'40','name'=>'quantity','style'=>'width: 100px;','value'=>$cl_quantity));
150 }
151 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'item_name','style'=>'width: 250px;','value'=>$cl_item_name));
152 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
153 $form->addInput(array('type'=>'calendar','name'=>'date','highlight'=>'expenses','value'=>$cl_date)); // calendar
154 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
155 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
156
157 // Submit.
158 if ($request->isPost()) {
159   if ($request->getParameter('btn_submit')) {
160     // Validate user input.
161     if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
162       $err->add($i18n->get('error.client'));
163     if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
164       if (!$cl_project) $err->add($i18n->get('error.project'));
165     }
166     if (!ttValidString($cl_item_name)) $err->add($i18n->get('error.field'), $i18n->get('label.item'));
167     if (!ttValidFloat($cl_cost)) $err->add($i18n->get('error.field'), $i18n->get('label.cost'));
168
169     // Prohibit creating entries in future.
170     if (!$user->future_entries) {
171       $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
172       if ($selected_date->after($browser_today))
173         $err->add($i18n->get('error.future_date'));
174     }
175     // Finished validating input data.
176
177     // Prohibit creating entries in locked range.
178     if ($user->isDateLocked($selected_date))
179       $err->add($i18n->get('error.range_locked'));
180
181     // Insert record.
182     if ($err->no()) {
183       if (ttExpenseHelper::insert(array('date'=>$cl_date,'user_id'=>$user->getActiveUser(),
184         'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
185         header('Location: expenses.php');
186         exit();
187       } else
188         $err->add($i18n->get('error.db'));
189     }
190   } elseif ($request->getParameter('onBehalfUser')) {
191     if($user->canManageTeam()) {
192       unset($_SESSION['behalf_id']);
193       unset($_SESSION['behalf_name']);
194
195       if($on_behalf_id != $user->id) {
196         $_SESSION['behalf_id'] = $on_behalf_id;
197         $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
198       }
199       header('Location: expenses.php');
200       exit();
201     }
202   }
203 }
204
205 $smarty->assign('next_date', $next_date);
206 $smarty->assign('prev_date', $prev_date);
207 $smarty->assign('day_total', ttExpenseHelper::getTotalForDay($user->getActiveUser(), $cl_date));
208 $smarty->assign('expense_items', ttExpenseHelper::getItems($user->getActiveUser(), $cl_date));
209 $smarty->assign('predefined_expenses', $predefined_expenses);
210 $smarty->assign('client_list', $client_list);
211 $smarty->assign('project_list', $project_list);
212 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
213 $smarty->assign('timestring', $selected_date->toString($user->date_format));
214 $smarty->assign('title', $i18n->get('title.expenses'));
215 $smarty->assign('content_page_name', 'mobile/expenses.tpl');
216 $smarty->display('mobile/index.tpl');