Some refactoring of access checks - in progress.
[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('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 // 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');
63
64 // Elements of expensesForm.
65 $form = new Form('expensesForm');
66
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,
75       'data'=>$user_list,
76       'datakeys'=>array('id','name')));
77     $smarty->assign('on_behalf_control', 1);
78   }
79 }
80
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);',
86       'name'=>'client',
87       'style'=>'width: 250px;',
88       'value'=>$cl_client,
89       'data'=>$active_clients,
90       'datakeys'=>array('id', 'name'),
91       'empty'=>array(''=>$i18n->get('dropdown.select'))));
92   // Note: in other modes the client list is filtered to relevant clients only. See below.
93 }
94
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);',
100     'name'=>'project',
101     'style'=>'width: 250px;',
102     'value'=>$cl_project,
103     'data'=>$project_list,
104     'datakeys'=>array('id','name'),
105     'empty'=>array(''=>$i18n->get('dropdown.select'))));
106
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'];
113
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);
119       if ($intersection) {
120         $client['projects'] = implode(',', $intersection);
121         $client_list[] = $client;
122       }
123     }
124     $form->addInput(array('type'=>'combobox',
125       'onchange'=>'fillProjectDropdown(this.value);',
126       'name'=>'client',
127       'style'=>'width: 250px;',
128       'value'=>$cl_client,
129       'data'=>$client_list,
130       'datakeys'=>array('id', 'name'),
131       'empty'=>array(''=>$i18n->get('dropdown.select'))));
132   }
133 }
134 // If predefined expenses are configured, add controls to select an expense and quantity.
135 $predefined_expenses = ttTeamHelper::getPredefinedExpenses($user->team_id);
136 if ($predefined_expenses) {
137   $form->addInput(array('type'=>'combobox',
138     'onchange'=>'recalculateCost();',
139     'name'=>'predefined_expense',
140     'style'=>'width: 250px;',
141     'value'=>$cl_predefined_expense,
142     'data'=>$predefined_expenses,
143     'datakeys'=>array('id', 'name'),
144     'empty'=>array(''=>$i18n->get('dropdown.select'))));
145   $form->addInput(array('type'=>'text','onchange'=>'recalculateCost();','maxlength'=>'40','name'=>'quantity','style'=>'width: 100px;','value'=>$cl_quantity));
146 }
147 $form->addInput(array('type'=>'textarea','maxlength'=>'800','name'=>'item_name','style'=>'width: 250px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_item_name));
148 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
149 $form->addInput(array('type'=>'calendar','name'=>'date','highlight'=>'expenses','value'=>$cl_date)); // calendar
150 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_submit click.
151 $form->addInput(array('type'=>'submit','name'=>'btn_submit','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.submit')));
152
153 // Submit.
154 if ($request->isPost()) {
155   if ($request->getParameter('btn_submit')) {
156     // Validate user input.
157     if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
158       $err->add($i18n->get('error.client'));
159     if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
160       if (!$cl_project) $err->add($i18n->get('error.project'));
161     }
162     if (!ttValidString($cl_item_name)) $err->add($i18n->get('error.field'), $i18n->get('label.comment'));
163     if (!ttValidFloat($cl_cost)) $err->add($i18n->get('error.field'), $i18n->get('label.cost'));
164
165     // Prohibit creating entries in future.
166     if (!$user->future_entries) {
167       $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
168       if ($selected_date->after($browser_today))
169         $err->add($i18n->get('error.future_date'));
170     }
171     // Finished validating input data.
172
173     // Prohibit creating entries in locked range.
174     if ($user->isDateLocked($selected_date))
175       $err->add($i18n->get('error.range_locked'));
176
177     // Insert record.
178     if ($err->no()) {
179       if (ttExpenseHelper::insert(array('date'=>$cl_date,'user_id'=>$user->getActiveUser(),
180         'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
181         header('Location: expenses.php');
182         exit();
183       } else
184         $err->add($i18n->get('error.db'));
185     }
186   } elseif ($request->getParameter('onBehalfUser')) {
187     if($user->canManageTeam()) {
188       unset($_SESSION['behalf_id']);
189       unset($_SESSION['behalf_name']);
190
191       if($on_behalf_id != $user->id) {
192         $_SESSION['behalf_id'] = $on_behalf_id;
193         $_SESSION['behalf_name'] = ttUserHelper::getUserName($on_behalf_id);
194       }
195       header('Location: expenses.php');
196       exit();
197     }
198   }
199 }
200
201 $smarty->assign('day_total', ttExpenseHelper::getTotalForDay($user->getActiveUser(), $cl_date));
202 $smarty->assign('expense_items', ttExpenseHelper::getItems($user->getActiveUser(), $cl_date));
203 $smarty->assign('predefined_expenses', $predefined_expenses);
204 $smarty->assign('client_list', $client_list);
205 $smarty->assign('project_list', $project_list);
206 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
207 $smarty->assign('timestring', $selected_date->toString($user->date_format));
208 $smarty->assign('title', $i18n->get('title.expenses'));
209 $smarty->assign('content_page_name', 'expenses.tpl');
210 $smarty->display('index.tpl');