A bit of cleanup for timesheet redesign.
[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 $use_checkboxes = $showForApproved || $showForInvoicesOrPaid;
86 if ($use_checkboxes)
87   $smarty->assign('use_checkboxes', true);
88
89 // Controls for "Mark approved" block.
90 if ($showForApproved) {
91   $mark_approved_select_options = array('1'=>$i18n->get('dropdown.all'),'2'=>$i18n->get('dropdown.select'));
92   $form->addInput(array('type'=>'combobox',
93     'name'=>'mark_approved_select_options',
94     'data'=>$mark_approved_select_options,
95     'value'=>$cl_mark_approved_select_option));
96   $mark_approved_action_options = array('1'=>$i18n->get('dropdown.approved'),'2'=>$i18n->get('dropdown.not_approved'));
97   $form->addInput(array('type'=>'combobox',
98     'name'=>'mark_approved_action_options',
99     'data'=>$mark_approved_action_options,
100     'value'=>$cl_mark_approved_action_option));
101   $form->addInput(array('type'=>'submit','name'=>'btn_mark_approved','value'=>$i18n->get('button.submit')));
102   $smarty->assign('use_mark_approved', true);
103 }
104
105 // Controls for "Mark paid" block.
106 if ($user->can('manage_invoices') && $bean->getAttribute('chpaid')) {
107   $mark_paid_select_options = array('1'=>$i18n->get('dropdown.all'),'2'=>$i18n->get('dropdown.select'));
108   $form->addInput(array('type'=>'combobox',
109     'name'=>'mark_paid_select_options',
110     'data'=>$mark_paid_select_options,
111     'value'=>$cl_mark_paid_select_option));
112   $mark_paid_action_options = array('1'=>$i18n->get('dropdown.paid'),'2'=>$i18n->get('dropdown.not_paid'));
113   $form->addInput(array('type'=>'combobox',
114     'name'=>'mark_paid_action_options',
115     'data'=>$mark_paid_action_options,
116     'value'=>$cl_mark_paid_action_option));
117   $form->addInput(array('type'=>'submit','name'=>'btn_mark_paid','value'=>$i18n->get('button.submit')));
118   $smarty->assign('use_mark_paid', true);
119 }
120
121 // Controls for "Assign to invoice" block.
122 if ($user->can('manage_invoices') &&
123   ($client_id && $bean->getAttribute('chinvoice') && ('no_grouping' == $bean->getAttribute('group_by1')) && !$user->isClient())) {
124   // Client is selected and we are displaying the invoice column.
125   $recent_invoices = ttGroupHelper::getRecentInvoices($client_id);
126   if ($recent_invoices) {
127     $assign_invoice_select_options = array('1'=>$i18n->get('dropdown.all'),'2'=>$i18n->get('dropdown.select'));
128     $form->addInput(array('type'=>'combobox',
129       'name'=>'assign_invoice_select_options',
130       'data'=>$assign_invoice_select_options,
131       'value'=>$cl_assign_invoice_select_option));
132     $form->addInput(array('type'=>'combobox',
133       'name'=>'recent_invoice',
134       'data'=>$recent_invoices,
135       'datakeys'=>array('id','name'),
136       'value'=>$cl_recent_invoice_option,
137       'empty'=>array(''=>$i18n->get('dropdown.select_invoice'))));
138     $form->addInput(array('type'=>'submit','name'=>'btn_assign','value'=>$i18n->get('button.submit')));
139     $smarty->assign('use_assign_to_invoice', true);
140   }
141 }
142
143 if ($request->isPost()) {
144
145   // Validate parameters and at the same time build arrays of record ids.
146   if (($request->getParameter('btn_mark_approved') && 2 == $request->getParameter('mark_approved_select_options'))
147        || ($request->getParameter('btn_mark_paid') && 2 == $request->getParameter('mark_paid_select_options'))
148        || ($request->getParameter('btn_assign') && 2 == $request->getParameter('assign_invoice_select_options'))) {
149     // We act on selected records. Are there any?
150     foreach($_POST as $key => $val) {
151       if ('log_id_' == substr($key, 0, 7))
152         $time_log_ids[] = substr($key, 7);
153       if ('item_id_' == substr($key, 0, 8))
154         $expense_item_ids[] = substr($key, 8);
155     }
156     if (!$time_log_ids && !$expense_item_ids) $err->Add($i18n->get('error.record')); // There are no selected records.
157     // Validation of parameteres ended here.
158   } else {
159     // We are assigning all report items. Get the arrays from session.
160     // Note: getting from session assures we act only on previously displayed records.
161     // Rebuilding from $bean may get us a different set.
162     $item_ids = ttReportHelper::getFromSession();
163     $time_log_ids = $item_ids['report_item_ids'];
164     $expense_item_ids = $item_ids['report_item_expense_ids'];
165     // The above code is here beacues the arrays are used in both "Mark paid" and "Assign to invoice" handlers below.
166   }
167
168   if ($err->no()) {
169     if ($request->getParameter('btn_mark_approved')) {
170       // User clicked the "Mark approved" button to mark some or all items either approved or not approved.
171
172       // Determine user action.
173       $mark_approved = $request->getParameter('mark_approved_action_options') == 1 ? true : false;
174
175       // Mark as requested.
176       if ($time_log_ids || $expense_item_ids) {
177         ttReportHelper::markApproved($time_log_ids, $expense_item_ids, $mark_approved);
178       }
179
180       // Re-display this form.
181       header('Location: report.php');
182       exit();
183     }
184
185     if ($request->getParameter('btn_mark_paid')) {
186       // User clicked the "Mark paid" button to mark some or all items either paid or not paid.
187
188       // Determine user action.
189       $mark_paid = $request->getParameter('mark_paid_action_options') == 1 ? true : false;
190
191       // Mark as requested.
192       if ($time_log_ids || $expense_item_ids) {
193         ttReportHelper::markPaid($time_log_ids, $expense_item_ids, $mark_paid);
194       }
195
196       // Re-display this form.
197       header('Location: report.php');
198       exit();
199     }
200
201     if ($request->getParameter('btn_assign')) {
202       // User clicked the Submit button to assign all or some items to a recent invoice.
203
204       // Determine invoice id.
205       $invoice_id = $request->getParameter('recent_invoice');
206
207       // Assign as requested.
208       if ($time_log_ids || $expense_item_ids) {
209         ttReportHelper::assignToInvoice($invoice_id, $time_log_ids, $expense_item_ids);
210       }
211       // Re-display this form.
212       header('Location: report.php');
213       exit();
214     }
215   }
216 } // isPost
217
218 $options = ttReportHelper::getReportOptions($bean);
219
220 $report_items = ttReportHelper::getItems($options);
221 // Store record ids in session in case user wants to act on records such as marking them all paid.
222 if ($request->isGet() && $use_checkboxes)
223   ttReportHelper::putInSession($report_items);
224
225 if (ttReportHelper::grouping($options)) {
226   $subtotals = ttReportHelper::getSubtotals($options);
227   $smarty->assign('group_by_header', ttReportHelper::makeGroupByHeader($options));
228 }
229 $totals = ttReportHelper::getTotals($options);
230
231 // Assign variables that are used to print subtotals.
232 if ($report_items) {
233   $smarty->assign('print_subtotals', true);
234   $smarty->assign('first_pass', true);
235   $smarty->assign('prev_grouped_by', '');
236   $smarty->assign('cur_grouped_by', '');
237 }
238
239 // Assign variables that are used to alternate color of rows for different dates.
240 $smarty->assign('prev_date', '');
241 $smarty->assign('cur_date', '');
242 $smarty->assign('report_row_class', 'rowReportItem');
243 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
244 $smarty->assign('report_items', $report_items);
245 $smarty->assign('subtotals', $subtotals);
246 $smarty->assign('totals', $totals);
247 $smarty->assign('bean', $bean);
248 $smarty->assign('title', $i18n->get('title.report').": ".$totals['start_date']." - ".$totals['end_date']);
249 $smarty->assign('content_page_name', 'report.tpl');
250 $smarty->display('index.tpl');