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');
40 if (!(ttAccessAllowed('view_own_reports') || ttAccessAllowed('view_reports'))) {
41 header('Location: access_denied.php');
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);
52 $form = new Form('reportForm');
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;',
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')."')"));
68 // Dropdown for clients if the clients plugin is enabled.
69 if ($user->isPluginEnabled('cl') && !($user->isClient() && $user->client_id)) {
70 if ($user->canManageTeam() || ($user->isClient() && !$user->client_id))
71 $client_list = ttClientHelper::getClients();
73 $client_list = ttClientHelper::getClientsForUser();
74 $form->addInput(array('type'=>'combobox',
76 'style'=>'width: 250px;',
78 'datakeys'=>array('id', 'name'),
79 'empty'=>array(''=>$i18n->get('dropdown.all'))));
82 // If we have a TYPE_DROPDOWN custom field - add control to select an option.
83 if ($custom_fields && $custom_fields->fields[0] && $custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
84 $form->addInput(array('type'=>'combobox','name'=>'option',
85 'style'=>'width: 250px;',
87 'data'=>$custom_fields->options,
88 'empty'=>array(''=>$i18n->get('dropdown.all'))));
91 // Add controls for projects and tasks.
92 if ($user->canManageTeam()) {
93 $project_list = ttProjectHelper::getProjects(); // Manager and co-managers can run reports on all active and inactive projects.
94 } elseif ($user->isClient()) {
95 $project_list = ttProjectHelper::getProjectsForClient();
97 $project_list = ttProjectHelper::getAssignedProjects($user->id);
99 $form->addInput(array('type'=>'combobox',
100 'onchange'=>'fillTaskDropdown(this.value);selectAssignedUsers(this.value);',
102 'style'=>'width: 250px;',
103 'data'=>$project_list,
104 'datakeys'=>array('id','name'),
105 'empty'=>array(''=>$i18n->get('dropdown.all'))));
106 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode) {
107 $task_list = ttTeamHelper::getActiveTasks($user->group_id);
108 $form->addInput(array('type'=>'combobox',
110 'style'=>'width: 250px;',
112 'datakeys'=>array('id','name'),
113 'empty'=>array(''=>$i18n->get('dropdown.all'))));
116 // Add include records control.
117 $include_options = array('1'=>$i18n->get('form.reports.include_billable'),
118 '2'=>$i18n->get('form.reports.include_not_billable'));
119 $form->addInput(array('type'=>'combobox',
120 'name'=>'include_records',
121 'style'=>'width: 250px;',
122 'data'=>$include_options,
123 'empty'=>array(''=>$i18n->get('dropdown.all'))));
125 // Add invoiced / not invoiced selector.
126 $invoice_options = array('1'=>$i18n->get('form.reports.include_invoiced'),
127 '2'=>$i18n->get('form.reports.include_not_invoiced'));
128 $form->addInput(array('type'=>'combobox',
130 'style'=>'width: 250px;',
131 'data'=>$invoice_options,
132 'empty'=>array(''=>$i18n->get('dropdown.all'))));
134 if ($user->canManageTeam() && $user->isPluginEnabled('ps')) {
135 $form->addInput(array('type'=>'combobox',
136 'name'=>'paid_status',
137 'style'=>'width: 250px;',
138 'data'=>array('1'=>$i18n->get('dropdown.paid'),'2'=>$i18n->get('dropdown.not_paid')),
139 'empty'=>array(''=>$i18n->get('dropdown.all'))
143 $user_list = array();
144 if ($user->can('view_reports') || $user->isClient()) {
145 // Prepare user and assigned projects arrays.
146 if ($user->can('view_reports')) {
147 if ($user->can('view_own_reports'))
148 $options = array('max_rank'=>$user->rank-1,'include_self'=>true);
150 $options = array('max_rank'=>$user->rank-1);
151 $users = $user->getUsers($options); // Active and inactive users.
153 elseif ($user->isClient())
154 $users = ttTeamHelper::getUsersForClient(); // Active and inactive users for clients.
156 foreach ($users as $single_user) {
157 $user_list[$single_user['id']] = $single_user['name'];
158 $projects = ttProjectHelper::getAssignedProjects($single_user['id']);
160 foreach ($projects as $single_project) {
161 $assigned_projects[$single_user['id']][] = $single_project['id'];
165 $row_count = ceil(count($user_list)/3);
166 $form->addInput(array('type'=>'checkboxgroup',
170 'groupin'=>$row_count,
171 'style'=>'width: 100%;'));
174 // Add control for time period.
175 $form->addInput(array('type'=>'combobox',
177 'style'=>'width: 250px;',
178 'data'=>array(INTERVAL_THIS_MONTH=>$i18n->get('dropdown.current_month'),
179 INTERVAL_LAST_MONTH=>$i18n->get('dropdown.previous_month'),
180 INTERVAL_THIS_WEEK=>$i18n->get('dropdown.current_week'),
181 INTERVAL_LAST_WEEK=>$i18n->get('dropdown.previous_week'),
182 INTERVAL_THIS_DAY=>$i18n->get('dropdown.current_day'),
183 INTERVAL_LAST_DAY=>$i18n->get('dropdown.previous_day')),
184 'empty'=>array(''=>$i18n->get('dropdown.select'))));
185 // Add controls for start and end dates.
186 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'start_date'));
187 $form->addInput(array('type'=>'datefield','maxlength'=>'20','name'=>'end_date'));
189 // Add checkboxes for fields.
190 if ($user->isPluginEnabled('cl'))
191 $form->addInput(array('type'=>'checkbox','name'=>'chclient'));
192 if (($user->canManageTeam() || $user->isClient()) && $user->isPluginEnabled('iv'))
193 $form->addInput(array('type'=>'checkbox','name'=>'chinvoice'));
194 if ($user->canManageTeam() && $user->isPluginEnabled('ps'))
195 $form->addInput(array('type'=>'checkbox','name'=>'chpaid'));
196 if ($user->canManageTeam())
197 $form->addInput(array('type'=>'checkbox','name'=>'chip'));
198 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
199 $form->addInput(array('type'=>'checkbox','name'=>'chproject'));
200 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
201 $form->addInput(array('type'=>'checkbox','name'=>'chtask'));
202 if ((TYPE_START_FINISH == $user->record_type) || (TYPE_ALL == $user->record_type)) {
203 $form->addInput(array('type'=>'checkbox','name'=>'chstart'));
204 $form->addInput(array('type'=>'checkbox','name'=>'chfinish'));
206 $form->addInput(array('type'=>'checkbox','name'=>'chduration'));
207 $form->addInput(array('type'=>'checkbox','name'=>'chnote'));
208 $form->addInput(array('type'=>'checkbox','name'=>'chcost'));
209 // If we have a custom field - add a checkbox for it.
210 if ($custom_fields && $custom_fields->fields[0])
211 $form->addInput(array('type'=>'checkbox','name'=>'chcf_1'));
212 // Add group by control.
213 $group_by_options['no_grouping'] = $i18n->get('form.reports.group_by_no');
214 $group_by_options['date'] = $i18n->get('form.reports.group_by_date');
215 if ($user->canManageTeam() || $user->isClient())
216 $group_by_options['user'] = $i18n->get('form.reports.group_by_user');
217 if ($user->isPluginEnabled('cl') && !($user->isClient() && $user->client_id))
218 $group_by_options['client'] = $i18n->get('form.reports.group_by_client');
219 if (MODE_PROJECTS == $user->tracking_mode || MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
220 $group_by_options['project'] = $i18n->get('form.reports.group_by_project');
221 if (MODE_PROJECTS_AND_TASKS == $user->tracking_mode)
222 $group_by_options['task'] = $i18n->get('form.reports.group_by_task');
223 if ($custom_fields && $custom_fields->fields[0] && $custom_fields->fields[0]['type'] == CustomFields::TYPE_DROPDOWN) {
224 $group_by_options['cf_1'] = $custom_fields->fields[0]['label'];
226 $form->addInput(array('type'=>'combobox','onchange'=>'handleCheckboxes();','name'=>'group_by','data'=>$group_by_options));
227 $form->addInput(array('type'=>'checkbox','name'=>'chtotalsonly'));
229 // Add text field for a new favorite report name.
230 $form->addInput(array('type'=>'text','name'=>'new_fav_report','maxlength'=>'30','style'=>'width: 250px;'));
232 $form->addInput(array('type'=>'submit','name'=>'btn_save','value'=>$i18n->get('button.save')));
234 $form->addInput(array('type'=>'submit','name'=>'btn_generate','value'=>$i18n->get('button.generate')));
236 // Create a bean (which is a mechanism to remember form values in session).
237 $bean = new ActionForm('reportBean', $form, $request);
238 // At this point form values are obtained from session if they are there.
240 if ($request->isGet() && !$bean->isSaved()) {
241 // No previous form data were found in session. Use the following default values.
242 $form->setValueByElement('users', array_keys($user_list));
243 $period = new Period(INTERVAL_THIS_MONTH, new DateAndTime($user->date_format));
244 $form->setValueByElement('start_date', $period->getStartDate());
245 $form->setValueByElement('end_date', $period->getEndDate());
246 $form->setValueByElement('chclient', '1');
247 $form->setValueByElement('chinvoice', '0');
248 $form->setValueByElement('chpaid', '0');
249 $form->setValueByElement('chip', '0');
250 $form->setValueByElement('chproject', '1');
251 $form->setValueByElement('chstart', '1');
252 $form->setValueByElement('chduration', '1');
253 $form->setValueByElement('chcost', '0');
254 $form->setValueByElement('chtask', '1');
255 $form->setValueByElement('chfinish', '1');
256 $form->setValueByElement('chnote', '1');
257 $form->setValueByElement('chcf_1', '0');
258 $form->setValueByElement('chtotalsonly', '0');
261 $form->setValueByElement('fav_report_changed','');
263 // Disable the Delete button when no favorite report is selected.
264 if (!$bean->getAttribute('favorite_report') || ($bean->getAttribute('favorite_report') == -1))
265 $form->getElement('btn_delete')->setEnabled(false);
267 if ($request->isPost()) {
268 if((!$bean->getAttribute('btn_generate') && ($request->getParameter('fav_report_changed')))) {
269 // User changed favorite report. We need to load new values into the form.
270 if ($bean->getAttribute('favorite_report')) {
271 // This loads new favorite report options into the bean (into our form).
272 ttFavReportHelper::loadReport($user->id, $bean);
274 // If user selected no favorite report - mark all user checkboxes (most probable scenario).
275 if ($bean->getAttribute('favorite_report') == -1)
276 $form->setValueByElement('users', array_keys($user_list));
278 // Save form data in session for future use.
280 header('Location: reports.php');
283 } elseif ($bean->getAttribute('btn_save')) {
284 // User clicked the Save button. We need to save form options as new favorite report.
285 if (!ttValidString($bean->getAttribute('new_fav_report'))) $err->add($i18n->get('error.field'), $i18n->get('form.reports.save_as_favorite'));
288 $id = ttFavReportHelper::saveReport($user->id, $bean);
290 $err->add($i18n->get('error.db'));
292 $bean->setAttribute('favorite_report', $id);
294 header('Location: reports.php');
298 } elseif($bean->getAttribute('btn_delete')) {
299 // Delete button pressed. User wants to delete a favorite report.
300 if ($bean->getAttribute('favorite_report')) {
301 ttFavReportHelper::deleteReport($bean->getAttribute('favorite_report'));
302 // Load default report.
303 $bean->setAttribute('favorite_report','');
304 $bean->setAttribute('new_fav_report', $report_list[0]['name']);
305 ttFavReportHelper::loadReport($user->id, $bean);
306 $form->setValueByElement('users', array_keys($user_list));
308 header('Location: reports.php');
312 // Generate button pressed. Check some values.
313 if (!$bean->getAttribute('period')) {
314 $start_date = new DateAndTime($user->date_format, $bean->getAttribute('start_date'));
316 if ($start_date->isError() || !$bean->getAttribute('start_date'))
317 $err->add($i18n->get('error.field'), $i18n->get('label.start_date'));
319 $end_date = new DateAndTime($user->date_format, $bean->getAttribute('end_date'));
320 if ($end_date->isError() || !$bean->getAttribute('end_date'))
321 $err->add($i18n->get('error.field'), $i18n->get('label.end_date'));
323 if ($start_date->compare($end_date) > 0)
324 $err->add($i18n->get('error.interval'), $i18n->get('label.end_date'), $i18n->get('label.start_date'));
330 // Now we can go ahead and create a report.
331 header('Location: report.php');
337 $smarty->assign('project_list', $project_list);
338 $smarty->assign('task_list', $task_list);
339 $smarty->assign('assigned_projects', $assigned_projects);
340 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
341 $smarty->assign('onload', 'onLoad="handleCheckboxes()"');
342 $smarty->assign('title', $i18n->get('title.reports'));
343 $smarty->assign('content_page_name', 'reports.tpl');
344 $smarty->display('index.tpl');