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