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('form.ActionForm');
32 import('DateAndTime');
33 import('ttGroupHelper');
35 import('ttProjectHelper');
36 import('ttFavReportHelper');
37 import('ttClientHelper');
38 import('ttReportHelper');
41 if (!(ttAccessAllowed('view_own_reports') || ttAccessAllowed('view_reports') || ttAccessAllowed('view_all_reports'))) {
42 header('Location: access_denied.php');
45 if (!$user->exists()) {
46 header('Location: access_denied.php'); // No users in subgroup.
49 // End of access checks.
51 $trackingMode = $user->getTrackingMode();
53 // Use custom fields plugin if it is enabled.
54 if ($user->isPluginEnabled('cf')) {
55 require_once('plugins/CustomFields.class.php');
56 $custom_fields = new CustomFields();
57 $smarty->assign('custom_fields', $custom_fields);
60 $form = new Form('reportForm');
62 // Get saved favorite reports for user.
63 $report_list = ttFavReportHelper::getReports();
64 $form->addInput(array('type'=>'combobox',
65 'name'=>'favorite_report',
66 'onchange'=>'this.form.fav_report_changed.value=1;this.form.submit();',
67 'style'=>'width: 250px;',
69 'datakeys'=>array('id','name'),
70 'empty'=>array('-1'=>$i18n->get('dropdown.no'))));
71 $form->addInput(array('type'=>'hidden','name'=>'fav_report_changed'));
72 // Generate and Delete buttons.
73 $form->addInput(array('type'=>'submit','name'=>'btn_generate','value'=>$i18n->get('button.generate')));
74 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete'),'onclick'=>"return confirm('".$i18n->get('form.reports.confirm_delete')."')"));
76 // Dropdown for clients if the clients plugin is enabled.
77 if ($user->isPluginEnabled('cl') && !$user->isClient()) {
78 if ($user->can('view_reports') || $user->can('view_all_reports')) {
79 $client_list = ttClientHelper::getClients(); // TODO: improve getClients for "view_reports"
80 // by filtering out not relevant clients.
82 $client_list = ttClientHelper::getClientsForUser();
83 $form->addInput(array('type'=>'combobox',
85 'style'=>'width: 250px;',
87 'datakeys'=>array('id', 'name'),
88 'empty'=>array(''=>$i18n->get('dropdown.all'))));
91 // If we have a TYPE_DROPDOWN custom field - add control to select an option.
92 if ($custom_fields && $custom_fields->fields[0] && $custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
93 $form->addInput(array('type'=>'combobox','name'=>'option',
94 'style'=>'width: 250px;',
96 'data'=>$custom_fields->options,
97 'empty'=>array(''=>$i18n->get('dropdown.all'))));
100 // Add controls for projects and tasks.
101 if ($user->can('view_reports') || $user->can('view_all_reports')) {
102 $project_list = ttProjectHelper::getProjects(); // All active and inactive projects.
103 } elseif ($user->isClient()) {
104 $project_list = ttProjectHelper::getProjectsForClient();
106 $project_list = ttProjectHelper::getAssignedProjects($user->getUser());
108 $form->addInput(array('type'=>'combobox',
109 'onchange'=>'fillTaskDropdown(this.value);selectAssignedUsers(this.value);',
111 'style'=>'width: 250px;',
112 'data'=>$project_list,
113 'datakeys'=>array('id','name'),
114 'empty'=>array(''=>$i18n->get('dropdown.all'))));
115 if (MODE_PROJECTS_AND_TASKS == $trackingMode) {
116 $task_list = ttGroupHelper::getActiveTasks();
117 $form->addInput(array('type'=>'combobox',
119 'style'=>'width: 250px;',
121 'datakeys'=>array('id','name'),
122 'empty'=>array(''=>$i18n->get('dropdown.all'))));
125 // Add include records control.
126 $include_options = array('1'=>$i18n->get('form.reports.include_billable'),
127 '2'=>$i18n->get('form.reports.include_not_billable'));
128 $form->addInput(array('type'=>'combobox',
129 'name'=>'include_records',
130 'style'=>'width: 250px;',
131 'data'=>$include_options,
132 'empty'=>array(''=>$i18n->get('dropdown.all'))));
134 // Add invoiced / not invoiced selector.
135 if ($user->can('manage_invoices')) {
136 $invoice_options = array('1'=>$i18n->get('form.reports.include_invoiced'),
137 '2'=>$i18n->get('form.reports.include_not_invoiced'));
138 $form->addInput(array('type'=>'combobox',
140 'style'=>'width: 250px;',
141 'data'=>$invoice_options,
142 'empty'=>array(''=>$i18n->get('dropdown.all'))));
145 if ($user->can('manage_invoices') && $user->isPluginEnabled('ps')) {
146 $form->addInput(array('type'=>'combobox',
147 'name'=>'paid_status',
148 'style'=>'width: 250px;',
149 'data'=>array('1'=>$i18n->get('dropdown.paid'),'2'=>$i18n->get('dropdown.not_paid')),
150 'empty'=>array(''=>$i18n->get('dropdown.all'))
154 $user_list = array();
155 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
156 // Prepare user and assigned projects arrays.
157 if ($user->can('view_reports') || $user->can('view_all_reports')) {
158 $rank = $user->getMaxRankForGroup($user->getGroup());
159 if ($user->can('view_all_reports')) $max_rank = MAX_RANK;
160 if ($user->can('view_own_reports'))
161 $options = array('max_rank'=>$max_rank,'include_self'=>true);
163 $options = array('max_rank'=>$max_rank);
164 $users = $user->getUsers($options); // Active and inactive users.
166 elseif ($user->isClient())
167 $users = ttGroupHelper::getUsersForClient(); // Active and inactive users for clients.
169 foreach ($users as $single_user) {
170 $user_list[$single_user['id']] = $single_user['name'];
171 $projects = ttProjectHelper::getAssignedProjects($single_user['id']);
173 foreach ($projects as $single_project) {
174 $assigned_projects[$single_user['id']][] = $single_project['id'];
178 $row_count = ceil(count($user_list)/3);
179 $form->addInput(array('type'=>'checkboxgroup',
183 'groupin'=>$row_count,
184 'style'=>'width: 100%;'));
187 // Add control for time period.
188 $form->addInput(array('type'=>'combobox',
190 'style'=>'width: 250px;',
191 'data'=>array(INTERVAL_THIS_MONTH=>$i18n->get('dropdown.current_month'),
192 INTERVAL_LAST_MONTH=>$i18n->get('dropdown.previous_month'),
193 INTERVAL_THIS_WEEK=>$i18n->get('dropdown.current_week'),
194 INTERVAL_LAST_WEEK=>$i18n->get('dropdown.previous_week'),
195 INTERVAL_THIS_DAY=>$i18n->get('dropdown.current_day'),
196 INTERVAL_LAST_DAY=>$i18n->get('dropdown.previous_day')),
197 'empty'=>array(''=>$i18n->get('dropdown.select'))));
198 // Add controls for start and end dates.
199 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'start_date'));
200 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'end_date'));
202 // Add checkboxes for fields.
203 if ($user->isPluginEnabled('cl'))
204 $form->addInput(array('type'=>'checkbox','name'=>'chclient'));
205 if (($user->can('manage_invoices') || $user->isClient()) && $user->isPluginEnabled('iv'))
206 $form->addInput(array('type'=>'checkbox','name'=>'chinvoice'));
207 if ($user->can('manage_invoices') && $user->isPluginEnabled('ps'))
208 $form->addInput(array('type'=>'checkbox','name'=>'chpaid'));
209 if ($user->can('view_reports') || $user->can('view_all_reports'))
210 $form->addInput(array('type'=>'checkbox','name'=>'chip'));
211 if (MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode)
212 $form->addInput(array('type'=>'checkbox','name'=>'chproject'));
213 if (MODE_PROJECTS_AND_TASKS == $trackingMode)
214 $form->addInput(array('type'=>'checkbox','name'=>'chtask'));
215 if ((TYPE_START_FINISH == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType())) {
216 $form->addInput(array('type'=>'checkbox','name'=>'chstart'));
217 $form->addInput(array('type'=>'checkbox','name'=>'chfinish'));
219 $form->addInput(array('type'=>'checkbox','name'=>'chduration'));
220 $form->addInput(array('type'=>'checkbox','name'=>'chnote'));
221 $form->addInput(array('type'=>'checkbox','name'=>'chcost'));
222 // If we have a custom field - add a checkbox for it.
223 if ($custom_fields && $custom_fields->fields[0])
224 $form->addInput(array('type'=>'checkbox','name'=>'chcf_1'));
225 if ($user->isPluginEnabled('wu'))
226 $form->addInput(array('type'=>'checkbox','name'=>'chunits'));
228 // Add group by control.
229 $group_by_options['no_grouping'] = $i18n->get('form.reports.group_by_no');
230 $group_by_options['date'] = $i18n->get('form.reports.group_by_date');
231 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
232 $group_by_options['user'] = $i18n->get('form.reports.group_by_user');
233 if ($user->isPluginEnabled('cl') && !($user->isClient() && $user->client_id))
234 $group_by_options['client'] = $i18n->get('form.reports.group_by_client');
235 if (MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode)
236 $group_by_options['project'] = $i18n->get('form.reports.group_by_project');
237 if (MODE_PROJECTS_AND_TASKS == $trackingMode)
238 $group_by_options['task'] = $i18n->get('form.reports.group_by_task');
239 if ($custom_fields && $custom_fields->fields[0] && $custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
240 $group_by_options['cf_1'] = $custom_fields->fields[0]['label'];
242 $group_by_options_size = sizeof($group_by_options);
243 $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by1','data'=>$group_by_options));
244 if ($group_by_options_size > 2) $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by2','data'=>$group_by_options));
245 if ($group_by_options_size > 3) $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by3','data'=>$group_by_options));
246 $form->addInput(array('type'=>'checkbox','name'=>'chtotalsonly'));
248 // Add text field for a new favorite report name.
249 $form->addInput(array('type'=>'text','name'=>'new_fav_report','maxlength'=>'30','style'=>'width: 250px;'));
251 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
253 $form->addInput(array('type'=>'submit','name'=>'btn_generate','value'=>$i18n->get('button.generate')));
255 // Create a bean (which is a mechanism to remember form values in session).
256 $bean = new ActionForm('reportBean', $form, $request);
257 // At this point form values are obtained from session if they are there.
259 if ($request->isGet() && !$bean->isSaved()) {
260 // No previous form data were found in session. Use the following default values.
261 $form->setValueByElement('users', array_keys($user_list));
262 $period = new Period(INTERVAL_THIS_MONTH, new DateAndTime($user->getDateFormat()));
263 $form->setValueByElement('start_date', $period->getStartDate());
264 $form->setValueByElement('end_date', $period->getEndDate());
265 $form->setValueByElement('chclient', '1');
266 $form->setValueByElement('chinvoice', '0');
267 $form->setValueByElement('chpaid', '0');
268 $form->setValueByElement('chip', '0');
269 $form->setValueByElement('chproject', '1');
270 $form->setValueByElement('chstart', '1');
271 $form->setValueByElement('chduration', '1');
272 $form->setValueByElement('chcost', '0');
273 $form->setValueByElement('chtask', '1');
274 $form->setValueByElement('chfinish', '1');
275 $form->setValueByElement('chnote', '1');
276 $form->setValueByElement('chcf_1', '0');
277 $form->setValueByElement('chunits', '0');
278 $form->setValueByElement('chtotalsonly', '0');
281 $form->setValueByElement('fav_report_changed','');
283 // Disable the Delete button when no favorite report is selected.
284 if (!$bean->getAttribute('favorite_report') || ($bean->getAttribute('favorite_report') == -1))
285 $form->getElement('btn_delete')->setEnabled(false);
287 if ($request->isPost()) {
288 if((!$bean->getAttribute('btn_generate') && ($request->getParameter('fav_report_changed')))) {
289 // User changed favorite report. We need to load new values into the form.
290 if ($bean->getAttribute('favorite_report')) {
291 // This loads new favorite report options into the bean (into our form).
292 ttFavReportHelper::loadReport($bean);
294 // If user selected no favorite report - mark all user checkboxes (most probable scenario).
295 if ($bean->getAttribute('favorite_report') == -1)
296 $form->setValueByElement('users', array_keys($user_list));
298 // Save form data in session for future use.
300 header('Location: reports.php');
303 } elseif ($bean->getAttribute('btn_save')) {
304 // User clicked the Save button. We need to save form options as new favorite report.
305 if (!ttValidString($bean->getAttribute('new_fav_report'))) $err->add($i18n->get('error.field'), $i18n->get('form.reports.save_as_favorite'));
308 $id = ttFavReportHelper::saveReport($bean);
310 $err->add($i18n->get('error.db'));
312 $bean->setAttribute('favorite_report', $id);
314 header('Location: reports.php');
318 } elseif($bean->getAttribute('btn_delete')) {
319 // Delete button pressed. User wants to delete a favorite report.
320 if ($bean->getAttribute('favorite_report')) {
321 ttFavReportHelper::deleteReport($bean->getAttribute('favorite_report'));
322 // Load default report.
323 $bean->setAttribute('favorite_report','');
324 $bean->setAttribute('new_fav_report', $report_list[0]['name']);
325 ttFavReportHelper::loadReport($bean);
326 $form->setValueByElement('users', array_keys($user_list));
328 header('Location: reports.php');
332 // Generate button pressed. Check some values.
333 if (!$bean->getAttribute('period')) {
334 $start_date = new DateAndTime($user->getDateFormat(), $bean->getAttribute('start_date'));
336 if ($start_date->isError() || !$bean->getAttribute('start_date'))
337 $err->add($i18n->get('error.field'), $i18n->get('label.start_date'));
339 $end_date = new DateAndTime($user->getDateFormat(), $bean->getAttribute('end_date'));
340 if ($end_date->isError() || !$bean->getAttribute('end_date'))
341 $err->add($i18n->get('error.field'), $i18n->get('label.end_date'));
343 if ($start_date->compare($end_date) > 0)
344 $err->add($i18n->get('error.interval'), $i18n->get('label.end_date'), $i18n->get('label.start_date'));
346 $group_by1 = $bean->getAttribute('group_by1');
347 $group_by2 = $bean->getAttribute('group_by2');
348 $group_by3 = $bean->getAttribute('group_by3');
349 if (($group_by3 != null && $group_by3 != 'no_grouping') && ($group_by3 == $group_by1 || $group_by3 == $group_by2))
350 $err->add($i18n->get('error.field'), $i18n->get('form.reports.group_by'));
351 if (($group_by2 != null && $group_by2 != 'no_grouping') && ($group_by2 == $group_by1 || $group_by3 == $group_by2))
352 $err->add($i18n->get('error.field'), $i18n->get('form.reports.group_by'));
353 // Check remaining values.
354 if (!ttReportHelper::verifyBean($bean)) $err->add($i18n->get('error.sys'));
358 // Now we can go ahead and create a report.
359 header('Location: report.php');
365 $smarty->assign('project_list', $project_list);
366 $smarty->assign('task_list', $task_list);
367 $smarty->assign('assigned_projects', $assigned_projects);
368 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
369 $smarty->assign('onload', 'onLoad="handleCheckboxes()"');
370 $smarty->assign('title', $i18n->get('title.reports'));
371 $smarty->assign('content_page_name', 'reports.tpl');
372 $smarty->display('index.tpl');