2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
14 . add StartReport/EndReport handlers for better bulk report support, with
15 . email/printer destination option should be selected on class creation instead
17 . add/use setter function for Header2 parameters (currently passed globally)
18 . in report files pass already prepared options to SetCommonData() to avoid need for
19 selection inside FrontReport generic class.
21 include_once(dirname(__FILE__)."/class.pdf.inc");
22 include_once(dirname(__FILE__)."/printer_class.inc");
23 include_once($path_to_root . "/reporting/includes/reporting.inc");
24 include_once($path_to_root . "/admin/db/company_db.inc");
25 include_once($path_to_root . "/admin/db/fiscalyears_db.inc");
26 include_once($path_to_root . "/admin/db/printers_db.inc");
27 include_once($path_to_root . "/config.php");
29 class FrontReport extends Cpdf
60 var $companyLogoEnable; // select whether to use a company logo graphic in some header templates
62 var $footerEnable; // select whether to print a page footer or not
63 var $footerText; // store user-generated footer text
64 var $headerTmpl; // store the name of the currently selected header template
65 var $tmplSize; // pdf header template size in pages
68 var $formData; // common data used for printing headers footers etc.
69 var $contactData; // contact data for sending emials/reportlanguage selection
71 var $dest; // destination: email or printer
73 function __construct($title, $filename, $size = 'A4', $fontsize = 9, $orientation = 'P', $margins = NULL, $excelColWidthFactor = NULL)
75 global $page_security;
77 $this->rep_id = $_POST['REP_ID']; // FIXME
79 if (!$_SESSION["wa_current_user"]->can_access_page($page_security))
81 display_error(_("The security settings on your account do not permit you to print this report"));
85 // Page margins - if user-specified, use those. Otherwise, use defaults below.
88 $this->topMargin = $margins['top'];
89 $this->bottomMargin = $margins['bottom'];
90 $this->leftMargin = $margins['left'];
91 $this->rightMargin = $margins['right'];
93 // Page orientation - P: portrait, L: landscape
94 $orientation = strtoupper($orientation);
96 switch (strtoupper($size))
101 if ($orientation == 'P')
103 $this->pageWidth=595;
104 $this->pageHeight=842;
105 if (!isset($margins))
108 $this->bottomMargin=30;
109 $this->leftMargin=40;
110 $this->rightMargin=30;
116 $this->pageWidth=842;
117 $this->pageHeight=595;
118 if (!isset($margins))
121 $this->bottomMargin=30;
122 $this->leftMargin=40;
123 $this->rightMargin=30;
129 if ($orientation == 'P')
131 $this->pageWidth=842;
132 $this->pageHeight=1190;
133 if (!isset($margins))
136 $this->bottomMargin=50;
137 $this->leftMargin=50;
138 $this->rightMargin=40;
144 $this->pageWidth=1190;
145 $this->pageHeight=842;
146 if (!isset($margins))
149 $this->bottomMargin=50;
150 $this->leftMargin=50;
151 $this->rightMargin=40;
157 if ($orientation == 'P')
159 $this->pageWidth=612;
160 $this->pageHeight=792;
161 if (!isset($margins))
164 $this->bottomMargin=30;
165 $this->leftMargin=30;
166 $this->rightMargin=25;
172 $this->pageWidth=792;
173 $this->pageHeight=612;
174 if (!isset($margins))
177 $this->bottomMargin=30;
178 $this->leftMargin=30;
179 $this->rightMargin=25;
185 if ($orientation == 'P')
187 $this->pageWidth=612;
188 $this->pageHeight=1008;
189 if (!isset($margins))
192 $this->bottomMargin=40;
193 $this->leftMargin=30;
194 $this->rightMargin=25;
200 $this->pageWidth=1008;
201 $this->pageHeight=612;
202 if (!isset($margins))
205 $this->bottomMargin=40;
206 $this->leftMargin=30;
207 $this->rightMargin=25;
212 $this->size = array(0, 0, $this->pageWidth, $this->pageHeight);
213 $this->title = $title;
214 $this->filename = $filename.".pdf";
215 $this->pageNumber = 0;
216 $this->endLine = $this->pageWidth - $this->rightMargin;
217 $this->lineHeight = 12;
218 $this->fontSize = $fontsize;
219 $this->oldFontSize = 0;
220 $this->row = $this->pageHeight - $this->topMargin;
221 $this->currency = '';
222 $this->scaleLogoWidth = false; // if Logo, scale on width (else height).
223 $this->SetHeaderType('Header'); // default
225 parent::__construct($size, $_SESSION['language']->code, $orientation);
229 * Select the font and style to use for following output until
230 * it's changed again.
233 * * a special case string (for backwards compatible with older code):
236 * * or a case-insensitive string where each char represents a style choice
237 * and you can use more than one or none at all. Possible choices:
238 * * empty string: regular
242 * * D: line trough (aka "strike through")
243 * $fontname should be a standard PDF font (like 'times', 'helvetica' or 'courier')
244 * or one that's been installed on your system (see TCPDF docs for details).
245 * An empty string can also be used which will retain the font currently in use if
246 * you just want to change the style.
248 function Font($style = '', $fontname = '')
250 $this->selectFont($fontname, $style);
253 function Info($params, $cols, $headers, $aligns,
254 $cols2 = null, $headers2 = null, $aligns2 = null,
255 $companylogoenable = false, $footerenable = false, $footertext = '')
257 global $SysPrefs, $version;
259 $this->addInfo('Title', $this->title);
260 $this->addInfo('Subject', $this->title);
261 $this->addInfo('Author', $SysPrefs->app_title . ' ' . $version);
262 $this->addInfo('Creator',$SysPrefs->power_by . ' - ' . $SysPrefs->power_url);
263 $year = get_current_fiscalyear();
264 if ($year['closed'] == 0)
268 $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . " " . "(" . $how . ")";
269 $this->company = get_company_prefs();
270 $this->user = $_SESSION["wa_current_user"]->name;
271 $this->host = $_SERVER['SERVER_NAME'];
272 $this->params = $params;
274 for ($i = 0; $i < count($this->cols); $i++)
275 $this->cols[$i] += $this->leftMargin;
276 $this->headers = $headers;
277 $this->aligns = $aligns;
278 $this->cols2 = $cols2;
279 if ($this->cols2 != null)
281 for ($i = 0; $i < count($this->cols2); $i++)
282 $this->cols2[$i] += $this->leftMargin;
284 $this->headers2 = $headers2;
285 $this->aligns2 = $aligns2;
287 // Set whether to display company logo in some header templates
288 $this->companyLogoEnable = $companylogoenable;
290 // Store footer settings
291 $this->footerEnable = $footerenable;
292 $this->footerText = $footertext;
295 // Header for listings
301 $companyCol = $this->endLine - 150;
302 $titleCol = $this->leftMargin + 100;
304 $this->row = $this->pageHeight - $this->topMargin;
306 $this->SetDrawColor(128, 128, 128);
307 $this->Line($this->row + 5, 1);
311 $this->fontSize += 4;
313 $this->Text($this->leftMargin, $this->title, $companyCol);
315 $this->fontSize -= 4;
316 $logo = company_path() . "/images/" . $this->company['coy_logo'];
317 if (!empty($SysPrefs->prefs['company_logo_report']) && $this->company['coy_logo'] != '' && file_exists($logo))
319 $size = getimagesize($logo);
320 $height = $size[0] > 150 ? $size[1] * 150 / $size[0] : 30;
321 $this->row -= ($height / 2);
322 $this->AddImage($logo, $companyCol, $this->row, 0, $height);
327 $this->Text($companyCol, $this->company['coy_name']);
328 $this->row -= ($this->lineHeight + 4);
330 $str = _("Print Out Date") . ':';
331 $this->Text($this->leftMargin, $str, $titleCol);
332 $str = Today() . ' ' . Now();
333 if ($this->company['time_zone'])
334 $str .= ' ' . date('O') . ' GMT';
335 $this->Text($titleCol, $str, $companyCol);
336 $this->Text($companyCol, $this->host);
339 $str = _("Fiscal Year") . ':';
340 $this->Text($this->leftMargin, $str, $titleCol);
341 $str = $this->fiscal_year;
342 $this->Text($titleCol, $str, $companyCol);
343 $this->Text($companyCol, $this->user);
344 for ($i = 1; $i < count_array($this->params); $i++)
346 if ($this->params[$i]['from'] != '')
349 $str = $this->params[$i]['text'] . ':';
350 $this->Text($this->leftMargin, $str, $titleCol);
351 $str = $this->params[$i]['from'];
352 if ($this->params[$i]['to'] != '')
353 $str .= " - " . $this->params[$i]['to'];
354 $this->Text($titleCol, $str, $companyCol);
357 if ($this->params[0] != '') // Comments
360 $str = _("Comments") . ':';
361 $this->Text($this->leftMargin, $str, $titleCol);
363 $this->Text($titleCol, $this->params[0], $this->endLine - 35);
366 $str = _("Page") . ' ' . $this->pageNumber;
367 $this->Text($this->endLine - 38, $str);
368 $this->Line($this->row - 5, 1);
370 $this->row -= ($this->lineHeight + 6);
371 $this->Font('italic');
372 if ($this->headers2 != null)
374 $count = count_array($this->headers2);
375 for ($i = 0; $i < $count; $i++)
376 $this->TextCol2($i, $i + 1, $this->headers2[$i]);
379 $count = count_array($this->headers);
380 for ($i = 0; $i < $count; $i++)
381 $this->TextCol($i, $i + 1, $this->headers[$i]);
383 $this->Line($this->row - 5, 1);
390 function SetCommonData($myrow, $branch, $sales_order, $bankaccount, $doctype, $contacts)
392 $this->pageNumber = 0;
393 $this->formData = array();
395 'myrow' => array('ord_date', 'date_', 'tran_date',
396 'order_no','reference', 'id', 'trans_no', 'name', 'location_name',
397 'delivery_address', 'supp_name', 'address',
398 'DebtorName', 'supp_account_no', 'wo_ref', 'debtor_ref','type', 'trans_no',
399 'StockItemName', 'tax_id', 'order_', 'delivery_date', 'units_issued',
400 'due_date', 'required_by', 'payment_terms', 'curr_code',
401 'ov_freight', 'ov_gst', 'ov_amount', 'prepaid', 'requisition_no', 'contact'),
402 'branch' => array('br_address', 'br_name', 'salesman', 'disable_branch'),
403 'sales_order' => array('deliver_to', 'delivery_address', 'customer_ref'),
404 'bankaccount' => array('bank_name', 'bank_account_number', 'payment_service')
407 foreach($datnames as $var => $fields) {
409 foreach($fields as $locname) {
410 if (isset(${$var}[$locname]) && (${$var}[$locname]!==null)) {
411 $this->formData[$locname] = ${$var}[$locname];
416 $this->formData['doctype'] = $doctype;
417 $this->formData['document_amount'] = @$this->formData['ov_amount']+@$this->formData['ov_freight']+@$this->formData['ov_gst'];
418 if (count($contacts)) {
419 if (!is_array($contacts[0]))
420 $contacts = array($contacts); // change to array when single contact passed
421 $this->contactData = $contacts;
422 // as report is currently generated once despite number of email recipients
423 // we select language for the first recipient as report language
424 $this->formData['rep_lang'] = $contacts[0]['lang'];
430 function SetHeaderType($name) {
431 $this->headerTmpl = $name;
434 Header for sales/purchase documents
438 global $dflt_lang; // FIXME should be passed as params
440 $this->SetLang(@$this->formData['rep_lang'] ? $this->formData['rep_lang'] : $dflt_lang);
441 $doctype = $this->formData['doctype'];
444 $lang = user_language();
445 $this->SetLang(@$this->formData['rep_lang'] ? $this->formData['rep_lang']
446 : ( $lang ? $lang : $dflt_lang));
448 // leave layout files names without path to enable including
449 // modified versions from company/x/reporting directory
450 include("includes/doctext.inc");
451 include("includes/header2.inc");
456 // Alternate header style which also supports a simple footer
459 // Turn off cell padding for the main report header, restoring the current setting later
460 $oldcMargin = $this->cMargin;
461 $this->SetCellPadding(0);
463 // Set some constants which control header item layout
464 // only set them once or the PHP interpreter gets angry
465 if ($this->pageNumber == 1)
467 define('COMPANY_WIDTH', 150);
468 define('LOGO_HEIGHT', 50);
469 define('LOGO_Y_POS_ADJ_FACTOR', 0.74);
470 define('LABEL_WIDTH', 80);
471 define('PAGE_NUM_WIDTH', 60);
472 define('TITLE_FONT_SIZE', 14);
473 define('HEADER1_FONT_SIZE', 10);
474 define('HEADER2_FONT_SIZE', 9);
475 define('FOOTER_FONT_SIZE', 10);
476 define('FOOTER_MARGIN', 4);
478 // Set some variables which control header item layout
479 $companyCol = $this->endLine - COMPANY_WIDTH;
480 $headerFieldCol = $this->leftMargin + LABEL_WIDTH;
481 $pageNumCol = $this->endLine - PAGE_NUM_WIDTH;
482 $footerCol = $this->leftMargin + PAGE_NUM_WIDTH;
483 $footerRow = $this->bottomMargin - FOOTER_MARGIN;
485 $this->row = $this->pageHeight - $this->topMargin;
487 // Set the color of dividing lines we'll draw
488 $oldDrawColor = $this->GetDrawColor();
489 $this->SetDrawColor(128, 128, 128);
491 // Tell TCPDF that we want to use its alias system to track the total number of pages
492 $this->AliasNbPages();
495 if ($this->footerEnable)
497 $this->Line($footerRow, 1);
498 $prevFontSize = $this->fontSize;
499 $this->fontSize = FOOTER_FONT_SIZE;
500 $this->TextWrap($footerCol, $footerRow - ($this->fontSize + 1),
501 $pageNumCol - $footerCol, $this->footerText, $align = 'center',
502 $border = 0, $fill = 0, $link = NULL, $stretch = 1);
503 $this->TextWrap($pageNumCol, $footerRow - ($this->fontSize + 1),
504 PAGE_NUM_WIDTH, _("Page") . ' ' . $this->pageNumber . '/' . $this->getAliasNbPages(),
505 $align = 'right', $border = 0, $fill = 0, $link = NULL, $stretch = 1);
506 $this->fontSize = $prevFontSize;
513 // Print gray line across the page
514 $this->Line($this->row + 8, 1);
518 // Print the report title nice and big
519 $oldFontSize = $this->fontSize;
520 $this->fontSize = TITLE_FONT_SIZE;
522 $this->Text($this->leftMargin, $this->title, $companyCol);
523 $this->fontSize = HEADER1_FONT_SIZE;
525 // Print company logo if present and requested, or else just print company name
526 // Build a string specifying the location of the company logo file
527 $logo = company_path() . "/images/" . $this->company['coy_logo'];
528 if ($this->companyLogoEnable && ($this->company['coy_logo'] != '') && file_exists($logo))
530 // Width being zero means that the image will be scaled to the specified height
531 // keeping its aspect ratio intact.
532 if ($this->scaleLogoWidth)
533 $this->AddImage($logo, $companyCol, $this->row + 15, COMPANY_WIDTH, 0);
535 $this->AddImage($logo, $companyCol, $this->row - (LOGO_HEIGHT * LOGO_Y_POS_ADJ_FACTOR), 0, LOGO_HEIGHT);
538 $this->Text($companyCol, $this->company['coy_name']);
540 // Dimension 1 - optional
541 // - only print if available and not blank
542 if (count($this->params) > 3)
543 if ($this->params[3]['from'] != '')
545 $this->NewLine(1, 0, $this->fontSize + 2);
546 $str = $this->params[3]['text'] . ':';
547 $this->Text($this->leftMargin, $str, $headerFieldCol);
548 $str = $this->params[3]['from'];
549 $this->Text($headerFieldCol, $str, $companyCol);
552 // Dimension 2 - optional
553 // - only print if available and not blank
554 if (count($this->params) > 4)
555 if ($this->params[4]['from'] != '')
557 $this->NewLine(1, 0, $this->fontSize + 2);
558 $str = $this->params[4]['text'] . ':';
559 $this->Text($this->leftMargin, $str, $headerFieldCol);
560 $str = $this->params[4]['from'];
561 $this->Text($headerFieldCol, $str, $companyCol);
565 // if present, it's an array of tag names
566 if (count($this->params) > 5)
567 if ($this->params[5]['from'] != '')
569 $this->NewLine(1, 0, $this->fontSize + 2);
570 $str = $this->params[5]['text'] . ':';
571 $this->Text($this->leftMargin, $str, $headerFieldCol);
573 for ($i = 0; $i < count($this->params[5]['from']); $i++)
577 $str .= $this->params[5]['from'][$i];
579 $this->Text($headerFieldCol, $str, $companyCol);
582 // Report Date - time period covered
583 // - can specify a range, or just the end date (and the report contents
584 // should make it obvious what the beginning date is)
585 $this->NewLine(1, 0, $this->fontSize + 2);
586 $str = _("Report Period") . ':';
587 $this->Text($this->leftMargin, $str, $headerFieldCol);
589 if (isset($this->params[1]['from']) && $this->params[1]['from'] != '')
590 $str = $this->params[1]['from'] . ' - ';
591 $str .= $this->params[1]['to'];
592 $this->Text($headerFieldCol, $str, $companyCol);
597 $this->NewLine(1, 0, $this->fontSize + 1);
599 // Make the remaining report headings a little less important
600 $this->fontSize = HEADER2_FONT_SIZE;
602 // Timestamp of when this copy of the report was generated
603 $str = _("Generated At") . ':';
604 $this->Text($this->leftMargin, $str, $headerFieldCol);
605 $str = Today() . ' ' . Now();
606 if ($this->company['time_zone'])
607 $str .= ' ' . date('O') . ' GMT';
608 $this->Text($headerFieldCol, $str, $companyCol);
610 // Name of the user that generated this copy of the report
611 $this->NewLine(1, 0, $this->fontSize + 1);
612 $str = _("Generated By") . ':';
613 $this->Text($this->leftMargin, $str, $headerFieldCol);
615 $this->Text($headerFieldCol, $str, $companyCol);
617 // Display any user-generated comments for this copy of the report
618 if ($this->params[0] != '') // Comments
620 $this->NewLine(1, 0, $this->fontSize + 1);
621 $str = _("Comments") . ':';
622 $this->Text($this->leftMargin, $str, $headerFieldCol);
624 $this->Text($headerFieldCol, $this->params[0], $companyCol, 0, 0, 'left', 0, 0, $link=NULL, 1);
628 // Add page numbering to header if footer is turned off
629 if (!$this->footerEnable)
631 $str = _("Page") . ' ' . $this->pageNumber . '/' . $this->getAliasNbPages();
632 $this->Text($pageNumCol, $str, 0, 0, 0, 'right', 0, 0, NULL, 1);
635 // Print gray line across the page
636 $this->Line($this->row - 5, 1);
638 // Restore font size to user-defined size
639 $this->fontSize = $oldFontSize;
641 // restore user-specified cell padding for column headers
642 $this->SetCellPadding($oldcMargin);
644 // scoot down the page a bit
645 $oldLineHeight = $this->lineHeight;
646 $this->lineHeight = $this->fontSize + 1;
647 $this->row -= ($this->lineHeight + 6);
648 $this->lineHeight = $oldLineHeight;
650 // Print the column headers!
652 if ($this->headers2 != null)
654 $count = count($this->headers2);
655 for ($i = 0; $i < $count; $i++)
656 $this->TextCol2($i, $i + 1, $this->headers2[$i], $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=1);
659 $count = count($this->headers);
660 for ($i = 0; $i < $count; $i++)
661 $this->TextCol($i, $i + 1, $this->headers[$i], $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=1);
666 // restore user-specified draw color
667 $this->SetDrawColor($oldDrawColor[0], $oldDrawColor[1], $oldDrawColor[2]);
671 * Format a numeric string date into something nicer looking.
673 * @param string $date Date string to be formatted.
674 * @param int $input_format Format of the input string. Possible values are:<ul><li>0: user's default (default)</li></ul>
675 * @param int $output_format Format of the output string. Possible values are:<ul><li>0: Month (word) Day (numeric), 4-digit Year - Example: January 1, 2000 (default)</li><li>1: Month 4-digit Year - Example: January 2000</li><li>2: Month Abbreviation 4-digit Year - Example: Jan 2000</li></ul>
678 function DatePrettyPrint($date, $input_format = 0, $output_format = 0)
682 $date = date2sql($date);
683 $year = (int) (substr($date, 0, 4));
684 $month = (int) (substr($date, 5, 2));
685 $day = (int) (substr($date, 8, 2));
686 if ($output_format == 0)
687 return(date('F j, Y', mktime(12, 0, 0, $month, $day, $year)));
688 elseif ($output_format == 1)
689 return(date('F Y', mktime(12, 0, 0, $month, $day, $year)));
690 elseif ($output_format == 2)
691 return(date('M Y', mktime(12, 0, 0, $month, $day, $year)));
697 function AddImage($logo, $x, $y, $w, $h)
699 if (strpos($logo, ".png") || strpos($logo, ".PNG"))
700 $this->addPngFromFile($logo, $x, $y, $w, $h);
702 $this->addJpegFromFile($logo, $x, $y, $w, $h);
705 // Get current draw color setting from TCPDF object; returns array of RGB numbers
706 function GetDrawColor()
708 // Convert the TCPDF stored DrawColor string into an array of strings
709 $colorFields = explode(' ', $this->DrawColor);
711 // Test last value: G == grayscale, single number; RG == RGB, 3 numbers
712 if ($colorFields[count($colorFields) - 1] == 'G')
713 // Convert a grayscale string value to the equivalent RGB value
714 $drawColor = array((float) $colorFields[0], (float) $colorFields[0], (float) $colorFields[0]);
716 // Convert RGB string values to the a numeric array
717 $drawColor = array((float) $colorFields[0], (float) $colorFields[1], (float) $colorFields[2]);
722 // Get current cell padding setting from TCPDF object
723 function GetCellPadding()
725 return $this->cMargin;
728 // Set desired cell padding (aka "cell margin")
729 // Seems to be just left and right margins...
730 function SetCellPadding($pad)
732 parent::SetCellPadding($pad);
735 function Text($c, $txt, $n=0, $corr=0, $r=0, $align='left', $border=0, $fill=0, $link=NULL, $stretch=1)
738 $n = $this->pageWidth - $this->rightMargin;
740 return $this->TextWrap($c, $this->row - $r, $n - $c + $corr, $txt, $align, $border, $fill, $link, $stretch);
743 function TextWrap($xpos, $ypos, $len, $str, $align = 'left', $border = 0, $fill = 0, $link = NULL, $stretch = 1, $spacebreak=false)
745 $str = strtr($str, array("\r"=>''));
747 if ($this->fontSize != $this->oldFontSize)
749 $this->SetFontSize($this->fontSize);
750 $this->oldFontSize = $this->fontSize;
752 return $this->addTextWrap($xpos, $ypos, $len, $this->fontSize, $str, $align, $border, $fill, $link, $stretch, $spacebreak);
755 function TextCol($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=1)
757 return $this->TextWrap($this->cols[$c], $this->row - $r, $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c], $border, $fill, $link, $stretch);
760 function AmountCol($c, $n, $txt, $dec=0, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=1, $color_red=false)
762 if ($color_red && $txt < 0)
763 $this->SetTextColor(255, 0, 0);
764 $ret = $this->TextCol($c, $n, number_format2($txt, $dec), $corr, $r, $border, $fill, $link, $stretch);
765 if ($color_red && $txt < 0)
766 $this->SetTextColor(0, 0, 0);
770 function AmountCol2($c, $n, $txt, $dec=0, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=1, $color_red=false, $amount_locale = 'en_US.UTF-8', $amount_format = '%(!.2n')
772 setlocale(LC_MONETARY, $amount_locale);
773 if ($color_red && $txt < 0)
774 $this->SetTextColor(255, 0, 0);
775 $ret = $this->TextCol($c, $n, money_format($amount_format, $txt), $corr, $r, $border, $fill, $link, $stretch);
776 if ($color_red && $txt < 0)
777 $this->SetTextColor(0, 0, 0);
781 function DateCol($c, $n, $txt, $conv=false, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=1)
784 $txt = sql2date($txt);
785 return $this->TextCol($c, $n, $txt, $corr, $r, $border, $fill, $link, $stretch);
788 function TextCol2($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=1)
790 return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c], $border, $fill, $link, $stretch);
793 function TextColLines($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0)
796 $this->TextWrapLines($this->cols[$c], $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c], $border, $fill, $link, $stretch, true);
799 function TextWrapLines($c, $width, $txt, $align='left', $border=0, $fill=0, $link=NULL, $stretch=0, $spacebreak=true)
801 $str = explode("\n", $txt);
803 for ($i = 0; $i < count($str); $i++)
808 $l = $this->TextWrap($c, $this->row , $width, $l, $align, $border, $fill, $link, $stretch, $spacebreak);
809 $this->row -= $this->lineHeight;
816 * Expose the underlying calcTextWrap() function in this API.
818 function TextWrapCalc($txt, $width, $spacebreak=false)
820 return $this->calcTextWrap($txt, $width, $spacebreak);
824 * Sets the line drawing style.
826 * Takes an associative array as arg so you don't need to specify all values.
829 * width (float) - the thickness of the line in user units
830 * cap (string) - the type of cap to put on the line, values can be 'butt','round','square'
831 * where the diffference between 'square' and 'butt' is that 'square' projects a flat end past the
833 * join (string) - can be 'miter', 'round', 'bevel'
834 * dash (mixed) - Dash pattern. Is 0 (without dash) or string with series of length values, which are the
835 * lengths of the on and off dashes. For example: "2" represents 2 on, 2 off, 2 on, 2 off, ...;
836 * "2,1" is 2 on, 1 off, 2 on, 1 off, ...
837 * phase (integer) - a modifier on the dash pattern which is used to shift the point at which the pattern starts.
838 * color (array) - draw color. Format: array(GREY), or array(R,G,B) or array(C,M,Y,K).
840 function SetLineStyle($style)
842 parent::SetLineStyle($style);
846 * Sets the line drawing width.
848 function SetLineWidth($width)
850 parent::SetLineWidth($width);
853 function LineTo($from, $row, $to, $row2)
855 parent::line($from, $row, $to, $row2);
858 function Line($row, $height = 0, $dummy1=null, $dummy2=null, $dummy3=null)
860 $oldLineWidth = $this->GetLineWidth();
861 $this->SetLineWidth($height + 1);
862 parent::line($this->pageWidth - $this->rightMargin, $row ,$this->leftMargin, $row);
863 $this->SetLineWidth($oldLineWidth);
867 * Underlines the contents of a cell, but not the cell padding area.
868 * Primarily useful for the last line before a "totals" line.
869 * @param int $c Column number to underline.
870 * @param int $r Print the underline(s) this number of rows below the current position. Can be negative in order to go up.
871 * @param int $type Type of underlining to draw. Possible values are:<ul><li>1: single underline (default)</li><li>2: double underline</li></ul>
872 * @param int $linewidth Thickness of the line to draw. Default value of zero will use the current line width defined for this document.
873 * @param array $style Line style. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array).
875 * @see SetLineWidth(), SetDrawColor(), SetLineStyle()
877 function UnderlineCell($c, $r = 0, $type = 1, $linewidth = 0, $style = array())
879 // If line width was specified, save current setting so we can reset it
882 $oldLineWidth = $this->GetLineWidth();
883 $this->SetLineWidth($linewidth);
886 // Figure out how far down to move the line based on current font size
887 // Calculate this because printing underline directly at $this->row goes on top
888 // of the parts of characters that "hang down", like the bottom of commas &
889 // lowercase letter 'g', etc.
890 if ($this->fontSize < 10)
894 parent::line($this->cols[$c] + $this->cMargin, $this->row - $r - $y_adj, $this->cols[$c + 1] - $this->cMargin, $this->row - $r - $y_adj, $style);
896 // Double underline, far enough below the first underline so as not to overlap
897 // the first underline (depends on current line thickness (aka "line width")
899 parent::line($this->cols[$c] + $this->cMargin, $this->row - $r - $y_adj - ($this->GetLineWidth() + 2), $this->cols[$c + 1] - $this->cMargin, $this->row - $r - $y_adj - ($this->GetLineWidth() + 2), $style);
901 // If line width was specified, reset it back to the original setting
903 $this->SetLineWidth($oldLineWidth);
906 function NewLine($l=1, $np=0, $h = NULL)
908 // If the line height wasn't specified, use the current setting
910 $h = $this->lineHeight;
912 // Move one line down the page
913 $this->row -= ($l * $h);
915 // Check to see if we're at the bottom and should insert a page break
916 if ($this->row < $this->bottomMargin + ($np * $h))
922 if ($this->pageNumber==0)
924 // check if there is pdf header template for this report
925 // and set if it is found
926 $tmpl_pdf = find_custom_file("/reporting/forms/".$this->headerTmpl.".pdf");
928 $this->tmplSize = $this->setSourceFile($tmpl_pdf);
935 if ($this->tmplSize) {
936 $this->row = $this->pageHeight - $this->topMargin; // reset row
937 $id = $this->importPage(min($this->pageNumber, $this->tmplSize));
938 $this->useTemplate($id);
941 // include related php file if any
942 $tmpl_php = find_custom_file("/reporting/forms/".$this->headerTmpl.".php");
947 if (method_exists($this, $this->headerTmpl)) // draw predefined page layout if any
948 $this->{$this->headerTmpl}();
951 function End($email=0, $subject='')
953 global $SysPrefs, $path_to_root;
955 if (!empty($SysPrefs->prefs['print_dialog_direct']))
956 $this->includeJS("print();"); // force to open print dialog
958 if ($SysPrefs->pdf_debug == 1)
960 $pdfcode = $this->Output('','S');
961 $pdfcode = str_replace("\n", "\n<br>", html_specials_encode($pdfcode));
964 echo '</body></html>';
968 $dir = company_path(). '/pdf_files';
970 if (!file_exists($dir))
974 // do not use standard filenames or your sensitive company data
975 // are world readable
976 $fname = $dir.'/'.random_id().'.pdf';
977 $this->Output($fname, 'F');
981 $contactData = array();
982 if ($this->contactData)
983 foreach($this->contactData as $contact)
984 if (!empty($contact['email']))
985 $contactData[] = $contact;
987 if(!count($contactData)) {
988 $this->SetLang(user_language());
989 display_warning(sprintf(_("You have no email contact defined for this type of document for '%s'."), $this->formData['recipient_name']));
994 $subject = $this->formData['document_name'] . ' '. $this->formData['document_number'];
995 foreach($contactData as $contact) {
996 if (!isset($contact['email']))
999 $this->SetLang($contact['lang']);
1001 require_once($path_to_root . "/reporting/includes/class.mail.inc");
1002 $mail = new email(str_replace(",", "", $this->company['coy_name']),
1003 $this->company['email']);
1004 $mail->charset = $this->encoding;
1006 $to = str_replace(",", "", $contact['name'].' '.$contact['name2'])
1007 ." <" . $contact['email'] . ">";
1008 $msg = _("Dear") . " " . $contact['name2'] . ",\n\n"
1009 . _("Attached you will find ") . " " . $subject ."\n\n";
1011 if (isset($this->formData['payment_service']))
1013 $amt = number_format($this->formData['document_amount'], user_price_dec());
1014 $service = $this->formData['payment_service'];
1015 $url = payment_link($service, array(
1016 'company_email' => $this->company['email'],
1018 'currency' => $this->formData['curr_code'],
1019 'comment' => $this->title . " " . $this->formData['document_number']
1022 $msg.= _("You can pay through"). " $service: $url\n\n";
1025 $msg .= _("Kindest regards") . "\n\n";
1026 $sender = $this->user . "\n" . $this->company['coy_name'] . "\n" . $this->company['postal_address'] . "\n" . $this->company['email'] . "\n" . $this->company['phone'];
1027 $mail->to($to); $try++;
1028 $mail->subject($subject);
1029 $mail->text($msg . $sender);
1030 $mail->attachment($fname, $this->filename);
1031 $emails .= " " . $contact['email'];
1032 if ($mail->send()) $sent++;
1033 } // foreach contact
1035 $this->SetLang(user_language());
1037 display_warning(sprintf(_("You have no email contact defined for this type of document for '%s'."), $this->formData['recipient_name']));
1039 display_warning($this->title . " " . $this->formData['document_number'] . ". "
1040 . _("Sending document by email failed") . ". " . _("Email:") . $emails);
1042 display_notification($this->title . " " . $this->formData['document_number'] . " "
1043 . _("has been sent by email to destination.") . " " . _("Email:") . $emails);
1048 $printer = get_report_printer(user_print_profile(), $this->rep_id);
1049 if ($printer == false) {
1053 if (user_rep_popup())
1054 $Ajax->popup($fname); // when embeded pdf viewer used
1056 $Ajax->redirect($fname); // otherwise use faster method
1058 header('Content-type: application/pdf');
1059 header('Content-Disposition: inline; filename='.$this->filename);
1060 header('Expires: 0');
1061 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
1062 header('Pragma: public');
1063 $this->Stream($this->filename);
1065 } else { // send report to network printer
1066 $prn = new remote_printer($printer['queue'],$printer['host'],
1067 $printer['port'], $printer['timeout']);
1068 $error = $prn->print_file($fname);
1070 display_error($error);
1072 display_notification(_('Report has been sent to network printer ').$printer['name']);
1075 // first have a look through the directory,
1076 // and remove old temporary pdfs
1077 if ($d = @opendir($dir)) {
1078 while (($file = readdir($d)) !== false) {
1079 if (!is_file($dir.'/'.$file) || $file == 'index.php') continue;
1080 // then check to see if this one is too old
1081 $ftime = filemtime($dir.'/'.$file);
1082 // seems 3 min is enough for any report download, isn't it?
1083 if (time()-$ftime > 180){
1084 unlink($dir.'/'.$file);