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