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