Some refactoring in localization 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('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   $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->team_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 $client_id = $bean->getAttribute('client');
66
67 // Do we need to show checkboxes?
68 if ($bean->getAttribute('chpaid') ||
69    ($client_id && $bean->getAttribute('chinvoice') && ('no_grouping' == $bean->getAttribute('group_by')) && !$user->isClient())) {
70   $smarty->assign('use_checkboxes', true);
71 }
72
73 // Controls for "Mark paid" block.
74 if ($bean->getAttribute('chpaid')) {
75   $mark_paid_select_options = array('1'=>$i18n->getKey('dropdown.all'),'2'=>$i18n->getKey('dropdown.select'));
76   $form->addInput(array('type'=>'combobox',
77     'name'=>'mark_paid_select_options',
78     'data'=>$mark_paid_select_options,
79     'value'=>$cl_mark_paid_select_option));
80   $mark_paid_action_options = array('1'=>$i18n->getKey('dropdown.paid'),'2'=>$i18n->getKey('dropdown.not_paid'));
81   $form->addInput(array('type'=>'combobox',
82     'name'=>'mark_paid_action_options',
83     'data'=>$mark_paid_action_options,
84     'value'=>$cl_mark_paid_action_option));
85   $form->addInput(array('type'=>'submit','name'=>'btn_mark_paid','value'=>$i18n->getKey('button.submit')));
86   $smarty->assign('use_mark_paid', true);
87 }
88
89 // Controls for "Assign to invoice" block.
90 if ($client_id && $bean->getAttribute('chinvoice') && ('no_grouping' == $bean->getAttribute('group_by')) && !$user->isClient()) {
91   // Client is selected and we are displaying the invoice column.
92   $recent_invoices = ttTeamHelper::getRecentInvoices($user->team_id, $client_id);
93   if ($recent_invoices) {
94     $assign_invoice_select_options = array('1'=>$i18n->getKey('dropdown.all'),'2'=>$i18n->getKey('dropdown.select'));
95     $form->addInput(array('type'=>'combobox',
96       'name'=>'assign_invoice_select_options',
97       'data'=>$assign_invoice_select_options,
98       'value'=>$cl_assign_invoice_select_option));
99     $form->addInput(array('type'=>'combobox',
100       'name'=>'recent_invoice',
101       'data'=>$recent_invoices,
102       'datakeys'=>array('id','name'),
103       'value'=>$cl_recent_invoice_option,
104       'empty'=>array(''=>$i18n->getKey('dropdown.select_invoice'))));
105     $form->addInput(array('type'=>'submit','name'=>'btn_assign','value'=>$i18n->getKey('button.submit')));
106     $smarty->assign('use_assign_to_invoice', true);
107   }
108 }
109
110 if ($request->isPost()) {
111   if ($request->getParameter('btn_mark_paid')) {
112     // User clicked the "Mark paid" button to mark some or all items either paid or not paid.
113
114     // Determine user action.
115     $mark_paid = $request->getParameter('mark_paid_action_options') == 1 ? true : false;
116
117     // Obtain 2 arrays or record ids, one for log, another for expense items.
118     if (1 == $request->getParameter('mark_paid_select_options')) {
119       // We are marking all report items. Get the arrays from session.
120       $item_ids = ttReportHelper::getFromSession();
121       $time_log_ids = $item_ids['report_item_ids'];
122       $expense_item_ids = $item_ids['report_item_expense_ids'];
123     } else if (2 == $request->getParameter('mark_paid_select_options')) {
124       // We are marking only selected items. Get the arrays from $_POST.
125       foreach($_POST as $key => $val) {
126         if ('log_id_' == substr($key, 0, 7))
127           $time_log_ids[] = substr($key, 7);
128         if ('item_id_' == substr($key, 0, 8))
129           $expense_item_ids[] = substr($key, 8);
130       }
131     }
132     // Mark as requested.
133     if ($time_log_ids || $expense_item_ids) {
134       ttReportHelper::markPaid($time_log_ids, $expense_item_ids, $mark_paid);
135     }
136
137     // Re-display this form.
138     header('Location: report.php');
139     exit();
140   }
141
142   if ($request->getParameter('btn_assign')) {
143     // User clicked the Submit button to assign all or some items to a recent invoice.
144
145     // Determine invoice id.
146     $invoice_id = $request->getParameter('recent_invoice');
147
148     // Obtain 2 arrays or record ids, one for log, another for expense items.
149     if (1 == $request->getParameter('assign_invoice_select_options')) {
150       // We are assigning all report items. Get the arrays from session.
151       $item_ids = ttReportHelper::getFromSession();
152       $time_log_ids = $item_ids['report_item_ids'];
153       $expense_item_ids = $item_ids['report_item_expense_ids'];
154     } else if (2 == $request->getParameter('assign_invoice_select_options')) {
155       // We are marking only selected items. Get the arrays from $_POST.
156       foreach($_POST as $key => $val) {
157         if ('log_id_' == substr($key, 0, 7))
158           $time_log_ids[] = substr($key, 7);
159         if ('item_id_' == substr($key, 0, 8))
160           $expense_item_ids[] = substr($key, 8);
161       }
162     }
163     // Assign as requested.
164     if ($time_log_ids || $expense_item_ids) {
165       ttReportHelper::assignToInvoice($invoice_id, $time_log_ids, $expense_item_ids);
166     }
167     // Re-display this form.
168     header('Location: report.php');
169     exit();
170   }
171 } // isPost
172
173 $group_by = $bean->getAttribute('group_by');
174
175 $report_items = ttReportHelper::getItems($bean);
176 // Store record ids in session in case user wants to act on records such as marking them all paid.
177 if ($request->isGet() && $user->isPluginEnabled('ps'))
178   ttReportHelper::putInSession($report_items);
179
180 if ('no_grouping' != $group_by)
181   $subtotals = ttReportHelper::getSubtotals($bean);
182 $totals = ttReportHelper::getTotals($bean);
183
184 // Assign variables that are used to print subtotals.
185 if ($report_items && 'no_grouping' != $group_by) {
186   $smarty->assign('print_subtotals', true);
187   $smarty->assign('first_pass', true);
188   $smarty->assign('group_by', $group_by);
189   $smarty->assign('prev_grouped_by', '');
190   $smarty->assign('cur_grouped_by', '');
191 }
192 // Determine group by header.
193 if ('no_grouping' != $group_by) {
194   if ('cf_1' == $group_by)
195     $smarty->assign('group_by_header', $custom_fields->fields[0]['label']);
196   else {
197     $key = 'label.'.$group_by;
198     $smarty->assign('group_by_header', $i18n->getKey($key));
199   }
200 }
201 // Assign variables that are used to alternate color of rows for different dates.
202 $smarty->assign('prev_date', '');
203 $smarty->assign('cur_date', '');
204 $smarty->assign('report_row_class', 'rowReportItem');
205
206 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
207
208 $smarty->assign('report_items', $report_items);
209 $smarty->assign('subtotals', $subtotals);
210 $smarty->assign('totals', $totals);
211 $smarty->assign('bean', $bean);
212 $smarty->assign('title', $i18n->getKey('title.report').": ".$totals['start_date']." - ".$totals['end_date']);
213 $smarty->assign('content_page_name', 'report.tpl');
214 $smarty->display('index.tpl');