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') || !$user->isPluginEnabled('ex')) {
37 header('Location: access_denied.php');
41 $cl_id = $request->getParameter('id');
43 // Get the expense item we are editing.
44 $expense_item = ttExpenseHelper::getItem($cl_id, $user->getActiveUser());
46 // Prohibit editing invoiced items.
47 if ($expense_item['invoice_id']) die($i18n->getKey('error.sys'));
49 $item_date = new DateAndTime(DB_DATEFORMAT, $expense_item['date']);
51 // Initialize variables.
52 $cl_date = $cl_client = $cl_project = $cl_item_name = $cl_cost = null;
53 if ($request->isPost()) {
54 $cl_date = trim($request->getParameter('date'));
55 $cl_client = $request->getParameter('client');
56 $cl_project = $request->getParameter('project');
57 $cl_item_name = trim($request->getParameter('item_name'));
58 $cl_cost = trim($request->getParameter('cost'));
59 if ($user->isPluginEnabled('ps'))
60 $cl_paid = $request->getParameter('paid');
62 $cl_date = $item_date->toString($user->date_format);
63 $cl_client = $expense_item['client_id'];
64 $cl_project = $expense_item['project_id'];
65 $cl_item_name = $expense_item['name'];
66 $cl_cost = $expense_item['cost'];
67 $cl_paid = $expense_item['paid'];
70 // Initialize elements of 'expenseItemForm'.
71 $form = new Form('expenseItemForm');
73 // Dropdown for clients in MODE_TIME. Use all active clients.
74 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
75 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
76 $form->addInput(array('type'=>'combobox',
77 'onchange'=>'fillProjectDropdown(this.value);',
79 'style'=>'width: 250px;',
81 'data'=>$active_clients,
82 'datakeys'=>array('id', 'name'),
83 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
84 // Note: in other modes the client list is filtered to relevant clients only. See below.
87 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
88 // Dropdown for projects assigned to user.
89 $project_list = $user->getAssignedProjects();
90 $form->addInput(array('type'=>'combobox',
92 'style'=>'width: 250px;',
94 'data'=>$project_list,
95 'datakeys'=>array('id','name'),
96 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
98 // Dropdown for clients if the clients plugin is enabled.
99 if ($user->isPluginEnabled('cl')) {
100 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
101 // We need an array of assigned project ids to do some trimming.
102 foreach($project_list as $project)
103 $projects_assigned_to_user[] = $project['id'];
105 // Build a client list out of active clients. Use only clients that are relevant to user.
106 // Also trim their associated project list to only assigned projects (to user).
107 foreach($active_clients as $client) {
108 $projects_assigned_to_client = explode(',', $client['projects']);
109 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
111 $client['projects'] = implode(',', $intersection);
112 $client_list[] = $client;
115 $form->addInput(array('type'=>'combobox',
116 'onchange'=>'fillProjectDropdown(this.value);',
118 'style'=>'width: 250px;',
120 'data'=>$client_list,
121 'datakeys'=>array('id', 'name'),
122 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
125 // If predefined expenses are configured, add controls to select an expense and quantity.
126 $predefined_expenses = ttTeamHelper::getPredefinedExpenses($user->team_id);
127 if ($predefined_expenses) {
128 $form->addInput(array('type'=>'combobox',
129 'onchange'=>'recalculateCost();',
130 'name'=>'predefined_expense',
131 'style'=>'width: 250px;',
132 'value'=>$cl_predefined_expense,
133 'data'=>$predefined_expenses,
134 'datakeys'=>array('id', 'name'),
135 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
136 $form->addInput(array('type'=>'text','onchange'=>'recalculateCost();','maxlength'=>'40','name'=>'quantity','style'=>'width: 100px;','value'=>$cl_quantity));
138 $form->addInput(array('type'=>'textarea','maxlength'=>'800','name'=>'item_name','style'=>'width: 250px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_item_name));
139 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
140 if ($user->canManageTeam() && $user->isPluginEnabled('ps'))
141 $form->addInput(array('type'=>'checkbox','name'=>'paid','value'=>$cl_paid));
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 $form->addInput(array('type'=>'submit','name'=>'btn_save','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.save')));
147 $form->addInput(array('type'=>'submit','name'=>'btn_copy','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.copy')));
148 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->getKey('label.delete')));
150 if ($request->isPost()) {
151 // Validate user input.
152 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
153 $err->add($i18n->getKey('error.client'));
154 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
155 if (!$cl_project) $err->add($i18n->getKey('error.project'));
157 if (!ttValidString($cl_item_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.item'));
158 if (!ttValidFloat($cl_cost)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.cost'));
159 if (!ttValidDate($cl_date)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.date'));
161 // This is a new date for the expense item.
162 $new_date = new DateAndTime($user->date_format, $cl_date);
164 // Prohibit creating entries in future.
165 if (!$user->future_entries) {
166 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
167 if ($new_date->after($browser_today))
168 $err->add($i18n->getKey('error.future_date'));
172 if ($request->getParameter('btn_save')) {
174 // 1) Prohibit updating locked entries (that are in locked range).
175 // 2) Prohibit saving unlocked entries into locked range.
177 // Now, step by step.
178 // 1) Prohibit saving locked entries in any form.
179 if ($user->isDateLocked($item_date))
180 $err->add($i18n->getKey('error.range_locked'));
182 // 2) Prohibit saving unlocked entries into locked range.
183 if ($err->no() && $user->isDateLocked($new_date))
184 $err->add($i18n->getKey('error.range_locked'));
188 if (ttExpenseHelper::update(array('id'=>$cl_id,'date'=>$new_date->toString(DB_DATEFORMAT),'user_id'=>$user->getActiveUser(),
189 'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'paid'=>$cl_paid))) {
190 header('Location: expenses.php?date='.$new_date->toString(DB_DATEFORMAT));
196 // Save as new record.
197 if ($request->getParameter('btn_copy')) {
198 // We need to prohibit saving into locked interval.
199 if ($user->isDateLocked($new_date))
200 $err->add($i18n->getKey('error.range_locked'));
202 // Now, a new insert.
204 if (ttExpenseHelper::insert(array('date'=>$new_date->toString(DB_DATEFORMAT),'user_id'=>$user->getActiveUser(),
205 'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
206 header('Location: expenses.php?date='.$new_date->toString(DB_DATEFORMAT));
209 $err->add($i18n->getKey('error.db'));
213 if ($request->getParameter('btn_delete')) {
214 header("Location: expense_delete.php?id=$cl_id");
219 $smarty->assign('predefined_expenses', $predefined_expenses);
220 $smarty->assign('client_list', $client_list);
221 $smarty->assign('project_list', $project_list);
222 $smarty->assign('task_list', $task_list);
223 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
224 $smarty->assign('title', $i18n->getKey('title.edit_expense'));
225 $smarty->assign('content_page_name', 'expense_edit.tpl');
226 $smarty->display('index.tpl');