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