Added approval handling to 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
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 if we allow setting approved or paid status,
77 // and also when we can assign / deassign records to invoces.
78 if ($bean->getAttribute('chapproved') && ($user->can('approve_reports') || $user->can('approve_all_eports')))
79   $showForApproved = true;
80 if ($bean->getAttribute('chpaid') ||
81    ($client_id && $bean->getAttribute('chinvoice') && ('no_grouping' == $bean->getAttribute('group_by1')) && !$user->isClient())) {
82   if ($user->can('manage_invoices'))
83     $showForInvoicesOrPaid = true;
84 }
85 if ($showForApproved || $showForInvoicesOrPaid)
86   $smarty->assign('use_checkboxes', true);
87
88 // Controls for "Mark approved" block.
89 if ($showForApproved) {
90   $mark_approved_select_options = array('1'=>$i18n->get('dropdown.all'),'2'=>$i18n->get('dropdown.select'));
91   $form->addInput(array('type'=>'combobox',
92     'name'=>'mark_approved_select_options',
93     'data'=>$mark_approved_select_options,
94     'value'=>$cl_mark_approved_select_option));
95   $mark_approved_action_options = array('1'=>$i18n->get('dropdown.approved'),'2'=>$i18n->get('dropdown.not_approved'));
96   $form->addInput(array('type'=>'combobox',
97     'name'=>'mark_approved_action_options',
98     'data'=>$mark_approved_action_options,
99     'value'=>$cl_mark_approved_action_option));
100   $form->addInput(array('type'=>'submit','name'=>'btn_mark_approved','value'=>$i18n->get('button.submit')));
101   $smarty->assign('use_mark_approved', true);
102 }
103
104 // Controls for "Mark paid" block.
105 if ($user->can('manage_invoices') && $bean->getAttribute('chpaid')) {
106   $mark_paid_select_options = array('1'=>$i18n->get('dropdown.all'),'2'=>$i18n->get('dropdown.select'));
107   $form->addInput(array('type'=>'combobox',
108     'name'=>'mark_paid_select_options',
109     'data'=>$mark_paid_select_options,
110     'value'=>$cl_mark_paid_select_option));
111   $mark_paid_action_options = array('1'=>$i18n->get('dropdown.paid'),'2'=>$i18n->get('dropdown.not_paid'));
112   $form->addInput(array('type'=>'combobox',
113     'name'=>'mark_paid_action_options',
114     'data'=>$mark_paid_action_options,
115     'value'=>$cl_mark_paid_action_option));
116   $form->addInput(array('type'=>'submit','name'=>'btn_mark_paid','value'=>$i18n->get('button.submit')));
117   $smarty->assign('use_mark_paid', true);
118 }
119
120 // Controls for "Assign to invoice" block.
121 if ($user->can('manage_invoices') &&
122   ($client_id && $bean->getAttribute('chinvoice') && ('no_grouping' == $bean->getAttribute('group_by1')) && !$user->isClient())) {
123   // Client is selected and we are displaying the invoice column.
124   $recent_invoices = ttGroupHelper::getRecentInvoices($client_id);
125   if ($recent_invoices) {
126     $assign_invoice_select_options = array('1'=>$i18n->get('dropdown.all'),'2'=>$i18n->get('dropdown.select'));
127     $form->addInput(array('type'=>'combobox',
128       'name'=>'assign_invoice_select_options',
129       'data'=>$assign_invoice_select_options,
130       'value'=>$cl_assign_invoice_select_option));
131     $form->addInput(array('type'=>'combobox',
132       'name'=>'recent_invoice',
133       'data'=>$recent_invoices,
134       'datakeys'=>array('id','name'),
135       'value'=>$cl_recent_invoice_option,
136       'empty'=>array(''=>$i18n->get('dropdown.select_invoice'))));
137     $form->addInput(array('type'=>'submit','name'=>'btn_assign','value'=>$i18n->get('button.submit')));
138     $smarty->assign('use_assign_to_invoice', true);
139   }
140 }
141
142 if ($request->isPost()) {
143
144   // Validate parameters and at the same time build arrays of record ids.
145   if (($request->getParameter('btn_mark_approved') && 2 == $request->getParameter('mark_approved_select_options'))
146        || ($request->getParameter('btn_mark_paid') && 2 == $request->getParameter('mark_paid_select_options'))
147        || ($request->getParameter('btn_assign') && 2 == $request->getParameter('assign_invoice_select_options'))) {
148     // We act on selected records. Are there any?
149     foreach($_POST as $key => $val) {
150       if ('log_id_' == substr($key, 0, 7))
151         $time_log_ids[] = substr($key, 7);
152       if ('item_id_' == substr($key, 0, 8))
153         $expense_item_ids[] = substr($key, 8);
154     }
155     if (!$time_log_ids && !$expense_item_ids) $err->Add($i18n->get('error.record')); // There are no selected records.
156     // Validation of parameteres ended here.
157   } else {
158     // We are assigning all report items. Get the arrays from session.
159     // Note: getting from session assures we act only on previously displayed records.
160     // Rebuilding from $bean may get us a different set.
161     $item_ids = ttReportHelper::getFromSession();
162     $time_log_ids = $item_ids['report_item_ids'];
163     $expense_item_ids = $item_ids['report_item_expense_ids'];
164     // The above code is here beacues the arrays are used in both "Mark paid" and "Assign to invoice" handlers below.
165   }
166
167   if ($err->no()) {
168     if ($request->getParameter('btn_mark_approved')) {
169       // User clicked the "Mark approved" button to mark some or all items either approved or not approved.
170
171       // Determine user action.
172       $mark_approved = $request->getParameter('mark_approved_action_options') == 1 ? true : false;
173
174       // Mark as requested.
175       if ($time_log_ids || $expense_item_ids) {
176         ttReportHelper::markApproved($time_log_ids, $expense_item_ids, $mark_approved);
177       }
178
179       // Re-display this form.
180       header('Location: report.php');
181       exit();
182     }
183
184     if ($request->getParameter('btn_mark_paid')) {
185       // User clicked the "Mark paid" button to mark some or all items either paid or not paid.
186
187       // Determine user action.
188       $mark_paid = $request->getParameter('mark_paid_action_options') == 1 ? true : false;
189
190       // Mark as requested.
191       if ($time_log_ids || $expense_item_ids) {
192         ttReportHelper::markPaid($time_log_ids, $expense_item_ids, $mark_paid);
193       }
194
195       // Re-display this form.
196       header('Location: report.php');
197       exit();
198     }
199
200     if ($request->getParameter('btn_assign')) {
201       // User clicked the Submit button to assign all or some items to a recent invoice.
202
203       // Determine invoice id.
204       $invoice_id = $request->getParameter('recent_invoice');
205
206       // Assign as requested.
207       if ($time_log_ids || $expense_item_ids) {
208         ttReportHelper::assignToInvoice($invoice_id, $time_log_ids, $expense_item_ids);
209       }
210       // Re-display this form.
211       header('Location: report.php');
212       exit();
213     }
214   }
215 } // isPost
216
217 $options = ttReportHelper::getReportOptions($bean);
218
219 $report_items = ttReportHelper::getItems($options);
220 // Store record ids in session in case user wants to act on records such as marking them all paid.
221 if ($request->isGet() && $user->isPluginEnabled('ps'))
222   ttReportHelper::putInSession($report_items);
223
224 if (ttReportHelper::grouping($options)) {
225   $subtotals = ttReportHelper::getSubtotals($options);
226   $smarty->assign('group_by_header', ttReportHelper::makeGroupByHeader($options));
227 }
228 $totals = ttReportHelper::getTotals($options);
229
230 // TODO: Determine if we can create a timesheet out of this report.
231 // There must be only one user, and nothing assigned to existing timesheets.
232 $canCreateTimesheet = false;
233 if ($user->isPluginEnabled('ts') && count($report_items) > 0 &&
234   ($user->can('manage_own_timesheets') || $user->can('manage_timesheets'))) {
235
236   $canCreateTimesheet = true; // Start with true and reset if we can't.
237   $first_user_id = null;
238   foreach ($report_items as $report_item) {
239     // Check user id.
240     if (!$first_user_id)
241       $first_user_id = $report_item['user_id'];
242     else {
243       if ($report_item['user_id'] != $first_user_id) {
244         // We have items for multiple users.
245         $canCreateTimesheet = false;
246         break;
247       }
248     }
249     // Check timesheet id.
250     if ($report_item['timesheet_id']) {
251       // We have an item already assigned to a timesheet.
252       $canCreateTimesheet = false;
253       break;
254     }
255   }
256   // Save user_id in session.
257   $bean->saveDetachedAttribute('timesheet_user_id', $first_user_id);
258
259   // TODO: Improve this for "view_all_reports" situation.
260   // We may need to add "manage_all_timesheets" right.
261 }
262
263 // Assign variables that are used to print subtotals.
264 if ($report_items) {
265   $smarty->assign('print_subtotals', true);
266   $smarty->assign('first_pass', true);
267   $smarty->assign('prev_grouped_by', '');
268   $smarty->assign('cur_grouped_by', '');
269 }
270
271 // Assign variables that are used to alternate color of rows for different dates.
272 $smarty->assign('prev_date', '');
273 $smarty->assign('cur_date', '');
274 $smarty->assign('report_row_class', 'rowReportItem');
275 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
276 $smarty->assign('report_items', $report_items);
277 $smarty->assign('subtotals', $subtotals);
278 $smarty->assign('totals', $totals);
279 $smarty->assign('can_create_timesheet', $canCreateTimesheet);
280 $smarty->assign('bean', $bean);
281 $smarty->assign('title', $i18n->get('title.report').": ".$totals['start_date']." - ".$totals['end_date']);
282 $smarty->assign('content_page_name', 'report.tpl');
283 $smarty->display('index.tpl');