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 (!ttAccessCheck(right_data_entry) || !$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'));
60 $cl_date = $item_date->toString($user->date_format);
61 $cl_client = $expense_item['client_id'];
62 $cl_project = $expense_item['project_id'];
63 $cl_item_name = $expense_item['name'];
64 $cl_cost = $expense_item['cost'];
67 // Initialize elements of 'expenseItemForm'.
68 $form = new Form('expenseItemForm');
70 // Dropdown for clients in MODE_TIME. Use all active clients.
71 if (MODE_TIME == $user->tracking_mode && $user->isPluginEnabled('cl')) {
72 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
73 $form->addInput(array('type'=>'combobox',
74 'onchange'=>'fillProjectDropdown(this.value);',
76 'style'=>'width: 250px;',
78 'data'=>$active_clients,
79 'datakeys'=>array('id', 'name'),
80 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
81 // Note: in other modes the client list is filtered to relevant clients only. See below.
84 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
85 // Dropdown for projects assigned to user.
86 $project_list = $user->getAssignedProjects();
87 $form->addInput(array('type'=>'combobox',
89 'style'=>'width: 250px;',
91 'data'=>$project_list,
92 'datakeys'=>array('id','name'),
93 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
95 // Dropdown for clients if the clients plugin is enabled.
96 if ($user->isPluginEnabled('cl')) {
97 $active_clients = ttTeamHelper::getActiveClients($user->team_id, true);
98 // We need an array of assigned project ids to do some trimming.
99 foreach($project_list as $project)
100 $projects_assigned_to_user[] = $project['id'];
102 // Build a client list out of active clients. Use only clients that are relevant to user.
103 // Also trim their associated project list to only assigned projects (to user).
104 foreach($active_clients as $client) {
105 $projects_assigned_to_client = explode(',', $client['projects']);
106 $intersection = array_intersect($projects_assigned_to_client, $projects_assigned_to_user);
108 $client['projects'] = implode(',', $intersection);
109 $client_list[] = $client;
112 $form->addInput(array('type'=>'combobox',
113 'onchange'=>'fillProjectDropdown(this.value);',
115 'style'=>'width: 250px;',
117 'data'=>$client_list,
118 'datakeys'=>array('id', 'name'),
119 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
122 // If predefined expenses are configured, add controls to select an expense and quantity.
123 $predefined_expenses = ttTeamHelper::getPredefinedExpenses($user->team_id);
124 if ($predefined_expenses) {
125 $form->addInput(array('type'=>'combobox',
126 'onchange'=>'recalculateCost();',
127 'name'=>'predefined_expense',
128 'style'=>'width: 250px;',
129 'value'=>$cl_predefined_expense,
130 'data'=>$predefined_expenses,
131 'datakeys'=>array('id', 'name'),
132 'empty'=>array(''=>$i18n->getKey('dropdown.select'))));
133 $form->addInput(array('type'=>'text','onchange'=>'recalculateCost();','maxlength'=>'40','name'=>'quantity','style'=>'width: 100px;','value'=>$cl_quantity));
135 $form->addInput(array('type'=>'textarea','maxlength'=>'800','name'=>'item_name','style'=>'width: 250px; height:'.NOTE_INPUT_HEIGHT.'px;','value'=>$cl_item_name));
136 $form->addInput(array('type'=>'text','maxlength'=>'40','name'=>'cost','style'=>'width: 100px;','value'=>$cl_cost));
137 $form->addInput(array('type'=>'datefield','name'=>'date','maxlength'=>'20','value'=>$cl_date));
138 // Hidden control for record id.
139 $form->addInput(array('type'=>'hidden','name'=>'id','value'=>$cl_id));
140 $form->addInput(array('type'=>'hidden','name'=>'browser_today','value'=>'')); // User current date, which gets filled in on btn_save or btn_copy click.
141 $form->addInput(array('type'=>'submit','name'=>'btn_save','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.save')));
142 $form->addInput(array('type'=>'submit','name'=>'btn_copy','onclick'=>'browser_today.value=get_date()','value'=>$i18n->getKey('button.copy')));
143 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->getKey('label.delete')));
145 if ($request->isPost()) {
146 // Validate user input.
147 if ($user->isPluginEnabled('cl') && $user->isPluginEnabled('cm') && !$cl_client)
148 $err->add($i18n->getKey('error.client'));
149 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
150 if (!$cl_project) $err->add($i18n->getKey('error.project'));
152 if (!ttValidString($cl_item_name)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.item'));
153 if (!ttValidFloat($cl_cost)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.cost'));
154 if (!ttValidDate($cl_date)) $err->add($i18n->getKey('error.field'), $i18n->getKey('label.date'));
156 // This is a new date for the expense item.
157 $new_date = new DateAndTime($user->date_format, $cl_date);
159 // Prohibit creating entries in future.
160 if (defined('FUTURE_ENTRIES') && !isTrue(FUTURE_ENTRIES)) {
161 $browser_today = new DateAndTime(DB_DATEFORMAT, $request->getParameter('browser_today', null));
162 if ($new_date->after($browser_today))
163 $err->add($i18n->getKey('error.future_date'));
167 if ($request->getParameter('btn_save')) {
169 // 1) Prohibit updating locked entries (that are in locked range).
170 // 2) Prohibit saving unlocked entries into locked range.
172 // Now, step by step.
173 // 1) Prohibit saving locked entries in any form.
174 if ($user->isDateLocked($item_date))
175 $err->add($i18n->getKey('error.range_locked'));
177 // 2) Prohibit saving unlocked entries into locked range.
178 if ($err->no() && $user->isDateLocked($new_date))
179 $err->add($i18n->getKey('error.range_locked'));
183 if (ttExpenseHelper::update(array('id'=>$cl_id,'date'=>$new_date->toString(DB_DATEFORMAT),'user_id'=>$user->getActiveUser(),
184 'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost))) {
185 header('Location: expenses.php?date='.$new_date->toString(DB_DATEFORMAT));
191 // Save as new record.
192 if ($request->getParameter('btn_copy')) {
193 // We need to prohibit saving into locked interval.
194 if ($user->isDateLocked($new_date))
195 $err->add($i18n->getKey('error.range_locked'));
197 // Now, a new insert.
199 if (ttExpenseHelper::insert(array('date'=>$new_date->toString(DB_DATEFORMAT),'user_id'=>$user->getActiveUser(),
200 'client_id'=>$cl_client,'project_id'=>$cl_project,'name'=>$cl_item_name,'cost'=>$cl_cost,'status'=>1))) {
201 header('Location: expenses.php?date='.$new_date->toString(DB_DATEFORMAT));
204 $err->add($i18n->getKey('error.db'));
208 if ($request->getParameter('btn_delete')) {
209 header("Location: expense_delete.php?id=$cl_id");
214 $smarty->assign('predefined_expenses', $predefined_expenses);
215 $smarty->assign('client_list', $client_list);
216 $smarty->assign('project_list', $project_list);
217 $smarty->assign('task_list', $task_list);
218 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
219 $smarty->assign('title', $i18n->getKey('title.edit_expense'));
220 $smarty->assign('content_page_name', 'expense_edit.tpl');
221 $smarty->display('index.tpl');