Fixed export to PDF to include work units.
[timetracker.git] / topdf.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 /*
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.
32  */
33 require_once('initialize.php');
34 import('form.Form');
35 import('form.ActionForm');
36 import('ttReportHelper');
37
38 // Access checks.
39 if (!(ttAccessAllowed('view_own_reports') || ttAccessAllowed('view_reports'))) {
40   header('Location: access_denied.php');
41   exit();
42 }
43 // End of access checks.
44
45 // Check whether TCPDF library is available.
46 if (!file_exists('WEB-INF/lib/tcpdf/'))
47   die('TCPDF library is not found in WEB-INF/lib/tcpdf/');
48
49 // Include TCPDF library.
50 require_once('WEB-INF/lib/tcpdf/tcpdf.php');
51
52 // Use custom fields plugin if it is enabled.
53 if ($user->isPluginEnabled('cf')) {
54   require_once('plugins/CustomFields.class.php');
55   $custom_fields = new CustomFields($user->group_id);
56 }
57
58 // Report settings are stored in session bean before we get here.
59 $bean = new ActionForm('reportBean', new Form('reportForm'), $request);
60
61 // There are 2 variations of report: totals only, or normal. Totals only means that the report
62 // is grouped by either date, user, client, project, task or cf_1 and user only needs to see subtotals by group.
63 $totals_only = ($bean->getAttribute('chtotalsonly') == '1');
64
65 // Determine group by header.
66 $group_by = $bean->getAttribute('group_by');
67 if ('no_grouping' != $group_by) {
68   if ('cf_1' == $group_by)
69     $group_by_header = $custom_fields->fields[0]['label'];
70   else {
71     $key = 'label.'.$group_by;
72     $group_by_header = $i18n->get($key);
73   }
74 }
75
76 // Obtain items for report.
77 if (!$totals_only)
78   $items = ttReportHelper::getItems($bean); // Individual entries.
79 if ($totals_only || 'no_grouping' != $group_by)
80   $subtotals = ttReportHelper::getSubtotals($bean); // Subtotals for groups of items.
81 $totals = ttReportHelper::getTotals($bean); // Totals for the entire report.
82
83 // Assign variables that are used to print subtotals.
84 if ($items && 'no_grouping' != $group_by) {
85   $print_subtotals = true;
86   $first_pass = true;
87   $prev_grouped_by = '';
88   $cur_grouped_by = '';
89 }
90
91 // Build a string to use as filename for the files being downloaded.
92 $filename = strtolower($i18n->get('title.report')).'_'.$bean->mValues['start_date'].'_'.$bean->mValues['end_date'];
93
94 // Start preparing HTML to build PDF from.
95 $styleHeader = 'style="background-color:#a6ccf7;"';
96 $styleSubtotal = 'style="background-color:#e0e0e0;"';
97 $styleCentered = 'style="text-align:center;"';
98 $styleRightAligned = 'style="text-align:right;"';
99
100 $title = $i18n->get('title.report').": ".$totals['start_date']." - ".$totals['end_date'];
101 $html = '<h1 style="text-align:center;">'.$title.'</h1>';
102 $html .= '<table border="1" cellpadding="3" cellspacing="0" width="100%">';
103
104 if ($totals_only) {
105   // We are building a "totals only" report with only subtotals and total.
106   $colspan = 1; // Column span for an empty row.
107   // Table header.
108   $html .= '<thead>';
109   $html .= "<tr $styleHeader>";
110   $html .= '<td>'.htmlspecialchars($group_by_header).'</td>';
111   if ($bean->getAttribute('chduration')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.duration').'</td>'; }
112   if ($bean->getAttribute('chunits')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.work_units_short').'</td>'; }
113   if ($bean->getAttribute('chcost')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.cost').'</td>'; }
114   $html .= '</tr>';
115   $html .= '</thead>';
116   // Print subtotals.
117   foreach ($subtotals as $subtotal) {
118     $html .= '<tr>';
119     $html .= '<td>'.htmlspecialchars($subtotal['name']).'</td>';
120     if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotal['time'].'</td>';
121     if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$subtotal['units'].'</td>';
122     if ($bean->getAttribute('chcost')) {
123       $html .= "<td $styleRightAligned>";
124       if ($user->can('manage_invoices') || $user->isClient())
125         $html .= $subtotal['cost'];
126       else
127         $html .= $subtotal['expenses'];
128       $html .= '</td>'; 
129     }
130     $html .= '</tr>';
131   }
132   // Print totals.
133   $html .= '<tr><td colspan="'.$colspan.'">&nbsp;</td></tr>';
134   $html .= "<tr $styleSubtotal>";
135   $html .= '<td>'.$i18n->get('label.total').'</td>';
136   if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$totals['time'].'</td>';
137   if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$totals['units'].'</td>';
138   if ($bean->getAttribute('chcost')) {
139       $html .= "<td $styleRightAligned>";
140       $html .= htmlspecialchars($user->currency).' ';
141       if ($user->can('manage_invoices') || $user->isClient())
142         $html .= $totals['cost'];
143       else
144         $html .= $totals['expenses'];
145       $html .= '</td>';
146     }
147   $html .= '</tr>';
148   $html .= '</table>';
149 } else {
150   // We are building a normal report with items, optionally grouped with subtotals, and total.
151   $colspan = 1; // Column span for an empty row.
152   // Table header.
153   $html .= '<thead>';
154   $html .= "<tr $styleHeader>";
155   $html .= '<td>'.$i18n->get('label.date').'</td>';
156   if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) { $colspan++; $html .= '<td>'.$i18n->get('label.user').'</td>'; }
157   if ($bean->getAttribute('chclient')) { $colspan++; $html .= '<td>'.$i18n->get('label.client').'</td>'; }
158   if ($bean->getAttribute('chproject')) { $colspan++; $html .= '<td>'.$i18n->get('label.project').'</td>'; }
159   if ($bean->getAttribute('chtask')) { $colspan++; $html .= '<td>'.$i18n->get('label.task').'</td>'; }
160   if ($bean->getAttribute('chcf_1')) { $colspan++; $html .= '<td>'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>'; }
161   if ($bean->getAttribute('chstart')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.start').'</td>'; }
162   if ($bean->getAttribute('chfinish')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.finish').'</td>'; }
163   if ($bean->getAttribute('chduration')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.duration').'</td>'; }
164   if ($bean->getAttribute('chunits')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.work_units_short').'</td>'; }
165   if ($bean->getAttribute('chnote')) { $colspan++; $html .= '<td>'.$i18n->get('label.note').'</td>'; }
166   if ($bean->getAttribute('chcost')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.cost').'</td>'; }
167   if ($bean->getAttribute('chpaid')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.paid').'</td>'; }
168   if ($bean->getAttribute('chip')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.ip').'</td>'; }
169   if ($bean->getAttribute('chinvoice')) { $colspan++; $html .= '<td>'.$i18n->get('label.invoice').'</td>'; }
170   $html .= '</tr>';
171   $html .= '</thead>';
172
173   foreach ($items as $item) {
174     // Print a subtotal for a block of grouped values.
175     $cur_date = $item['date'];
176     if ($print_subtotals) {
177       $cur_grouped_by = $item['grouped_by'];
178       if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
179         $html .= '<tr style="background-color:#e0e0e0;">';
180         $html .= '<td>'.$i18n->get('label.subtotal').'</td>';
181         if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
182             $html .= '<td>';
183             if ($group_by == 'user') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
184             $html .= '</td>';
185         }
186         if ($bean->getAttribute('chclient')) {
187             $html .= '<td>';
188             if ($group_by == 'client') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
189             $html .= '</td>';
190         }
191         if ($bean->getAttribute('chproject')) {
192             $html .= '<td>';
193             if ($group_by == 'project') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
194             $html .= '</td>';
195         }
196         if ($bean->getAttribute('chtask')) {
197             $html .= '<td>';
198             if ($group_by == 'task') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
199             $html .= '</td>';
200         }
201         if ($bean->getAttribute('chcf_1')) {
202             $html .= '<td>';
203             if ($group_by == 'cf_1') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
204             $html .= '</td>';
205         }
206         if ($bean->getAttribute('chstart')) $html .= '<td></td>';
207         if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
208         if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['time'].'</td>';
209         if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['units'].'</td>';
210         if ($bean->getAttribute('chnote')) $html .= '<td></td>';
211         if ($bean->getAttribute('chcost')) {
212           $html .= "<td $styleRightAligned>";
213           if ($user->can('manage_invoices') || $user->isClient())
214             $html .= $subtotals[$prev_grouped_by]['cost'];
215           else
216             $html .= $subtotals[$prev_grouped_by]['expenses'];
217           $html .= '</td>';
218         }
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         $html .= '</tr>';
223         $html .= '<tr><td colspan="'.$colspan.'">&nbsp;</td></tr>';
224       }
225       $first_pass = false; 
226     }
227
228     // Print a regular row.
229     $html .= '<tr>';
230     $html .= '<td>'.$item['date'].'</td>';
231     if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) $html .= '<td>'.htmlspecialchars($item['user']).'</td>';
232     if ($bean->getAttribute('chclient')) $html .= '<td>'.htmlspecialchars($item['client']).'</td>';
233     if ($bean->getAttribute('chproject')) $html .= '<td>'.htmlspecialchars($item['project']).'</td>';
234     if ($bean->getAttribute('chtask')) $html .= '<td>'.htmlspecialchars($item['task']).'</td>';
235     if ($bean->getAttribute('chcf_1')) $html .= '<td>'.htmlspecialchars($item['cf_1']).'</td>';
236     if ($bean->getAttribute('chstart')) $html .= "<td $styleRightAligned>".$item['start'].'</td>';
237     if ($bean->getAttribute('chfinish')) $html .= "<td $styleRightAligned>".$item['finish'].'</td>';
238     if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$item['duration'].'</td>';
239     if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$item['units'].'</td>';
240     if ($bean->getAttribute('chnote')) $html .= '<td>'.htmlspecialchars($item['note']).'</td>';
241     if ($bean->getAttribute('chcost')) {
242       $html .= "<td $styleRightAligned>";
243       if ($user->can('manage_invoices') || $user->isClient())
244         $html .= $item['cost'];
245       else
246         $html .= $item['expense'];
247       $html .= '</td>';
248     }
249     if ($bean->getAttribute('chpaid')) {
250         $html .= '<td>';
251         $html .= $item['paid'] == 1 ? $i18n->get('label.yes') : $i18n->get('label.no');
252         $html .= '</td>';
253     }
254     if ($bean->getAttribute('chip')) {
255         $html .= '<td>';
256         $html .= $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
257         $html .= '</td>';
258     }
259     if ($bean->getAttribute('chinvoice')) $html .= '<td>'.htmlspecialchars($item['invoice']).'</td>';
260     $html .= '</tr>';
261
262     $prev_date = $item['date'];
263     if ($print_subtotals) $prev_grouped_by = $item['grouped_by'];
264   }
265
266   // Print a terminating subtotal.
267   if ($print_subtotals) {
268     $html .= '<tr style="background-color:#e0e0e0;">';
269     $html .= '<td>'.$i18n->get('label.subtotal').'</td>';
270     if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
271       $html .= '<td>';
272       if ($group_by == 'user') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
273       $html .= '</td>';
274     }
275     if ($bean->getAttribute('chclient')) {
276       $html .= '<td>';
277       if ($group_by == 'client') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
278       $html .= '</td>';
279     }
280     if ($bean->getAttribute('chproject')) {
281       $html .= '<td>';
282       if ($group_by == 'project') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
283       $html .= '</td>';
284     }
285     if ($bean->getAttribute('chtask')) {
286       $html .= '<td>';
287       if ($group_by == 'task') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
288       $html .= '</td>';
289     }
290     if ($bean->getAttribute('chcf_1')) {
291       $html .= '<td>';
292       if ($group_by == 'cf_1') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
293       $html .= '</td>';
294     }
295     if ($bean->getAttribute('chstart')) $html .= '<td></td>';
296     if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
297     if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['time'].'</td>';
298     if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['units'].'</td>';
299     if ($bean->getAttribute('chnote')) $html .= '<td></td>';
300     if ($bean->getAttribute('chcost')) {
301       $html .= "<td $styleRightAligned>";
302       if ($user->can('manage_invoices') || $user->isClient())
303         $html .= $subtotals[$prev_grouped_by]['cost'];
304       else
305         $html .= $subtotals[$prev_grouped_by]['expenses'];
306       $html .= '</td>';
307     }
308     if ($bean->getAttribute('chpaid')) $html .= '<td></td>';
309     if ($bean->getAttribute('chip')) $html .= '<td></td>';
310     if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
311     $html .= '</tr>';
312   }
313
314   // Print totals.
315   $html .= '<tr><td colspan="'.$colspan.'">&nbsp;</td></tr>';
316   $html .= '<tr style="background-color:#e0e0e0;">';
317   $html .= '<td>'.$i18n->get('label.total').'</td>';
318   if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) $html .= '<td></td>';
319   if ($bean->getAttribute('chclient')) $html .= '<td></td>';
320   if ($bean->getAttribute('chproject')) $html .= '<td></td>';
321   if ($bean->getAttribute('chtask')) $html .= '<td></td>';
322   if ($bean->getAttribute('chcf_1')) $html .= '<td></td>';
323   if ($bean->getAttribute('chstart')) $html .= '<td></td>';
324   if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
325   if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$totals['time'].'</td>';
326   if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$totals['units'].'</td>';
327   if ($bean->getAttribute('chnote')) $html .= '<td></td>';
328   if ($bean->getAttribute('chcost')) {
329     $html .= "<td $styleRightAligned>".htmlspecialchars($user->currency).' ';
330     if ($user->can('manage_invoices') || $user->isClient())
331       $html .= $totals['cost'];
332     else
333       $html .= $totals['expenses'];
334     $html .= '</td>';
335   }
336   if ($bean->getAttribute('chpaid')) $html .= '<td></td>';
337   if ($bean->getAttribute('chip')) $html .= '<td></td>';
338   if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
339   $html .= '</tr>';
340   $html .= '</table>';
341 }
342
343 // Output footer.
344 if (!defined('REPORT_FOOTER') || !(REPORT_FOOTER == false)) // By default we print it unless explicitely defined as false.
345   $html .= '<p style="text-align: center;">'.$i18n->get('form.mail.footer').'</p>';
346
347 // By this time we have html ready.
348
349 // Determine title for report.
350 $title = $i18n->get('title.report').": ".$totals['start_date']." - ".$totals['end_date'];
351
352 header('Pragma: public'); // This is needed for IE8 to download files over https.
353 header('Content-Type: text/html; charset=utf-8');
354 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
355 header('Cache-Control: no-store, no-cache, must-revalidate');
356 header('Cache-Control: post-check=0, pre-check=0', false);
357 header('Cache-Control: private', false);
358
359 header('Content-Type: application/pdf');
360 header('Content-Disposition: attachment; filename="'.$filename.'.pdf"');
361
362
363 // Beginning of TCPDF code here.
364
365 // Extend TCPDF class so that we can use custom header and footer.
366 class ttPDF extends TCPDF {
367
368   public $image_file = 'images/tt_logo.png'; // Image file for the logo in header.
369   public $page_word = 'Page'; // Localized "Page" word in footer, ex: Page 1/2.
370
371   // SetImageFile - sets image file name.
372   public function SetImageFile($imgFile) {
373     $this->image_file = $imgFile;
374   }
375
376   // SetPageWord - sets page word for footer.
377   public function SetPageWord($pageWord) {
378     $this->page_word = $pageWord;
379   }
380
381   // Page header.
382   public function Header() {
383     // Print logo, which is the only element of our custom header.
384     $this->Image($this->image_file, 10, 10, '', '', '', '', 'T', false, 300, 'C', false, false, 0, false, false, false);
385   }
386
387   // Page footer.
388   public function Footer() {
389     // Position at 15 mm from bottom.
390     $this->SetY(-15);
391     // Set font.
392     $this->SetFont('freeserif', 'I', 8);
393     // Print localized page number.
394     $this->Cell(0, 10, $this->page_word.' '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
395   }
396 }
397
398 // Create new PDF document.
399 $pdf = new ttPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
400
401 // If custom logo file exists - set it.
402 if (file_exists('images/'.$user->group_id.'.png'))
403   $pdf->SetImageFile('images/'.$user->group_id.'.png');
404
405 // Set page word for the footer.
406 $pdf->SetPageWord($i18n->get('label.page'));
407
408 // Set document information.
409 $pdf->SetCreator(PDF_CREATOR);
410 $pdf->SetAuthor('Anuko Time Tracker');
411 $pdf->SetTitle('Anuko Time Tracker Report');
412 $pdf->SetSubject('Anuko Time Tracker Report');
413 $pdf->SetKeywords('Anuko, time, tracker, report');
414
415 // Set margins.
416 $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
417 $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
418 $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
419
420 // Set auto page breaks.
421 $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
422
423 // Set image scale factor.
424 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
425
426 // Add a page.
427 $pdf->AddPage();
428
429 // Set font (freeserif seems to work for all languages).
430 $pdf->SetFont('freeserif', '', 10); // helvetica here does not work for Russian.
431
432 // Write HTML.
433 $pdf->writeHTML($html, true, false, false, false, '');
434
435 // Close and output PDF document.
436 // $pdf->Output('timesheet.pdf', 'I'); // This will display inline in browser.
437 $pdf->Output($filename.'.pdf', 'D'); // D is for downloads.
438
439 // End of of TCPDF code.