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');
35 import('form.ActionForm');
36 import('ttReportHelper');
39 if (!(ttAccessAllowed('view_own_reports') || ttAccessAllowed('view_reports') || ttAccessAllowed('view_all_reports') || ttAccessAllowed('view_client_reports'))) {
40 header('Location: access_denied.php');
43 // End of access checks.
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/');
49 // Include TCPDF library.
50 require_once('WEB-INF/lib/tcpdf/tcpdf.php');
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();
58 // Report settings are stored in session bean before we get here.
59 $bean = new ActionForm('reportBean', new Form('reportForm'), $request);
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');
65 // Obtain items for report.
66 $options = ttReportHelper::getReportOptions($bean);
67 $grouping = ttReportHelper::grouping($options);
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.
74 // Assign variables that are used to print subtotals.
75 if ($items && $grouping) {
76 $print_subtotals = true;
78 $prev_grouped_by = '';
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'];
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;"';
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%">';
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.
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>'; }
109 foreach ($subtotals as $subtotal) {
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'];
119 $html .= $subtotal['expenses'];
125 $html .= '<tr><td colspan="'.$colspan.'"> </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'];
136 $html .= $totals['expenses'];
142 // We are building a normal report with items, optionally grouped with subtotals, and total.
143 $colspan = 1; // Column span for an empty row.
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>'; }
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()) {
176 $html .= htmlspecialchars($subtotals[$prev_grouped_by]['user']);
179 if ($bean->getAttribute('chclient')) {
181 $html .= htmlspecialchars($subtotals[$prev_grouped_by]['client']);
184 if ($bean->getAttribute('chproject')) {
186 $html .= htmlspecialchars($subtotals[$prev_grouped_by]['project']);
189 if ($bean->getAttribute('chtask')) {
191 $html .= htmlspecialchars($subtotals[$prev_grouped_by]['task']);
194 if ($bean->getAttribute('chcf_1')) {
196 $html .= htmlspecialchars($subtotals[$prev_grouped_by]['cf_1']);
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'];
208 $html .= $subtotals[$prev_grouped_by]['expenses'];
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>';
217 $html .= '<tr><td colspan="'.$colspan.'"> </td></tr>';
218 // TODO: page breaks on PDF reports is a rarely used feature.
219 // Currently without configuration capability.
220 // Consider adding an option to user profile instead.
221 if (isTrue('PDF_REPORT_PAGE_BREAKS')) {
222 import('ttUserConfig');
223 $uc = new ttUserConfig();
224 $use_breaks = $uc->getValue(SYSC_PDF_REPORT_PAGE_BREAKS);
225 if ($use_breaks) $html .= '<br pagebreak="true"/>';
231 // Print a regular row.
233 $html .= '<td>'.$item['date'].'</td>';
234 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) $html .= '<td>'.htmlspecialchars($item['user']).'</td>';
235 if ($bean->getAttribute('chclient')) $html .= '<td>'.htmlspecialchars($item['client']).'</td>';
236 if ($bean->getAttribute('chproject')) $html .= '<td>'.htmlspecialchars($item['project']).'</td>';
237 if ($bean->getAttribute('chtask')) $html .= '<td>'.htmlspecialchars($item['task']).'</td>';
238 if ($bean->getAttribute('chcf_1')) $html .= '<td>'.htmlspecialchars($item['cf_1']).'</td>';
239 if ($bean->getAttribute('chstart')) $html .= "<td $styleRightAligned>".$item['start'].'</td>';
240 if ($bean->getAttribute('chfinish')) $html .= "<td $styleRightAligned>".$item['finish'].'</td>';
241 if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$item['duration'].'</td>';
242 if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$item['units'].'</td>';
243 if ($bean->getAttribute('chcost')) {
244 $html .= "<td $styleRightAligned>";
245 if ($user->can('manage_invoices') || $user->isClient())
246 $html .= $item['cost'];
248 $html .= $item['expense'];
251 if ($bean->getAttribute('chapproved')) {
253 $html .= $item['approved'] == 1 ? $i18n->get('label.yes') : $i18n->get('label.no');
256 if ($bean->getAttribute('chpaid')) {
258 $html .= $item['paid'] == 1 ? $i18n->get('label.yes') : $i18n->get('label.no');
261 if ($bean->getAttribute('chip')) {
263 $html .= $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
266 if ($bean->getAttribute('chinvoice')) $html .= '<td>'.htmlspecialchars($item['invoice']).'</td>';
267 if ($bean->getAttribute('chtimesheet')) $html .= '<td>'.htmlspecialchars($item['timesheet_name']).'</td>';
270 if ($bean->getAttribute('chnote') && $item['note']) {
272 $html .= "<td $styleRightAligned>".$i18n->get('label.note').'</td>';
273 $noteSpan = $colspan-1;
274 $html .= '<td colspan="'.$noteSpan.'">'.htmlspecialchars($item['note']).'</td>';
278 $prev_date = $item['date'];
279 if ($print_subtotals) $prev_grouped_by = $item['grouped_by'];
282 // Print a terminating subtotal.
283 if ($print_subtotals) {
284 $html .= '<tr style="background-color:#e0e0e0;">';
285 $html .= '<td>'.$i18n->get('label.subtotal').'</td>';
286 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
288 $html .= htmlspecialchars($subtotals[$prev_grouped_by]['user']);
291 if ($bean->getAttribute('chclient')) {
293 $html .= htmlspecialchars($subtotals[$prev_grouped_by]['client']);
296 if ($bean->getAttribute('chproject')) {
298 $html .= htmlspecialchars($subtotals[$prev_grouped_by]['project']);
301 if ($bean->getAttribute('chtask')) {
303 $html .= htmlspecialchars($subtotals[$prev_grouped_by]['task']);
306 if ($bean->getAttribute('chcf_1')) {
308 $html .= htmlspecialchars($subtotals[$prev_grouped_by]['cf_1']);
311 if ($bean->getAttribute('chstart')) $html .= '<td></td>';
312 if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
313 if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['time'].'</td>';
314 if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['units'].'</td>';
315 if ($bean->getAttribute('chcost')) {
316 $html .= "<td $styleRightAligned>";
317 if ($user->can('manage_invoices') || $user->isClient())
318 $html .= $subtotals[$prev_grouped_by]['cost'];
320 $html .= $subtotals[$prev_grouped_by]['expenses'];
323 if ($bean->getAttribute('chapproved')) $html .= '<td></td>';
324 if ($bean->getAttribute('chpaid')) $html .= '<td></td>';
325 if ($bean->getAttribute('chip')) $html .= '<td></td>';
326 if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
327 if ($bean->getAttribute('chtimesheet')) $html .= '<td></td>';
332 $html .= '<tr><td colspan="'.$colspan.'"> </td></tr>';
333 $html .= '<tr style="background-color:#e0e0e0;">';
334 $html .= '<td>'.$i18n->get('label.total').'</td>';
335 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) $html .= '<td></td>';
336 if ($bean->getAttribute('chclient')) $html .= '<td></td>';
337 if ($bean->getAttribute('chproject')) $html .= '<td></td>';
338 if ($bean->getAttribute('chtask')) $html .= '<td></td>';
339 if ($bean->getAttribute('chcf_1')) $html .= '<td></td>';
340 if ($bean->getAttribute('chstart')) $html .= '<td></td>';
341 if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
342 if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$totals['time'].'</td>';
343 if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$totals['units'].'</td>';
344 if ($bean->getAttribute('chcost')) {
345 $html .= "<td $styleRightAligned>".htmlspecialchars($user->currency).' ';
346 if ($user->can('manage_invoices') || $user->isClient())
347 $html .= $totals['cost'];
349 $html .= $totals['expenses'];
352 if ($bean->getAttribute('chapproved')) $html .= '<td></td>';
353 if ($bean->getAttribute('chpaid')) $html .= '<td></td>';
354 if ($bean->getAttribute('chip')) $html .= '<td></td>';
355 if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
356 if ($bean->getAttribute('chtimesheet')) $html .= '<td></td>';
362 if (!defined('REPORT_FOOTER') || !(REPORT_FOOTER == false)) // By default we print it unless explicitely defined as false.
363 $html .= '<p style="text-align: center;">'.$i18n->get('form.mail.footer').'</p>';
365 // By this time we have html ready.
367 // Determine title for report.
368 $title = $i18n->get('title.report').": ".$totals['start_date']." - ".$totals['end_date'];
370 header('Pragma: public'); // This is needed for IE8 to download files over https.
371 header('Content-Type: text/html; charset=utf-8');
372 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
373 header('Cache-Control: no-store, no-cache, must-revalidate');
374 header('Cache-Control: post-check=0, pre-check=0', false);
375 header('Cache-Control: private', false);
377 header('Content-Type: application/pdf');
378 header('Content-Disposition: attachment; filename="'.$filename.'.pdf"');
381 // Beginning of TCPDF code here.
383 // Extend TCPDF class so that we can use custom header and footer.
384 class ttPDF extends TCPDF {
386 public $image_file = 'images/tt_logo.png'; // Image file for the logo in header.
387 public $page_word = 'Page'; // Localized "Page" word in footer, ex: Page 1/2.
389 // SetImageFile - sets image file name.
390 public function SetImageFile($imgFile) {
391 $this->image_file = $imgFile;
394 // SetPageWord - sets page word for footer.
395 public function SetPageWord($pageWord) {
396 $this->page_word = $pageWord;
400 public function Header() {
401 // Print logo, which is the only element of our custom header.
402 $this->Image($this->image_file, 10, 10, '', '', '', '', 'T', false, 300, 'C', false, false, 0, false, false, false);
406 public function Footer() {
407 // Position at 15 mm from bottom.
410 $this->SetFont('freeserif', 'I', 8);
411 // Print localized page number.
412 $this->Cell(0, 10, $this->page_word.' '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
416 // Create new PDF document.
417 $pdf = new ttPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
419 // If custom logo file exists - set it.
420 if (file_exists('images/'.$user->group_id.'.png'))
421 $pdf->SetImageFile('images/'.$user->group_id.'.png');
423 // Set page word for the footer.
424 $pdf->SetPageWord($i18n->get('label.page'));
426 // Set document information.
427 $pdf->SetCreator(PDF_CREATOR);
428 $pdf->SetAuthor('Anuko Time Tracker');
429 $pdf->SetTitle('Anuko Time Tracker Report');
430 $pdf->SetSubject('Anuko Time Tracker Report');
431 $pdf->SetKeywords('Anuko, time, tracker, report');
434 $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
435 $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
436 $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
438 // Set auto page breaks.
439 $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
441 // Set image scale factor.
442 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
447 // Set font (freeserif seems to work for all languages).
448 $pdf->SetFont('freeserif', '', 10); // helvetica here does not work for Russian.
451 $pdf->writeHTML($html, true, false, false, false, '');
453 // Close and output PDF document.
454 // $pdf->Output('timesheet.pdf', 'I'); // This will display inline in browser.
455 $pdf->Output($filename.'.pdf', 'D'); // D is for downloads.
457 // End of of TCPDF code.