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->getActiveUser());
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']);
55 // Initialize variables.
56 $cl_date = $cl_client = $cl_project = $cl_item_name = $cl_cost = null;
57 if ($request->isPost()) {
58 $cl_date = trim($request->getParameter('date'));
59 $cl_client = $request->getParameter('client');
60 $cl_project = $request->getParameter('project');
61 $cl_item_name = trim($request->getParameter('item_name'));
62 $cl_cost = trim($request->getParameter('cost'));
63 if ($user->isPluginEnabled('ps'))
64 $cl_paid = $request->getParameter('paid');
66 $cl_date = $item_date->toString($user->date_format);
67 $cl_client = $expense_item['client_id'];
68 $cl_project = $expense_item['project_id'];
69 $cl_item_name = $expense_item['name'];
70 $cl_cost = $expense_item['cost'];
71 $cl_paid = $expense_item['paid'];
74 // Initialize elements of 'expenseItemForm'.
75 $form = new Form('expenseItemForm');
77 // Dropdown for clients in MODE_TIME. Use all active clients.
78 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
79 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
80 $form->addInput(array('type'=>'combobox',
81 'onchange'=>'fillProjectDropdown(this.value);',
83 'style'=>'width: 250px;',
85 'data'=>$active_clients,
86 'datakeys'=>array('id', 'name'),
87 'empty'=>array(''=>$i18n->get('dropdown.select'))));
88 // Note: in other modes the client list is filtered to relevant clients only. See below.
91 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
92 // Dropdown for projects assigned to user.
93 $project_list = $user->getAssignedProjects();
94 $form->addInput(array('type'=>'combobox',
96 'style'=>'width: 250px;',
98 'data'=>$project_list,
99 'datakeys'=>array('id','name'),
100 'empty'=>array(''=>$i18n->get('dropdown.select'))));
102 // Dropdown for clients if the clients plugin is enabled.
103 if ($user->isPluginEnabled('cl')) {
104 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
105 // We need an array of assigned project ids to do some trimming.
106 foreach($project_list as $project)
107 $projects_assigned_to_user[] = $project['id'];
109 // Build a client list out of active clients. Use only clients that are relevant to user.
110 // Also trim their associated project list to only assigned projects (to user).
111 foreach($active_clients as $client) {
112 $projects_assigned_to_client = explode(',', $client['projects']);
113 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
115 $client['projects'] = implode(',', $intersection);
116 $client_list[] = $client;
119 $form->addInput(array('type'=>'combobox',
120 'onchange'=>'fillProjectDropdown(this.value);',
122 'style'=>'width: 250px;',
124 'data'=>$client_list,
125 'datakeys'=>array('id', 'name'),
126 'empty'=>array(''=>$i18n->get('dropdown.select'))));
129 // If predefined expenses are configured, add controls to select an expense and quantity.
130 $predefined_expenses = ttTeamHelper::getPredefinedExpenses($user->team_id);
131 if ($predefined_expenses) {
132 $form->addInput(array('type'=>'combobox',
133 'onchange'=>'recalculateCost();',
134 'name'=>'predefined_expense',
135 'style'=>'width: 250px;',
136 'value'=>$cl_predefined_expense,
137 'data'=>$predefined_expenses,
138 'datakeys'=>array('id', 'name'),
139 'empty'=>array(''=>$i18n->get('dropdown.select'))));
140 $form->addInput(array('type'=>'text','onchange'=>'recalculateCost();','maxlength'=>'40','name'=>'quantity','style'=>'width: 100px;','value'=>$cl_quantity));
142 $form->addInput(array('type'=>'textarea','maxlength'=>'800','name'=>'item_name','style'=>'width: 250px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_item_name));
143 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
144 if ($user->canManageTeam() && $user->isPluginEnabled('ps'))
145 $form->addInput(array('type'=>'checkbox','name'=>'paid','value'=>$cl_paid));
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 $form->addInput(array('type'=>'submit','name'=>'btn_save','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.save')));
151 $form->addInput(array('type'=>'submit','name'=>'btn_copy','onclick'=>'browser_today.value=get_date()','value'=>$i18n->get('button.copy')));
152 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete')));
154 if ($request->isPost()) {
155 // Validate user input.
156 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
157 $err->add($i18n->get('error.client'));
158 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
159 if (!$cl_project) $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->date_format, $cl_date);
168 // Prohibit creating entries in future.
169 if (!$user->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),'user_id'=>$user->getActiveUser(),
193 'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'paid'=>$cl_paid))) {
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),'user_id'=>$user->getActiveUser(),
209 'client_id'=>$cl_client,'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");
223 $smarty->assign('predefined_expenses', $predefined_expenses);
224 $smarty->assign('client_list', $client_list);
225 $smarty->assign('project_list', $project_list);
226 $smarty->assign('task_list', $task_list);
227 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
228 $smarty->assign('title', $i18n->get('title.edit_expense'));
229 $smarty->assign('content_page_name', 'expense_edit.tpl');
230 $smarty->display('index.tpl');