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'));
67 if ($user->isPluginEnabled('ps'))
68 $cl_paid = $request->getParameter('paid');
70 $cl_date = $item_date->toString($user->getDateFormat());
71 $cl_client = $expense_item['client_id'];
72 $cl_project = $expense_item['project_id'];
73 $cl_item_name = $expense_item['name'];
74 $cl_cost = $expense_item['cost'];
75 $cl_paid = $expense_item['paid'];
78 // Initialize elements of 'expenseItemForm'.
79 $form = new Form('expenseItemForm');
81 // Dropdown for clients in MODE_TIME. Use all active clients.
82 if (MODE_TIME == $trackingMode && $user->isPluginEnabled('cl')) {
83 $active_clients = ttGroupHelper::getActiveClients(true);
84 $form->addInput(array('type'=>'combobox',
85 'onchange'=>'fillProjectDropdown(this.value);',
87 'style'=>'width: 250px;',
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.
96 // Dropdown for projects assigned to user.
97 $project_list = $user->getAssignedProjects();
98 $form->addInput(array('type'=>'combobox',
100 'style'=>'width: 250px;',
101 'value'=>$cl_project,
102 'data'=>$project_list,
103 'datakeys'=>array('id','name'),
104 'empty'=>array(''=>$i18n->get('dropdown.select'))));
106 // Dropdown for clients if the clients plugin is enabled.
107 if ($user->isPluginEnabled('cl')) {
108 $active_clients = ttGroupHelper::getActiveClients(true);
109 // We need an array of assigned project ids to do some trimming.
110 foreach($project_list as $project)
111 $projects_assigned_to_user[] = $project['id'];
113 // Build a client list out of active clients. Use only clients that are relevant to user.
114 // Also trim their associated project list to only assigned projects (to user).
115 foreach($active_clients as $client) {
116 $projects_assigned_to_client = explode(',', $client['projects']);
117 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
119 $client['projects'] = implode(',', $intersection);
120 $client_list[] = $client;
123 $form->addInput(array('type'=>'combobox',
124 'onchange'=>'fillProjectDropdown(this.value);',
126 'style'=>'width: 250px;',
128 'data'=>$client_list,
129 'datakeys'=>array('id', 'name'),
130 'empty'=>array(''=>$i18n->get('dropdown.select'))));
133 // If predefined expenses are configured, add controls to select an expense and quantity.
134 $predefined_expenses = ttGroupHelper::getPredefinedExpenses();
135 if ($predefined_expenses) {
136 $form->addInput(array('type'=>'combobox',
137 'onchange'=>'recalculateCost();',
138 'name'=>'predefined_expense',
139 'style'=>'width: 250px;',
140 'value'=>$cl_predefined_expense,
141 'data'=>$predefined_expenses,
142 'datakeys'=>array('id', 'name'),
143 'empty'=>array(''=>$i18n->get('dropdown.select'))));
144 $form->addInput(array('type'=>'text','onchange'=>'recalculateCost();','maxlength'=>'40','name'=>'quantity','style'=>'width: 100px;','value'=>$cl_quantity));
146 $form->addInput(array('type'=>'textarea','maxlength'=>'800','name'=>'item_name','style'=>'width: 250px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_item_name));
147 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
148 if ($user->can('manage_invoices') && $user->isPluginEnabled('ps'))
149 $form->addInput(array('type'=>'checkbox','name'=>'paid','value'=>$cl_paid));
150 $form->addInput(array('type'=>'datefield','name'=>'date','maxlength'=>'20','value'=>$cl_date));
151 // Hidden control for record id.
152 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_id));
153 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_save or btn_copy click.
154 $on_click_action = 'browser_today.value=get_date();';
155 $form->addInput(array('type'=>'submit','name'=>'btn_copy','onclick'=>$on_click_action,'value'=>$i18n->get('button.copy')));
156 if ($confirm_save) $on_click_action .= 'return(confirmSave());';
157 $form->addInput(array('type'=>'submit','name'=>'btn_save','onclick'=>$on_click_action,'value'=>$i18n->get('button.save')));
158 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete')));
160 if ($request->isPost()) {
161 // Validate user input.
162 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
163 $err->add($i18n->get('error.client'));
164 if ($show_project && !$cl_project)
165 $err->add($i18n->get('error.project'));
166 if (!ttValidString($cl_item_name)) $err->add($i18n->get('error.field'), $i18n->get('label.item'));
167 if (!ttValidFloat($cl_cost)) $err->add($i18n->get('error.field'), $i18n->get('label.cost'));
168 if (!ttValidDate($cl_date)) $err->add($i18n->get('error.field'), $i18n->get('label.date'));
170 // This is a new date for the expense item.
171 $new_date = new DateAndTime($user->getDateFormat(), $cl_date);
173 // Prohibit creating entries in future.
174 if (!$user->getConfigOption('future_entries')) {
175 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
176 if ($new_date->after($browser_today))
177 $err->add($i18n->get('error.future_date'));
181 if ($request->getParameter('btn_save')) {
183 // 1) Prohibit updating locked entries (that are in locked range).
184 // 2) Prohibit saving unlocked entries into locked range.
186 // Now, step by step.
187 // 1) Prohibit saving locked entries in any form.
188 if ($user->isDateLocked($item_date))
189 $err->add($i18n->get('error.range_locked'));
191 // 2) Prohibit saving unlocked entries into locked range.
192 if ($err->no() && $user->isDateLocked($new_date))
193 $err->add($i18n->get('error.range_locked'));
197 if (ttExpenseHelper::update(array('id'=>$cl_id,'date'=>$new_date->toString(DB_DATEFORMAT),'user_id'=>$user->getUser(),
198 'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'paid'=>$cl_paid))) {
199 header('Location: expenses.php?date='.$new_date->toString(DB_DATEFORMAT));
205 // Save as new record.
206 if ($request->getParameter('btn_copy')) {
207 // We need to prohibit saving into locked interval.
208 if ($user->isDateLocked($new_date))
209 $err->add($i18n->get('error.range_locked'));
211 // Now, a new insert.
213 if (ttExpenseHelper::insert(array('date'=>$new_date->toString(DB_DATEFORMAT),'client_id'=>$cl_client,
214 'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
215 header('Location: expenses.php?date='.$new_date->toString(DB_DATEFORMAT));
218 $err->add($i18n->get('error.db'));
222 if ($request->getParameter('btn_delete')) {
223 header("Location: expense_delete.php?id=$cl_id");
229 $smarty->assign('confirm_save', true);
230 $smarty->assign('entry_date', $cl_date);
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('forms', array($form->getName()=>$form->toArray()));
237 $smarty->assign('title', $i18n->get('title.edit_expense'));
238 $smarty->assign('content_page_name', 'expense_edit.tpl');
239 $smarty->display('index.tpl');