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.
11 // | There are only two ways to violate the license:
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).
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).
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
24 // +----------------------------------------------------------------------+
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
29 require_once('initialize.php');
31 import('form.ActionForm');
32 import('ttReportHelper');
33 import('ttTeamHelper');
36 if (!ttAccessCheck(right_view_reports)) {
37 header('Location: access_denied.php');
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;
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;
52 // Use custom fields plugin if it is enabled.
53 if ($user->isPluginEnabled('cf')) {
54 require_once('plugins/CustomFields.class.php');
55 $custom_fields = new CustomFields($user->team_id);
56 $smarty->assign('custom_fields', $custom_fields);
59 $form = new Form('reportForm');
61 // Report settings are stored in session bean before we get here from reports.php.
62 $bean = new ActionForm('reportBean', $form, $request);
63 $client_id = $bean->getAttribute('client');
65 // Do we need to show checkboxes?
66 if ($bean->getAttribute('chpaid') ||
67 ($client_id && $bean->getAttribute('chinvoice') && ('no_grouping' == $bean->getAttribute('group_by')) && !$user->isClient())) {
68 $smarty->assign('use_checkboxes', true);
71 // Controls for "Mark paid" block.
72 if ($bean->getAttribute('chpaid')) {
73 $mark_paid_select_options = array('1'=>$i18n->getKey('dropdown.all'),'2'=>$i18n->getKey('dropdown.select'));
74 $form->addInput(array('type'=>'combobox',
75 'name'=>'mark_paid_select_options',
76 'data'=>$mark_paid_select_options,
77 'value'=>$cl_mark_paid_select_option));
78 $mark_paid_action_options = array('1'=>$i18n->getKey('dropdown.paid'),'2'=>$i18n->getKey('dropdown.not_paid'));
79 $form->addInput(array('type'=>'combobox',
80 'name'=>'mark_paid_action_options',
81 'data'=>$mark_paid_action_options,
82 'value'=>$cl_mark_paid_action_option));
83 $form->addInput(array('type'=>'submit','name'=>'btn_mark_paid','value'=>$i18n->getKey('button.submit')));
84 $smarty->assign('use_mark_paid', true);
87 // Controls for "Assign to invoice" block.
88 if ($client_id && $bean->getAttribute('chinvoice') && ('no_grouping' == $bean->getAttribute('group_by')) && !$user->isClient()) {
89 // Client is selected and we are displaying the invoice column.
90 $recent_invoices = ttTeamHelper::getRecentInvoices($user->team_id, $client_id);
91 if ($recent_invoices) {
92 $assign_invoice_select_options = array('1'=>$i18n->getKey('dropdown.all'),'2'=>$i18n->getKey('dropdown.select'));
93 $form->addInput(array('type'=>'combobox',
94 'name'=>'assign_invoice_select_options',
95 'data'=>$assign_invoice_select_options,
96 'value'=>$cl_assign_invoice_select_option));
97 $form->addInput(array('type'=>'combobox',
98 'name'=>'recent_invoice',
99 'data'=>$recent_invoices,
100 'datakeys'=>array('id','name'),
101 'empty'=>array(''=>$i18n->getKey('dropdown.select_invoice'))));
102 $form->addInput(array('type'=>'submit','name'=>'btn_assign','value'=>$i18n->getKey('button.submit')));
103 $smarty->assign('use_assign_to_invoice', true);
107 if ($request->isPost()) {
108 if ($request->getParameter('btn_mark_paid')) {
109 // User clicked the "Mark paid" button to mark some or all items either paid or not paid.
111 // Determine user action.
112 $mark_paid = $request->getParameter('mark_paid_action_options') == 1 ? true : false;
114 // Obtain 2 arrays or record ids, one for log, another for expense items.
115 if (1 == $request->getParameter('mark_paid_select_options')) {
116 // We are marking all report items. Get the arrays from session.
117 $item_ids = ttReportHelper::getFromSession();
118 $time_log_ids = $item_ids['report_item_ids'];
119 $expense_item_ids = $item_ids['report_item_expense_ids'];
120 } else if (2 == $request->getParameter('mark_paid_select_options')) {
121 // We are marking only selected items. Get the arrays from $_POST.
122 foreach($_POST as $key => $val) {
123 if ('log_id_' == substr($key, 0, 7))
124 $time_log_ids[] = substr($key, 7);
125 if ('item_id_' == substr($key, 0, 8))
126 $expense_item_ids[] = substr($key, 8);
129 // Mark as requested.
130 if ($time_log_ids || $expense_item_ids) {
131 ttReportHelper::markPaid($time_log_ids, $expense_item_ids, $mark_paid);
134 // Re-display this form.
135 header('Location: report.php');
139 if ($request->getParameter('btn_assign')) {
140 // User clicked the Submit button to assign all or some items to a recent invoice.
142 // Determine invoice id.
143 $invoice_id = $request->getParameter('recent_invoice');
145 // Obtain 2 arrays or record ids, one for log, another for expense items.
146 if (1 == $request->getParameter('assign_invoice_select_options')) {
147 // We are assigning all report items. Get the arrays from session.
148 $item_ids = ttReportHelper::getFromSession();
149 $time_log_ids = $item_ids['report_item_ids'];
150 $expense_item_ids = $item_ids['report_item_expense_ids'];
151 } else if (2 == $request->getParameter('assign_invoice_select_options')) {
152 // We are marking only selected items. Get the arrays from $_POST.
153 foreach($_POST as $key => $val) {
154 if ('log_id_' == substr($key, 0, 7))
155 $time_log_ids[] = substr($key, 7);
156 if ('item_id_' == substr($key, 0, 8))
157 $expense_item_ids[] = substr($key, 8);
160 // Assign as requested.
161 if ($time_log_ids || $expense_item_ids) {
162 ttReportHelper::assignToInvoice($invoice_id, $time_log_ids, $expense_item_ids);
164 // Re-display this form.
165 header('Location: report.php');
170 $group_by = $bean->getAttribute('group_by');
172 $report_items = ttReportHelper::getItems($bean);
173 // Store record ids in session in case user wants to act on records such as marking them all paid.
174 if ($request->isGet() && $user->isPluginEnabled('ps'))
175 ttReportHelper::putInSession($report_items);
177 if ('no_grouping' != $group_by)
178 $subtotals = ttReportHelper::getSubtotals($bean);
179 $totals = ttReportHelper::getTotals($bean);
181 // Assign variables that are used to print subtotals.
182 if ($report_items && 'no_grouping' != $group_by) {
183 $smarty->assign('print_subtotals', true);
184 $smarty->assign('first_pass', true);
185 $smarty->assign('group_by', $group_by);
186 $smarty->assign('prev_grouped_by', '');
187 $smarty->assign('cur_grouped_by', '');
189 // Determine group by header.
190 if ('no_grouping' != $group_by) {
191 if ('cf_1' == $group_by)
192 $smarty->assign('group_by_header', $custom_fields->fields[0]['label']);
194 $key = 'label.'.$group_by;
195 $smarty->assign('group_by_header', $i18n->getKey($key));
198 // Assign variables that are used to alternate color of rows for different dates.
199 $smarty->assign('prev_date', '');
200 $smarty->assign('cur_date', '');
201 $smarty->assign('report_row_class', 'rowReportItem');
203 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
205 $smarty->assign('report_items', $report_items);
206 $smarty->assign('subtotals', $subtotals);
207 $smarty->assign('totals', $totals);
208 $smarty->assign('bean', $bean);
209 $smarty->assign('title', $i18n->getKey('title.report').": ".$totals['start_date']." - ".$totals['end_date']);
210 $smarty->assign('content_page_name', 'report.tpl');
211 $smarty->display('index.tpl');