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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 require_once('../initialize.php');
31 import('ttGroupHelper');
32 import('DateAndTime');
33 import('ttTimeHelper');
34 import('ttExpenseHelper');
37 if (!(ttAccessAllowed('track_own_expenses') || ttAccessAllowed('track_expenses'))) {
38 header('Location: access_denied.php');
41 if (!$user->isPluginEnabled('ex')) {
42 header('Location: feature_disabled.php');
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');
53 // End of access checks.
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;
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'));
69 $cl_date = $item_date->toString($user->getDateFormat());
70 $cl_client = $expense_item['client_id'];
71 $cl_project = $expense_item['project_id'];
72 $cl_item_name = $expense_item['name'];
73 $cl_cost = $expense_item['cost'];
76 // Initialize elements of 'expenseItemForm'.
77 $form = new Form('expenseItemForm');
79 // Dropdown for clients in MODE_TIME. Use all active clients.
80 if (MODE_TIME == $trackingMode && $user->isPluginEnabled('cl')) {
81 $active_clients = ttGroupHelper::getActiveClients(true);
82 $form->addInput(array('type'=>'combobox',
83 'onchange'=>'fillProjectDropdown(this.value);',
85 'style'=>'width: 250px;',
87 'data'=>$active_clients,
88 'datakeys'=>array('id', 'name'),
89 'empty'=>array(''=>$i18n->get('dropdown.select'))));
90 // Note: in other modes the client list is filtered to relevant clients only. See below.
94 // Dropdown for projects assigned to user.
95 $project_list = $user->getAssignedProjects();
96 $form->addInput(array('type'=>'combobox',
98 'style'=>'width: 250px;',
100 'data'=>$project_list,
101 'datakeys'=>array('id','name'),
102 'empty'=>array(''=>$i18n->get('dropdown.select'))));
104 // Dropdown for clients if the clients plugin is enabled.
105 if ($user->isPluginEnabled('cl')) {
106 $active_clients = ttGroupHelper::getActiveClients(true);
107 // We need an array of assigned project ids to do some trimming.
108 foreach($project_list as $project)
109 $projects_assigned_to_user[] = $project['id'];
111 // Build a client list out of active clients. Use only clients that are relevant to user.
112 // Also trim their associated project list to only assigned projects (to user).
113 foreach($active_clients as $client) {
114 $projects_assigned_to_client = explode(',', $client['projects']);
115 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
117 $client['projects'] = implode(',', $intersection);
118 $client_list[] = $client;
121 $form->addInput(array('type'=>'combobox',
122 'onchange'=>'fillProjectDropdown(this.value);',
124 'style'=>'width: 250px;',
126 'data'=>$client_list,
127 'datakeys'=>array('id', 'name'),
128 'empty'=>array(''=>$i18n->get('dropdown.select'))));
131 // If predefined expenses are configured, add controls to select an expense and quantity.
132 $predefined_expenses = ttGroupHelper::getPredefinedExpenses();
133 if ($predefined_expenses) {
134 $form->addInput(array('type'=>'combobox',
135 'onchange'=>'recalculateCost();',
136 'name'=>'predefined_expense',
137 'style'=>'width: 250px;',
138 'value'=>$cl_predefined_expense,
139 'data'=>$predefined_expenses,
140 'datakeys'=>array('id', 'name'),
141 'empty'=>array(''=>$i18n->get('dropdown.select'))));
142 $form->addInput(array('type'=>'text','onchange'=>'recalculateCost();','maxlength'=>'40','name'=>'quantity','style'=>'width: 100px;','value'=>$cl_quantity));
144 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'item_name','style'=>'width: 250px;','value'=>$cl_item_name));
145 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
146 $form->addInput(array('type'=>'datefield','name'=>'date','maxlength'=>'20','value'=>$cl_date));
147 // Hidden control for record id.
148 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_id));
149 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_save or btn_copy click.
150 $on_click_action = 'browser_today.value=get_date();';
151 $form->addInput(array('type'=>'submit','name'=>'btn_copy','onclick'=>$on_click_action,'value'=>$i18n->get('button.copy')));
152 if ($confirm_save) $on_click_action .= 'return(confirmSave());';
153 $form->addInput(array('type'=>'submit','name'=>'btn_save','onclick'=>$on_click_action,'value'=>$i18n->get('button.save')));
154 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete')));
156 if ($request->isPost()) {
157 // Validate user input.
158 if ($user->isPluginEnabled('cl') && $user->isOptionEnabled('client_required') && !$cl_client)
159 $err->add($i18n->get('error.client'));
160 if ($show_project && !$cl_project)
161 $err->add($i18n->get('error.project'));
162 if (!ttValidString($cl_item_name)) $err->add($i18n->get('error.field'), $i18n->get('label.item'));
163 if (!ttValidFloat($cl_cost)) $err->add($i18n->get('error.field'), $i18n->get('label.cost'));
164 if (!ttValidDate($cl_date)) $err->add($i18n->get('error.field'), $i18n->get('label.date'));
166 // This is a new date for the expense item.
167 $new_date = new DateAndTime($user->getDateFormat(), $cl_date);
169 // Prohibit creating entries in future.
170 if (!$user->getConfigOption('future_entries')) {
171 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
172 if ($new_date->after($browser_today))
173 $err->add($i18n->get('error.future_date'));
175 if (!ttTimeHelper::canAdd()) $err->add($i18n->get('error.expired'));
176 // Finished validating input data.
179 if ($request->getParameter('btn_save')) {
181 // 1) Prohibit updating locked entries (that are in locked range).
182 // 2) Prohibit saving unlocked entries into locked range.
184 // Now, step by step.
185 // 1) Prohibit saving locked entries in any form.
186 if ($user->isDateLocked($item_date))
187 $err->add($i18n->get('error.range_locked'));
189 // 2) Prohibit saving unlocked entries into locked range.
190 if ($err->no() && $user->isDateLocked($new_date))
191 $err->add($i18n->get('error.range_locked'));
195 if (ttExpenseHelper::update(array('id'=>$cl_id,'date'=>$new_date->toString(DB_DATEFORMAT),
196 'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost))) {
197 header('Location: expenses.php?date='.$new_date->toString(DB_DATEFORMAT));
203 // Save as new record.
204 if ($request->getParameter('btn_copy')) {
205 // We need to prohibit saving into locked interval.
206 if ($user->isDateLocked($new_date))
207 $err->add($i18n->get('error.range_locked'));
209 // Now, a new insert.
211 if (ttExpenseHelper::insert(array('date'=>$new_date->toString(DB_DATEFORMAT),'client_id'=>$cl_client,
212 'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
213 header('Location: expenses.php?date='.$new_date->toString(DB_DATEFORMAT));
216 $err->add($i18n->get('error.db'));
220 if ($request->getParameter('btn_delete')) {
221 header("Location: expense_delete.php?id=$cl_id");
227 $smarty->assign('confirm_save', true);
228 $smarty->assign('entry_date', $cl_date);
230 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
231 $smarty->assign('show_project', $show_project);
232 $smarty->assign('predefined_expenses', $predefined_expenses);
233 $smarty->assign('client_list', $client_list);
234 $smarty->assign('project_list', $project_list);
235 $smarty->assign('task_list', $task_list);
236 $smarty->assign('title', $i18n->get('title.edit_expense'));
237 $smarty->assign('content_page_name', 'mobile/expense_edit.tpl');
238 $smarty->display('mobile/index.tpl');