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