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('chunits')) {
99 print "\t<units><![CDATA[".$subtotal['units']."]]></units>\n";
101 if ($bean->getAttribute('chcost')) {
102 print "\t<cost><![CDATA[";
103 if ($user->can('manage_invoices') || $user->isClient())
104 print $subtotal['cost'];
106 print $subtotal['expenses'];
107 print "]]></cost>\n";
113 foreach ($items as $item) {
116 print "\t<date><![CDATA[".$item['date']."]]></date>\n";
117 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) print "\t<user><![CDATA[".$item['user']."]]></user>\n";
118 if ($bean->getAttribute('chclient')) print "\t<client><![CDATA[".$item['client']."]]></client>\n";
119 if ($bean->getAttribute('chproject')) print "\t<project><![CDATA[".$item['project']."]]></project>\n";
120 if ($bean->getAttribute('chtask')) print "\t<task><![CDATA[".$item['task']."]]></task>\n";
121 if ($bean->getAttribute('chcf_1')) print "\t<cf_1><![CDATA[".$item['cf_1']."]]></cf_1>\n";
122 if ($bean->getAttribute('chstart')) print "\t<start><![CDATA[".$item['start']."]]></start>\n";
123 if ($bean->getAttribute('chfinish')) print "\t<finish><![CDATA[".$item['finish']."]]></finish>\n";
124 if ($bean->getAttribute('chduration')) {
125 $duration = $item['duration'];
126 if($duration && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
127 $duration = time_to_decimal($duration);
128 print "\t<duration><![CDATA[".$duration."]]></duration>\n";
130 if ($bean->getAttribute('chunits')) print "\t<units><![CDATA[".$item['units']."]]></units>\n";
131 if ($bean->getAttribute('chnote')) print "\t<note><![CDATA[".$item['note']."]]></note>\n";
132 if ($bean->getAttribute('chcost')) {
133 print "\t<cost><![CDATA[";
134 if ($user->can('manage_invoices') || $user->isClient())
137 print $item['expense'];
138 print "]]></cost>\n";
140 if ($bean->getAttribute('chpaid')) print "\t<paid><![CDATA[".$item['paid']."]]></paid>\n";
141 if ($bean->getAttribute('chip')) {
142 $ip = $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
143 print "\t<ip><![CDATA[".$ip."]]></ip>\n";
145 if ($bean->getAttribute('chinvoice')) print "\t<invoice><![CDATA[".$item['invoice']."]]></invoice>\n";
154 // 2) entries exported to csv
155 if ('csv' == $type) {
156 header('Content-Type: application/csv');
157 header('Content-Disposition: attachment; filename="'.$filename.'.csv"');
159 // Print UTF8 BOM first to identify encoding.
160 $bom = chr(239).chr(187).chr(191); // 0xEF 0xBB 0xBF in the beginning of the file is UTF8 BOM.
161 print $bom; // Without this Excel does not display UTF8 characters properly.
163 $group_by = $bean->getAttribute('group_by');
165 // Totals only report.
167 // Determine group_by header.
168 if ('cf_1' == $group_by)
169 $group_by_header = $custom_fields->fields[0]['label'];
171 $key = 'label.'.$group_by;
172 $group_by_header = $i18n->get($key);
176 print '"'.$group_by_header.'"';
177 if ($bean->getAttribute('chduration')) print ',"'.$i18n->get('label.duration').'"';
178 if ($bean->getAttribute('chunits')) print ',"'.$i18n->get('label.work_units_short').'"';
179 if ($bean->getAttribute('chcost')) print ',"'.$i18n->get('label.cost').'"';
183 foreach ($subtotals as $subtotal) {
184 print '"'.$subtotal['name'].'"';
185 if ($bean->getAttribute('chduration')) {
186 $val = $subtotal['time'];
187 if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
188 $val = time_to_decimal($val);
191 if ($bean->getAttribute('chunits')) print ',"'.$subtotal['units'].'"';
192 if ($bean->getAttribute('chcost')) {
193 if ($user->can('manage_invoices') || $user->isClient())
194 print ',"'.$subtotal['cost'].'"';
196 print ',"'.$subtotal['expenses'].'"';
201 // Normal report. Print headers.
202 print '"'.$i18n->get('label.date').'"';
203 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) print ',"'.$i18n->get('label.user').'"';
204 if ($bean->getAttribute('chclient')) print ',"'.$i18n->get('label.client').'"';
205 if ($bean->getAttribute('chproject')) print ',"'.$i18n->get('label.project').'"';
206 if ($bean->getAttribute('chtask')) print ',"'.$i18n->get('label.task').'"';
207 if ($bean->getAttribute('chcf_1')) print ',"'.$custom_fields->fields[0]['label'].'"';
208 if ($bean->getAttribute('chstart')) print ',"'.$i18n->get('label.start').'"';
209 if ($bean->getAttribute('chfinish')) print ',"'.$i18n->get('label.finish').'"';
210 if ($bean->getAttribute('chduration')) print ',"'.$i18n->get('label.duration').'"';
211 if ($bean->getAttribute('chunits')) print ',"'.$i18n->get('label.work_units_short').'"';
212 if ($bean->getAttribute('chnote')) print ',"'.$i18n->get('label.note').'"';
213 if ($bean->getAttribute('chcost')) print ',"'.$i18n->get('label.cost').'"';
214 if ($bean->getAttribute('chpaid')) print ',"'.$i18n->get('label.paid').'"';
215 if ($bean->getAttribute('chip')) print ',"'.$i18n->get('label.ip').'"';
216 if ($bean->getAttribute('chinvoice')) print ',"'.$i18n->get('label.invoice').'"';
220 foreach ($items as $item) {
221 print '"'.$item['date'].'"';
222 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) print ',"'.str_replace('"','""',$item['user']).'"';
223 if ($bean->getAttribute('chclient')) print ',"'.str_replace('"','""',$item['client']).'"';
224 if ($bean->getAttribute('chproject')) print ',"'.str_replace('"','""',$item['project']).'"';
225 if ($bean->getAttribute('chtask')) print ',"'.str_replace('"','""',$item['task']).'"';
226 if ($bean->getAttribute('chcf_1')) print ',"'.str_replace('"','""',$item['cf_1']).'"';
227 if ($bean->getAttribute('chstart')) print ',"'.$item['start'].'"';
228 if ($bean->getAttribute('chfinish')) print ',"'.$item['finish'].'"';
229 if ($bean->getAttribute('chduration')) {
230 $val = $item['duration'];
231 if($val && defined('EXPORT_DECIMAL_DURATION') && isTrue(EXPORT_DECIMAL_DURATION))
232 $val = time_to_decimal($val);
235 if ($bean->getAttribute('chunits')) print ',"'.$item['units'].'"';
236 if ($bean->getAttribute('chnote')) print ',"'.str_replace('"','""',$item['note']).'"';
237 if ($bean->getAttribute('chcost')) {
238 if ($user->can('manage_invoices') || $user->isClient())
239 print ',"'.$item['cost'].'"';
241 print ',"'.$item['expense'].'"';
243 if ($bean->getAttribute('chpaid')) print ',"'.$item['paid'].'"';
244 if ($bean->getAttribute('chip')) {
245 $ip = $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
248 if ($bean->getAttribute('chinvoice')) print ',"'.str_replace('"','""',$item['invoice']).'"';