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();
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');
61 $options = ttReportHelper::getReportOptions($bean);
63 $subtotals = ttReportHelper::getSubtotals($options);
65 $items = ttReportHelper::getItems($options);
67 // Build a string to use as filename for the files being downloaded.
68 $filename = strtolower($i18n->get('title.report')).'_'.$bean->mValues['start_date'].'_'.$bean->mValues['end_date'];
70 header('Pragma: public'); // This is needed for IE8 to download files over https.
71 header('Content-Type: text/html; charset=utf-8');
72 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
73 header('Cache-Control: no-store, no-cache, must-revalidate');
74 header('Cache-Control: post-check=0, pre-check=0', false);
75 header('Cache-Control: private', false);
77 // Handle 2 cases of possible exports individually.
79 // 1) entries exported to xml
81 header('Content-Type: application/xml');
82 header('Content-Disposition: attachment; filename="'.$filename.'.xml"');
84 print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
88 // Totals only report.
89 $group_by_tag = ttReportHelper::makeGroupByXmlTag($options);
92 foreach ($subtotals as $subtotal) {
94 print "\t<".$group_by_tag."><![CDATA[".$subtotal['name']."]]></".$group_by_tag.">\n";
95 if ($bean->getAttribute('chduration')) {
96 $val = $subtotal['time'];
97 if($val && isTrue('EXPORT_DECIMAL_DURATION'))
98 $val = time_to_decimal($val);
99 print "\t<duration><![CDATA[".$val."]]></duration>\n";
101 if ($bean->getAttribute('chunits')) {
102 print "\t<units><![CDATA[".$subtotal['units']."]]></units>\n";
104 if ($bean->getAttribute('chcost')) {
105 print "\t<cost><![CDATA[";
106 if ($user->can('manage_invoices') || $user->isClient())
107 print $subtotal['cost'];
109 print $subtotal['expenses'];
110 print "]]></cost>\n";
116 foreach ($items as $item) {
119 print "\t<date><![CDATA[".$item['date']."]]></date>\n";
120 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) print "\t<user><![CDATA[".$item['user']."]]></user>\n";
121 if ($bean->getAttribute('chclient')) print "\t<client><![CDATA[".$item['client']."]]></client>\n";
122 if ($bean->getAttribute('chproject')) print "\t<project><![CDATA[".$item['project']."]]></project>\n";
123 if ($bean->getAttribute('chtask')) print "\t<task><![CDATA[".$item['task']."]]></task>\n";
124 if ($bean->getAttribute('chcf_1')) print "\t<cf_1><![CDATA[".$item['cf_1']."]]></cf_1>\n";
125 if ($bean->getAttribute('chstart')) print "\t<start><![CDATA[".$item['start']."]]></start>\n";
126 if ($bean->getAttribute('chfinish')) print "\t<finish><![CDATA[".$item['finish']."]]></finish>\n";
127 if ($bean->getAttribute('chduration')) {
128 $duration = $item['duration'];
129 if($duration && isTrue('EXPORT_DECIMAL_DURATION'))
130 $duration = time_to_decimal($duration);
131 print "\t<duration><![CDATA[".$duration."]]></duration>\n";
133 if ($bean->getAttribute('chunits')) print "\t<units><![CDATA[".$item['units']."]]></units>\n";
134 if ($bean->getAttribute('chnote')) print "\t<note><![CDATA[".$item['note']."]]></note>\n";
135 if ($bean->getAttribute('chcost')) {
136 print "\t<cost><![CDATA[";
137 if ($user->can('manage_invoices') || $user->isClient())
140 print $item['expense'];
141 print "]]></cost>\n";
143 if ($bean->getAttribute('chpaid')) print "\t<paid><![CDATA[".$item['paid']."]]></paid>\n";
144 if ($bean->getAttribute('chip')) {
145 $ip = $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
146 print "\t<ip><![CDATA[".$ip."]]></ip>\n";
148 if ($bean->getAttribute('chinvoice')) print "\t<invoice><![CDATA[".$item['invoice']."]]></invoice>\n";
157 // 2) entries exported to csv
158 if ('csv' == $type) {
159 header('Content-Type: application/csv');
160 header('Content-Disposition: attachment; filename="'.$filename.'.csv"');
162 // Print UTF8 BOM first to identify encoding.
163 $bom = chr(239).chr(187).chr(191); // 0xEF 0xBB 0xBF in the beginning of the file is UTF8 BOM.
164 print $bom; // Without this Excel does not display UTF8 characters properly.
167 // Totals only report.
168 $group_by_header = ttReportHelper::makeGroupByHeader($options);
171 print '"'.$group_by_header.'"';
172 if ($bean->getAttribute('chduration')) print ',"'.$i18n->get('label.duration').'"';
173 if ($bean->getAttribute('chunits')) print ',"'.$i18n->get('label.work_units_short').'"';
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 && isTrue('EXPORT_DECIMAL_DURATION'))
183 $val = time_to_decimal($val);
186 if ($bean->getAttribute('chunits')) print ',"'.$subtotal['units'].'"';
187 if ($bean->getAttribute('chcost')) {
188 if ($user->can('manage_invoices') || $user->isClient())
189 print ',"'.$subtotal['cost'].'"';
191 print ',"'.$subtotal['expenses'].'"';
196 // Normal report. Print headers.
197 print '"'.$i18n->get('label.date').'"';
198 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) print ',"'.$i18n->get('label.user').'"';
199 if ($bean->getAttribute('chclient')) print ',"'.$i18n->get('label.client').'"';
200 if ($bean->getAttribute('chproject')) print ',"'.$i18n->get('label.project').'"';
201 if ($bean->getAttribute('chtask')) print ',"'.$i18n->get('label.task').'"';
202 if ($bean->getAttribute('chcf_1')) print ',"'.$custom_fields->fields[0]['label'].'"';
203 if ($bean->getAttribute('chstart')) print ',"'.$i18n->get('label.start').'"';
204 if ($bean->getAttribute('chfinish')) print ',"'.$i18n->get('label.finish').'"';
205 if ($bean->getAttribute('chduration')) print ',"'.$i18n->get('label.duration').'"';
206 if ($bean->getAttribute('chunits')) print ',"'.$i18n->get('label.work_units_short').'"';
207 if ($bean->getAttribute('chnote')) print ',"'.$i18n->get('label.note').'"';
208 if ($bean->getAttribute('chcost')) print ',"'.$i18n->get('label.cost').'"';
209 if ($bean->getAttribute('chpaid')) print ',"'.$i18n->get('label.paid').'"';
210 if ($bean->getAttribute('chip')) print ',"'.$i18n->get('label.ip').'"';
211 if ($bean->getAttribute('chinvoice')) print ',"'.$i18n->get('label.invoice').'"';
215 foreach ($items as $item) {
216 print '"'.$item['date'].'"';
217 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) print ',"'.str_replace('"','""',$item['user']).'"';
218 if ($bean->getAttribute('chclient')) print ',"'.str_replace('"','""',$item['client']).'"';
219 if ($bean->getAttribute('chproject')) print ',"'.str_replace('"','""',$item['project']).'"';
220 if ($bean->getAttribute('chtask')) print ',"'.str_replace('"','""',$item['task']).'"';
221 if ($bean->getAttribute('chcf_1')) print ',"'.str_replace('"','""',$item['cf_1']).'"';
222 if ($bean->getAttribute('chstart')) print ',"'.$item['start'].'"';
223 if ($bean->getAttribute('chfinish')) print ',"'.$item['finish'].'"';
224 if ($bean->getAttribute('chduration')) {
225 $val = $item['duration'];
226 if($val && isTrue('EXPORT_DECIMAL_DURATION'))
227 $val = time_to_decimal($val);
230 if ($bean->getAttribute('chunits')) print ',"'.$item['units'].'"';
231 if ($bean->getAttribute('chnote')) print ',"'.str_replace('"','""',$item['note']).'"';
232 if ($bean->getAttribute('chcost')) {
233 if ($user->can('manage_invoices') || $user->isClient())
234 print ',"'.$item['cost'].'"';
236 print ',"'.$item['expense'].'"';
238 if ($bean->getAttribute('chpaid')) print ',"'.$item['paid'].'"';
239 if ($bean->getAttribute('chip')) {
240 $ip = $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
243 if ($bean->getAttribute('chinvoice')) print ',"'.str_replace('"','""',$item['invoice']).'"';