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