Added timesheet output to CSV, XML, and PDF reports.
[timetracker.git] / tofile.php
1 <?php
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.
10 // |
11 // | There are only two ways to violate the license:
12 // |
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).
16 // |
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).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 require_once('initialize.php');
30 import('form.Form');
31 import('form.ActionForm');
32 import('ttReportHelper');
33
34 // Access checks.
35 if (!(ttAccessAllowed('view_own_reports') || ttAccessAllowed('view_reports') || ttAccessAllowed('view_all_reports')  || ttAccessAllowed('view_client_reports'))) {
36   header('Location: access_denied.php');
37   exit();
38 }
39 // End of access checks.
40
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();
45 }
46
47 // Report settings are stored in session bean before we get here.
48 $bean = new ActionForm('reportBean', new Form('reportForm'), $request);
49
50 // This file handles 2 types of export to a file:
51 // 1) xml
52 // 2) csv
53 // Export to pdf is handled separately in topdf.php.
54 $type = $request->getParameter('type');
55
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');
59
60 // Obtain items.
61 $options = ttReportHelper::getReportOptions($bean);
62 if ($totals_only)
63   $subtotals = ttReportHelper::getSubtotals($options);
64 else
65   $items = ttReportHelper::getItems($options);
66
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'];
69
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);
76
77 // Handle 2 cases of possible exports individually.
78
79 // 1) entries exported to xml
80 if ('xml' == $type) {
81   header('Content-Type: application/xml');
82   header('Content-Disposition: attachment; filename="'.$filename.'.xml"');
83
84   print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
85   print "<rows>\n";
86
87   if ($totals_only) {
88     // Totals only report.
89     $group_by_tag = ttReportHelper::makeGroupByXmlTag($options);
90
91     // Print subtotals.
92     foreach ($subtotals as $subtotal) {
93       print "<row>\n";
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";
100       }
101       if ($bean->getAttribute('chunits')) {
102         print "\t<units><![CDATA[".$subtotal['units']."]]></units>\n";
103       }
104       if ($bean->getAttribute('chcost')) {
105         print "\t<cost><![CDATA[";
106         if ($user->can('manage_invoices') || $user->isClient())
107           print $subtotal['cost'];
108         else
109           print $subtotal['expenses'];
110         print "]]></cost>\n";
111       }
112       print "</row>\n";
113     }
114   } else {
115     // Normal report.
116     foreach ($items as $item) {
117       print "<row>\n";
118
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";
132       }
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())
138           print $item['cost'];
139         else
140           print $item['expense'];
141         print "]]></cost>\n";
142       }
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";
147       }
148       if ($bean->getAttribute('chinvoice')) print "\t<invoice><![CDATA[".$item['invoice']."]]></invoice>\n";
149       if ($bean->getAttribute('chtimesheet')) print "\t<timesheet><![CDATA[".$item['timesheet_name']."]]></timesheet>\n";
150
151       print "</row>\n";
152     }
153   }
154
155   print "</rows>";
156 }
157
158 // 2) entries exported to csv
159 if ('csv' == $type) {
160   header('Content-Type: application/csv');
161   header('Content-Disposition: attachment; filename="'.$filename.'.csv"');
162
163   // Print UTF8 BOM first to identify encoding.
164   $bom = chr(239).chr(187).chr(191); // 0xEF 0xBB 0xBF in the beginning of the file is UTF8 BOM.
165   print $bom; // Without this Excel does not display UTF8 characters properly.
166
167   if ($totals_only) {
168     // Totals only report.
169     $group_by_header = ttReportHelper::makeGroupByHeader($options);
170
171     // Print headers.
172     print '"'.$group_by_header.'"';
173     if ($bean->getAttribute('chduration')) print ',"'.$i18n->get('label.duration').'"';
174     if ($bean->getAttribute('chunits')) print ',"'.$i18n->get('label.work_units_short').'"';
175     if ($bean->getAttribute('chcost')) print ',"'.$i18n->get('label.cost').'"';
176     print "\n";
177
178     // Print subtotals.
179     foreach ($subtotals as $subtotal) {
180       print '"'.$subtotal['name'].'"';
181       if ($bean->getAttribute('chduration')) {
182         $val = $subtotal['time'];
183         if($val && isTrue('EXPORT_DECIMAL_DURATION'))
184           $val = time_to_decimal($val);
185         print ',"'.$val.'"';
186       }
187       if ($bean->getAttribute('chunits')) print ',"'.$subtotal['units'].'"';
188       if ($bean->getAttribute('chcost')) {
189         if ($user->can('manage_invoices') || $user->isClient())
190           print ',"'.$subtotal['cost'].'"';
191         else
192           print ',"'.$subtotal['expenses'].'"';
193       }
194       print "\n";
195     }
196   } else {
197     // Normal report. Print headers.
198     print '"'.$i18n->get('label.date').'"';
199     if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) print ',"'.$i18n->get('label.user').'"';
200     if ($bean->getAttribute('chclient')) print ',"'.$i18n->get('label.client').'"';
201     if ($bean->getAttribute('chproject')) print ',"'.$i18n->get('label.project').'"';
202     if ($bean->getAttribute('chtask')) print ',"'.$i18n->get('label.task').'"';
203     if ($bean->getAttribute('chcf_1')) print ',"'.$custom_fields->fields[0]['label'].'"';
204     if ($bean->getAttribute('chstart')) print ',"'.$i18n->get('label.start').'"';
205     if ($bean->getAttribute('chfinish')) print ',"'.$i18n->get('label.finish').'"';
206     if ($bean->getAttribute('chduration')) print ',"'.$i18n->get('label.duration').'"';
207     if ($bean->getAttribute('chunits')) print ',"'.$i18n->get('label.work_units_short').'"';
208     if ($bean->getAttribute('chnote')) print ',"'.$i18n->get('label.note').'"';
209     if ($bean->getAttribute('chcost')) print ',"'.$i18n->get('label.cost').'"';
210     if ($bean->getAttribute('chpaid')) print ',"'.$i18n->get('label.paid').'"';
211     if ($bean->getAttribute('chip')) print ',"'.$i18n->get('label.ip').'"';
212     if ($bean->getAttribute('chinvoice')) print ',"'.$i18n->get('label.invoice').'"';
213     if ($bean->getAttribute('chtimesheet')) print ',"'.$i18n->get('label.timesheet').'"';
214     print "\n";
215
216     // Print items.
217     foreach ($items as $item) {
218       print '"'.$item['date'].'"';
219       if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) print ',"'.str_replace('"','""',$item['user']).'"';
220       if ($bean->getAttribute('chclient')) print ',"'.str_replace('"','""',$item['client']).'"';
221       if ($bean->getAttribute('chproject')) print ',"'.str_replace('"','""',$item['project']).'"';
222       if ($bean->getAttribute('chtask')) print ',"'.str_replace('"','""',$item['task']).'"';
223       if ($bean->getAttribute('chcf_1')) print ',"'.str_replace('"','""',$item['cf_1']).'"';
224       if ($bean->getAttribute('chstart')) print ',"'.$item['start'].'"';
225       if ($bean->getAttribute('chfinish')) print ',"'.$item['finish'].'"';
226       if ($bean->getAttribute('chduration')) {
227         $val = $item['duration'];
228         if($val && isTrue('EXPORT_DECIMAL_DURATION'))
229           $val = time_to_decimal($val);
230         print ',"'.$val.'"';
231       }
232       if ($bean->getAttribute('chunits')) print ',"'.$item['units'].'"';
233       if ($bean->getAttribute('chnote')) print ',"'.str_replace('"','""',$item['note']).'"';
234       if ($bean->getAttribute('chcost')) {
235         if ($user->can('manage_invoices') || $user->isClient())
236           print ',"'.$item['cost'].'"';
237         else
238           print ',"'.$item['expense'].'"';
239       }
240       if ($bean->getAttribute('chpaid')) print ',"'.$item['paid'].'"';
241       if ($bean->getAttribute('chip')) {
242         $ip = $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
243         print ',"'.$ip.'"';
244       }
245       if ($bean->getAttribute('chinvoice')) print ',"'.str_replace('"','""',$item['invoice']).'"';
246       if ($bean->getAttribute('chtimesheet')) print ',"'.str_replace('"','""',$item['timesheet_name']).'"';
247       print "\n";
248     }
249   }
250 }