Renamed client access rights for clarity.
[timetracker.git] / reports.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 require_once('initialize.php');
30 import('form.Form');
31 import('form.ActionForm');
32 import('DateAndTime');
33 import('ttGroupHelper');
34 import('Period');
35 import('ttProjectHelper');
36 import('ttFavReportHelper');
37 import('ttClientHelper');
38 import('ttReportHelper');
39
40 // Access check.
41 if (!(ttAccessAllowed('view_own_reports') || ttAccessAllowed('view_reports') || ttAccessAllowed('view_all_reports') || ttAccessAllowed('view_client_reports'))) {
42   header('Location: access_denied.php');
43   exit();
44 }
45 if (!$user->exists()) {
46   header('Location: access_denied.php'); // No users in subgroup.
47   exit();
48 }
49 // End of access checks.
50
51 $trackingMode = $user->getTrackingMode();
52
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;
62 }
63
64 $form = new Form('reportForm');
65
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;',
72   'data'=>$report_list,
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')."')"));
79
80 // Dropdown for clients if the clients plugin is enabled.
81 $showClient = $user->isPluginEnabled('cl') && !$user->isClient();
82 if ($showClient) {
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.
86   } else
87     $client_list = ttClientHelper::getClientsForUser();
88   if (count($client_list) == 0) $showClient = false;
89 }
90 if ($showClient) {
91   $form->addInput(array('type'=>'combobox',
92     'name'=>'client',
93     'style'=>'width: 250px;',
94     'data'=>$client_list,
95     'datakeys'=>array('id', 'name'),
96     'empty'=>array(''=>$i18n->get('dropdown.all'))));
97 }
98
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;',
103     'value'=>$cl_cf_1,
104     'data'=>$custom_fields->options,
105     'empty'=>array(''=>$i18n->get('dropdown.all'))));
106 }
107
108 // Add project dropdown.
109 $showProject = MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode;
110 if ($showProject) {
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();
115   } else {
116     $project_list = ttProjectHelper::getAssignedProjects($user->getUser());
117   }
118   if (count($project_list) == 0) $showProject = false;
119 }
120 if ($showProject) {
121   $form->addInput(array('type'=>'combobox',
122     'onchange'=>'fillTaskDropdown(this.value);selectAssignedUsers(this.value);',
123     'name'=>'project',
124     'style'=>'width: 250px;',
125     'data'=>$project_list,
126     'datakeys'=>array('id','name'),
127     'empty'=>array(''=>$i18n->get('dropdown.all'))));
128 }
129
130 // Add task dropdown.
131 $showTask = MODE_PROJECTS_AND_TASKS == $trackingMode;
132 if ($showTask) {
133   $task_list = ttGroupHelper::getActiveTasks();
134   if (count($task_list) == 0) $showTask = false;
135 }
136 if ($showTask) {
137   $form->addInput(array('type'=>'combobox',
138     'name'=>'task',
139     'style'=>'width: 250px;',
140     'data'=>$task_list,
141     'datakeys'=>array('id','name'),
142     'empty'=>array(''=>$i18n->get('dropdown.all'))));
143 }
144
145 // Add billable dropdown.
146 $showBillable = $user->isPluginEnabled('iv');
147 if ($showBillable) {
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'))));
155 }
156
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',
163     'name'=>'invoice',
164     'style'=>'width: 250px;',
165     'data'=>$invoice_options,
166     'empty'=>array(''=>$i18n->get('dropdown.all'))));
167 }
168 $showInvoiceCheckbox = $user->isPluginEnabled('iv') && ($user->can('manage_invoices') || $user->isClient());
169
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'))
178  ));
179 }
180
181 // Add timesheet assignment selector.
182 $showTimesheet = $user->isPluginEnabled('ts') &&
183   ($user->can('view_own_timesheets') || $user->can('manage_own_timesheets') ||
184   $user->can('view_timesheets') || $user->can('manage_timesheets') || $user->can('approve_timesheets'));
185 if ($showTimesheet) {
186   $form->addInput(array('type'=>'combobox',
187    'name'=>'timesheet',
188    'style'=>'width: 250px;',
189    'data'=>array('1'=>$i18n->get('form.reports.include_assigned'),'2'=>$i18n->get('form.reports.include_not_assigned')),
190    'empty'=>array(''=>$i18n->get('dropdown.all'))
191   ));
192 }
193
194 // Add user table.
195 $showUsers = $user->can('view_reports') || $user->can('view_all_reports') || $user->isClient();
196 $user_list = array();
197 if ($showUsers) {
198   // Prepare user and assigned projects arrays.
199   if ($user->can('view_reports') || $user->can('view_all_reports')) {
200     $rank = $user->getMaxRankForGroup($user->getGroup());
201     if ($user->can('view_all_reports')) $max_rank = MAX_RANK;
202     if ($user->can('view_own_reports'))
203       $options = array('max_rank'=>$max_rank,'include_self'=>true);
204     else
205       $options = array('max_rank'=>$max_rank);
206     $users = $user->getUsers($options); // Active and inactive users.
207   }
208   elseif ($user->isClient())
209     $users = ttGroupHelper::getUsersForClient(); // Active and inactive users for clients.
210
211   foreach ($users as $single_user) {
212     $user_list[$single_user['id']] = $single_user['name'];
213     $projects = ttProjectHelper::getAssignedProjects($single_user['id']);
214     if ($projects) {
215       foreach ($projects as $single_project) {
216         $assigned_projects[$single_user['id']][] = $single_project['id'];
217       }
218     }
219   }
220   $row_count = ceil(count($user_list)/3);
221   $form->addInput(array('type'=>'checkboxgroup',
222     'name'=>'users',
223     'data'=>$user_list,
224     'layout'=>'V',
225     'groupin'=>$row_count,
226     'style'=>'width: 100%;'));
227 }
228
229 // Add control for time period.
230 $form->addInput(array('type'=>'combobox',
231   'name'=>'period',
232   'style'=>'width: 250px;',
233   'data'=>array(INTERVAL_THIS_MONTH=>$i18n->get('dropdown.current_month'),
234     INTERVAL_LAST_MONTH=>$i18n->get('dropdown.previous_month'),
235     INTERVAL_THIS_WEEK=>$i18n->get('dropdown.current_week'),
236     INTERVAL_LAST_WEEK=>$i18n->get('dropdown.previous_week'),
237     INTERVAL_THIS_DAY=>$i18n->get('dropdown.current_day'),
238     INTERVAL_LAST_DAY=>$i18n->get('dropdown.previous_day')),
239   'empty'=>array(''=>$i18n->get('dropdown.select'))));
240 // Add controls for start and end dates.
241 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'start_date'));
242 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'end_date'));
243
244 // Add checkboxes for "Show fields" block.
245 if ($showClient)
246   $form->addInput(array('type'=>'checkbox','name'=>'chclient'));
247 if ($showProject)
248   $form->addInput(array('type'=>'checkbox','name'=>'chproject'));
249 if ($showTask)
250   $form->addInput(array('type'=>'checkbox','name'=>'chtask'));
251 if ($showCustomFieldCheckbox)
252   $form->addInput(array('type'=>'checkbox','name'=>'chcf_1'));
253 if ($showInvoiceCheckbox)
254   $form->addInput(array('type'=>'checkbox','name'=>'chinvoice'));
255 if ($showPaidStatus)
256   $form->addInput(array('type'=>'checkbox','name'=>'chpaid'));
257 $showIP = $user->can('view_reports') || $user->can('view_all_reports');
258 if ($showIP)
259   $form->addInput(array('type'=>'checkbox','name'=>'chip'));
260 $recordType = $user->getRecordType();
261 $showStart = TYPE_START_FINISH == $recordType || TYPE_ALL == $recordType;
262 $showFinish = $showStart;
263 if ($showStart)
264   $form->addInput(array('type'=>'checkbox','name'=>'chstart'));
265 if ($showFinish)
266   $form->addInput(array('type'=>'checkbox','name'=>'chfinish'));
267 $form->addInput(array('type'=>'checkbox','name'=>'chduration'));
268 $form->addInput(array('type'=>'checkbox','name'=>'chnote'));
269 $form->addInput(array('type'=>'checkbox','name'=>'chcost'));
270 $showWorkUnits = $user->isPluginEnabled('wu');
271 if ($showWorkUnits)
272   $form->addInput(array('type'=>'checkbox','name'=>'chunits'));
273 if ($showTimesheet)
274   $form->addInput(array('type'=>'checkbox','name'=>'chtimesheet'));
275
276 // Add a hidden control for timesheet_user_id (who to generate a timesheet for).
277 if ($showTimesheet)
278   $form->addInput(array('type'=>'hidden','name'=>'timesheet_user_id'));
279
280 // Add group by control.
281 $group_by_options['no_grouping'] = $i18n->get('form.reports.group_by_no');
282 $group_by_options['date'] = $i18n->get('form.reports.group_by_date');
283 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
284   $group_by_options['user'] = $i18n->get('form.reports.group_by_user');
285 if ($user->isPluginEnabled('cl') && !($user->isClient() && $user->client_id))
286   $group_by_options['client'] = $i18n->get('form.reports.group_by_client');
287 if (MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode)
288   $group_by_options['project'] = $i18n->get('form.reports.group_by_project');
289 if (MODE_PROJECTS_AND_TASKS == $trackingMode)
290   $group_by_options['task'] = $i18n->get('form.reports.group_by_task');
291 if ($custom_fields && $custom_fields->fields[0] && $custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
292   $group_by_options['cf_1'] = $custom_fields->fields[0]['label'];
293 }
294 $group_by_options_size = sizeof($group_by_options);
295 $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by1','data'=>$group_by_options));
296 if ($group_by_options_size > 2) $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by2','data'=>$group_by_options));
297 if ($group_by_options_size > 3) $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by3','data'=>$group_by_options));
298 $form->addInput(array('type'=>'checkbox','name'=>'chtotalsonly'));
299
300 // Add text field for a new favorite report name.
301 $form->addInput(array('type'=>'text','name'=>'new_fav_report','maxlength'=>'30','style'=>'width: 250px;'));
302 // Save button.
303 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
304
305 $form->addInput(array('type'=>'submit','name'=>'btn_generate','value'=>$i18n->get('button.generate')));
306
307 // Create a bean (which is a mechanism to remember form values in session).
308 $bean = new ActionForm('reportBean', $form, $request);
309 // At this point form values are obtained from session if they are there.
310
311 if ($request->isGet() && !$bean->isSaved()) {
312   // No previous form data were found in session. Use the following default values.
313   $form->setValueByElement('users', array_keys($user_list));
314   $period = new Period(INTERVAL_THIS_MONTH, new DateAndTime($user->getDateFormat()));
315   $form->setValueByElement('start_date', $period->getStartDate());
316   $form->setValueByElement('end_date', $period->getEndDate());
317   $form->setValueByElement('chclient', '1');
318   $form->setValueByElement('chinvoice', '0');
319   $form->setValueByElement('chpaid', '0');
320   $form->setValueByElement('chip', '0');
321   $form->setValueByElement('chproject', '1');
322   $form->setValueByElement('chstart', '1');
323   $form->setValueByElement('chduration', '1');
324   $form->setValueByElement('chcost', '0');
325   $form->setValueByElement('chtask', '1');
326   $form->setValueByElement('chfinish', '1');
327   $form->setValueByElement('chnote', '1');
328   $form->setValueByElement('chcf_1', '0');
329   $form->setValueByElement('chunits', '0');
330   $form->setValueByElement('chtimesheet', '0');
331   $form->setValueByElement('chtotalsonly', '0');
332 }
333
334 $form->setValueByElement('fav_report_changed','');
335
336 // Disable the Delete button when no favorite report is selected.
337 if (!$bean->getAttribute('favorite_report') || ($bean->getAttribute('favorite_report') == -1))
338   $form->getElement('btn_delete')->setEnabled(false);
339
340 if ($request->isPost()) {
341   if((!$bean->getAttribute('btn_generate') && ($request->getParameter('fav_report_changed')))) {
342     // User changed favorite report. We need to load new values into the form.
343     if ($bean->getAttribute('favorite_report')) {
344       // This loads new favorite report options into the bean (into our form).
345       ttFavReportHelper::loadReport($bean);
346
347       // If user selected no favorite report - mark all user checkboxes (most probable scenario).
348       if ($bean->getAttribute('favorite_report') == -1)
349         $form->setValueByElement('users', array_keys($user_list));
350
351       // Save form data in session for future use.
352       $bean->saveBean();
353       header('Location: reports.php');
354       exit();
355     }
356   } elseif ($bean->getAttribute('btn_save')) {
357     // User clicked the Save button. We need to save form options as new favorite report.
358     if (!ttValidString($bean->getAttribute('new_fav_report'))) $err->add($i18n->get('error.field'), $i18n->get('form.reports.save_as_favorite'));
359
360     if ($err->no()) {
361       $id = ttFavReportHelper::saveReport($bean);
362       if (!$id)
363         $err->add($i18n->get('error.db'));
364       if ($err->no()) {
365         $bean->setAttribute('favorite_report', $id);
366         $bean->saveBean();
367         header('Location: reports.php');
368         exit();
369       }
370     }
371   } elseif($bean->getAttribute('btn_delete')) {
372     // Delete button pressed. User wants to delete a favorite report.
373     if ($bean->getAttribute('favorite_report')) {
374       ttFavReportHelper::deleteReport($bean->getAttribute('favorite_report'));
375       // Load default report.
376       $bean->setAttribute('favorite_report','');
377       $bean->setAttribute('new_fav_report', $report_list[0]['name']);
378       ttFavReportHelper::loadReport($bean);
379       $form->setValueByElement('users', array_keys($user_list));
380       $bean->saveBean();
381       header('Location: reports.php');
382       exit();
383     }
384   } else {
385     // Generate button pressed. Check some values.
386     if (!$bean->getAttribute('period')) {
387       $start_date = new DateAndTime($user->getDateFormat(), $bean->getAttribute('start_date'));
388
389       if ($start_date->isError() || !$bean->getAttribute('start_date'))
390         $err->add($i18n->get('error.field'), $i18n->get('label.start_date'));
391
392       $end_date = new DateAndTime($user->getDateFormat(), $bean->getAttribute('end_date'));
393       if ($end_date->isError() || !$bean->getAttribute('end_date'))
394         $err->add($i18n->get('error.field'), $i18n->get('label.end_date'));
395
396       if ($start_date->compare($end_date) > 0)
397         $err->add($i18n->get('error.interval'), $i18n->get('label.end_date'), $i18n->get('label.start_date'));
398     }
399     $group_by1 = $bean->getAttribute('group_by1');
400     $group_by2 = $bean->getAttribute('group_by2');
401     $group_by3 = $bean->getAttribute('group_by3');
402     if (($group_by3 != null && $group_by3 != 'no_grouping') && ($group_by3 == $group_by1 || $group_by3 == $group_by2))
403       $err->add($i18n->get('error.field'), $i18n->get('form.reports.group_by'));
404     if (($group_by2 != null && $group_by2 != 'no_grouping') && ($group_by2 == $group_by1 || $group_by3 == $group_by2))
405       $err->add($i18n->get('error.field'), $i18n->get('form.reports.group_by'));
406     // Check remaining values.
407     if (!ttReportHelper::verifyBean($bean)) $err->add($i18n->get('error.sys'));
408
409     if ($err->no()) {
410       $bean->saveBean();
411       // Now we can go ahead and create a report.
412       header('Location: report.php');
413       exit();
414     }
415   }
416 } // isPost
417
418 $smarty->assign('show_client', $showClient);
419 $smarty->assign('show_cf_1_dropdown', $showCustomFieldDropdown);
420 $smarty->assign('show_cf_1_checkbox', $showCustomFieldCheckbox);
421 $smarty->assign('show_project', $showProject);
422 $smarty->assign('show_task', $showTask);
423 $smarty->assign('show_billable', $showBillable);
424 $smarty->assign('show_invoice_dropdown', $showInvoiceDropdown);
425 $smarty->assign('show_invoice_checkbox', $showInvoiceCheckbox);
426 $smarty->assign('show_paid_status', $showPaidStatus);
427 $smarty->assign('show_timesheet', $showTimesheet);
428 $smarty->assign('show_users', $showUsers);
429 $smarty->assign('show_start', $showStart);
430 $smarty->assign('show_finish', $showFinish);
431 $smarty->assign('show_work_units', $showWorkUnits);
432 $smarty->assign('show_ip', $showIP);
433 $smarty->assign('project_list', $project_list);
434 $smarty->assign('task_list', $task_list);
435 $smarty->assign('assigned_projects', $assigned_projects);
436 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
437 $smarty->assign('onload', 'onLoad="handleCheckboxes()"');
438 $smarty->assign('title', $i18n->get('title.reports'));
439 $smarty->assign('content_page_name', 'reports.tpl');
440 $smarty->display('index.tpl');