A fix for assign to invoice feature - presentation.
[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 (!ttAccessCheck(right_view_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 }
51
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);
57 }
58
59 $form = new Form('reportForm');
60
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');
64
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);
69 }
70
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);
85 }
86
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);
104   }
105 }
106
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.
110
111     // Determine user action.
112     $mark_paid = $request->getParameter('mark_paid_action_options') == 1 ? true : false;
113
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);
127       }
128     }
129     // Mark as requested.
130     if ($time_log_ids || $expense_item_ids) {
131       ttReportHelper::markPaid($time_log_ids, $expense_item_ids, $mark_paid);
132     }
133
134     // Re-display this form.
135     header('Location: report.php');
136     exit();
137   }
138
139   if ($request->getParameter('btn_assign')) {
140     // User clicked the Submit button to assign all or some items to a recent invoice.
141
142     // Determine invoice id.
143     $invoice_id = $request->getParameter('recent_invoice');
144
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);
158       }
159     }
160     // Assign as requested.
161     if ($time_log_ids || $expense_item_ids) {
162       ttReportHelper::assignToInvoice($invoice_id, $time_log_ids, $expense_item_ids);
163     }
164     // Re-display this form.
165     header('Location: report.php');
166     exit();
167   }
168 } // isPost
169
170 $group_by = $bean->getAttribute('group_by');
171
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);
176
177 if ('no_grouping' != $group_by)
178   $subtotals = ttReportHelper::getSubtotals($bean);
179 $totals = ttReportHelper::getTotals($bean);
180
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', '');
188 }
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']);
193   else {
194     $key = 'label.'.$group_by;
195     $smarty->assign('group_by_header', $i18n->getKey($key));
196   }
197 }
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');
202
203 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
204
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');