More refactoring in translation files.
[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_paid') && 2 == $request->getParameter('mark_paid_select_options'))
146        || ($request->getParameter('btn_assign') && 2 == $request->getParameter('assign_invoice_select_options'))) {
147     // We act on selected records. Are there any?
148     foreach($_POST as $key => $val) {
149       if ('log_id_' == substr($key, 0, 7))
150         $time_log_ids[] = substr($key, 7);
151       if ('item_id_' == substr($key, 0, 8))
152         $expense_item_ids[] = substr($key, 8);
153     }
154     if (!$time_log_ids && !$expense_item_ids) $err->Add($i18n->get('error.record')); // There are no selected records.
155     // Validation of parameteres ended here.
156   } else {
157     // We are assigning all report items. Get the arrays from session.
158     // Note: getting from session assures we act only on previously displayed records.
159     // Rebuilding from $bean may get us a different set.
160     $item_ids = ttReportHelper::getFromSession();
161     $time_log_ids = $item_ids['report_item_ids'];
162     $expense_item_ids = $item_ids['report_item_expense_ids'];
163     // The above code is here beacues the arrays are used in both "Mark paid" and "Assign to invoice" handlers below.
164   }
165
166   if ($err->no()) {
167     if ($request->getParameter('btn_mark_paid')) {
168       // User clicked the "Mark paid" button to mark some or all items either paid or not paid.
169
170       // Determine user action.
171       $mark_paid = $request->getParameter('mark_paid_action_options') == 1 ? true : false;
172
173       // Mark as requested.
174       if ($time_log_ids || $expense_item_ids) {
175         ttReportHelper::markPaid($time_log_ids, $expense_item_ids, $mark_paid);
176       }
177
178       // Re-display this form.
179       header('Location: report.php');
180       exit();
181     }
182
183     if ($request->getParameter('btn_assign')) {
184       // User clicked the Submit button to assign all or some items to a recent invoice.
185
186       // Determine invoice id.
187       $invoice_id = $request->getParameter('recent_invoice');
188
189       // Assign as requested.
190       if ($time_log_ids || $expense_item_ids) {
191         ttReportHelper::assignToInvoice($invoice_id, $time_log_ids, $expense_item_ids);
192       }
193       // Re-display this form.
194       header('Location: report.php');
195       exit();
196     }
197   }
198 } // isPost
199
200 $options = ttReportHelper::getReportOptions($bean);
201
202 $report_items = ttReportHelper::getItems($options);
203 // Store record ids in session in case user wants to act on records such as marking them all paid.
204 if ($request->isGet() && $user->isPluginEnabled('ps'))
205   ttReportHelper::putInSession($report_items);
206
207 if (ttReportHelper::grouping($options)) {
208   $subtotals = ttReportHelper::getSubtotals($options);
209   $smarty->assign('group_by_header', ttReportHelper::makeGroupByHeader($options));
210 }
211 $totals = ttReportHelper::getTotals($options);
212
213 // TODO: Determine if we can create a timesheet out of this report.
214 // There must be only one user, and nothing assigned to existing timesheets.
215 $canCreateTimesheet = false;
216 if ($user->isPluginEnabled('ts') && count($report_items) > 0 &&
217   ($user->can('manage_own_timesheets') || $user->can('manage_timesheets'))) {
218
219   $canCreateTimesheet = true; // Start with true and reset if we can't.
220   $first_user_id = null;
221   foreach ($report_items as $report_item) {
222     // Check user id.
223     if (!$first_user_id)
224       $first_user_id = $report_item['user_id'];
225     else {
226       if ($report_item['user_id'] != $first_user_id) {
227         // We have items for multiple users.
228         $canCreateTimesheet = false;
229         break;
230       }
231     }
232     // Check timesheet id.
233     if ($report_item['timesheet_id']) {
234       // We have an item already assigned to a timesheet.
235       $canCreateTimesheet = false;
236       break;
237     }
238   }
239   // Save user_id in session.
240   $bean->saveDetachedAttribute('timesheet_user_id', $first_user_id);
241
242   // TODO: Improve this for "view_all_reports" situation.
243   // We may need to add "manage_all_timesheets" right.
244 }
245
246 // Assign variables that are used to print subtotals.
247 if ($report_items) {
248   $smarty->assign('print_subtotals', true);
249   $smarty->assign('first_pass', true);
250   $smarty->assign('prev_grouped_by', '');
251   $smarty->assign('cur_grouped_by', '');
252 }
253
254 // Assign variables that are used to alternate color of rows for different dates.
255 $smarty->assign('prev_date', '');
256 $smarty->assign('cur_date', '');
257 $smarty->assign('report_row_class', 'rowReportItem');
258 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
259 $smarty->assign('report_items', $report_items);
260 $smarty->assign('subtotals', $subtotals);
261 $smarty->assign('totals', $totals);
262 $smarty->assign('can_create_timesheet', $canCreateTimesheet);
263 $smarty->assign('bean', $bean);
264 $smarty->assign('title', $i18n->get('title.report').": ".$totals['start_date']." - ".$totals['end_date']);
265 $smarty->assign('content_page_name', 'report.tpl');
266 $smarty->display('index.tpl');