A bit more work in progress on timesheets.
[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'))) {
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 }
59
60 $form = new Form('reportForm');
61
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;',
68   'data'=>$report_list,
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')."')"));
75
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.
81   } else
82     $client_list = ttClientHelper::getClientsForUser();
83   $form->addInput(array('type'=>'combobox',
84     'name'=>'client',
85     'style'=>'width: 250px;',
86     'data'=>$client_list,
87     'datakeys'=>array('id', 'name'),
88     'empty'=>array(''=>$i18n->get('dropdown.all'))));
89 }
90
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;',
95       'value'=>$cl_cf_1,
96       'data'=>$custom_fields->options,
97       'empty'=>array(''=>$i18n->get('dropdown.all'))));
98 }
99
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();
105 } else {
106   $project_list = ttProjectHelper::getAssignedProjects($user->getUser());
107 }
108 $form->addInput(array('type'=>'combobox',
109   'onchange'=>'fillTaskDropdown(this.value);selectAssignedUsers(this.value);',
110   'name'=>'project',
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',
118     'name'=>'task',
119     'style'=>'width: 250px;',
120     'data'=>$task_list,
121     'datakeys'=>array('id','name'),
122     'empty'=>array(''=>$i18n->get('dropdown.all'))));
123 }
124
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'))));
133
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',
139     'name'=>'invoice',
140     'style'=>'width: 250px;',
141     'data'=>$invoice_options,
142     'empty'=>array(''=>$i18n->get('dropdown.all'))));
143 }
144
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'))
151  ));
152 }
153
154 // TODO: check rights.
155 if ($user->isPluginEnabled('ts')) {
156   $form->addInput(array('type'=>'combobox',
157    'name'=>'timesheet',
158    'style'=>'width: 250px;',
159    'data'=>array('1'=>$i18n->get('form.reports.include_assigned'),'2'=>$i18n->get('form.reports.include_not_assigned')),
160    'empty'=>array(''=>$i18n->get('dropdown.all'))
161  ));
162 }
163
164 $user_list = array();
165 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
166   // Prepare user and assigned projects arrays.
167   if ($user->can('view_reports') || $user->can('view_all_reports')) {
168     $rank = $user->getMaxRankForGroup($user->getGroup());
169     if ($user->can('view_all_reports')) $max_rank = MAX_RANK;
170     if ($user->can('view_own_reports'))
171       $options = array('max_rank'=>$max_rank,'include_self'=>true);
172     else
173       $options = array('max_rank'=>$max_rank);
174     $users = $user->getUsers($options); // Active and inactive users.
175   }
176   elseif ($user->isClient())
177     $users = ttGroupHelper::getUsersForClient(); // Active and inactive users for clients.
178
179   foreach ($users as $single_user) {
180     $user_list[$single_user['id']] = $single_user['name'];
181     $projects = ttProjectHelper::getAssignedProjects($single_user['id']);
182     if ($projects) {
183       foreach ($projects as $single_project) {
184         $assigned_projects[$single_user['id']][] = $single_project['id'];
185       }
186     }
187   }
188   $row_count = ceil(count($user_list)/3);
189   $form->addInput(array('type'=>'checkboxgroup',
190     'name'=>'users',
191     'data'=>$user_list,
192     'layout'=>'V',
193     'groupin'=>$row_count,
194     'style'=>'width: 100%;'));
195 }
196
197 // Add control for time period.
198 $form->addInput(array('type'=>'combobox',
199   'name'=>'period',
200   'style'=>'width: 250px;',
201   'data'=>array(INTERVAL_THIS_MONTH=>$i18n->get('dropdown.current_month'),
202     INTERVAL_LAST_MONTH=>$i18n->get('dropdown.previous_month'),
203     INTERVAL_THIS_WEEK=>$i18n->get('dropdown.current_week'),
204     INTERVAL_LAST_WEEK=>$i18n->get('dropdown.previous_week'),
205     INTERVAL_THIS_DAY=>$i18n->get('dropdown.current_day'),
206     INTERVAL_LAST_DAY=>$i18n->get('dropdown.previous_day')),
207   'empty'=>array(''=>$i18n->get('dropdown.select'))));
208 // Add controls for start and end dates.
209 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'start_date'));
210 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'end_date'));
211
212 // Add checkboxes for fields.
213 if ($user->isPluginEnabled('cl'))
214   $form->addInput(array('type'=>'checkbox','name'=>'chclient'));
215 if (($user->can('manage_invoices') || $user->isClient()) && $user->isPluginEnabled('iv'))
216   $form->addInput(array('type'=>'checkbox','name'=>'chinvoice'));
217 if ($user->can('manage_invoices') && $user->isPluginEnabled('ps'))
218   $form->addInput(array('type'=>'checkbox','name'=>'chpaid'));
219 if ($user->can('view_reports') || $user->can('view_all_reports'))
220   $form->addInput(array('type'=>'checkbox','name'=>'chip'));
221 if (MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode)
222   $form->addInput(array('type'=>'checkbox','name'=>'chproject'));
223 if (MODE_PROJECTS_AND_TASKS == $trackingMode)
224   $form->addInput(array('type'=>'checkbox','name'=>'chtask'));
225 if ((TYPE_START_FINISH == $user->getRecordType()) || (TYPE_ALL == $user->getRecordType())) {
226   $form->addInput(array('type'=>'checkbox','name'=>'chstart'));
227   $form->addInput(array('type'=>'checkbox','name'=>'chfinish'));
228 }
229 $form->addInput(array('type'=>'checkbox','name'=>'chduration'));
230 $form->addInput(array('type'=>'checkbox','name'=>'chnote'));
231 $form->addInput(array('type'=>'checkbox','name'=>'chcost'));
232 // If we have a custom field - add a checkbox for it.
233 if ($custom_fields && $custom_fields->fields[0])
234   $form->addInput(array('type'=>'checkbox','name'=>'chcf_1'));
235 if ($user->isPluginEnabled('wu'))
236   $form->addInput(array('type'=>'checkbox','name'=>'chunits'));
237
238 // Add group by control.
239 $group_by_options['no_grouping'] = $i18n->get('form.reports.group_by_no');
240 $group_by_options['date'] = $i18n->get('form.reports.group_by_date');
241 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient())
242   $group_by_options['user'] = $i18n->get('form.reports.group_by_user');
243 if ($user->isPluginEnabled('cl') && !($user->isClient() && $user->client_id))
244   $group_by_options['client'] = $i18n->get('form.reports.group_by_client');
245 if (MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode)
246   $group_by_options['project'] = $i18n->get('form.reports.group_by_project');
247 if (MODE_PROJECTS_AND_TASKS == $trackingMode)
248   $group_by_options['task'] = $i18n->get('form.reports.group_by_task');
249 if ($custom_fields && $custom_fields->fields[0] && $custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
250   $group_by_options['cf_1'] = $custom_fields->fields[0]['label'];
251 }
252 $group_by_options_size = sizeof($group_by_options);
253 $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by1','data'=>$group_by_options));
254 if ($group_by_options_size > 2) $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by2','data'=>$group_by_options));
255 if ($group_by_options_size > 3) $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by3','data'=>$group_by_options));
256 $form->addInput(array('type'=>'checkbox','name'=>'chtotalsonly'));
257
258 // Add text field for a new favorite report name.
259 $form->addInput(array('type'=>'text','name'=>'new_fav_report','maxlength'=>'30','style'=>'width: 250px;'));
260 // Save button.
261 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
262
263 $form->addInput(array('type'=>'submit','name'=>'btn_generate','value'=>$i18n->get('button.generate')));
264
265 // Create a bean (which is a mechanism to remember form values in session).
266 $bean = new ActionForm('reportBean', $form, $request);
267 // At this point form values are obtained from session if they are there.
268
269 if ($request->isGet() && !$bean->isSaved()) {
270   // No previous form data were found in session. Use the following default values.
271   $form->setValueByElement('users', array_keys($user_list));
272   $period = new Period(INTERVAL_THIS_MONTH, new DateAndTime($user->getDateFormat()));
273   $form->setValueByElement('start_date', $period->getStartDate());
274   $form->setValueByElement('end_date', $period->getEndDate());
275   $form->setValueByElement('chclient', '1');
276   $form->setValueByElement('chinvoice', '0');
277   $form->setValueByElement('chpaid', '0');
278   $form->setValueByElement('chip', '0');
279   $form->setValueByElement('chproject', '1');
280   $form->setValueByElement('chstart', '1');
281   $form->setValueByElement('chduration', '1');
282   $form->setValueByElement('chcost', '0');
283   $form->setValueByElement('chtask', '1');
284   $form->setValueByElement('chfinish', '1');
285   $form->setValueByElement('chnote', '1');
286   $form->setValueByElement('chcf_1', '0');
287   $form->setValueByElement('chunits', '0');
288   $form->setValueByElement('chtotalsonly', '0');
289 }
290
291 $form->setValueByElement('fav_report_changed','');
292
293 // Disable the Delete button when no favorite report is selected.
294 if (!$bean->getAttribute('favorite_report') || ($bean->getAttribute('favorite_report') == -1))
295   $form->getElement('btn_delete')->setEnabled(false);
296
297 if ($request->isPost()) {
298   if((!$bean->getAttribute('btn_generate') && ($request->getParameter('fav_report_changed')))) {
299     // User changed favorite report. We need to load new values into the form.
300     if ($bean->getAttribute('favorite_report')) {
301       // This loads new favorite report options into the bean (into our form).
302       ttFavReportHelper::loadReport($bean);
303
304       // If user selected no favorite report - mark all user checkboxes (most probable scenario).
305       if ($bean->getAttribute('favorite_report') == -1)
306         $form->setValueByElement('users', array_keys($user_list));
307
308       // Save form data in session for future use.
309       $bean->saveBean();
310       header('Location: reports.php');
311       exit();
312     }
313   } elseif ($bean->getAttribute('btn_save')) {
314     // User clicked the Save button. We need to save form options as new favorite report.
315     if (!ttValidString($bean->getAttribute('new_fav_report'))) $err->add($i18n->get('error.field'), $i18n->get('form.reports.save_as_favorite'));
316
317     if ($err->no()) {
318       $id = ttFavReportHelper::saveReport($bean);
319       if (!$id)
320         $err->add($i18n->get('error.db'));
321       if ($err->no()) {
322         $bean->setAttribute('favorite_report', $id);
323         $bean->saveBean();
324         header('Location: reports.php');
325         exit();
326       }
327     }
328   } elseif($bean->getAttribute('btn_delete')) {
329     // Delete button pressed. User wants to delete a favorite report.
330     if ($bean->getAttribute('favorite_report')) {
331       ttFavReportHelper::deleteReport($bean->getAttribute('favorite_report'));
332       // Load default report.
333       $bean->setAttribute('favorite_report','');
334       $bean->setAttribute('new_fav_report', $report_list[0]['name']);
335       ttFavReportHelper::loadReport($bean);
336       $form->setValueByElement('users', array_keys($user_list));
337       $bean->saveBean();
338       header('Location: reports.php');
339       exit();
340     }
341   } else {
342     // Generate button pressed. Check some values.
343     if (!$bean->getAttribute('period')) {
344       $start_date = new DateAndTime($user->getDateFormat(), $bean->getAttribute('start_date'));
345
346       if ($start_date->isError() || !$bean->getAttribute('start_date'))
347         $err->add($i18n->get('error.field'), $i18n->get('label.start_date'));
348
349       $end_date = new DateAndTime($user->getDateFormat(), $bean->getAttribute('end_date'));
350       if ($end_date->isError() || !$bean->getAttribute('end_date'))
351         $err->add($i18n->get('error.field'), $i18n->get('label.end_date'));
352
353       if ($start_date->compare($end_date) > 0)
354         $err->add($i18n->get('error.interval'), $i18n->get('label.end_date'), $i18n->get('label.start_date'));
355     }
356     $group_by1 = $bean->getAttribute('group_by1');
357     $group_by2 = $bean->getAttribute('group_by2');
358     $group_by3 = $bean->getAttribute('group_by3');
359     if (($group_by3 != null && $group_by3 != 'no_grouping') && ($group_by3 == $group_by1 || $group_by3 == $group_by2))
360       $err->add($i18n->get('error.field'), $i18n->get('form.reports.group_by'));
361     if (($group_by2 != null && $group_by2 != 'no_grouping') && ($group_by2 == $group_by1 || $group_by3 == $group_by2))
362       $err->add($i18n->get('error.field'), $i18n->get('form.reports.group_by'));
363     // Check remaining values.
364     if (!ttReportHelper::verifyBean($bean)) $err->add($i18n->get('error.sys'));
365
366     if ($err->no()) {
367       $bean->saveBean();
368       // Now we can go ahead and create a report.
369       header('Location: report.php');
370       exit();
371     }
372   }
373 } // isPost
374
375 $smarty->assign('project_list', $project_list);
376 $smarty->assign('task_list', $task_list);
377 $smarty->assign('assigned_projects', $assigned_projects);
378 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
379 $smarty->assign('onload', 'onLoad="handleCheckboxes()"');
380 $smarty->assign('title', $i18n->get('title.reports'));
381 $smarty->assign('content_page_name', 'reports.tpl');
382 $smarty->display('index.tpl');