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