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'))) {
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($user->group_id);
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);
68 $items = ttReportHelper::getItems($options); // Individual entries.
69 if ($totals_only || 'no_grouping' != $group_by1)
70 $subtotals = ttReportHelper::getSubtotals($options); // Subtotals for groups of items.
71 $totals = ttReportHelper::getTotals($options); // Totals for the entire report.
73 // Assign variables that are used to print subtotals.
74 if ($items && 'no_grouping' != $group_by1) {
75 $print_subtotals = true;
77 $prev_grouped_by = '';
81 // Build a string to use as filename for the files being downloaded.
82 $filename = strtolower($i18n->get('title.report')).'_'.$bean->mValues['start_date'].'_'.$bean->mValues['end_date'];
84 // Start preparing HTML to build PDF from.
85 $styleHeader = 'style="background-color:#a6ccf7;"';
86 $styleSubtotal = 'style="background-color:#e0e0e0;"';
87 $styleCentered = 'style="text-align:center;"';
88 $styleRightAligned = 'style="text-align:right;"';
90 $title = $i18n->get('title.report').": ".$totals['start_date']." - ".$totals['end_date'];
91 $html = '<h1 style="text-align:center;">'.$title.'</h1>';
92 $html .= '<table border="1" cellpadding="3" cellspacing="0" width="100%">';
95 // We are building a "totals only" report with only subtotals and total.
96 $group_by_header = ttReportHelper::makeGroupByHeader($options);
97 $colspan = 1; // Column span for an empty row.
100 $html .= "<tr $styleHeader>";
101 $html .= '<td>'.htmlspecialchars($group_by_header).'</td>';
102 if ($bean->getAttribute('chduration')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.duration').'</td>'; }
103 if ($bean->getAttribute('chunits')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.work_units_short').'</td>'; }
104 if ($bean->getAttribute('chcost')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.cost').'</td>'; }
108 foreach ($subtotals as $subtotal) {
110 $html .= '<td>'.htmlspecialchars($subtotal['name']).'</td>';
111 if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotal['time'].'</td>';
112 if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$subtotal['units'].'</td>';
113 if ($bean->getAttribute('chcost')) {
114 $html .= "<td $styleRightAligned>";
115 if ($user->can('manage_invoices') || $user->isClient())
116 $html .= $subtotal['cost'];
118 $html .= $subtotal['expenses'];
124 $html .= '<tr><td colspan="'.$colspan.'"> </td></tr>';
125 $html .= "<tr $styleSubtotal>";
126 $html .= '<td>'.$i18n->get('label.total').'</td>';
127 if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$totals['time'].'</td>';
128 if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$totals['units'].'</td>';
129 if ($bean->getAttribute('chcost')) {
130 $html .= "<td $styleRightAligned>";
131 $html .= htmlspecialchars($user->currency).' ';
132 if ($user->can('manage_invoices') || $user->isClient())
133 $html .= $totals['cost'];
135 $html .= $totals['expenses'];
141 // We are building a normal report with items, optionally grouped with subtotals, and total.
142 $colspan = 1; // Column span for an empty row.
145 $html .= "<tr $styleHeader>";
146 $html .= '<td>'.$i18n->get('label.date').'</td>';
147 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) { $colspan++; $html .= '<td>'.$i18n->get('label.user').'</td>'; }
148 if ($bean->getAttribute('chclient')) { $colspan++; $html .= '<td>'.$i18n->get('label.client').'</td>'; }
149 if ($bean->getAttribute('chproject')) { $colspan++; $html .= '<td>'.$i18n->get('label.project').'</td>'; }
150 if ($bean->getAttribute('chtask')) { $colspan++; $html .= '<td>'.$i18n->get('label.task').'</td>'; }
151 if ($bean->getAttribute('chcf_1')) { $colspan++; $html .= '<td>'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>'; }
152 if ($bean->getAttribute('chstart')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.start').'</td>'; }
153 if ($bean->getAttribute('chfinish')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.finish').'</td>'; }
154 if ($bean->getAttribute('chduration')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.duration').'</td>'; }
155 if ($bean->getAttribute('chunits')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.work_units_short').'</td>'; }
156 if ($bean->getAttribute('chnote')) { $colspan++; $html .= '<td>'.$i18n->get('label.note').'</td>'; }
157 if ($bean->getAttribute('chcost')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.cost').'</td>'; }
158 if ($bean->getAttribute('chpaid')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.paid').'</td>'; }
159 if ($bean->getAttribute('chip')) { $colspan++; $html .= "<td $styleCentered>".$i18n->get('label.ip').'</td>'; }
160 if ($bean->getAttribute('chinvoice')) { $colspan++; $html .= '<td>'.$i18n->get('label.invoice').'</td>'; }
164 foreach ($items as $item) {
165 // Print a subtotal for a block of grouped values.
166 $cur_date = $item['date'];
167 if ($print_subtotals) {
168 $cur_grouped_by = $item['grouped_by'];
169 if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
170 $html .= '<tr style="background-color:#e0e0e0;">';
171 $html .= '<td>'.$i18n->get('label.subtotal').'</td>';
172 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
174 if ($group_by1 == 'user') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
177 if ($bean->getAttribute('chclient')) {
179 if ($group_by1 == 'client') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
182 if ($bean->getAttribute('chproject')) {
184 if ($group_by1 == 'project') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
187 if ($bean->getAttribute('chtask')) {
189 if ($group_by1 == 'task') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
192 if ($bean->getAttribute('chcf_1')) {
194 if ($group_by1 == 'cf_1') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
197 if ($bean->getAttribute('chstart')) $html .= '<td></td>';
198 if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
199 if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['time'].'</td>';
200 if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['units'].'</td>';
201 if ($bean->getAttribute('chnote')) $html .= '<td></td>';
202 if ($bean->getAttribute('chcost')) {
203 $html .= "<td $styleRightAligned>";
204 if ($user->can('manage_invoices') || $user->isClient())
205 $html .= $subtotals[$prev_grouped_by]['cost'];
207 $html .= $subtotals[$prev_grouped_by]['expenses'];
210 if ($bean->getAttribute('chpaid')) $html .= '<td></td>';
211 if ($bean->getAttribute('chip')) $html .= '<td></td>';
212 if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
214 $html .= '<tr><td colspan="'.$colspan.'"> </td></tr>';
219 // Print a regular row.
221 $html .= '<td>'.$item['date'].'</td>';
222 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) $html .= '<td>'.htmlspecialchars($item['user']).'</td>';
223 if ($bean->getAttribute('chclient')) $html .= '<td>'.htmlspecialchars($item['client']).'</td>';
224 if ($bean->getAttribute('chproject')) $html .= '<td>'.htmlspecialchars($item['project']).'</td>';
225 if ($bean->getAttribute('chtask')) $html .= '<td>'.htmlspecialchars($item['task']).'</td>';
226 if ($bean->getAttribute('chcf_1')) $html .= '<td>'.htmlspecialchars($item['cf_1']).'</td>';
227 if ($bean->getAttribute('chstart')) $html .= "<td $styleRightAligned>".$item['start'].'</td>';
228 if ($bean->getAttribute('chfinish')) $html .= "<td $styleRightAligned>".$item['finish'].'</td>';
229 if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$item['duration'].'</td>';
230 if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$item['units'].'</td>';
231 if ($bean->getAttribute('chnote')) $html .= '<td>'.htmlspecialchars($item['note']).'</td>';
232 if ($bean->getAttribute('chcost')) {
233 $html .= "<td $styleRightAligned>";
234 if ($user->can('manage_invoices') || $user->isClient())
235 $html .= $item['cost'];
237 $html .= $item['expense'];
240 if ($bean->getAttribute('chpaid')) {
242 $html .= $item['paid'] == 1 ? $i18n->get('label.yes') : $i18n->get('label.no');
245 if ($bean->getAttribute('chip')) {
247 $html .= $item['modified'] ? $item['modified_ip'].' '.$item['modified'] : $item['created_ip'].' '.$item['created'];
250 if ($bean->getAttribute('chinvoice')) $html .= '<td>'.htmlspecialchars($item['invoice']).'</td>';
253 $prev_date = $item['date'];
254 if ($print_subtotals) $prev_grouped_by = $item['grouped_by'];
257 // Print a terminating subtotal.
258 if ($print_subtotals) {
259 $html .= '<tr style="background-color:#e0e0e0;">';
260 $html .= '<td>'.$i18n->get('label.subtotal').'</td>';
261 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) {
263 if ($group_by1 == 'user') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
266 if ($bean->getAttribute('chclient')) {
268 if ($group_by1 == 'client') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
271 if ($bean->getAttribute('chproject')) {
273 if ($group_by1 == 'project') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
276 if ($bean->getAttribute('chtask')) {
278 if ($group_by1 == 'task') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
281 if ($bean->getAttribute('chcf_1')) {
283 if ($group_by1 == 'cf_1') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
286 if ($bean->getAttribute('chstart')) $html .= '<td></td>';
287 if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
288 if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['time'].'</td>';
289 if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['units'].'</td>';
290 if ($bean->getAttribute('chnote')) $html .= '<td></td>';
291 if ($bean->getAttribute('chcost')) {
292 $html .= "<td $styleRightAligned>";
293 if ($user->can('manage_invoices') || $user->isClient())
294 $html .= $subtotals[$prev_grouped_by]['cost'];
296 $html .= $subtotals[$prev_grouped_by]['expenses'];
299 if ($bean->getAttribute('chpaid')) $html .= '<td></td>';
300 if ($bean->getAttribute('chip')) $html .= '<td></td>';
301 if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
306 $html .= '<tr><td colspan="'.$colspan.'"> </td></tr>';
307 $html .= '<tr style="background-color:#e0e0e0;">';
308 $html .= '<td>'.$i18n->get('label.total').'</td>';
309 if ($user->can('view_reports') || $user->can('view_all_reports') || $user->isClient()) $html .= '<td></td>';
310 if ($bean->getAttribute('chclient')) $html .= '<td></td>';
311 if ($bean->getAttribute('chproject')) $html .= '<td></td>';
312 if ($bean->getAttribute('chtask')) $html .= '<td></td>';
313 if ($bean->getAttribute('chcf_1')) $html .= '<td></td>';
314 if ($bean->getAttribute('chstart')) $html .= '<td></td>';
315 if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
316 if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$totals['time'].'</td>';
317 if ($bean->getAttribute('chunits')) $html .= "<td $styleRightAligned>".$totals['units'].'</td>';
318 if ($bean->getAttribute('chnote')) $html .= '<td></td>';
319 if ($bean->getAttribute('chcost')) {
320 $html .= "<td $styleRightAligned>".htmlspecialchars($user->currency).' ';
321 if ($user->can('manage_invoices') || $user->isClient())
322 $html .= $totals['cost'];
324 $html .= $totals['expenses'];
327 if ($bean->getAttribute('chpaid')) $html .= '<td></td>';
328 if ($bean->getAttribute('chip')) $html .= '<td></td>';
329 if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
335 if (!defined('REPORT_FOOTER') || !(REPORT_FOOTER == false)) // By default we print it unless explicitely defined as false.
336 $html .= '<p style="text-align: center;">'.$i18n->get('form.mail.footer').'</p>';
338 // By this time we have html ready.
340 // Determine title for report.
341 $title = $i18n->get('title.report').": ".$totals['start_date']." - ".$totals['end_date'];
343 header('Pragma: public'); // This is needed for IE8 to download files over https.
344 header('Content-Type: text/html; charset=utf-8');
345 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
346 header('Cache-Control: no-store, no-cache, must-revalidate');
347 header('Cache-Control: post-check=0, pre-check=0', false);
348 header('Cache-Control: private', false);
350 header('Content-Type: application/pdf');
351 header('Content-Disposition: attachment; filename="'.$filename.'.pdf"');
354 // Beginning of TCPDF code here.
356 // Extend TCPDF class so that we can use custom header and footer.
357 class ttPDF extends TCPDF {
359 public $image_file = 'images/tt_logo.png'; // Image file for the logo in header.
360 public $page_word = 'Page'; // Localized "Page" word in footer, ex: Page 1/2.
362 // SetImageFile - sets image file name.
363 public function SetImageFile($imgFile) {
364 $this->image_file = $imgFile;
367 // SetPageWord - sets page word for footer.
368 public function SetPageWord($pageWord) {
369 $this->page_word = $pageWord;
373 public function Header() {
374 // Print logo, which is the only element of our custom header.
375 $this->Image($this->image_file, 10, 10, '', '', '', '', 'T', false, 300, 'C', false, false, 0, false, false, false);
379 public function Footer() {
380 // Position at 15 mm from bottom.
383 $this->SetFont('freeserif', 'I', 8);
384 // Print localized page number.
385 $this->Cell(0, 10, $this->page_word.' '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
389 // Create new PDF document.
390 $pdf = new ttPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
392 // If custom logo file exists - set it.
393 if (file_exists('images/'.$user->group_id.'.png'))
394 $pdf->SetImageFile('images/'.$user->group_id.'.png');
396 // Set page word for the footer.
397 $pdf->SetPageWord($i18n->get('label.page'));
399 // Set document information.
400 $pdf->SetCreator(PDF_CREATOR);
401 $pdf->SetAuthor('Anuko Time Tracker');
402 $pdf->SetTitle('Anuko Time Tracker Report');
403 $pdf->SetSubject('Anuko Time Tracker Report');
404 $pdf->SetKeywords('Anuko, time, tracker, report');
407 $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
408 $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
409 $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
411 // Set auto page breaks.
412 $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
414 // Set image scale factor.
415 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
420 // Set font (freeserif seems to work for all languages).
421 $pdf->SetFont('freeserif', '', 10); // helvetica here does not work for Russian.
424 $pdf->writeHTML($html, true, false, false, false, '');
426 // Close and output PDF document.
427 // $pdf->Output('timesheet.pdf', 'I'); // This will display inline in browser.
428 $pdf->Output($filename.'.pdf', 'D'); // D is for downloads.
430 // End of of TCPDF code.