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