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 // +----------------------------------------------------------------------+
 
  30  * This file generates a report in PDF format using TCPDF library from http://www.tcpdf.org/.
 
  31  * If installed, it is expected to be in WEB-INF/lib/tcpdf/ folder.
 
  33 require_once('initialize.php');
 
  34 import('ttReportHelper');
 
  36 import('form.ActionForm');
 
  37 import('ttReportHelper');
 
  40 if (!(ttAccessAllowed('view_own_reports') || ttAccessAllowed('view_reports') || ttAccessAllowed('view_all_reports') || ttAccessAllowed('view_client_reports'))) {
 
  41   header('Location: access_denied.php');
 
  44 // End of access checks.
 
  46 // Check whether TCPDF library is available.
 
  47 if (!file_exists('WEB-INF/lib/tcpdf/'))
 
  48   die('TCPDF library is not found in WEB-INF/lib/tcpdf/');
 
  50 // Include TCPDF library.
 
  51 require_once('WEB-INF/lib/tcpdf/tcpdf.php');
 
  53 // Use custom fields plugin if it is enabled.
 
  54 if ($user->isPluginEnabled('cf')) {
 
  55   require_once('plugins/CustomFields.class.php');
 
  56   $custom_fields = new CustomFields();
 
  59 // Report settings are stored in session bean before we get here.
 
  60 $bean = new ActionForm('reportBean', new Form('reportForm'), $request);
 
  62 $config = new ttConfigHelper($user->getConfig());
 
  63 $show_note_column = $bean->getAttribute('chnote') && !$config->getDefinedValue('report_note_on_separate_row');
 
  64 $show_note_row = $bean->getAttribute('chnote') && $config->getDefinedValue('report_note_on_separate_row');
 
  66 // There are 2 variations of report: totals only, or normal. Totals only means that the report
 
  67 // is grouped by either date, user, client, project, task or cf_1 and user only needs to see subtotals by group.
 
  68 $totals_only = ($bean->getAttribute('chtotalsonly') == '1');
 
  70 // Obtain items for report.
 
  71 $options = ttReportHelper::getReportOptions($bean);
 
  72 $grouping = ttReportHelper::grouping($options);
 
  74   $items = ttReportHelper::getItems($options); // Individual entries.
 
  75 if ($totals_only || $grouping)
 
  76   $subtotals = ttReportHelper::getSubtotals($options); // Subtotals for groups of items.
 
  77 $totals = ttReportHelper::getTotals($options); // Totals for the entire report.
 
  79 // Assign variables that are used to print subtotals.
 
  80 if ($items && $grouping) {
 
  81   $print_subtotals = true;
 
  83   $prev_grouped_by = '';
 
  87 // Build a string to use as filename for the files being downloaded.
 
  88 $filename = strtolower($i18n->get('title.report')).'_'.$bean->mValues['start_date'].'_'.$bean->mValues['end_date'];
 
  90 // Start preparing HTML to build PDF from.
 
  91 $styleHeader = 'style="background-color:#a6ccf7;"';
 
  92 $styleSubtotal = 'style="background-color:#e0e0e0;"';
 
  93 $styleCentered = 'style="text-align:center;"';
 
  94 $styleRightAligned = 'style="text-align:right;"';
 
  96 $title = $i18n->get('title.report').": ".$totals['start_date']." - ".$totals['end_date'];
 
  97 $html = '<h1 style="text-align:center;">'.$title.'</h1>';
 
  98 $html .= '<table border="1" cellpadding="3" cellspacing="0" width="100%">';
 
 101   // We are building a "totals only" report with only subtotals and total.
 
 102   $group_by_header = ttReportHelper::makeGroupByHeader($options);
 
 103   $colspan = 1; // Column span for an empty row.
 
 106   $html .= "<tr $styleHeader>";
 
 107   $html .= '<td>'.htmlspecialchars($group_by_header).'</td>';
 
 108   if ($bean->getAttribute('chduration')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.duration').'</td>'; }
 
 109   if ($bean->getAttribute('chunits')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.work_units_short').'</td>'; }
 
 110   if ($bean->getAttribute('chcost')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.cost').'</td>'; }
 
 114   foreach ($subtotals as $subtotal) {
 
 116     $html .= '<td>'.htmlspecialchars($subtotal['name']).'</td>';
 
 117     if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotal['time'].'</td>';
 
 118     if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$subtotal['units'].'</td>';
 
 119     if ($bean->getAttribute('chcost')) {
 
 120       $html .= "<td $styleRightAligned>";
 
 121       if ($user->can('manage_invoices') || $user->isClient())
 
 122         $html .= $subtotal['cost'];
 
 124         $html .= $subtotal['expenses'];
 
 130   $html .= '<tr><td colspan="'.$colspan.'"> </td></tr>';
 
 131   $html .= "<tr $styleSubtotal>";
 
 132   $html .= '<td>'.$i18n->get('label.total').'</td>';
 
 133   if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$totals['time'].'</td>';
 
 134   if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$totals['units'].'</td>';
 
 135   if ($bean->getAttribute('chcost')) {
 
 136       $html .= "<td $styleRightAligned>";
 
 137       $html .= htmlspecialchars($user->currency).' ';
 
 138       if ($user->can('manage_invoices') || $user->isClient())
 
 139         $html .= $totals['cost'];
 
 141         $html .= $totals['expenses'];
 
 147   // We are building a normal report with items, optionally grouped with subtotals, and total.
 
 148   $colspan = 1; // Column span for an empty row.
 
 151   $html .= "<tr $styleHeader>";
 
 152   $html .= '<td>'.$i18n->get('label.date').'</td>';
 
 153   if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) { $colspan++; $html .= '<td>'.$i18n->get('label.user').'</td>'; }
 
 154   if ($bean->getAttribute('chclient')) { $colspan++; $html .= '<td>'.$i18n->get('label.client').'</td>'; }
 
 155   if ($bean->getAttribute('chproject')) { $colspan++; $html .= '<td>'.$i18n->get('label.project').'</td>'; }
 
 156   if ($bean->getAttribute('chtask')) { $colspan++; $html .= '<td>'.$i18n->get('label.task').'</td>'; }
 
 157   if ($bean->getAttribute('chcf_1')) { $colspan++; $html .= '<td>'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>'; }
 
 158   if ($bean->getAttribute('chstart')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.start').'</td>'; }
 
 159   if ($bean->getAttribute('chfinish')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.finish').'</td>'; }
 
 160   if ($bean->getAttribute('chduration')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.duration').'</td>'; }
 
 161   if ($bean->getAttribute('chunits')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.work_units_short').'</td>'; }
 
 162   if ($show_note_column) { $colspan++; $html .= '<td>'.$i18n->get('label.note').'</td>'; }
 
 163   if ($bean->getAttribute('chcost')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.cost').'</td>'; }
 
 164   if ($bean->getAttribute('chapproved')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.approved').'</td>'; }
 
 165   if ($bean->getAttribute('chpaid')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.paid').'</td>'; }
 
 166   if ($bean->getAttribute('chip')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.ip').'</td>'; }
 
 167   if ($bean->getAttribute('chinvoice')) { $colspan++; $html .= '<td>'.$i18n->get('label.invoice').'</td>'; }
 
 168   if ($bean->getAttribute('chtimesheet')) { $colspan++; $html .= '<td>'.$i18n->get('label.timesheet').'</td>'; }
 
 172   foreach ($items as $item) {
 
 173     // Print a subtotal for a block of grouped values.
 
 174     $cur_date = $item['date'];
 
 175     if ($print_subtotals) {
 
 176       $cur_grouped_by = $item['grouped_by'];
 
 177       if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
 
 178         $html .= '<tr style="background-color:#e0e0e0;">';
 
 179         $html .= '<td>'.$i18n->get('label.subtotal').'</td>';
 
 180         if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
 
 182             $html .= htmlspecialchars($subtotals[$prev_grouped_by]['user']);
 
 185         if ($bean->getAttribute('chclient')) {
 
 187             $html .= htmlspecialchars($subtotals[$prev_grouped_by]['client']);
 
 190         if ($bean->getAttribute('chproject')) {
 
 192             $html .= htmlspecialchars($subtotals[$prev_grouped_by]['project']);
 
 195         if ($bean->getAttribute('chtask')) {
 
 197             $html .= htmlspecialchars($subtotals[$prev_grouped_by]['task']);
 
 200         if ($bean->getAttribute('chcf_1')) {
 
 202             $html .= htmlspecialchars($subtotals[$prev_grouped_by]['cf_1']);
 
 205         if ($bean->getAttribute('chstart')) $html .= '<td></td>';
 
 206         if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
 
 207         if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['time'].'</td>';
 
 208         if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['units'].'</td>';
 
 209         if ($show_note_column) $html .= '<td></td>';
 
 210         if ($bean->getAttribute('chcost')) {
 
 211           $html .= "<td $styleRightAligned>";
 
 212           if ($user->can('manage_invoices') || $user->isClient())
 
 213             $html .= $subtotals[$prev_grouped_by]['cost'];
 
 215             $html .= $subtotals[$prev_grouped_by]['expenses'];
 
 218         if ($bean->getAttribute('chapproved')) $html .= '<td></td>';
 
 219         if ($bean->getAttribute('chpaid')) $html .= '<td></td>';
 
 220         if ($bean->getAttribute('chip')) $html .= '<td></td>';
 
 221         if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
 
 222         if ($bean->getAttribute('chtimesheet')) $html .= '<td></td>';
 
 224         $html .= '<tr><td colspan="'.$colspan.'"> </td></tr>';
 
 225         // TODO: page breaks on PDF reports is a rarely used feature.
 
 226         // Currently without configuration capability.
 
 227         // Consider adding an option to user profile instead.
 
 228         if (isTrue('PDF_REPORT_PAGE_BREAKS')) {
 
 229           import('ttUserConfig');
 
 230           $uc = new ttUserConfig();
 
 231           $use_breaks = $uc->getValue(SYSC_PDF_REPORT_PAGE_BREAKS);
 
 232           if ($use_breaks) $html .= '<br pagebreak="true"/>';
 
 238     // Print a regular row.
 
 240     $html .= '<td>'.$item['date'].'</td>';
 
 241     if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) $html .= '<td>'.htmlspecialchars($item['user']).'</td>';
 
 242     if ($bean->getAttribute('chclient')) $html .= '<td>'.htmlspecialchars($item['client']).'</td>';
 
 243     if ($bean->getAttribute('chproject')) $html .= '<td>'.htmlspecialchars($item['project']).'</td>';
 
 244     if ($bean->getAttribute('chtask')) $html .= '<td>'.htmlspecialchars($item['task']).'</td>';
 
 245     if ($bean->getAttribute('chcf_1')) $html .= '<td>'.htmlspecialchars($item['cf_1']).'</td>';
 
 246     if ($bean->getAttribute('chstart')) $html .= "<td $styleRightAligned>".$item['start'].'</td>';
 
 247     if ($bean->getAttribute('chfinish')) $html .= "<td $styleRightAligned>".$item['finish'].'</td>';
 
 248     if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$item['duration'].'</td>';
 
 249     if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$item['units'].'</td>';
 
 250     if ($show_note_column) $html .= '<td>'.htmlspecialchars($item['note']).'</td>';
 
 251     if ($bean->getAttribute('chcost')) {
 
 252       $html .= "<td $styleRightAligned>";
 
 253       if ($user->can('manage_invoices') || $user->isClient())
 
 254         $html .= $item['cost'];
 
 256         $html .= $item['expense'];
 
 259     if ($bean->getAttribute('chapproved')) {
 
 261         $html .= $item['approved'] == 1 ? $i18n->get('label.yes') : $i18n->get('label.no');
 
 264     if ($bean->getAttribute('chpaid')) {
 
 266         $html .= $item['paid'] == 1 ? $i18n->get('label.yes') : $i18n->get('label.no');
 
 269     if ($bean->getAttribute('chip')) {
 
 271         $html .= $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
 
 274     if ($bean->getAttribute('chinvoice')) $html .= '<td>'.htmlspecialchars($item['invoice']).'</td>';
 
 275     if ($bean->getAttribute('chtimesheet')) $html .= '<td>'.htmlspecialchars($item['timesheet_name']).'</td>';
 
 278     if ($show_note_row && $item['note']) {
 
 280       $html .= "<td $styleRightAligned>".$i18n->get('label.note').'</td>';
 
 281       $noteSpan = $colspan-1;
 
 282       $html .= '<td colspan="'.$noteSpan.'">'.htmlspecialchars($item['note']).'</td>';
 
 286     $prev_date = $item['date'];
 
 287     if ($print_subtotals) $prev_grouped_by = $item['grouped_by'];
 
 290   // Print a terminating subtotal.
 
 291   if ($print_subtotals) {
 
 292     $html .= '<tr style="background-color:#e0e0e0;">';
 
 293     $html .= '<td>'.$i18n->get('label.subtotal').'</td>';
 
 294     if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
 
 296       $html .= htmlspecialchars($subtotals[$prev_grouped_by]['user']);
 
 299     if ($bean->getAttribute('chclient')) {
 
 301       $html .= htmlspecialchars($subtotals[$prev_grouped_by]['client']);
 
 304     if ($bean->getAttribute('chproject')) {
 
 306       $html .= htmlspecialchars($subtotals[$prev_grouped_by]['project']);
 
 309     if ($bean->getAttribute('chtask')) {
 
 311       $html .= htmlspecialchars($subtotals[$prev_grouped_by]['task']);
 
 314     if ($bean->getAttribute('chcf_1')) {
 
 316       $html .= htmlspecialchars($subtotals[$prev_grouped_by]['cf_1']);
 
 319     if ($bean->getAttribute('chstart')) $html .= '<td></td>';
 
 320     if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
 
 321     if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['time'].'</td>';
 
 322     if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['units'].'</td>';
 
 323     if ($show_note_column) $html .= '<td></td>';
 
 324     if ($bean->getAttribute('chcost')) {
 
 325       $html .= "<td $styleRightAligned>";
 
 326       if ($user->can('manage_invoices') || $user->isClient())
 
 327         $html .= $subtotals[$prev_grouped_by]['cost'];
 
 329         $html .= $subtotals[$prev_grouped_by]['expenses'];
 
 332     if ($bean->getAttribute('chapproved')) $html .= '<td></td>';
 
 333     if ($bean->getAttribute('chpaid')) $html .= '<td></td>';
 
 334     if ($bean->getAttribute('chip')) $html .= '<td></td>';
 
 335     if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
 
 336     if ($bean->getAttribute('chtimesheet')) $html .= '<td></td>';
 
 341   $html .= '<tr><td colspan="'.$colspan.'"> </td></tr>';
 
 342   $html .= '<tr style="background-color:#e0e0e0;">';
 
 343   $html .= '<td>'.$i18n->get('label.total').'</td>';
 
 344   if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) $html .= '<td></td>';
 
 345   if ($bean->getAttribute('chclient')) $html .= '<td></td>';
 
 346   if ($bean->getAttribute('chproject')) $html .= '<td></td>';
 
 347   if ($bean->getAttribute('chtask')) $html .= '<td></td>';
 
 348   if ($bean->getAttribute('chcf_1')) $html .= '<td></td>';
 
 349   if ($bean->getAttribute('chstart')) $html .= '<td></td>';
 
 350   if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
 
 351   if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$totals['time'].'</td>';
 
 352   if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$totals['units'].'</td>';
 
 353   if ($show_note_column) $html .= '<td></td>';
 
 354   if ($bean->getAttribute('chcost')) {
 
 355     $html .= "<td $styleRightAligned>".htmlspecialchars($user->currency).' ';
 
 356     if ($user->can('manage_invoices') || $user->isClient())
 
 357       $html .= $totals['cost'];
 
 359       $html .= $totals['expenses'];
 
 362   if ($bean->getAttribute('chapproved')) $html .= '<td></td>';
 
 363   if ($bean->getAttribute('chpaid')) $html .= '<td></td>';
 
 364   if ($bean->getAttribute('chip')) $html .= '<td></td>';
 
 365   if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
 
 366   if ($bean->getAttribute('chtimesheet')) $html .= '<td></td>';
 
 372 if (!defined('REPORT_FOOTER') || !(REPORT_FOOTER == false)) // By default we print it unless explicitely defined as false.
 
 373   $html .= '<p style="text-align: center;">'.$i18n->get('form.mail.footer').'</p>';
 
 375 // By this time we have html ready.
 
 377 // Determine title for report.
 
 378 $title = $i18n->get('title.report').": ".$totals['start_date']." - ".$totals['end_date'];
 
 380 header('Pragma: public'); // This is needed for IE8 to download files over https.
 
 381 header('Content-Type: text/html; charset=utf-8');
 
 382 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
 
 383 header('Cache-Control: no-store, no-cache, must-revalidate');
 
 384 header('Cache-Control: post-check=0, pre-check=0', false);
 
 385 header('Cache-Control: private', false);
 
 387 header('Content-Type: application/pdf');
 
 388 header('Content-Disposition: attachment; filename="'.$filename.'.pdf"');
 
 391 // Beginning of TCPDF code here.
 
 393 // Extend TCPDF class so that we can use custom header and footer.
 
 394 class ttPDF extends TCPDF {
 
 396   public $image_file = 'images/tt_logo.png'; // Image file for the logo in header.
 
 397   public $page_word = 'Page'; // Localized "Page" word in footer, ex: Page 1/2.
 
 399   // SetImageFile - sets image file name.
 
 400   public function SetImageFile($imgFile) {
 
 401     $this->image_file = $imgFile;
 
 404   // SetPageWord - sets page word for footer.
 
 405   public function SetPageWord($pageWord) {
 
 406     $this->page_word = $pageWord;
 
 410   public function Header() {
 
 411     // Print logo, which is the only element of our custom header.
 
 412     $this->Image($this->image_file, 10, 10, '', '', '', '', 'T', false, 300, 'C', false, false, 0, false, false, false);
 
 416   public function Footer() {
 
 417     // Position at 15 mm from bottom.
 
 420     $this->SetFont('freeserif', 'I', 8);
 
 421     // Print localized page number.
 
 422     $this->Cell(0, 10, $this->page_word.' '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
 
 426 // Create new PDF document.
 
 427 $pdf = new ttPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
 
 429 // If custom logo file exists - set it.
 
 430 if (file_exists('images/'.$user->group_id.'.png'))
 
 431   $pdf->SetImageFile('images/'.$user->group_id.'.png');
 
 433 // Set page word for the footer.
 
 434 $pdf->SetPageWord($i18n->get('label.page'));
 
 436 // Set document information.
 
 437 $pdf->SetCreator(PDF_CREATOR);
 
 438 $pdf->SetAuthor('Anuko Time Tracker');
 
 439 $pdf->SetTitle('Anuko Time Tracker Report');
 
 440 $pdf->SetSubject('Anuko Time Tracker Report');
 
 441 $pdf->SetKeywords('Anuko, time, tracker, report');
 
 444 $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
 
 445 $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
 
 446 $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
 
 448 // Set auto page breaks.
 
 449 $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
 
 451 // Set image scale factor.
 
 452 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
 
 457 // Set font (freeserif seems to work for all languages).
 
 458 $pdf->SetFont('freeserif', '', 10); // helvetica here does not work for Russian.
 
 461 $pdf->writeHTML($html, true, false, false, false, '');
 
 463 // Close and output PDF document.
 
 464 // $pdf->Output('timesheet.pdf', 'I'); // This will display inline in browser.
 
 465 $pdf->Output($filename.'.pdf', 'D'); // D is for downloads.
 
 467 // End of of TCPDF code.