Improved access check for client edit.
[timetracker.git] / mobile / expense_edit.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('ttTeamHelper');
32 import('ttGroupHelper');
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 $cl_id = (int)$request->getParameter('id');
46 // Get the expense item we are editing.
47 $expense_item = ttExpenseHelper::getItem($cl_id, $user->getUser());
48 if (!$expense_item || $expense_item['invoice_id']) {
49   // Prohibit editing not ours or invoiced items.
50   header('Location: access_denied.php');
51   exit();
52 }
53
54 $item_date = new DateAndTime(DB_DATEFORMAT, $expense_item['date']);
55 $confirm_save = $user->getConfigOption('confirm_save');
56
57 // Initialize variables.
58 $cl_date = $cl_client = $cl_project = $cl_item_name = $cl_cost = null;
59 if ($request->isPost()) {
60   $cl_date = trim($request->getParameter('date'));
61   $cl_client = $request->getParameter('client');
62   $cl_project = $request->getParameter('project');
63   $cl_item_name = trim($request->getParameter('item_name'));
64   $cl_cost = trim($request->getParameter('cost'));
65 } else {
66   $cl_date = $item_date->toString($user->date_format);
67   $cl_client = $expense_item['client_id'];
68   $cl_project = $expense_item['project_id'];
69   $cl_item_name = $expense_item['name'];
70   $cl_cost = $expense_item['cost'];
71 }
72
73 // Initialize elements of 'expenseItemForm'.
74 $form = new Form('expenseItemForm');
75
76 // Dropdown for clients in MODE_TIME. Use all active clients.
77 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
78   $active_clients = ttGroupHelper::getActiveClients(true);
79   $form->addInput(array('type'=>'combobox',
80     'onchange'=>'fillProjectDropdown(this.value);',
81     'name'=>'client',
82     'style'=>'width: 250px;',
83     'value'=>$cl_client,
84     'data'=>$active_clients,
85     'datakeys'=>array('id', 'name'),
86     'empty'=>array(''=>$i18n->get('dropdown.select'))));
87   // Note: in other modes the client list is filtered to relevant clients only. See below.
88 }
89
90 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
91   // Dropdown for projects assigned to user.
92   $project_list = $user->getAssignedProjects();
93   $form->addInput(array('type'=>'combobox',
94     'name'=>'project',
95     'style'=>'width: 250px;',
96     'value'=>$cl_project,
97     'data'=>$project_list,
98     'datakeys'=>array('id','name'),
99     'empty'=>array(''=>$i18n->get('dropdown.select'))));
100
101   // Dropdown for clients if the clients plugin is enabled.
102   if ($user->isPluginEnabled('cl')) {
103     $active_clients = ttGroupHelper::getActiveClients(true);
104     // We need an array of assigned project ids to do some trimming. 
105     foreach($project_list as $project)
106       $projects_assigned_to_user[] = $project['id'];
107
108     // Build a client list out of active clients. Use only clients that are relevant to user.
109     // Also trim their associated project list to only assigned projects (to user).
110     foreach($active_clients as $client) {
111       $projects_assigned_to_client = explode(',', $client['projects']);
112       $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
113       if ($intersection) {
114         $client['projects'] = implode(',', $intersection);
115         $client_list[] = $client;
116       }
117     }
118     $form->addInput(array('type'=>'combobox',
119       'onchange'=>'fillProjectDropdown(this.value);',
120       'name'=>'client',
121       'style'=>'width: 250px;',
122       'value'=>$cl_client,
123       'data'=>$client_list,
124       'datakeys'=>array('id', 'name'),
125       'empty'=>array(''=>$i18n->get('dropdown.select'))));
126   }
127 }
128 // If predefined expenses are configured, add controls to select an expense and quantity.
129 $predefined_expenses = ttTeamHelper::getPredefinedExpenses($user->group_id);
130 if ($predefined_expenses) {
131     $form->addInput(array('type'=>'combobox',
132       'onchange'=>'recalculateCost();',
133       'name'=>'predefined_expense',
134       'style'=>'width: 250px;',
135       'value'=>$cl_predefined_expense,
136       'data'=>$predefined_expenses,
137       'datakeys'=>array('id', 'name'),
138       'empty'=>array(''=>$i18n->get('dropdown.select'))));
139     $form->addInput(array('type'=>'text','onchange'=>'recalculateCost();','maxlength'=>'40','name'=>'quantity','style'=>'width: 100px;','value'=>$cl_quantity));
140 }
141 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'item_name','style'=>'width: 250px;','value'=>$cl_item_name));
142 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
143 $form->addInput(array('type'=>'datefield','name'=>'date','maxlength'=>'20','value'=>$cl_date));
144 // Hidden control for record id.
145 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_id));
146 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_save or btn_copy click.
147 $on_click_action = 'browser_today.value=get_date();';
148 $form->addInput(array('type'=>'submit','name'=>'btn_copy','onclick'=>$on_click_action,'value'=>$i18n->get('button.copy')));
149 if ($confirm_save) $on_click_action .= 'return(confirmSave());';
150 $form->addInput(array('type'=>'submit','name'=>'btn_save','onclick'=>$on_click_action,'value'=>$i18n->get('button.save')));
151 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete')));
152
153 if ($request->isPost()) {
154   // Validate user input.
155   if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
156     $err->add($i18n->get('error.client'));
157   if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
158     if (!$cl_project) $err->add($i18n->get('error.project'));
159   }
160   if (!ttValidString($cl_item_name)) $err->add($i18n->get('error.field'), $i18n->get('label.item'));
161   if (!ttValidFloat($cl_cost)) $err->add($i18n->get('error.field'), $i18n->get('label.cost'));
162   if (!ttValidDate($cl_date)) $err->add($i18n->get('error.field'), $i18n->get('label.date'));
163
164   // This is a new date for the expense item.
165   $new_date = new DateAndTime($user->date_format, $cl_date);
166
167   // Prohibit creating entries in future.
168   if (!$user->future_entries) {
169     $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
170     if ($new_date->after($browser_today))
171       $err->add($i18n->get('error.future_date'));
172   }
173
174   // Save record.
175   if ($request->getParameter('btn_save')) {
176     // We need to:
177     // 1) Prohibit updating locked entries (that are in locked range).
178     // 2) Prohibit saving unlocked entries into locked range.
179
180     // Now, step by step.
181     // 1) Prohibit saving locked entries in any form.
182     if ($user->isDateLocked($item_date))
183       $err->add($i18n->get('error.range_locked'));
184
185     // 2) Prohibit saving unlocked entries into locked range.
186     if ($err->no() && $user->isDateLocked($new_date))
187       $err->add($i18n->get('error.range_locked'));
188
189     // Now, an update.
190     if ($err->no()) {
191       if (ttExpenseHelper::update(array('id'=>$cl_id,'date'=>$new_date->toString(DB_DATEFORMAT),'user_id'=>$user->getUser(),
192           'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost))) {
193         header('Location: expenses.php?date='.$new_date->toString(DB_DATEFORMAT));
194         exit();
195       }
196     }
197   }
198
199   // Save as new record.
200   if ($request->getParameter('btn_copy')) {
201     // We need to prohibit saving into locked interval.
202     if ($user->isDateLocked($new_date))
203       $err->add($i18n->get('error.range_locked'));
204
205     // Now, a new insert.
206     if ($err->no()) {
207       if (ttExpenseHelper::insert(array('date'=>$new_date->toString(DB_DATEFORMAT),'client_id'=>$cl_client,
208           'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
209         header('Location: expenses.php?date='.$new_date->toString(DB_DATEFORMAT));
210         exit();
211       } else
212         $err->add($i18n->get('error.db'));
213     }
214   }
215
216   if ($request->getParameter('btn_delete')) {
217     header("Location: expense_delete.php?id=$cl_id");
218     exit();
219   }
220 } // isPost
221
222 if ($confirm_save) {
223   $smarty->assign('confirm_save', true);
224   $smarty->assign('entry_date', $cl_date);
225 }
226 $smarty->assign('predefined_expenses', $predefined_expenses);
227 $smarty->assign('client_list', $client_list);
228 $smarty->assign('project_list', $project_list);
229 $smarty->assign('task_list', $task_list);
230 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
231 $smarty->assign('title', $i18n->get('title.edit_expense'));
232 $smarty->assign('content_page_name', 'mobile/expense_edit.tpl');
233 $smarty->display('mobile/index.tpl');