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($bean, $options);
65 $items = ttReportHelper::getItems($bean, $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_by = $bean->getAttribute('group_by');
89 // Totals only report. Print subtotals.
90 foreach ($subtotals as $subtotal) {
92 print "\t<".$group_by."><![CDATA[".$subtotal['name']."]]></".$group_by.">\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_by = $bean->getAttribute('group_by');
166 // Totals only report.
168 // Determine group_by header.
169 if ('cf_1' == $group_by)
170 $group_by_header = $custom_fields->fields[0]['label'];
172 $key = 'label.'.$group_by;
173 $group_by_header = $i18n->get($key);
177 print '"'.$group_by_header.'"';
178 if ($bean->getAttribute('chduration')) print ',"'.$i18n->get('label.duration').'"';
179 if ($bean->getAttribute('chunits')) print ',"'.$i18n->get('label.work_units_short').'"';
180 if ($bean->getAttribute('chcost')) print ',"'.$i18n->get('label.cost').'"';
184 foreach ($subtotals as $subtotal) {
185 print '"'.$subtotal['name'].'"';
186 if ($bean->getAttribute('chduration')) {
187 $val = $subtotal['time'];
188 if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
189 $val = time_to_decimal($val);
192 if ($bean->getAttribute('chunits')) print ',"'.$subtotal['units'].'"';
193 if ($bean->getAttribute('chcost')) {
194 if ($user->can('manage_invoices') || $user->isClient())
195 print ',"'.$subtotal['cost'].'"';
197 print ',"'.$subtotal['expenses'].'"';
202 // Normal report. Print headers.
203 print '"'.$i18n->get('label.date').'"';
204 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) print ',"'.$i18n->get('label.user').'"';
205 if ($bean->getAttribute('chclient')) print ',"'.$i18n->get('label.client').'"';
206 if ($bean->getAttribute('chproject')) print ',"'.$i18n->get('label.project').'"';
207 if ($bean->getAttribute('chtask')) print ',"'.$i18n->get('label.task').'"';
208 if ($bean->getAttribute('chcf_1')) print ',"'.$custom_fields->fields[0]['label'].'"';
209 if ($bean->getAttribute('chstart')) print ',"'.$i18n->get('label.start').'"';
210 if ($bean->getAttribute('chfinish')) print ',"'.$i18n->get('label.finish').'"';
211 if ($bean->getAttribute('chduration')) print ',"'.$i18n->get('label.duration').'"';
212 if ($bean->getAttribute('chunits')) print ',"'.$i18n->get('label.work_units_short').'"';
213 if ($bean->getAttribute('chnote')) print ',"'.$i18n->get('label.note').'"';
214 if ($bean->getAttribute('chcost')) print ',"'.$i18n->get('label.cost').'"';
215 if ($bean->getAttribute('chpaid')) print ',"'.$i18n->get('label.paid').'"';
216 if ($bean->getAttribute('chip')) print ',"'.$i18n->get('label.ip').'"';
217 if ($bean->getAttribute('chinvoice')) print ',"'.$i18n->get('label.invoice').'"';
221 foreach ($items as $item) {
222 print '"'.$item['date'].'"';
223 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) print ',"'.str_replace('"','""',$item['user']).'"';
224 if ($bean->getAttribute('chclient')) print ',"'.str_replace('"','""',$item['client']).'"';
225 if ($bean->getAttribute('chproject')) print ',"'.str_replace('"','""',$item['project']).'"';
226 if ($bean->getAttribute('chtask')) print ',"'.str_replace('"','""',$item['task']).'"';
227 if ($bean->getAttribute('chcf_1')) print ',"'.str_replace('"','""',$item['cf_1']).'"';
228 if ($bean->getAttribute('chstart')) print ',"'.$item['start'].'"';
229 if ($bean->getAttribute('chfinish')) print ',"'.$item['finish'].'"';
230 if ($bean->getAttribute('chduration')) {
231 $val = $item['duration'];
232 if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
233 $val = time_to_decimal($val);
236 if ($bean->getAttribute('chunits')) print ',"'.$item['units'].'"';
237 if ($bean->getAttribute('chnote')) print ',"'.str_replace('"','""',$item['note']).'"';
238 if ($bean->getAttribute('chcost')) {
239 if ($user->can('manage_invoices') || $user->isClient())
240 print ',"'.$item['cost'].'"';
242 print ',"'.$item['expense'].'"';
244 if ($bean->getAttribute('chpaid')) print ',"'.$item['paid'].'"';
245 if ($bean->getAttribute('chip')) {
246 $ip = $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
249 if ($bean->getAttribute('chinvoice')) print ',"'.str_replace('"','""',$item['invoice']).'"';