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