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