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');
35 if (!(ttAccessAllowed('view_own_reports') || ttAccessAllowed('view_reports'))) {
36 header('Location: access_denied.php');
39 // End of access checks.
41 // Use custom fields plugin if it is enabled.
42 if ($user->isPluginEnabled('cf')) {
43 require_once('plugins/CustomFields.class.php');
44 $custom_fields = new CustomFields($user->group_id);
47 // Report settings are stored in session bean before we get here.
48 $bean = new ActionForm('reportBean', new Form('reportForm'), $request);
50 // This file handles 2 types of export to a file:
53 // Export to pdf is handled separately in topdf.php.
54 $type = $request->getParameter('type');
56 // Also, there are 2 variations of report: totals only, or normal. Totals only means that the report
57 // is grouped by (either date, user, client, project, task, or cf_1) and user only needs to see subtotals by group.
58 $totals_only = $bean->getAttribute('chtotalsonly');
62 $subtotals = ttReportHelper::getSubtotals($bean);
64 $items = ttReportHelper::getItems($bean);
66 // Build a string to use as filename for the files being downloaded.
67 $filename = strtolower($i18n->get('title.report')).'_'.$bean->mValues['start_date'].'_'.$bean->mValues['end_date'];
69 header('Pragma: public'); // This is needed for IE8 to download files over https.
70 header('Content-Type: text/html; charset=utf-8');
71 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
72 header('Cache-Control: no-store, no-cache, must-revalidate');
73 header('Cache-Control: post-check=0, pre-check=0', false);
74 header('Cache-Control: private', false);
76 // Handle 2 cases of possible exports individually.
78 // 1) entries exported to xml
80 header('Content-Type: application/xml');
81 header('Content-Disposition: attachment; filename="'.$filename.'.xml"');
83 print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
86 $group_by = $bean->getAttribute('group_by');
88 // Totals only report. Print subtotals.
89 foreach ($subtotals as $subtotal) {
91 print "\t<".$group_by."><![CDATA[".$subtotal['name']."]]></".$group_by.">\n";
92 if ($bean->getAttribute('chduration')) {
93 $val = $subtotal['time'];
94 if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
95 $val = time_to_decimal($val);
96 print "\t<duration><![CDATA[".$val."]]></duration>\n";
98 if ($bean->getAttribute('chcost')) {
99 print "\t<cost><![CDATA[";
100 if ($user->can('manage_invoices') || $user->isClient())
101 print $subtotal['cost'];
103 print $subtotal['expenses'];
104 print "]]></cost>\n";
110 foreach ($items as $item) {
113 print "\t<date><![CDATA[".$item['date']."]]></date>\n";
114 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) print "\t<user><![CDATA[".$item['user']."]]></user>\n";
115 if ($bean->getAttribute('chclient')) print "\t<client><![CDATA[".$item['client']."]]></client>\n";
116 if ($bean->getAttribute('chproject')) print "\t<project><![CDATA[".$item['project']."]]></project>\n";
117 if ($bean->getAttribute('chtask')) print "\t<task><![CDATA[".$item['task']."]]></task>\n";
118 if ($bean->getAttribute('chcf_1')) print "\t<cf_1><![CDATA[".$item['cf_1']."]]></cf_1>\n";
119 if ($bean->getAttribute('chstart')) print "\t<start><![CDATA[".$item['start']."]]></start>\n";
120 if ($bean->getAttribute('chfinish')) print "\t<finish><![CDATA[".$item['finish']."]]></finish>\n";
121 if ($bean->getAttribute('chduration')) {
122 $duration = $item['duration'];
123 if($duration && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
124 $duration = time_to_decimal($duration);
125 print "\t<duration><![CDATA[".$duration."]]></duration>\n";
127 if ($bean->getAttribute('chnote')) print "\t<note><![CDATA[".$item['note']."]]></note>\n";
128 if ($bean->getAttribute('chcost')) {
129 print "\t<cost><![CDATA[";
130 if ($user->can('manage_invoices') || $user->isClient())
133 print $item['expense'];
134 print "]]></cost>\n";
136 if ($bean->getAttribute('chpaid')) print "\t<paid><![CDATA[".$item['paid']."]]></paid>\n";
137 if ($bean->getAttribute('chip')) {
138 $ip = $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
139 print "\t<ip><![CDATA[".$ip."]]></ip>\n";
141 if ($bean->getAttribute('chinvoice')) print "\t<invoice><![CDATA[".$item['invoice']."]]></invoice>\n";
150 // 2) entries exported to csv
151 if ('csv' == $type) {
152 header('Content-Type: application/csv');
153 header('Content-Disposition: attachment; filename="'.$filename.'.csv"');
155 // Print UTF8 BOM first to identify encoding.
156 $bom = chr(239).chr(187).chr(191); // 0xEF 0xBB 0xBF in the beginning of the file is UTF8 BOM.
157 print $bom; // Without this Excel does not display UTF8 characters properly.
159 $group_by = $bean->getAttribute('group_by');
161 // Totals only report.
163 // Determine group_by header.
164 if ('cf_1' == $group_by)
165 $group_by_header = $custom_fields->fields[0]['label'];
167 $key = 'label.'.$group_by;
168 $group_by_header = $i18n->get($key);
172 print '"'.$group_by_header.'"';
173 if ($bean->getAttribute('chduration')) print ',"'.$i18n->get('label.duration').'"';
174 if ($bean->getAttribute('chcost')) print ',"'.$i18n->get('label.cost').'"';
178 foreach ($subtotals as $subtotal) {
179 print '"'.$subtotal['name'].'"';
180 if ($bean->getAttribute('chduration')) {
181 $val = $subtotal['time'];
182 if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
183 $val = time_to_decimal($val);
186 if ($bean->getAttribute('chcost')) {
187 if ($user->can('manage_invoices') || $user->isClient())
188 print ',"'.$subtotal['cost'].'"';
190 print ',"'.$subtotal['expenses'].'"';
195 // Normal report. Print headers.
196 print '"'.$i18n->get('label.date').'"';
197 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) print ',"'.$i18n->get('label.user').'"';
198 if ($bean->getAttribute('chclient')) print ',"'.$i18n->get('label.client').'"';
199 if ($bean->getAttribute('chproject')) print ',"'.$i18n->get('label.project').'"';
200 if ($bean->getAttribute('chtask')) print ',"'.$i18n->get('label.task').'"';
201 if ($bean->getAttribute('chcf_1')) print ',"'.$custom_fields->fields[0]['label'].'"';
202 if ($bean->getAttribute('chstart')) print ',"'.$i18n->get('label.start').'"';
203 if ($bean->getAttribute('chfinish')) print ',"'.$i18n->get('label.finish').'"';
204 if ($bean->getAttribute('chduration')) print ',"'.$i18n->get('label.duration').'"';
205 if ($bean->getAttribute('chnote')) print ',"'.$i18n->get('label.note').'"';
206 if ($bean->getAttribute('chcost')) print ',"'.$i18n->get('label.cost').'"';
207 if ($bean->getAttribute('chpaid')) print ',"'.$i18n->get('label.paid').'"';
208 if ($bean->getAttribute('chip')) print ',"'.$i18n->get('label.ip').'"';
209 if ($bean->getAttribute('chinvoice')) print ',"'.$i18n->get('label.invoice').'"';
213 foreach ($items as $item) {
214 print '"'.$item['date'].'"';
215 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) print ',"'.str_replace('"','""',$item['user']).'"';
216 if ($bean->getAttribute('chclient')) print ',"'.str_replace('"','""',$item['client']).'"';
217 if ($bean->getAttribute('chproject')) print ',"'.str_replace('"','""',$item['project']).'"';
218 if ($bean->getAttribute('chtask')) print ',"'.str_replace('"','""',$item['task']).'"';
219 if ($bean->getAttribute('chcf_1')) print ',"'.str_replace('"','""',$item['cf_1']).'"';
220 if ($bean->getAttribute('chstart')) print ',"'.$item['start'].'"';
221 if ($bean->getAttribute('chfinish')) print ',"'.$item['finish'].'"';
222 if ($bean->getAttribute('chduration')) {
223 $val = $item['duration'];
224 if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
225 $val = time_to_decimal($val);
228 if ($bean->getAttribute('chnote')) print ',"'.str_replace('"','""',$item['note']).'"';
229 if ($bean->getAttribute('chcost')) {
230 if ($user->can('manage_invoices') || $user->isClient())
231 print ',"'.$item['cost'].'"';
233 print ',"'.$item['expense'].'"';
235 if ($bean->getAttribute('chpaid')) print ',"'.$item['paid'].'"';
236 if ($bean->getAttribute('chip')) {
237 $ip = $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
240 if ($bean->getAttribute('chinvoice')) print ',"'.str_replace('"','""',$item['invoice']).'"';