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') || ttAccessAllowed('view_client_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);
58 $showCustomFieldCheckbox = $custom_fields->fields[0];
59 $showCustomFieldDropdown = $custom_fields->fields[0] && $custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN;
60 if ($showCustomFieldDropdown)
61 $showCustomFieldDropdown &= count($custom_fields->options) > 0;
64 $form = new Form('reportForm');
66 // Get saved favorite reports for user.
67 $report_list = ttFavReportHelper::getReports();
68 $form->addInput(array('type'=>'combobox',
69 'name'=>'favorite_report',
70 'onchange'=>'this.form.fav_report_changed.value=1;this.form.submit();',
71 'style'=>'width: 250px;',
73 'datakeys'=>array('id','name'),
74 'empty'=>array('-1'=>$i18n->get('dropdown.no'))));
75 $form->addInput(array('type'=>'hidden','name'=>'fav_report_changed'));
76 // Generate and Delete buttons.
77 $form->addInput(array('type'=>'submit','name'=>'btn_generate','value'=>$i18n->get('button.generate')));
78 $form->addInput(array('type'=>'submit','name'=>'btn_delete','value'=>$i18n->get('label.delete'),'onclick'=>"return confirm('".$i18n->get('form.reports.confirm_delete')."')"));
80 // Dropdown for clients if the clients plugin is enabled.
81 $showClient = $user->isPluginEnabled('cl') && !$user->isClient();
83 if ($user->can('view_reports') || $user->can('view_all_reports')) {
84 $client_list = ttClientHelper::getClients(); // TODO: improve getClients for "view_reports"
85 // by filtering out not relevant clients.
87 $client_list = ttClientHelper::getClientsForUser();
88 if (count($client_list) == 0) $showClient = false;
91 $form->addInput(array('type'=>'combobox',
93 'style'=>'width: 250px;',
95 'datakeys'=>array('id', 'name'),
96 'empty'=>array(''=>$i18n->get('dropdown.all'))));
99 // If we have a TYPE_DROPDOWN custom field - add a control to select an option.
100 if ($showCustomFieldDropdown) {
101 $form->addInput(array('type'=>'combobox','name'=>'option',
102 'style'=>'width: 250px;',
104 'data'=>$custom_fields->options,
105 'empty'=>array(''=>$i18n->get('dropdown.all'))));
108 // Add project dropdown.
109 $showProject = MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode;
111 if ($user->can('view_reports') || $user->can('view_all_reports')) {
112 $project_list = ttProjectHelper::getProjects(); // All active and inactive projects.
113 } elseif ($user->isClient()) {
114 $project_list = ttProjectHelper::getProjectsForClient();
116 $project_list = ttProjectHelper::getAssignedProjects($user->getUser());
118 if (count($project_list) == 0) $showProject = false;
121 $form->addInput(array('type'=>'combobox',
122 'onchange'=>'fillTaskDropdown(this.value);selectAssignedUsers(this.value);',
124 'style'=>'width: 250px;',
125 'data'=>$project_list,
126 'datakeys'=>array('id','name'),
127 'empty'=>array(''=>$i18n->get('dropdown.all'))));
130 // Add task dropdown.
131 $showTask = MODE_PROJECTS_AND_TASKS == $trackingMode;
133 $task_list = ttGroupHelper::getActiveTasks();
134 if (count($task_list) == 0) $showTask = false;
137 $form->addInput(array('type'=>'combobox',
139 'style'=>'width: 250px;',
141 'datakeys'=>array('id','name'),
142 'empty'=>array(''=>$i18n->get('dropdown.all'))));
145 // Add billable dropdown.
146 $showBillable = $user->isPluginEnabled('iv');
148 $include_options = array('1'=>$i18n->get('form.reports.include_billable'),
149 '2'=>$i18n->get('form.reports.include_not_billable'));
150 $form->addInput(array('type'=>'combobox',
151 'name'=>'include_records', // TODO: how about a better name here?
152 'style'=>'width: 250px;',
153 'data'=>$include_options,
154 'empty'=>array(''=>$i18n->get('dropdown.all'))));
157 // Add invoiced / not invoiced selector.
158 $showInvoiceDropdown = $user->isPluginEnabled('iv') && $user->can('manage_invoices');
159 if ($showInvoiceDropdown) {
160 $invoice_options = array('1'=>$i18n->get('form.reports.include_invoiced'),
161 '2'=>$i18n->get('form.reports.include_not_invoiced'));
162 $form->addInput(array('type'=>'combobox',
164 'style'=>'width: 250px;',
165 'data'=>$invoice_options,
166 'empty'=>array(''=>$i18n->get('dropdown.all'))));
168 $showInvoiceCheckbox = $user->isPluginEnabled('iv') && ($user->can('manage_invoices') || $user->isClient());
170 // Add paid status selector.
171 $showPaidStatus = $user->isPluginEnabled('ps') && $user->can('manage_invoices');
172 if ($showPaidStatus) {
173 $form->addInput(array('type'=>'combobox',
174 'name'=>'paid_status',
175 'style'=>'width: 250px;',
176 'data'=>array('1'=>$i18n->get('dropdown.paid'),'2'=>$i18n->get('dropdown.not_paid')),
177 'empty'=>array(''=>$i18n->get('dropdown.all'))
181 // Add timesheet assignment selector.
182 $showTimesheetDropdown = $user->isPluginEnabled('ts') &&
183 ($user->can('view_own_timesheets') || $user->can('view_timesheets') ||
184 $user->can('view_all_timesheets') || ($user->can('view_client_timesheets') && $user->can('view_client_unapproved')));
185 if ($showTimesheetDropdown) {
186 $form->addInput(array('type'=>'combobox',
188 'style'=>'width: 250px;',
189 'data'=>array(TIMESHEET_NOT_ASSIGNED=>$i18n->get('form.reports.include_not_assigned'),
190 TIMESHEET_ASSIGNED=>$i18n->get('form.reports.include_assigned'),
191 TIMESHEET_PENDING=>$i18n->get('form.reports.include_pending'),
192 TIMESHEET_APPROVED=>$i18n->get('form.reports.include_approved'),
193 TIMESHEET_NOT_APPROVED=>$i18n->get('form.reports.include_not_approved')),
194 'empty'=>array(''=>$i18n->get('dropdown.all'))
197 $showTimesheetCheckbox = $user->isPluginEnabled('ts') &&
198 ($user->can('view_own_timesheets') || $user->can('view_timesheets') ||
199 $user->can('view_all_timesheets') || $user->can('view_client_timesheets'));
202 $showUsers = $user->can('view_reports') || $user->can('view_all_reports') || $user->isClient();
203 $user_list = array();
205 // Prepare user and assigned projects arrays.
206 if ($user->can('view_reports') || $user->can('view_all_reports')) {
207 $rank = $user->getMaxRankForGroup($user->getGroup());
208 if ($user->can('view_all_reports')) $max_rank = MAX_RANK;
209 if ($user->can('view_own_reports'))
210 $options = array('max_rank'=>$max_rank,'include_self'=>true);
212 $options = array('max_rank'=>$max_rank);
213 $users = $user->getUsers($options); // Active and inactive users.
215 elseif ($user->isClient())
216 $users = ttGroupHelper::getUsersForClient(); // Active and inactive users for clients.
218 foreach ($users as $single_user) {
219 $user_list[$single_user['id']] = $single_user['name'];
220 $projects = ttProjectHelper::getAssignedProjects($single_user['id']);
222 foreach ($projects as $single_project) {
223 $assigned_projects[$single_user['id']][] = $single_project['id'];
227 $row_count = ceil(count($user_list)/3);
228 $form->addInput(array('type'=>'checkboxgroup',
232 'groupin'=>$row_count,
233 'style'=>'width: 100%;'));
236 // Add control for time period.
237 $form->addInput(array('type'=>'combobox',
239 'style'=>'width: 250px;',
240 'data'=>array(INTERVAL_THIS_MONTH=>$i18n->get('dropdown.current_month'),
241 INTERVAL_LAST_MONTH=>$i18n->get('dropdown.previous_month'),
242 INTERVAL_THIS_WEEK=>$i18n->get('dropdown.current_week'),
243 INTERVAL_LAST_WEEK=>$i18n->get('dropdown.previous_week'),
244 INTERVAL_THIS_DAY=>$i18n->get('dropdown.current_day'),
245 INTERVAL_LAST_DAY=>$i18n->get('dropdown.previous_day')),
246 'empty'=>array(''=>$i18n->get('dropdown.select'))));
247 // Add controls for start and end dates.
248 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'start_date'));
249 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'end_date'));
251 // Add checkboxes for "Show fields" block.
253 $form->addInput(array('type'=>'checkbox','name'=>'chclient'));
255 $form->addInput(array('type'=>'checkbox','name'=>'chproject'));
257 $form->addInput(array('type'=>'checkbox','name'=>'chtask'));
258 if ($showCustomFieldCheckbox)
259 $form->addInput(array('type'=>'checkbox','name'=>'chcf_1'));
260 if ($showInvoiceCheckbox)
261 $form->addInput(array('type'=>'checkbox','name'=>'chinvoice'));
263 $form->addInput(array('type'=>'checkbox','name'=>'chpaid'));
264 $showIP = $user->can('view_reports') || $user->can('view_all_reports');
266 $form->addInput(array('type'=>'checkbox','name'=>'chip'));
267 $recordType = $user->getRecordType();
268 $showStart = TYPE_START_FINISH == $recordType || TYPE_ALL == $recordType;
269 $showFinish = $showStart;
271 $form->addInput(array('type'=>'checkbox','name'=>'chstart'));
273 $form->addInput(array('type'=>'checkbox','name'=>'chfinish'));
274 $form->addInput(array('type'=>'checkbox','name'=>'chduration'));
275 $form->addInput(array('type'=>'checkbox','name'=>'chnote'));
276 $form->addInput(array('type'=>'checkbox','name'=>'chcost'));
277 $showWorkUnits = $user->isPluginEnabled('wu');
279 $form->addInput(array('type'=>'checkbox','name'=>'chunits'));
280 if ($showTimesheetCheckbox)
281 $form->addInput(array('type'=>'checkbox','name'=>'chtimesheet'));
283 // Add a hidden control for timesheet_user_id (who to generate a timesheet for).
284 if ($showTimesheetCheckbox)
285 $form->addInput(array('type'=>'hidden','name'=>'timesheet_user_id'));
287 // Add group by control.
288 $group_by_options['no_grouping'] = $i18n->get('form.reports.group_by_no');
289 $group_by_options['date'] = $i18n->get('form.reports.group_by_date');
290 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
291 $group_by_options['user'] = $i18n->get('form.reports.group_by_user');
292 if ($user->isPluginEnabled('cl') && !($user->isClient() && $user->client_id))
293 $group_by_options['client'] = $i18n->get('form.reports.group_by_client');
294 if (MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode)
295 $group_by_options['project'] = $i18n->get('form.reports.group_by_project');
296 if (MODE_PROJECTS_AND_TASKS == $trackingMode)
297 $group_by_options['task'] = $i18n->get('form.reports.group_by_task');
298 if ($custom_fields && $custom_fields->fields[0] && $custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
299 $group_by_options['cf_1'] = $custom_fields->fields[0]['label'];
301 $group_by_options_size = sizeof($group_by_options);
302 $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by1','data'=>$group_by_options));
303 if ($group_by_options_size > 2) $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by2','data'=>$group_by_options));
304 if ($group_by_options_size > 3) $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by3','data'=>$group_by_options));
305 $form->addInput(array('type'=>'checkbox','name'=>'chtotalsonly'));
307 // Add text field for a new favorite report name.
308 $form->addInput(array('type'=>'text','name'=>'new_fav_report','maxlength'=>'30','style'=>'width: 250px;'));
310 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
312 $form->addInput(array('type'=>'submit','name'=>'btn_generate','value'=>$i18n->get('button.generate')));
314 // Create a bean (which is a mechanism to remember form values in session).
315 $bean = new ActionForm('reportBean', $form, $request);
316 // At this point form values are obtained from session if they are there.
318 if ($request->isGet() && !$bean->isSaved()) {
319 // No previous form data were found in session. Use the following default values.
320 $form->setValueByElement('users', array_keys($user_list));
321 $period = new Period(INTERVAL_THIS_MONTH, new DateAndTime($user->getDateFormat()));
322 $form->setValueByElement('start_date', $period->getStartDate());
323 $form->setValueByElement('end_date', $period->getEndDate());
324 $form->setValueByElement('chclient', '1');
325 $form->setValueByElement('chinvoice', '0');
326 $form->setValueByElement('chpaid', '0');
327 $form->setValueByElement('chip', '0');
328 $form->setValueByElement('chproject', '1');
329 $form->setValueByElement('chstart', '1');
330 $form->setValueByElement('chduration', '1');
331 $form->setValueByElement('chcost', '0');
332 $form->setValueByElement('chtask', '1');
333 $form->setValueByElement('chfinish', '1');
334 $form->setValueByElement('chnote', '1');
335 $form->setValueByElement('chcf_1', '0');
336 $form->setValueByElement('chunits', '0');
337 $form->setValueByElement('chtimesheet', '0');
338 $form->setValueByElement('chtotalsonly', '0');
341 $form->setValueByElement('fav_report_changed','');
343 // Disable the Delete button when no favorite report is selected.
344 if (!$bean->getAttribute('favorite_report') || ($bean->getAttribute('favorite_report') == -1))
345 $form->getElement('btn_delete')->setEnabled(false);
347 if ($request->isPost()) {
348 if((!$bean->getAttribute('btn_generate') && ($request->getParameter('fav_report_changed')))) {
349 // User changed favorite report. We need to load new values into the form.
350 if ($bean->getAttribute('favorite_report')) {
351 // This loads new favorite report options into the bean (into our form).
352 ttFavReportHelper::loadReport($bean);
354 // If user selected no favorite report - mark all user checkboxes (most probable scenario).
355 if ($bean->getAttribute('favorite_report') == -1)
356 $form->setValueByElement('users', array_keys($user_list));
358 // Save form data in session for future use.
360 header('Location: reports.php');
363 } elseif ($bean->getAttribute('btn_save')) {
364 // User clicked the Save button. We need to save form options as new favorite report.
365 if (!ttValidString($bean->getAttribute('new_fav_report'))) $err->add($i18n->get('error.field'), $i18n->get('form.reports.save_as_favorite'));
368 $id = ttFavReportHelper::saveReport($bean);
370 $err->add($i18n->get('error.db'));
372 $bean->setAttribute('favorite_report', $id);
374 header('Location: reports.php');
378 } elseif($bean->getAttribute('btn_delete')) {
379 // Delete button pressed. User wants to delete a favorite report.
380 if ($bean->getAttribute('favorite_report')) {
381 ttFavReportHelper::deleteReport($bean->getAttribute('favorite_report'));
382 // Load default report.
383 $bean->setAttribute('favorite_report','');
384 $bean->setAttribute('new_fav_report', $report_list[0]['name']);
385 ttFavReportHelper::loadReport($bean);
386 $form->setValueByElement('users', array_keys($user_list));
388 header('Location: reports.php');
392 // Generate button pressed. Check some values.
393 if (!$bean->getAttribute('period')) {
394 $start_date = new DateAndTime($user->getDateFormat(), $bean->getAttribute('start_date'));
396 if ($start_date->isError() || !$bean->getAttribute('start_date'))
397 $err->add($i18n->get('error.field'), $i18n->get('label.start_date'));
399 $end_date = new DateAndTime($user->getDateFormat(), $bean->getAttribute('end_date'));
400 if ($end_date->isError() || !$bean->getAttribute('end_date'))
401 $err->add($i18n->get('error.field'), $i18n->get('label.end_date'));
403 if ($start_date->compare($end_date) > 0)
404 $err->add($i18n->get('error.interval'), $i18n->get('label.end_date'), $i18n->get('label.start_date'));
406 $group_by1 = $bean->getAttribute('group_by1');
407 $group_by2 = $bean->getAttribute('group_by2');
408 $group_by3 = $bean->getAttribute('group_by3');
409 if (($group_by3 != null && $group_by3 != 'no_grouping') && ($group_by3 == $group_by1 || $group_by3 == $group_by2))
410 $err->add($i18n->get('error.field'), $i18n->get('form.reports.group_by'));
411 if (($group_by2 != null && $group_by2 != 'no_grouping') && ($group_by2 == $group_by1 || $group_by3 == $group_by2))
412 $err->add($i18n->get('error.field'), $i18n->get('form.reports.group_by'));
413 // Check remaining values.
414 if (!ttReportHelper::verifyBean($bean)) $err->add($i18n->get('error.sys'));
418 // Now we can go ahead and create a report.
419 header('Location: report.php');
425 $smarty->assign('show_client', $showClient);
426 $smarty->assign('show_cf_1_dropdown', $showCustomFieldDropdown);
427 $smarty->assign('show_cf_1_checkbox', $showCustomFieldCheckbox);
428 $smarty->assign('show_project', $showProject);
429 $smarty->assign('show_task', $showTask);
430 $smarty->assign('show_billable', $showBillable);
431 $smarty->assign('show_invoice_dropdown', $showInvoiceDropdown);
432 $smarty->assign('show_invoice_checkbox', $showInvoiceCheckbox);
433 $smarty->assign('show_paid_status', $showPaidStatus);
434 $smarty->assign('show_timesheet_dropdown', $showTimesheetDropdown);
435 $smarty->assign('show_timesheet_checkbox', $showTimesheetCheckbox);
436 $smarty->assign('show_users', $showUsers);
437 $smarty->assign('show_start', $showStart);
438 $smarty->assign('show_finish', $showFinish);
439 $smarty->assign('show_work_units', $showWorkUnits);
440 $smarty->assign('show_ip', $showIP);
441 $smarty->assign('project_list', $project_list);
442 $smarty->assign('task_list', $task_list);
443 $smarty->assign('assigned_projects', $assigned_projects);
444 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
445 $smarty->assign('onload', 'onLoad="handleCheckboxes()"');
446 $smarty->assign('title', $i18n->get('title.reports'));
447 $smarty->assign('content_page_name', 'reports.tpl');
448 $smarty->display('index.tpl');