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') || ttAccessAllowed('view_all_reports')  || ttAccessAllowed('view_client_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('chapproved')) print "\t<approved><![CDATA[".$item['approved']."]]></approved>\n";
 
 144       if ($bean->getAttribute('chpaid')) print "\t<paid><![CDATA[".$item['paid']."]]></paid>\n";
 
 145       if ($bean->getAttribute('chip')) {
 
 146         $ip = $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
 
 147         print "\t<ip><![CDATA[".$ip."]]></ip>\n";
 
 149       if ($bean->getAttribute('chinvoice')) print "\t<invoice><![CDATA[".$item['invoice']."]]></invoice>\n";
 
 150       if ($bean->getAttribute('chtimesheet')) print "\t<timesheet><![CDATA[".$item['timesheet_name']."]]></timesheet>\n";
 
 159 // 2) entries exported to csv
 
 160 if ('csv' == $type) {
 
 161   header('Content-Type: application/csv');
 
 162   header('Content-Disposition: attachment; filename="'.$filename.'.csv"');
 
 164   // Print UTF8 BOM first to identify encoding.
 
 165   $bom = chr(239).chr(187).chr(191); // 0xEF 0xBB 0xBF in the beginning of the file is UTF8 BOM.
 
 166   print $bom; // Without this Excel does not display UTF8 characters properly.
 
 169     // Totals only report.
 
 170     $group_by_header = ttReportHelper::makeGroupByHeader($options);
 
 173     print '"'.$group_by_header.'"';
 
 174     if ($bean->getAttribute('chduration')) print ',"'.$i18n->get('label.duration').'"';
 
 175     if ($bean->getAttribute('chunits')) print ',"'.$i18n->get('label.work_units_short').'"';
 
 176     if ($bean->getAttribute('chcost')) print ',"'.$i18n->get('label.cost').'"';
 
 180     foreach ($subtotals as $subtotal) {
 
 181       print '"'.$subtotal['name'].'"';
 
 182       if ($bean->getAttribute('chduration')) {
 
 183         $val = $subtotal['time'];
 
 184         if($val && isTrue('EXPORT_DECIMAL_DURATION'))
 
 185           $val = time_to_decimal($val);
 
 188       if ($bean->getAttribute('chunits')) print ',"'.$subtotal['units'].'"';
 
 189       if ($bean->getAttribute('chcost')) {
 
 190         if ($user->can('manage_invoices') || $user->isClient())
 
 191           print ',"'.$subtotal['cost'].'"';
 
 193           print ',"'.$subtotal['expenses'].'"';
 
 198     // Normal report. Print headers.
 
 199     print '"'.$i18n->get('label.date').'"';
 
 200     if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) print ',"'.$i18n->get('label.user').'"';
 
 201     if ($bean->getAttribute('chclient')) print ',"'.$i18n->get('label.client').'"';
 
 202     if ($bean->getAttribute('chproject')) print ',"'.$i18n->get('label.project').'"';
 
 203     if ($bean->getAttribute('chtask')) print ',"'.$i18n->get('label.task').'"';
 
 204     if ($bean->getAttribute('chcf_1')) print ',"'.$custom_fields->fields[0]['label'].'"';
 
 205     if ($bean->getAttribute('chstart')) print ',"'.$i18n->get('label.start').'"';
 
 206     if ($bean->getAttribute('chfinish')) print ',"'.$i18n->get('label.finish').'"';
 
 207     if ($bean->getAttribute('chduration')) print ',"'.$i18n->get('label.duration').'"';
 
 208     if ($bean->getAttribute('chunits')) print ',"'.$i18n->get('label.work_units_short').'"';
 
 209     if ($bean->getAttribute('chnote')) print ',"'.$i18n->get('label.note').'"';
 
 210     if ($bean->getAttribute('chcost')) print ',"'.$i18n->get('label.cost').'"';
 
 211     if ($bean->getAttribute('chapproved')) print ',"'.$i18n->get('label.approved').'"';
 
 212     if ($bean->getAttribute('chpaid')) print ',"'.$i18n->get('label.paid').'"';
 
 213     if ($bean->getAttribute('chip')) print ',"'.$i18n->get('label.ip').'"';
 
 214     if ($bean->getAttribute('chinvoice')) print ',"'.$i18n->get('label.invoice').'"';
 
 215     if ($bean->getAttribute('chtimesheet')) print ',"'.$i18n->get('label.timesheet').'"';
 
 219     foreach ($items as $item) {
 
 220       print '"'.$item['date'].'"';
 
 221       if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) print ',"'.str_replace('"','""',$item['user']).'"';
 
 222       if ($bean->getAttribute('chclient')) print ',"'.str_replace('"','""',$item['client']).'"';
 
 223       if ($bean->getAttribute('chproject')) print ',"'.str_replace('"','""',$item['project']).'"';
 
 224       if ($bean->getAttribute('chtask')) print ',"'.str_replace('"','""',$item['task']).'"';
 
 225       if ($bean->getAttribute('chcf_1')) print ',"'.str_replace('"','""',$item['cf_1']).'"';
 
 226       if ($bean->getAttribute('chstart')) print ',"'.$item['start'].'"';
 
 227       if ($bean->getAttribute('chfinish')) print ',"'.$item['finish'].'"';
 
 228       if ($bean->getAttribute('chduration')) {
 
 229         $val = $item['duration'];
 
 230         if($val && isTrue('EXPORT_DECIMAL_DURATION'))
 
 231           $val = time_to_decimal($val);
 
 234       if ($bean->getAttribute('chunits')) print ',"'.$item['units'].'"';
 
 235       if ($bean->getAttribute('chnote')) print ',"'.str_replace('"','""',$item['note']).'"';
 
 236       if ($bean->getAttribute('chcost')) {
 
 237         if ($user->can('manage_invoices') || $user->isClient())
 
 238           print ',"'.$item['cost'].'"';
 
 240           print ',"'.$item['expense'].'"';
 
 242       if ($bean->getAttribute('chapproved')) print ',"'.$item['approved'].'"';
 
 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']).'"';
 
 249       if ($bean->getAttribute('chtimesheet')) print ',"'.str_replace('"','""',$item['timesheet_name']).'"';