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