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');
38 // Check whether TCPDF library is available.
39 if (!file_exists('WEB-INF/lib/tcpdf/'))
40 die('TCPDF library is not found in WEB-INF/lib/tcpdf/');
42 // Include TCPDF library.
43 require_once('WEB-INF/lib/tcpdf/tcpdf.php');
46 if (!ttAccessCheck(right_view_reports)) {
47 header('Location: access_denied.php');
51 // Use custom fields plugin if it is enabled.
52 if (in_array('cf', explode(',', $user->plugins))) {
53 require_once('plugins/CustomFields.class.php');
54 $custom_fields = new CustomFields($user->team_id);
57 // Report settings are stored in session bean before we get here.
58 $bean = new ActionForm('reportBean', new Form('reportForm'), $request);
60 // There are 2 variations of report: totals only, or normal. Totals only means that the report
61 // is grouped by either date, user, client, project, task or cf_1 and user only needs to see subtotals by group.
62 $totals_only = ($bean->getAttribute('chtotalsonly') == '1');
64 // Determine group by header.
65 $group_by = $bean->getAttribute('group_by');
66 if ('no_grouping' != $group_by) {
67 if ('cf_1' == $group_by)
68 $group_by_header = $custom_fields->fields[0]['label'];
70 $key = 'label.'.$group_by;
71 $group_by_header = $i18n->getKey($key);
75 // Obtain items for report.
77 $items = ttReportHelper::getItems($bean); // Individual entries.
78 if ($totals_only || 'no_grouping' != $group_by)
79 $subtotals = ttReportHelper::getSubtotals($bean); // Subtotals for groups of items.
80 $totals = ttReportHelper::getTotals($bean); // Totals for the entire report.
82 // Assign variables that are used to print subtotals.
83 if ($items && 'no_grouping' != $group_by) {
84 $print_subtotals = true;
86 $prev_grouped_by = '';
90 // Start preparing HTML to build PDF from.
91 $styleHeader = 'style="background-color:#a6ccf7;"';
92 $styleSubtotal = 'style="background-color:#e0e0e0;"';
93 $styleCentered = 'style="text-align:center;"';
94 $styleRightAligned = 'style="text-align:right;"';
96 $title = $i18n->getKey('title.report').": ".$totals['start_date']." - ".$totals['end_date'];
97 $html = '<h1 style="text-align:center;">'.$title.'</h1>';
98 $html .= '<table border="1" cellpadding="3" cellspacing="0" width="100%">';
101 // We are building a "totals only" report with only subtotals and total.
102 $colspan = 1; // Column span for an empty row.
105 $html .= "<tr $styleHeader>";
106 $html .= '<td>'.htmlspecialchars($group_by_header).'</td>';
107 if ($bean->getAttribute('chduration')) { $colspan++; $html .= "<td $styleCentered>".$i18n->getKey('label.duration').'</td>'; }
108 if ($bean->getAttribute('chcost')) { $colspan++; $html .= "<td $styleCentered>".$i18n->getKey('label.cost').'</td>'; }
112 foreach ($subtotals as $subtotal) {
114 $html .= '<td>'.htmlspecialchars($subtotal['name']).'</td>';
115 if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotal['time'].'</td>';
116 if ($bean->getAttribute('chcost')) {
117 $html .= "<td $styleRightAligned>";
118 if ($user->canManageTeam() || $user->isClient())
119 $html .= $subtotal['cost'];
121 $html .= $subtotal['expenses'];
127 $html .= '<tr><td colspan="'.$colspan.'"> </td></tr>';
128 $html .= "<tr $styleSubtotal>";
129 $html .= '<td>'.$i18n->getKey('label.total').'</td>';
130 if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$totals['time'].'</td>';
131 if ($bean->getAttribute('chcost')) {
132 $html .= "<td $styleRightAligned>";
133 $html .= htmlspecialchars($user->currency).' ';
134 if ($user->canManageTeam() || $user->isClient())
135 $html .= $totals['cost'];
137 $html .= $totals['expenses'];
143 // We are building a normal report with items, optionally grouped with subtotals, and total.
144 $colspan = 1; // Column span for an empty row.
147 $html .= "<tr $styleHeader>";
148 $html .= '<td>'.$i18n->getKey('label.date').'</td>';
149 if ($user->canManageTeam() || $user->isClient()) { $colspan++; $html .= '<td>'.$i18n->getKey('label.user').'</td>'; }
150 if ($bean->getAttribute('chclient')) { $colspan++; $html .= '<td>'.$i18n->getKey('label.client').'</td>'; }
151 if ($bean->getAttribute('chproject')) { $colspan++; $html .= '<td>'.$i18n->getKey('label.project').'</td>'; }
152 if ($bean->getAttribute('chtask')) { $colspan++; $html .= '<td>'.$i18n->getKey('label.task').'</td>'; }
153 if ($bean->getAttribute('chcf_1')) { $colspan++; $html .= '<td>'.htmlspecialchars($custom_fields->fields[0]['label']).'</td>'; }
154 if ($bean->getAttribute('chstart')) { $colspan++; $html .= "<td $styleCentered>".$i18n->getKey('label.start').'</td>'; }
155 if ($bean->getAttribute('chfinish')) { $colspan++; $html .= "<td $styleCentered>".$i18n->getKey('label.finish').'</td>'; }
156 if ($bean->getAttribute('chduration')) { $colspan++; $html .= "<td $styleCentered>".$i18n->getKey('label.duration').'</td>'; }
157 if ($bean->getAttribute('chnote')) { $colspan++; $html .= '<td>'.$i18n->getKey('label.note').'</td>'; }
158 if ($bean->getAttribute('chcost')) { $colspan++; $html .= "<td $styleCentered>".$i18n->getKey('label.cost').'</td>'; }
159 if ($bean->getAttribute('chinvoice')) { $colspan++; $html .= '<td>'.$i18n->getKey('label.invoice').'</td>'; }
163 foreach ($items as $item) {
164 // Print a subtotal for a block of grouped values.
165 $cur_date = $item['date'];
166 if ($print_subtotals) {
167 $cur_grouped_by = $item['grouped_by'];
168 if ($cur_grouped_by != $prev_grouped_by && !$first_pass) {
169 $html .= '<tr style="background-color:#e0e0e0;">';
170 $html .= '<td>'.$i18n->getKey('label.subtotal').'</td>';
171 if ($user->canManageTeam() || $user->isClient()) {
173 if ($group_by == 'user') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
176 if ($bean->getAttribute('chclient')) {
178 if ($group_by == 'client') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
181 if ($bean->getAttribute('chproject')) {
183 if ($group_by == 'project') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
186 if ($bean->getAttribute('chtask')) {
188 if ($group_by == 'task') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
191 if ($bean->getAttribute('chcf_1')) {
193 if ($group_by == 'cf_1') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
196 if ($bean->getAttribute('chstart')) $html .= '<td></td>';
197 if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
198 if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['time'].'</td>';
199 if ($bean->getAttribute('chnote')) $html .= '<td></td>';
200 if ($bean->getAttribute('chcost')) {
201 $html .= "<td $styleRightAligned>";
202 if ($user->canManageTeam() || $user->isClient())
203 $html .= $subtotals[$prev_grouped_by]['cost'];
205 $html .= $subtotals[$prev_grouped_by]['expenses'];
208 if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
210 $html .= '<tr><td colspan="'.$colspan.'"> </td></tr>';
215 // Print a regular row.
217 $html .= '<td>'.$item['date'].'</td>';
218 if ($user->canManageTeam() || $user->isClient()) $html .= '<td>'.htmlspecialchars($item['user']).'</td>';
219 if ($bean->getAttribute('chclient')) $html .= '<td>'.htmlspecialchars($item['client']).'</td>';
220 if ($bean->getAttribute('chproject')) $html .= '<td>'.htmlspecialchars($item['project']).'</td>';
221 if ($bean->getAttribute('chtask')) $html .= '<td>'.htmlspecialchars($item['task']).'</td>';
222 if ($bean->getAttribute('chcf_1')) $html .= '<td>'.htmlspecialchars($item['cf_1']).'</td>';
223 if ($bean->getAttribute('chstart')) $html .= "<td $styleRightAligned>".$item['start'].'</td>';
224 if ($bean->getAttribute('chfinish')) $html .= "<td $styleRightAligned>".$item['finish'].'</td>';
225 if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$item['duration'].'</td>';
226 if ($bean->getAttribute('chnote')) $html .= '<td>'.htmlspecialchars($item['note']).'</td>';
227 if ($bean->getAttribute('chcost')) {
228 $html .= "<td $styleRightAligned>";
229 if ($user->canManageTeam() || $user->isClient())
230 $html .= $item['cost'];
232 $html .= $item['expense'];
235 if ($bean->getAttribute('chinvoice')) $html .= '<td>'.htmlspecialchars($item['invoice']).'</td>';
238 $prev_date = $item['date'];
239 if ($print_subtotals) $prev_grouped_by = $item['grouped_by'];
242 // Print a terminating subtotal.
243 if ($print_subtotals) {
244 $html .= '<tr style="background-color:#e0e0e0;">';
245 $html .= '<td>'.$i18n->getKey('label.subtotal').'</td>';
246 if ($user->canManageTeam() || $user->isClient()) {
248 if ($group_by == 'user') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
251 if ($bean->getAttribute('chclient')) {
253 if ($group_by == 'client') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
256 if ($bean->getAttribute('chproject')) {
258 if ($group_by == 'project') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
261 if ($bean->getAttribute('chtask')) {
263 if ($group_by == 'task') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
266 if ($bean->getAttribute('chcf_1')) {
268 if ($group_by == 'cf_1') $html .= htmlspecialchars($subtotals[$prev_grouped_by]['name']);
271 if ($bean->getAttribute('chstart')) $html .= '<td></td>';
272 if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
273 if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$subtotals[$prev_grouped_by]['time'].'</td>';
274 if ($bean->getAttribute('chnote')) $html .= '<td></td>';
275 if ($bean->getAttribute('chcost')) {
276 $html .= "<td $styleRightAligned>";
277 if ($user->canManageTeam() || $user->isClient())
278 $html .= $subtotals[$prev_grouped_by]['cost'];
280 $html .= $subtotals[$prev_grouped_by]['expenses'];
283 if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
288 $html .= '<tr><td colspan="'.$colspan.'"> </td></tr>';
289 $html .= '<tr style="background-color:#e0e0e0;">';
290 $html .= '<td>'.$i18n->getKey('label.total').'</td>';
291 if ($user->canManageTeam() || $user->isClient()) $html .= '<td></td>';
292 if ($bean->getAttribute('chclient')) $html .= '<td></td>';
293 if ($bean->getAttribute('chproject')) $html .= '<td></td>';
294 if ($bean->getAttribute('chtask')) $html .= '<td></td>';
295 if ($bean->getAttribute('chcf_1')) $html .= '<td></td>';
296 if ($bean->getAttribute('chstart')) $html .= '<td></td>';
297 if ($bean->getAttribute('chfinish')) $html .= '<td></td>';
298 if ($bean->getAttribute('chduration')) $html .= "<td $styleRightAligned>".$totals['time'].'</td>';
299 if ($bean->getAttribute('chnote')) $html .= '<td></td>';
300 if ($bean->getAttribute('chcost')) {
301 $html .= "<td $styleRightAligned>".htmlspecialchars($user->currency).' ';
302 if ($user->canManageTeam() || $user->isClient())
303 $html .= $totals['cost'];
305 $html .= $totals['expenses'];
308 if ($bean->getAttribute('chinvoice')) $html .= '<td></td>';
312 // By this time we have html ready.
314 // Determine title for report.
315 $title = $i18n->getKey('title.report').": ".$totals['start_date']." - ".$totals['end_date'];
317 header('Pragma: public'); // This is needed for IE8 to download files over https.
318 header('Content-Type: text/html; charset=utf-8');
319 header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
320 header('Cache-Control: no-store, no-cache, must-revalidate');
321 header('Cache-Control: post-check=0, pre-check=0', false);
322 header('Cache-Control: private', false);
324 header('Content-Type: application/pdf');
325 header('Content-Disposition: attachment; filename="timesheet.pdf"');
328 // Beginning of TCPDF code here.
330 // Extend TCPDF class so that we can use custom header and footer.
331 class MyyPDF extends TCPDF {
333 public $image_file = 'images/tt_logo.png'; // Image file for the logo in header.
334 public $page_word = 'Page'; // Localized "Page" word in footer, ex: Page 1/2.
336 // SetImageFile - sets image file name.
337 public function SetImageFile($imgFile) {
338 $this->image_file = $imgFile;
341 // SetPageWord - sets page word for footer.
342 public function SetPageWord($pageWord) {
343 $this->page_word = $pageWord;
347 public function Header() {
348 // Print logo, which is the only element of our custom header.
349 $this->Image($this->image_file, 10, 10, '', '', '', '', 'T', false, 300, 'C', false, false, 0, false, false, false);
353 public function Footer() {
354 // Position at 15 mm from bottom.
357 $this->SetFont('freeserif', 'I', 8);
358 // Print localized page number.
359 $this->Cell(0, 10, $this->page_word.' '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
363 // Create new PDF document.
364 $pdf = new MyyPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
366 // If custom logo file exists - set it.
367 if (file_exists('images/'.$user->team_id.'.png'))
368 $pdf->SetImageFile('images/'.$user->team_id.'.png');
370 // Set page word for the footer.
371 $pdf->SetPageWord($i18n->getKey('label.page'));
373 // Set document information.
374 $pdf->SetCreator(PDF_CREATOR);
375 $pdf->SetAuthor('Anuko Time Tracker');
376 $pdf->SetTitle('Anuko Time Tracker Report');
377 $pdf->SetSubject('Anuko Time Tracker Report');
378 $pdf->SetKeywords('Anuko, time, tracker, report');
381 $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
382 $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
383 $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
385 // Set auto page breaks.
386 $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
388 // Set image scale factor.
389 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
394 // Set font (freeserif seems to work for all languages).
395 $pdf->SetFont('freeserif', '', 10); // helvetica here does not work for Russian.
398 $pdf->writeHTML($html, true, false, false, false, '');
400 // Close and output PDF document.
401 // $pdf->Output('timesheet.pdf', 'I'); // This will display inline in browser.
402 $pdf->Output('timesheet.pdf', 'D'); // D is for downloads.
404 // End of of TCPDF code.