posaune
[timetracker.git] / 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('ttGroupHelper');
32 import('DateAndTime');
33 import('ttTimeHelper');
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);
48 if (!$expense_item || $expense_item['approved'] || $expense_item['invoice_id']) {
49   // Prohibit editing not ours, approved, or invoiced items.
50   header('Location: access_denied.php');
51   exit();
52 }
53 // End of access checks.
54
55 $item_date = new DateAndTime(DB_DATEFORMAT, $expense_item['date']);
56 $confirm_save = $user->getConfigOption('confirm_save');
57 $trackingMode = $user->getTrackingMode();
58 $show_project = MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode;
59
60 // Initialize variables.
61 $cl_date = $cl_client = $cl_project = $cl_item_name = $cl_cost = null;
62 if ($request->isPost()) {
63   $cl_date = trim($request->getParameter('date'));
64   $cl_client = $request->getParameter('client');
65   $cl_project = $request->getParameter('project');
66   $cl_item_name = trim($request->getParameter('item_name'));
67   $cl_cost = trim($request->getParameter('cost'));
68   if ($user->isPluginEnabled('ps'))
69     $cl_paid = $request->getParameter('paid');
70 } else {
71   $cl_date = $item_date->toString($user->getDateFormat());
72   $cl_client = $expense_item['client_id'];
73   $cl_project = $expense_item['project_id'];
74   $cl_item_name = $expense_item['name'];
75   $cl_cost = $expense_item['cost'];
76   $cl_paid = $expense_item['paid'];
77 }
78
79 // Initialize elements of 'expenseItemForm'.
80 $form = new Form('expenseItemForm');
81
82 // Dropdown for clients in MODE_TIME. Use all active clients.
83 if (MODE_TIME == $trackingMode && $user->isPluginEnabled('cl')) {
84   $active_clients = ttGroupHelper::getActiveClients(true);
85   $form->addInput(array('type'=>'combobox',
86     'onchange'=>'fillProjectDropdown(this.value);',
87     'name'=>'client',
88     'style'=>'width: 250px;',
89     'value'=>$cl_client,
90     'data'=>$active_clients,
91     'datakeys'=>array('id', 'name'),
92     'empty'=>array(''=>$i18n->get('dropdown.select'))));
93   // Note: in other modes the client list is filtered to relevant clients only. See below.
94 }
95
96 if ($show_project) {
97   // Dropdown for projects assigned to user.
98   $project_list = $user->getAssignedProjects();
99   $form->addInput(array('type'=>'combobox',
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 = ttGroupHelper::getActiveClients(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 = ttGroupHelper::getPredefinedExpenses();
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 if ($user->can('manage_invoices') && $user->isPluginEnabled('ps'))
150   $form->addInput(array('type'=>'checkbox','name'=>'paid','value'=>$cl_paid));
151 $form->addInput(array('type'=>'datefield','name'=>'date','maxlength'=>'20','value'=>$cl_date));
152 // Hidden control for record id.
153 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_id));
154 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_save or btn_copy click.
155 $on_click_action = 'browser_today.value=get_date();';
156 $form->addInput(array('type'=>'submit','name'=>'btn_copy','onclick'=>$on_click_action,'value'=>$i18n->get('button.copy')));
157 if ($confirm_save) $on_click_action .= 'return(confirmSave());';
158 $form->addInput(array('type'=>'submit','name'=>'btn_save','onclick'=>$on_click_action,'value'=>$i18n->get('button.save')));
159 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete')));
160
161 if ($request->isPost()) {
162   // Validate user input.
163   if ($user->isPluginEnabled('cl') && $user->isOptionEnabled('client_required') && !$cl_client)
164     $err->add($i18n->get('error.client'));
165   if ($show_project && !$cl_project)
166     $err->add($i18n->get('error.project'));
167   if (!ttValidString($cl_item_name)) $err->add($i18n->get('error.field'), $i18n->get('label.item'));
168   if (!ttValidFloat($cl_cost)) $err->add($i18n->get('error.field'), $i18n->get('label.cost'));
169   if (!ttValidDate($cl_date)) $err->add($i18n->get('error.field'), $i18n->get('label.date'));
170
171   // This is a new date for the expense item.
172   $new_date = new DateAndTime($user->getDateFormat(), $cl_date);
173
174   // Prohibit creating entries in future.
175   if (!$user->getConfigOption('future_entries')) {
176     $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
177     if ($new_date->after($browser_today))
178       $err->add($i18n->get('error.future_date'));
179   }
180   if (!ttTimeHelper::canAdd()) $err->add($i18n->get('error.expired'));
181   // Finished validating user input.
182
183   // Save record.
184   if ($request->getParameter('btn_save')) {
185     // We need to:
186     // 1) Prohibit updating locked entries (that are in locked range).
187     // 2) Prohibit saving unlocked entries into locked range.
188
189     // Now, step by step.
190     // 1) Prohibit saving locked entries in any form.
191     if ($user->isDateLocked($item_date))
192       $err->add($i18n->get('error.range_locked'));
193
194     // 2) Prohibit saving unlocked entries into locked range.
195     if ($err->no() && $user->isDateLocked($new_date))
196       $err->add($i18n->get('error.range_locked'));
197
198     // Now, an update.
199     if ($err->no()) {
200       if (ttExpenseHelper::update(array('id'=>$cl_id,'date'=>$new_date->toString(DB_DATEFORMAT),
201           'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'paid'=>$cl_paid))) {
202         header('Location: expenses.php?date='.$new_date->toString(DB_DATEFORMAT));
203         exit();
204       }
205     }
206   }
207
208   // Save as new record.
209   if ($request->getParameter('btn_copy')) {
210     // We need to prohibit saving into locked interval.
211     if ($user->isDateLocked($new_date))
212       $err->add($i18n->get('error.range_locked'));
213
214     // Now, a new insert.
215     if ($err->no()) {
216       if (ttExpenseHelper::insert(array('date'=>$new_date->toString(DB_DATEFORMAT),'client_id'=>$cl_client,
217           'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
218         header('Location: expenses.php?date='.$new_date->toString(DB_DATEFORMAT));
219         exit();
220       } else
221         $err->add($i18n->get('error.db'));
222     }
223   }
224
225   if ($request->getParameter('btn_delete')) {
226     header("Location: expense_delete.php?id=$cl_id");
227     exit();
228   }
229 } // isPost
230
231 if ($confirm_save) {
232   $smarty->assign('confirm_save', true);
233   $smarty->assign('entry_date', $cl_date);
234 }
235 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
236 $smarty->assign('show_project', $show_project);
237 $smarty->assign('predefined_expenses', $predefined_expenses);
238 $smarty->assign('client_list', $client_list);
239 $smarty->assign('project_list', $project_list);
240 $smarty->assign('task_list', $task_list);
241 $smarty->assign('title', $i18n->get('title.edit_expense'));
242 $smarty->assign('content_page_name', 'expense_edit.tpl');
243 $smarty->display('index.tpl');