Work in progress on configurable display options.
[timetracker.git] / report.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('ttConfigHelper');
31 import('form.Form');
32 import('form.ActionForm');
33 import('ttReportHelper');
34 import('ttGroupHelper');
35 import('ttTimesheetHelper');
36
37 // Access checks.
38 if (!(ttAccessAllowed('view_own_reports') || ttAccessAllowed('view_reports') || ttAccessAllowed('view_all_reports')  || ttAccessAllowed('view_client_reports'))) {
39   header('Location: access_denied.php');
40   exit();
41 }
42 // End of access checks.
43
44 $config = new ttConfigHelper($user->getConfig());
45
46 if ($user->isPluginEnabled('ap')) {
47   $cl_mark_approved_select_option = $request->getParameter('mark_approved_select_options', ($request->isPost() ? null : @$_SESSION['mark_approved_select_option']));
48   $_SESSION['mark_approved_select_option'] = $cl_mark_approved_select_option;
49   $cl_mark_approved_action_option = $request->getParameter('mark_approved_action_options', ($request->isPost() ? null : @$_SESSION['mark_approved_action_option']));
50   $_SESSION['mark_aproved_action_option'] = $cl_mark_approved_action_option;
51 }
52 if ($user->isPluginEnabled('ps')) {
53   $cl_mark_paid_select_option = $request->getParameter('mark_paid_select_options', ($request->isPost() ? null : @$_SESSION['mark_paid_select_option']));
54   $_SESSION['mark_paid_select_option'] = $cl_mark_paid_select_option;
55   $cl_mark_paid_action_option = $request->getParameter('mark_paid_action_options', ($request->isPost() ? null : @$_SESSION['mark_paid_action_option']));
56   $_SESSION['mark_paid_action_option'] = $cl_mark_paid_action_option;
57 }
58 if ($user->isPluginEnabled('iv')) {
59   $cl_assign_invoice_select_option = $request->getParameter('assign_invoice_select_options', ($request->isPost() ? null : @$_SESSION['assign_invoice_select_option']));
60   $_SESSION['assign_invoice_select_option'] = $cl_assign_invoice_select_option;
61   $cl_recent_invoice_option = $request->getParameter('recent_invoice', ($request->isPost() ? null : @$_SESSION['recent_invoice_option']));
62   $_SESSION['recent_invoice_option'] = $cl_recent_invoice_option;
63 }
64 if ($user->isPluginEnabled('ts')) {
65   $cl_assign_timesheet_select_option = $request->getParameter('assign_timesheet_select_options', ($request->isPost() ? null : @$_SESSION['assign_timesheet_select_option']));
66   $_SESSION['assign_timesheet_select_option'] = $cl_assign_timesheet_select_option;
67   $cl_timesheet_option = $request->getParameter('timesheet', ($request->isPost() ? null : @$_SESSION['timesheet_option']));
68   $_SESSION['timesheet_option'] = $cl_timesheet_option;
69 }
70
71 // Use custom fields plugin if it is enabled.
72 if ($user->isPluginEnabled('cf')) {
73   require_once('plugins/CustomFields.class.php');
74   $custom_fields = new CustomFields();
75   $smarty->assign('custom_fields', $custom_fields);
76 }
77
78 $form = new Form('reportViewForm');
79
80 // Report settings are stored in session bean before we get here from reports.php.
81 $bean = new ActionForm('reportBean', new Form('reportForm'), $request);
82 // If we are in post, load the bean from session, as the constructor does it only in get.
83 if ($request->isPost()) $bean->loadBean();
84
85 $client_id = $bean->getAttribute('client');
86 $options = ttReportHelper::getReportOptions($bean);
87
88 // Do we need to show checkboxes? We show them in the following 4 situations:
89 // - We can approve items.
90 // - We can mark items as paid.
91 // - We can sssign items to invoices.
92 // - We can assign items to a timesheet.
93 // Determine these conditions separately.
94 if ($bean->getAttribute('chapproved') && ($user->can('approve_reports') || $user->can('approve_all_eports')))
95   $useMarkApproved = true;
96 if ($bean->getAttribute('chpaid') && $user->can('manage_invoices'))
97   $useMarkPaid = true;
98 if ($bean->getAttribute('chinvoice') && $client_id && 'no_grouping' == $bean->getAttribute('group_by1') && !$user->isClient() && $user->can('manage_invoices'))
99   $useAssignToInvoice = true;
100 if ($bean->getAttribute('chtimesheet')) {
101   $timesheets = ttTimesheetHelper::getMatchingTimesheets($options);
102   if ($timesheets) $useAssignToTimesheet = true;
103 }
104
105 $use_checkboxes = $useMarkApproved || $useMarkPaid || $useAssignToInvoice || $useAssignToTimesheet;
106 if ($use_checkboxes)
107   $smarty->assign('use_checkboxes', true);
108
109 // Controls for "Mark approved" block.
110 if ($useMarkApproved) {
111   $mark_approved_select_options = array('1'=>$i18n->get('dropdown.all'),'2'=>$i18n->get('dropdown.select'));
112   $form->addInput(array('type'=>'combobox',
113     'name'=>'mark_approved_select_options',
114     'data'=>$mark_approved_select_options,
115     'value'=>$cl_mark_approved_select_option));
116   $mark_approved_action_options = array('1'=>$i18n->get('dropdown.approved'),'2'=>$i18n->get('dropdown.not_approved'));
117   $form->addInput(array('type'=>'combobox',
118     'name'=>'mark_approved_action_options',
119     'data'=>$mark_approved_action_options,
120     'value'=>$cl_mark_approved_action_option));
121   $form->addInput(array('type'=>'submit','name'=>'btn_mark_approved','value'=>$i18n->get('button.submit')));
122   $smarty->assign('use_mark_approved', true);
123 }
124
125 // Controls for "Mark paid" block.
126 if ($useMarkPaid) {
127   $mark_paid_select_options = array('1'=>$i18n->get('dropdown.all'),'2'=>$i18n->get('dropdown.select'));
128   $form->addInput(array('type'=>'combobox',
129     'name'=>'mark_paid_select_options',
130     'data'=>$mark_paid_select_options,
131     'value'=>$cl_mark_paid_select_option));
132   $mark_paid_action_options = array('1'=>$i18n->get('dropdown.paid'),'2'=>$i18n->get('dropdown.not_paid'));
133   $form->addInput(array('type'=>'combobox',
134     'name'=>'mark_paid_action_options',
135     'data'=>$mark_paid_action_options,
136     'value'=>$cl_mark_paid_action_option));
137   $form->addInput(array('type'=>'submit','name'=>'btn_mark_paid','value'=>$i18n->get('button.submit')));
138   $smarty->assign('use_mark_paid', true);
139 }
140
141 // Controls for "Assign to invoice" block.
142 if ($useAssignToInvoice) {
143   // Client is selected and we are displaying the invoice column.
144   $recent_invoices = ttGroupHelper::getRecentInvoices($client_id);
145   if ($recent_invoices) {
146     $assign_invoice_select_options = array('1'=>$i18n->get('dropdown.all'),'2'=>$i18n->get('dropdown.select'));
147     $form->addInput(array('type'=>'combobox',
148       'name'=>'assign_invoice_select_options',
149       'data'=>$assign_invoice_select_options,
150       'value'=>$cl_assign_invoice_select_option));
151     $form->addInput(array('type'=>'combobox',
152       'name'=>'recent_invoice',
153       'data'=>$recent_invoices,
154       'datakeys'=>array('id','name'),
155       'value'=>$cl_recent_invoice_option,
156       'empty'=>array(''=>$i18n->get('dropdown.select_invoice'))));
157     $form->addInput(array('type'=>'submit','name'=>'btn_assign_invoice','value'=>$i18n->get('button.submit')));
158     $smarty->assign('use_assign_to_invoice', true);
159   }
160 }
161
162 // Controls for "Assign to timesheet" block.
163 if ($useAssignToTimesheet) {
164   $assign_timesheet_select_options = array('1'=>$i18n->get('dropdown.all'),'2'=>$i18n->get('dropdown.select'));
165   $form->addInput(array('type'=>'combobox',
166       'name'=>'assign_timesheet_select_options',
167       'data'=>$assign_timesheet_select_options,
168       'value'=>$cl_assign_timesheet_select_option));
169   $form->addInput(array('type'=>'combobox',
170       'name'=>'timesheet',
171       'data'=>$timesheets,
172       'datakeys'=>array('id','name'),
173       'value'=>$cl_timesheet_option,
174       'empty'=>array(''=>$i18n->get('dropdown.select_timesheet'))));
175   $form->addInput(array('type'=>'submit','name'=>'btn_assign_timesheet','value'=>$i18n->get('button.submit')));
176   $smarty->assign('use_assign_to_timesheet', true);
177 }
178
179 if ($request->isPost()) {
180
181   // Validate parameters and at the same time build arrays of record ids.
182   if (($request->getParameter('btn_mark_approved') && 2 == $request->getParameter('mark_approved_select_options'))
183        || ($request->getParameter('btn_mark_paid') && 2 == $request->getParameter('mark_paid_select_options'))
184        || ($request->getParameter('btn_assign_invoice') && 2 == $request->getParameter('assign_invoice_select_options'))
185        || ($request->getParameter('btn_assign_timesheet') && 2 == $request->getParameter('assign_timesheet_select_options'))) {
186     // We act on selected records. Are there any?
187     foreach($_POST as $key => $val) {
188       if ('log_id_' == substr($key, 0, 7))
189         $time_log_ids[] = substr($key, 7);
190       if ('item_id_' == substr($key, 0, 8))
191         $expense_item_ids[] = substr($key, 8);
192     }
193     if (!$time_log_ids && !$expense_item_ids) $err->Add($i18n->get('error.record')); // There are no selected records.
194     // Validation of parameteres ended here.
195   } else {
196     // We are assigning all report items. Get the arrays from session.
197     // Note: getting from session assures we act only on previously displayed records.
198     // Rebuilding from $bean may get us a different set.
199     $item_ids = ttReportHelper::getFromSession();
200     $time_log_ids = $item_ids['report_item_ids'];
201     $expense_item_ids = $item_ids['report_item_expense_ids'];
202     // The above code is here beacues the arrays are used in both "Mark paid" and "Assign to invoice" handlers below.
203   }
204
205   if ($err->no()) {
206     if ($request->getParameter('btn_mark_approved')) {
207       // User clicked the "Mark approved" button to mark some or all items either approved or not approved.
208
209       // Determine user action.
210       $mark_approved = $request->getParameter('mark_approved_action_options') == 1 ? true : false;
211
212       // Mark as requested.
213       if ($time_log_ids || $expense_item_ids) {
214         ttReportHelper::markApproved($time_log_ids, $expense_item_ids, $mark_approved);
215       }
216
217       // Re-display this form.
218       header('Location: report.php');
219       exit();
220     }
221
222     if ($request->getParameter('btn_mark_paid')) {
223       // User clicked the "Mark paid" button to mark some or all items either paid or not paid.
224
225       // Determine user action.
226       $mark_paid = $request->getParameter('mark_paid_action_options') == 1 ? true : false;
227
228       // Mark as requested.
229       if ($time_log_ids || $expense_item_ids) {
230         ttReportHelper::markPaid($time_log_ids, $expense_item_ids, $mark_paid);
231       }
232
233       // Re-display this form.
234       header('Location: report.php');
235       exit();
236     }
237
238     if ($request->getParameter('btn_assign_invoice')) {
239       // User clicked the Submit button to assign all or some items to a recent invoice.
240
241       // Determine invoice id.
242       $invoice_id = $request->getParameter('recent_invoice');
243
244       // Assign as requested.
245       if ($time_log_ids || $expense_item_ids) {
246         ttReportHelper::assignToInvoice($invoice_id, $time_log_ids, $expense_item_ids);
247       }
248       // Re-display this form.
249       header('Location: report.php');
250       exit();
251     }
252
253     if ($request->getParameter('btn_assign_timesheet')) {
254       // User clicked the Submit button to assign all or some items to a timesheet.
255
256       // Determine invoice id.
257       $timesheet_id = $request->getParameter('timesheet');
258
259       // Assign as requested.
260       if ($time_log_ids) {
261         ttReportHelper::assignToTimesheet($timesheet_id, $time_log_ids);
262       }
263       // Re-display this form.
264       header('Location: report.php');
265       exit();
266     }
267   }
268 } // isPost
269
270 $report_items = ttReportHelper::getItems($options);
271 // Store record ids in session in case user wants to act on records such as marking them all paid.
272 if ($request->isGet() && $use_checkboxes)
273   ttReportHelper::putInSession($report_items);
274
275 if (ttReportHelper::grouping($options)) {
276   $subtotals = ttReportHelper::getSubtotals($options);
277   $smarty->assign('group_by_header', ttReportHelper::makeGroupByHeader($options));
278 }
279 $totals = ttReportHelper::getTotals($options);
280
281 // Assign variables that are used to print subtotals.
282 if ($report_items) {
283   $smarty->assign('print_subtotals', true);
284   $smarty->assign('first_pass', true);
285   $smarty->assign('prev_grouped_by', '');
286   $smarty->assign('cur_grouped_by', '');
287 }
288
289 // Determine column span for note field.
290 $colspan = 1;
291 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) $colspan++;
292 if ($bean->getAttribute('chclient')) $colspan++;
293 if ($bean->getAttribute('chproject')) $colspan++;
294 if ($bean->getAttribute('chtask')) $colspan++;
295 if ($bean->getAttribute('chcf_1')) $colspan++;
296 if ($bean->getAttribute('chstart')) $colspan++;
297 if ($bean->getAttribute('chfinish')) $colspan++;
298 if ($bean->getAttribute('chduration')) $colspan++;
299 if ($bean->getAttribute('chunits')) $colspan++;
300 if ($bean->getAttribute('chcost')) $colspan++;
301 if ($bean->getAttribute('chapproved')) $colspan++;
302 if ($bean->getAttribute('chpaid')) $colspan++;
303 if ($bean->getAttribute('chip')) $colspan++;
304 if ($bean->getAttribute('chinvoice')) $colspan++;
305 if ($bean->getAttribute('chtimesheet')) $colspan++;
306 if ($bean->getAttribute('chfiles')) $colspan++;
307
308 // Assign variables that are used to alternate color of rows for different dates.
309 $smarty->assign('prev_date', '');
310 $smarty->assign('cur_date', '');
311 $smarty->assign('report_row_class', 'rowReportItem');
312 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
313 $smarty->assign('report_items', $report_items);
314 $smarty->assign('subtotals', $subtotals);
315 $smarty->assign('totals', $totals);
316 $smarty->assign('note_on_separate_row', $user->getConfigOption('report_note_on_separate_row'));
317 $smarty->assign('colspan', $colspan);
318 $smarty->assign('bean', $bean);
319 $smarty->assign('title', $i18n->get('title.report').": ".$totals['start_date']." - ".$totals['end_date']);
320 $smarty->assign('content_page_name', 'report.tpl');
321 $smarty->display('index.tpl');