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');
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";
87 $group_by1 = $bean->getAttribute('group_by1');
89 // Totals only report. Print subtotals.
90 foreach ($subtotals as $subtotal) {
92 print "\t<".$group_by1."><![CDATA[".$subtotal['name']."]]></".$group_by1.">\n";
93 if ($bean->getAttribute('chduration')) {
94 $val = $subtotal['time'];
95 if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
96 $val = time_to_decimal($val);
97 print "\t<duration><![CDATA[".$val."]]></duration>\n";
99 if ($bean->getAttribute('chunits')) {
100 print "\t<units><![CDATA[".$subtotal['units']."]]></units>\n";
102 if ($bean->getAttribute('chcost')) {
103 print "\t<cost><![CDATA[";
104 if ($user->can('manage_invoices') || $user->isClient())
105 print $subtotal['cost'];
107 print $subtotal['expenses'];
108 print "]]></cost>\n";
114 foreach ($items as $item) {
117 print "\t<date><![CDATA[".$item['date']."]]></date>\n";
118 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) print "\t<user><![CDATA[".$item['user']."]]></user>\n";
119 if ($bean->getAttribute('chclient')) print "\t<client><![CDATA[".$item['client']."]]></client>\n";
120 if ($bean->getAttribute('chproject')) print "\t<project><![CDATA[".$item['project']."]]></project>\n";
121 if ($bean->getAttribute('chtask')) print "\t<task><![CDATA[".$item['task']."]]></task>\n";
122 if ($bean->getAttribute('chcf_1')) print "\t<cf_1><![CDATA[".$item['cf_1']."]]></cf_1>\n";
123 if ($bean->getAttribute('chstart')) print "\t<start><![CDATA[".$item['start']."]]></start>\n";
124 if ($bean->getAttribute('chfinish')) print "\t<finish><![CDATA[".$item['finish']."]]></finish>\n";
125 if ($bean->getAttribute('chduration')) {
126 $duration = $item['duration'];
127 if($duration && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
128 $duration = time_to_decimal($duration);
129 print "\t<duration><![CDATA[".$duration."]]></duration>\n";
131 if ($bean->getAttribute('chunits')) print "\t<units><![CDATA[".$item['units']."]]></units>\n";
132 if ($bean->getAttribute('chnote')) print "\t<note><![CDATA[".$item['note']."]]></note>\n";
133 if ($bean->getAttribute('chcost')) {
134 print "\t<cost><![CDATA[";
135 if ($user->can('manage_invoices') || $user->isClient())
138 print $item['expense'];
139 print "]]></cost>\n";
141 if ($bean->getAttribute('chpaid')) print "\t<paid><![CDATA[".$item['paid']."]]></paid>\n";
142 if ($bean->getAttribute('chip')) {
143 $ip = $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
144 print "\t<ip><![CDATA[".$ip."]]></ip>\n";
146 if ($bean->getAttribute('chinvoice')) print "\t<invoice><![CDATA[".$item['invoice']."]]></invoice>\n";
155 // 2) entries exported to csv
156 if ('csv' == $type) {
157 header('Content-Type: application/csv');
158 header('Content-Disposition: attachment; filename="'.$filename.'.csv"');
160 // Print UTF8 BOM first to identify encoding.
161 $bom = chr(239).chr(187).chr(191); // 0xEF 0xBB 0xBF in the beginning of the file is UTF8 BOM.
162 print $bom; // Without this Excel does not display UTF8 characters properly.
164 $group_by1 = $bean->getAttribute('group_by1');
166 // Totals only report.
167 $group_by_header = ttReportHelper::makeGroupByHeader($options);
170 print '"'.$group_by_header.'"';
171 if ($bean->getAttribute('chduration')) print ',"'.$i18n->get('label.duration').'"';
172 if ($bean->getAttribute('chunits')) print ',"'.$i18n->get('label.work_units_short').'"';
173 if ($bean->getAttribute('chcost')) print ',"'.$i18n->get('label.cost').'"';
177 foreach ($subtotals as $subtotal) {
178 print '"'.$subtotal['name'].'"';
179 if ($bean->getAttribute('chduration')) {
180 $val = $subtotal['time'];
181 if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
182 $val = time_to_decimal($val);
185 if ($bean->getAttribute('chunits')) print ',"'.$subtotal['units'].'"';
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('chunits')) print ',"'.$i18n->get('label.work_units_short').'"';
206 if ($bean->getAttribute('chnote')) print ',"'.$i18n->get('label.note').'"';
207 if ($bean->getAttribute('chcost')) print ',"'.$i18n->get('label.cost').'"';
208 if ($bean->getAttribute('chpaid')) print ',"'.$i18n->get('label.paid').'"';
209 if ($bean->getAttribute('chip')) print ',"'.$i18n->get('label.ip').'"';
210 if ($bean->getAttribute('chinvoice')) print ',"'.$i18n->get('label.invoice').'"';
214 foreach ($items as $item) {
215 print '"'.$item['date'].'"';
216 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) print ',"'.str_replace('"','""',$item['user']).'"';
217 if ($bean->getAttribute('chclient')) print ',"'.str_replace('"','""',$item['client']).'"';
218 if ($bean->getAttribute('chproject')) print ',"'.str_replace('"','""',$item['project']).'"';
219 if ($bean->getAttribute('chtask')) print ',"'.str_replace('"','""',$item['task']).'"';
220 if ($bean->getAttribute('chcf_1')) print ',"'.str_replace('"','""',$item['cf_1']).'"';
221 if ($bean->getAttribute('chstart')) print ',"'.$item['start'].'"';
222 if ($bean->getAttribute('chfinish')) print ',"'.$item['finish'].'"';
223 if ($bean->getAttribute('chduration')) {
224 $val = $item['duration'];
225 if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
226 $val = time_to_decimal($val);
229 if ($bean->getAttribute('chunits')) print ',"'.$item['units'].'"';
230 if ($bean->getAttribute('chnote')) print ',"'.str_replace('"','""',$item['note']).'"';
231 if ($bean->getAttribute('chcost')) {
232 if ($user->can('manage_invoices') || $user->isClient())
233 print ',"'.$item['cost'].'"';
235 print ',"'.$item['expense'].'"';
237 if ($bean->getAttribute('chpaid')) print ',"'.$item['paid'].'"';
238 if ($bean->getAttribute('chip')) {
239 $ip = $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
242 if ($bean->getAttribute('chinvoice')) print ',"'.str_replace('"','""',$item['invoice']).'"';