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