X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;f=reporting%2Fincludes%2Fpdf_report.inc;h=13896dc8b2cc3888eea8c1484b1720649434fd26;hb=49d2702ed98abe7a564a4abb4c8c5fa225f362e8;hp=b0277f1eda17de64b9bdd71c384b8ce9c0e60598;hpb=6e6624033d50ab821261cc9c25f49d3e43eeebec;p=fa-stable.git diff --git a/reporting/includes/pdf_report.inc b/reporting/includes/pdf_report.inc index b0277f1e..13896dc8 100644 --- a/reporting/includes/pdf_report.inc +++ b/reporting/includes/pdf_report.inc @@ -13,6 +13,7 @@ include_once(dirname(__FILE__)."/class.pdf.inc"); include_once(dirname(__FILE__)."/printer_class.inc"); include_once($path_to_root . "/admin/db/company_db.inc"); +include_once($path_to_root . "/admin/db/fiscalyears_db.inc"); include_once($path_to_root . "/admin/db/printers_db.inc"); include_once($path_to_root . "/config.php"); class FrontReport extends Cpdf @@ -41,14 +42,17 @@ class FrontReport extends Cpdf var $headers2; var $aligns2; var $cols2; - var $companyCol; - var $titleCol; var $pageNumber; var $fontSize; var $oldFontSize; var $currency; + var $companyLogoEnable; // select whether to use a company logo graphic in some header templates + var $scaleLogoWidth; + var $footerEnable; // select whether to print a page footer or not + var $footerText; // store user-generated footer text + var $headerFunc; // store the name of the currently selected header function - function FrontReport($title, $filename, $size = 'A4', $fontsize = 9) + function FrontReport($title, $filename, $size = 'A4', $fontsize = 9, $orientation = 'P', $margins = NULL, $excelColWidthFactor = NULL) { global $page_security; if (!$_SESSION["wa_current_user"]->can_access_page($page_security)) @@ -57,74 +61,131 @@ class FrontReport extends Cpdf end_page(); exit; } - switch ($size) + // Page margins - if user-specified, use those. Otherwise, use defaults below. + if (isset($margins)) + { + $this->topMargin = $margins['top']; + $this->bottomMargin = $margins['bottom']; + $this->leftMargin = $margins['left']; + $this->rightMargin = $margins['right']; + } + // Page orientation - P: portrait, L: landscape + $orientation = strtoupper($orientation); + // Page size name + switch (strtoupper($size)) { default: case 'A4': - case 'a4': - $this->pageWidth=595; - $this->pageHeight=842; - $this->topMargin=40; - $this->bottomMargin=30; - $this->leftMargin=40; - $this->rightMargin=30; - break; - case 'A4_Landscape': - $this->pageWidth=842; - $this->pageHeight=595; - $this->topMargin=30; - $this->bottomMargin=30; - $this->leftMargin=40; - $this->rightMargin=30; + // Portrait + if ($orientation == 'P') + { + $this->pageWidth=595; + $this->pageHeight=842; + if (!isset($margins)) + { + $this->topMargin=40; + $this->bottomMargin=30; + $this->leftMargin=40; + $this->rightMargin=30; + } + } + // Landscape + else + { + $this->pageWidth=842; + $this->pageHeight=595; + if (!isset($margins)) + { + $this->topMargin=30; + $this->bottomMargin=30; + $this->leftMargin=40; + $this->rightMargin=30; + } + } break; case 'A3': - $this->pageWidth=842; - $this->pageHeight=1190; - $this->topMargin=50; - $this->bottomMargin=50; - $this->leftMargin=50; - $this->rightMargin=40; - break; - case 'A3_landscape': - $this->pageWidth=1190; - $this->pageHeight=842; - $this->topMargin=50; - $this->bottomMargin=50; - $this->leftMargin=50; - $this->rightMargin=40; + // Portrait + if ($orientation == 'P') + { + $this->pageWidth=842; + $this->pageHeight=1190; + if (!isset($margins)) + { + $this->topMargin=50; + $this->bottomMargin=50; + $this->leftMargin=50; + $this->rightMargin=40; + } + } + // Landscape + else + { + $this->pageWidth=1190; + $this->pageHeight=842; + if (!isset($margins)) + { + $this->topMargin=50; + $this->bottomMargin=50; + $this->leftMargin=50; + $this->rightMargin=40; + } + } break; - case 'letter': - case 'Letter': - $this->pageWidth=612; - $this->pageHeight=792; - $this->topMargin=30; - $this->bottomMargin=30; - $this->leftMargin=30; - $this->rightMargin=25; + case 'LETTER': + // Portrait + if ($orientation == 'P') + { + $this->pageWidth=612; + $this->pageHeight=792; + if (!isset($margins)) + { + $this->topMargin=30; + $this->bottomMargin=30; + $this->leftMargin=30; + $this->rightMargin=25; + } + } + // Landscape + else + { + $this->pageWidth=792; + $this->pageHeight=612; + if (!isset($margins)) + { + $this->topMargin=30; + $this->bottomMargin=30; + $this->leftMargin=30; + $this->rightMargin=25; + } + } break; - case 'letter_landscape': - $this->pageWidth=792; - $this->pageHeight=612; - $this->topMargin=30; - $this->bottomMargin=30; - $this->leftMargin=30; - $this->rightMargin=25; - break; - case 'legal': - $this->pageWidth=612; - $this->pageHeight=1008; - $this->topMargin=50; - $this->bottomMargin=40; - $this->leftMargin=30; - $this->rightMargin=25; - break; - case 'legal_landscape': - $this->pageWidth=1008; - $this->pageHeight=612; - $this->topMargin=50; - $this->bottomMargin=40; - $this->leftMargin=30; - $this->rightMargin=25; + case 'LEGAL': + // Portrait + if ($orientation == 'P') + { + $this->pageWidth=612; + $this->pageHeight=1008; + if (!isset($margins)) + { + $this->topMargin=50; + $this->bottomMargin=40; + $this->leftMargin=30; + $this->rightMargin=25; + } + } + // Landscape + else + { + $this->pageWidth=1008; + $this->pageHeight=612; + if (!isset($margins)) + { + $this->topMargin=50; + $this->bottomMargin=40; + $this->leftMargin=30; + $this->rightMargin=25; + } + } break; } $this->size = array(0, 0, $this->pageWidth, $this->pageHeight); @@ -132,28 +193,49 @@ class FrontReport extends Cpdf $this->filename = $filename.".pdf"; $this->pageNumber = 0; $this->endLine = $this->pageWidth - $this->rightMargin; - $this->companyCol = $this->endLine - 150; - $this->titleCol = $this->leftMargin + 100; $this->lineHeight = 12; $this->fontSize = $fontsize; $this->oldFontSize = 0; $this->row = $this->pageHeight - $this->topMargin; $this->currency = ''; + $this->scaleLogoWidth = false; // if Logo, scale on width (else height). + $this->headerFunc = 'Header'; // default to the original header template $rtl = ($_SESSION['language']->dir === 'rtl' ? 'rtl' : 'ltr'); $code = $_SESSION['language']->code; $enc = strtoupper($_SESSION['language']->encoding); // for the language array in class.pdf.inc $l = array('a_meta_charset' => $enc, 'a_meta_dir' => $rtl, 'a_meta_language' => $code, 'w_page' => 'page'); - $this->Cpdf($size, $l); + $this->Cpdf($size, $l, $orientation); } - function Font($style = 'normal') + /* + * Select the font and style to use for following output until + * it's changed again. + * + * $style is either: + * * a special case string (for backwards compatible with older code): + * * bold + * * italic + * * or a case-insensitive string where each char represents a style choice + * and you can use more than one or none at all. Possible choices: + * * empty string: regular + * * B: bold + * * I: italic + * * U: underline + * * D: line trough (aka "strike through") + * $fontname should be a standard PDF font (like 'times', 'helvetica' or 'courier') + * or one that's been installed on your system (see TCPDF docs for details). + * An empty string can also be used which will retain the font currently in use if + * you just want to change the style. + */ + function Font($style = '', $fontname = '') { - $this->selectFont("", $style); + $this->selectFont($fontname, $style); } function Info($params, $cols, $headers, $aligns, - $cols2 = null, $headers2 = null, $aligns2 = null) + $cols2 = null, $headers2 = null, $aligns2 = null, + $companylogoenable = false, $footerenable = false, $footertext = '') { global $app_title, $version, $power_by, $power_url; @@ -184,10 +266,20 @@ class FrontReport extends Cpdf } $this->headers2 = $headers2; $this->aligns2 = $aligns2; + + // Set whether to display company logo in some header templates + $this->companyLogoEnable = $companylogoenable; + + // Store footer settings + $this->footerEnable = $footerenable; + $this->footerText = $footertext; } function Header() { + $companyCol = $this->endLine - 150; + $titleCol = $this->leftMargin + 100; + $this->pageNumber++; if ($this->pageNumber > 1) $this->newPage(); @@ -200,46 +292,46 @@ class FrontReport extends Cpdf $this->fontSize += 4; $this->Font('bold'); - $this->Text($this->leftMargin, $this->title, $this->companyCol); + $this->Text($this->leftMargin, $this->title, $companyCol); $this->Font(); $this->fontSize -= 4; - $this->Text($this->companyCol, $this->company['coy_name']); + $this->Text($companyCol, $this->company['coy_name']); $this->row -= ($this->lineHeight + 4); $str = _("Print Out Date") . ':'; - $this->Text($this->leftMargin, $str, $this->titleCol); + $this->Text($this->leftMargin, $str, $titleCol); $str = Today() . ' ' . Now(); if ($this->company['time_zone']) $str .= ' ' . date('O') . ' GMT'; - $this->Text($this->titleCol, $str, $this->companyCol); - $this->Text($this->companyCol, $this->host); + $this->Text($titleCol, $str, $companyCol); + $this->Text($companyCol, $this->host); $this->NewLine(); $str = _("Fiscal Year") . ':'; - $this->Text($this->leftMargin, $str, $this->titleCol); + $this->Text($this->leftMargin, $str, $titleCol); $str = $this->fiscal_year; - $this->Text($this->titleCol, $str, $this->companyCol); - $this->Text($this->companyCol, $this->user); + $this->Text($titleCol, $str, $companyCol); + $this->Text($companyCol, $this->user); for ($i = 1; $i < count($this->params); $i++) { if ($this->params[$i]['from'] != '') { $this->NewLine(); $str = $this->params[$i]['text'] . ':'; - $this->Text($this->leftMargin, $str, $this->titleCol); + $this->Text($this->leftMargin, $str, $titleCol); $str = $this->params[$i]['from']; if ($this->params[$i]['to'] != '') $str .= " - " . $this->params[$i]['to']; - $this->Text($this->titleCol, $str, $this->companyCol); + $this->Text($titleCol, $str, $companyCol); } } if ($this->params[0] != '') // Comments { $this->NewLine(); $str = _("Comments") . ':'; - $this->Text($this->leftMargin, $str, $this->titleCol); + $this->Text($this->leftMargin, $str, $titleCol); $this->Font('bold'); - $this->Text($this->titleCol, $this->params[0], $this->endLine - 35); + $this->Text($titleCol, $this->params[0], $this->endLine - 35); $this->Font(); } $str = _("Page") . ' ' . $this->pageNumber; @@ -266,7 +358,7 @@ class FrontReport extends Cpdf function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype) { - global $comp_path, $path_to_root, $print_as_quote, $print_invoice_no; + global $comp_path, $path_to_root, $print_as_quote, $print_invoice_no, $packing_slip; $this->pageNumber++; if ($this->pageNumber > 1) @@ -286,6 +378,265 @@ class FrontReport extends Cpdf $this->row = $temp; } + // Alternate header style which also supports a simple footer + function Header3() + { + global $comp_path; + + // Make this header the default for the current report ( used by NewLine() ) + $this->headerFunc = 'Header3'; + + // Turn off cell padding for the main report header, restoring the current setting later + $oldcMargin = $this->cMargin; + $this->SetCellPadding(0); + + // Set some constants which control header item layout + // only set them once or the PHP interpreter gets angry + if ($this->pageNumber == 0) + { + define('COMPANY_WIDTH', 150); + define('LOGO_HEIGHT', 50); + define('LOGO_Y_POS_ADJ_FACTOR', 0.74); + define('LABEL_WIDTH', 80); + define('PAGE_NUM_WIDTH', 60); + define('TITLE_FONT_SIZE', 14); + define('HEADER1_FONT_SIZE', 10); + define('HEADER2_FONT_SIZE', 9); + define('FOOTER_FONT_SIZE', 10); + define('FOOTER_MARGIN', 4); + } + // Set some variables which control header item layout + $companyCol = $this->endLine - COMPANY_WIDTH; + $headerFieldCol = $this->leftMargin + LABEL_WIDTH; + $pageNumCol = $this->endLine - PAGE_NUM_WIDTH; + $footerCol = $this->leftMargin + PAGE_NUM_WIDTH; + $footerRow = $this->bottomMargin - FOOTER_MARGIN; + + // Calling this function generates a new PDF page after the first instance + $this->pageNumber++; + if ($this->pageNumber > 1) + { +// // TODO: experimenting with line drawing to highlight current period +// $this->SetLineWidth(1); +// $this->LineTo($this->cols[3], 33, $this->cols[3], 534); +// $this->LineTo($this->cols[4], 33, $this->cols[4], 534); +// $this->SetLineWidth(0.1); + + $this->newPage(); + } + $this->row = $this->pageHeight - $this->topMargin; + + // Set the color of dividing lines we'll draw + $oldDrawColor = $this->GetDrawColor(); + $this->SetDrawColor(128, 128, 128); + + // Tell TCPDF that we want to use its alias system to track the total number of pages + $this->AliasNbPages(); + + // Footer + if ($this->footerEnable) + { + $this->Line($footerRow, 1); + $prevFontSize = $this->fontSize; + $this->fontSize = FOOTER_FONT_SIZE; + $this->TextWrap($footerCol, $footerRow - ($this->fontSize + 1), + $pageNumCol - $footerCol, $this->footerText, $align = 'center', + $border = 0, $fill = 0, $link = NULL, $stretch = 1); + $this->TextWrap($pageNumCol, $footerRow - ($this->fontSize + 1), + PAGE_NUM_WIDTH, _("Page") . ' ' . $this->pageNumber . '/' . $this->getAliasNbPages(), + $align = 'right', $border = 0, $fill = 0, $link = NULL, $stretch = 1); + $this->fontSize = $prevFontSize; + } + + // + // Header + // + + // Print gray line across the page + $this->Line($this->row + 8, 1); + + $this->NewLine(); + + // Print the report title nice and big + $oldFontSize = $this->fontSize; + $this->fontSize = TITLE_FONT_SIZE; + $this->Font('B'); + $this->Text($this->leftMargin, $this->title, $companyCol); + $this->fontSize = HEADER1_FONT_SIZE; + + // Print company logo if present and requested, or else just print company name + if ($this->companyLogoEnable && ($this->company['coy_logo'] != '')) + { + // Build a string specifying the location of the company logo file + $logo = $comp_path .'/'. user_company() . "/images/" . $this->company['coy_logo']; + + // Width being zero means that the image will be scaled to the specified height + // keeping its aspect ratio intact. + if ($this->scaleLogoWidth) + $this->AddImage($logo, $companyCol, $this->row, COMPANY_WIDTH, 0); + else + $this->AddImage($logo, $companyCol, $this->row - (LOGO_HEIGHT * LOGO_Y_POS_ADJ_FACTOR), 0, LOGO_HEIGHT); + } + else + $this->Text($companyCol, $this->company['coy_name']); + + // Dimension 1 - optional + // - only print if available and not blank + if (count($this->params) > 3) + if ($this->params[3]['from'] != '') + { + $this->NewLine(1, 0, $this->fontSize + 2); + $str = $this->params[3]['text'] . ':'; + $this->Text($this->leftMargin, $str, $headerFieldCol); + $str = $this->params[3]['from']; + $this->Text($headerFieldCol, $str, $companyCol); + } + + // Dimension 2 - optional + // - only print if available and not blank + if (count($this->params) > 4) + if ($this->params[4]['from'] != '') + { + $this->NewLine(1, 0, $this->fontSize + 2); + $str = $this->params[4]['text'] . ':'; + $this->Text($this->leftMargin, $str, $headerFieldCol); + $str = $this->params[4]['from']; + $this->Text($headerFieldCol, $str, $companyCol); + } + + // Tags - optional + // if present, it's an array of tag names + if (count($this->params) > 5) + if ($this->params[5]['from'] != '') + { + $this->NewLine(1, 0, $this->fontSize + 2); + $str = $this->params[5]['text'] . ':'; + $this->Text($this->leftMargin, $str, $headerFieldCol); + $str = ''; + for ($i = 0; $i < count($this->params[5]['from']); $i++) + { + if($i != 0) + $str .= ', '; + $str .= $this->params[5]['from'][$i]; + } + $this->Text($headerFieldCol, $str, $companyCol); + } + + // Report Date - time period covered + // - can specify a range, or just the end date (and the report contents + // should make it obvious what the beginning date is) + $this->NewLine(1, 0, $this->fontSize + 2); + $str = _("Report Period") . ':'; + $this->Text($this->leftMargin, $str, $headerFieldCol); + $str = ''; + if (isset($this->params[1]['from']) && $this->params[1]['from'] != '') + $str = $this->params[1]['from'] . ' - '; + $str .= $this->params[1]['to']; + $this->Text($headerFieldCol, $str, $companyCol); + + // Turn off Bold + $this->Font(); + + $this->NewLine(1, 0, $this->fontSize + 1); + + // Make the remaining report headings a little less important + $this->fontSize = HEADER2_FONT_SIZE; + + // Timestamp of when this copy of the report was generated + $str = _("Generated At") . ':'; + $this->Text($this->leftMargin, $str, $headerFieldCol); + $str = Today() . ' ' . Now(); + if ($this->company['time_zone']) + $str .= ' ' . date('O') . ' GMT'; + $this->Text($headerFieldCol, $str, $companyCol); + + // Name of the user that generated this copy of the report + $this->NewLine(1, 0, $this->fontSize + 1); + $str = _("Generated By") . ':'; + $this->Text($this->leftMargin, $str, $headerFieldCol); + $str = $this->user; + $this->Text($headerFieldCol, $str, $companyCol); + + // Display any user-generated comments for this copy of the report + if ($this->params[0] != '') // Comments + { + $this->NewLine(1, 0, $this->fontSize + 1); + $str = _("Comments") . ':'; + $this->Text($this->leftMargin, $str, $headerFieldCol); + $this->Font('B'); + $this->Text($headerFieldCol, $this->params[0], $companyCol, 0, 0, 'left', 0, 0, $link=NULL, 1); + $this->Font(); + } + + // Add page numbering to header if footer is turned off + if (!$this->footerEnable) + { + $str = _("Page") . ' ' . $this->pageNumber . '/' . $this->getAliasNbPages(); + $this->Text($pageNumCol, $str, 0, 0, 0, 'right', 0, 0, NULL, 1); + } + + // Print gray line across the page + $this->Line($this->row - 5, 1); + + // Restore font size to user-defined size + $this->fontSize = $oldFontSize; + + // restore user-specified cell padding for column headers + $this->SetCellPadding($oldcMargin); + + // scoot down the page a bit + $oldLineHeight = $this->lineHeight; + $this->lineHeight = $this->fontSize + 1; + $this->row -= ($this->lineHeight + 6); + $this->lineHeight = $oldLineHeight; + + // Print the column headers! + $this->Font('I'); + if ($this->headers2 != null) + { + $count = count($this->headers2); + for ($i = 0; $i < $count; $i++) + $this->TextCol2($i, $i + 1, $this->headers2[$i], $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=1); + $this->NewLine(); + } + $count = count($this->headers); + for ($i = 0; $i < $count; $i++) + $this->TextCol($i, $i + 1, $this->headers[$i], $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=1); + $this->Font(); + + $this->NewLine(2); + + // restore user-specified draw color + $this->SetDrawColor($oldDrawColor[0], $oldDrawColor[1], $oldDrawColor[2]); + } + + /** + * Format a numeric string date into something nicer looking. + * + * @param string $date Date string to be formatted. + * @param int $input_format Format of the input string. Possible values are: