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