X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;f=reporting%2Fincludes%2Fpdf_report.inc;h=05942ecaeb22565450128e320b35ad707b40fa10;hb=398ad92b6dcb1e6c04dd6011906ff73e9fe17e13;hp=c72fce927ca2e02e30fd9544958b0ec4e094d304;hpb=d6717a19307620f2a2a209f7134064efc1d931b0;p=fa-stable.git diff --git a/reporting/includes/pdf_report.inc b/reporting/includes/pdf_report.inc index c72fce92..05942eca 100644 --- a/reporting/includes/pdf_report.inc +++ b/reporting/includes/pdf_report.inc @@ -1,9 +1,20 @@ . +***********************************************************************/ +//include_once($path_to_root . "reporting/includes/class.pdf.inc"); +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/printers_db.inc"); +include_once($path_to_root . "/config.php"); class FrontReport extends Cpdf { var $size; @@ -21,7 +32,7 @@ class FrontReport extends Cpdf var $rightMargin; var $endLine; var $lineHeight; - var $rtl; + //var $rtl; var $cols; var $params; @@ -30,132 +41,202 @@ 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) { - switch ($size) + global $page_security; + if (!$_SESSION["wa_current_user"]->can_access_page($page_security)) { - + display_error(_("The security settings on your account do not permit you to print this report")); + end_page(); + exit; + } + // 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; + // 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 'A3_landscape': - $this->pageWidth=1190; - $this->pageHeight=842; - $this->topMargin=50; - $this->bottomMargin=50; - $this->leftMargin=50; - $this->rightMargin=40; + 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': - case 'Letter': - $this->pageWidth=612; - $this->pageHeight=792; - $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); $this->title = $title; - $this->filename = $filename; + $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->rtl = ($_SESSION['language']->dir == 'rtl'); - // for quick testing - //$this->rtl = true; - $this->Cpdf($this->size); + $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, $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 = '') { - global $path_to_root; - $own = false; - $locale = $path_to_root . "lang/" . $_SESSION['language']->code . "/locale.inc"; - if (file_exists($locale)) - { - $fontinclude = true; - include($locale); - } - //if ($this->rtl) // this is good for presentation only - if (!$own) - { - if (user_price_dec() == 3) // only for presentation of farsi - { - $this->rtl = true; - $path = $path_to_root . 'reporting/fonts/farsi_1.afm'; - } - elseif ($style == 'italic') - $path = $path_to_root . 'reporting/fonts/Helvetica-Oblique.afm'; - elseif ($style == 'bold') - $path = $path_to_root . 'reporting/fonts/Helvetica-Bold.afm'; - else // even take misspelled styles - $path = $path_to_root . 'reporting/fonts/Helvetica.afm'; - } - $this->selectFont($path, 'WinAnsiEncoding'); + $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, $path_to_root, $db_connections; + global $app_title, $version, $power_by, $power_url; $this->addinfo('Title', $this->title); $this->addinfo('Subject', $this->title); @@ -169,7 +250,6 @@ class FrontReport extends Cpdf $this->fiscal_year = sql2date($year['begin']) . " - " . sql2date($year['end']) . " " . "(" . $how . ")"; $this->company = get_company_prefs(); $this->user = $_SESSION["wa_current_user"]->name; - //$this->host = $db_connections[$_SESSION["wa_current_user"]->company]["host"]; $this->host = $_SERVER['SERVER_NAME']; $this->params = $params; $this->cols = $cols; @@ -185,10 +265,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(); @@ -201,48 +291,50 @@ 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(); - $this->Text($this->titleCol, $str, $this->companyCol); - $this->Text($this->companyCol, $this->host); + if ($this->company['time_zone']) + $str .= ' ' . date('O') . ' GMT'; + $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; - $this->Text($this->endLine - 35, $str); + $this->Text($this->endLine - 38, $str); $this->Line($this->row - 5, 1); $this->row -= ($this->lineHeight + 6); @@ -265,351 +357,522 @@ class FrontReport extends Cpdf function Header2($myrow, $branch, $sales_order, $bankaccount, $doctype) { - global $path_to_root, $print_as_quote; - + global $comp_path, $path_to_root, $print_as_quote, $print_invoice_no, $packing_slip; + $this->pageNumber++; if ($this->pageNumber > 1) $this->newPage(); $header2type = true; - if ($this->currency != $myrow['curr_code']) + if (isset($myrow['curr_code']) && $this->currency != $myrow['curr_code']) { - include($path_to_root . "reporting/includes/doctext2.inc"); - } + include($path_to_root . "/reporting/includes/doctext2.inc"); + } else { - include($path_to_root . "reporting/includes/doctext.inc"); - } - $this->row = $this->pageHeight - $this->topMargin; - - $upper = $this->row - 2 * $this->lineHeight; - $lower = $this->bottomMargin + 6 * $this->lineHeight; - $iline1 = $upper - 4 * $this->lineHeight; - $iline2 = $iline1 - 8 * $this->lineHeight; - $iline3 = $iline2 - 8 * $this->lineHeight; - $iline4 = $iline3 - 2 * $this->lineHeight; - $iline5 = $lower + 5 * $this->lineHeight; - $icol = $this->pageWidth / 2; - $ccol = $this->cols[0] + 4; - $ccol2 = $icol / 2; - $mcol = $icol + 8; - $mcol2 = $this->pageWidth - $ccol2; + include($path_to_root . "/reporting/includes/doctext.inc"); + } - $this->SetDrawColor(128, 128, 128); - $this->LineTo($this->pageWidth - $this->rightMargin, $upper ,$this->leftMargin, $upper); - $this->LineTo($this->leftMargin, $upper ,$this->leftMargin, $lower); - $this->LineTo($this->pageWidth - $this->rightMargin, $lower ,$this->leftMargin, $lower); - $this->LineTo($this->pageWidth - $this->rightMargin, $lower ,$this->pageWidth - $this->rightMargin, $upper); - $this->Line($iline1); - $this->Line($iline2); - $this->Line($iline3); - $this->Line($iline4); - $this->Line($iline5); - $this->LineTo($icol, $upper ,$icol, $iline1); - $this->NewLine(); + include($path_to_root . "/reporting/includes/header2.inc"); - $this->fontSize += 4; - $this->Font('bold'); - $this->Text($mcol, $this->title); - $this->Font(); - $this->fontSize -= 4; - if ($this->pageNumber > 1 && !strstr($this->filename, "Bulk")) - $this->Text($this->endLine - 35, _("Page") . ' ' . $this->pageNumber); - $this->fontSize -= 4; - $this->row = $upper - 5; + $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'; - $this->Text($mcol, $doc_Invoice_no, $mcol + 90); - $this->Text($mcol + 90, $doc_Cust_no, $mcol + 180); - $this->Text($mcol + 180, $doc_Date); - $this->fontSize += 4; + // Turn off cell padding for the main report header, restoring the current setting later + $oldcMargin = $this->cMargin; + $this->SetCellPadding(0); - $this->row = $upper - 2 * $this->lineHeight - 2; - if ($this->company['coy_logo'] != '') + // Set some constants which control header item layout + // only set them once or the PHP interpreter gets angry + if ($this->pageNumber == 0) { - $logo = $path_to_root . "themes/default/images/" . $this->company['coy_logo']; - $this->AddImage($logo, $ccol, $iline1 + 5, 250, 40); + 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); } - else + // 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) { - $this->fontSize += 4; - $this->Font('bold'); - $this->Text($ccol, $this->company['coy_name'], $icol); - $this->Font(); - $this->fontSize -= 4; +// // 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(); } - if ($doctype == 8) // PO - $this->Text($mcol, $myrow['order_no'], $mcol + 90); - else if ($doctype == 9) // SO - $this->Text($mcol, $myrow['order_no'] ." ".$myrow['customer_ref'], $mcol + 90); - else // INV/CRE/STA - $this->Text($mcol, $myrow['reference'], $mcol + 90); - $this->Text($mcol + 90, $myrow['debtor_no'], $mcol + 180); - if ($doctype == 8 || $doctype == 9) - $this->Text($mcol + 180, sql2date($myrow['ord_date'])); - else - $this->Text($mcol + 180, sql2date($myrow['tran_date'])); + $this->row = $this->pageHeight - $this->topMargin; - $this->fontSize -= 4; - $this->row = $iline1 - 5; - $this->Text($ccol, $doc_Charge_To, $icol); - $this->Text($mcol, $doc_Delivered_To); - $this->fontSize += 4; + // 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(); - $this->NewLine(2); - $temp = $this->row; - if ($doctype == 9) + // Footer + if ($this->footerEnable) { - $this->Text($ccol, $myrow['name'], $icol); - //$adr = ?; - } - else - { - if ($doctype == 8) - $this->Text($ccol, $myrow['supp_name'], $icol); - else - $this->Text($ccol, $myrow['DebtorName'], $icol); - $adr = explode("\n", $myrow['address']); - } - for ($i = 0; $i < count($adr); $i++) - { - $this->NewLine(); - $this->Text($ccol, $adr[$i], $icol); + $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; } - if ($sales_order != NULL) + + // + // 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'] != '')) { - $this->row = $temp; - if ($doctype == 8) - $this->Text($mcol, $this->company['coy_name']); + // 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->Text($mcol, $sales_order['deliver_to']); - $adr = explode("\n", $sales_order['delivery_address']); - for ($i = 0; $i < count($adr); $i++) - { - $this->NewLine(); - $this->Text($mcol, $adr[$i]); - } + $this->AddImage($logo, $companyCol, $this->row - (LOGO_HEIGHT * LOGO_Y_POS_ADJ_FACTOR), 0, LOGO_HEIGHT); } - $this->row = $iline2 - 2 * $this->lineHeight; - $this->Text($ccol, $doc_Shipping_Company . ":", $ccol2); - if ($doctype != 8) - $this->Text($ccol2, $myrow['shipper_name'], $mcol); - $this->Text($mcol, $doc_Due_Date . ":", $mcol2); - if ($doctype == 9) - $this->Text($mcol2, sql2date($myrow['delivery_date'])); - else if ($doctype != 8) - $this->Text($mcol2, sql2date($myrow['due_date'])); - if ($branch != null) - { - $this->NewLine(); - $this->Text($ccol, $doc_Your_Ref . ":", $ccol2); - $this->Text($ccol2, $branch['contact_name'], $mcol); - $this->Text($mcol, $doc_Our_Ref . ":", $mcol2); + else + $this->Text($companyCol, $this->company['coy_name']); - $id = $branch['salesman']; - $sql = "SELECT salesman_name FROM ".TB_PREF."salesman WHERE salesman_code='$id'"; - $result = db_query($sql,"could not get sales person"); - $row = db_fetch($result); + // 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); + } - $this->Text($mcol2, $row['salesman_name']); - } - $this->NewLine(); - $this->Text($ccol, $doc_Your_VAT_no . ":", $ccol2); - if ($doctype != 8) - $this->Text($ccol2, $myrow['tax_id'], $mcol); - $this->Text($mcol, $doc_Our_VAT_no . ":", $mcol2); - $this->Text($mcol2, $this->company['gst_no']); - $this->NewLine(); - $this->Text($ccol, $doc_Payment_Terms . ":", $ccol2); + // 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); + } - $id = $myrow['payment_terms']; - $sql = "SELECT terms FROM ".TB_PREF."payment_terms WHERE terms_indicator='$id'"; - $result = db_query($sql,"could not get paymentterms"); - $row = db_fetch($result); + // 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); + } - $this->Text($ccol2, $row["terms"], $mcol); - $this->Text($mcol, $doc_Our_Order_No . ":", $mcol2); - $this->Text($mcol2, $myrow['order_']); - - $locale = $path_to_root . "lang/" . $_SESSION['language']->code . "/locale.inc"; - if (file_exists($locale)) - { - $header2include = true; - include($locale); - } - $this->row = $iline3 - $this->lineHeight - 2; - $this->Font('bold'); - $count = count($this->headers); - for ($i = 0; $i < $count; $i++) - $this->TextCol($i, $i + 1, $this->headers[$i], -2); + // 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(); - $temp = $this->row - 2 * $this->lineHeight; - $this->row = $iline5 - $this->lineHeight - 6; - $this->Text($ccol, $doc_Please_Quote . " - " . $myrow['curr_code']); - $this->NewLine(); - if ($branch['disable_branch'] > 0 && $doctype == 10) // payment links + + $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 { - if ($branch['disable_branch'] == 1) - { - $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] + $myrow["ov_amount"], user_price_dec()); - $txt = $doc_Payment_Link . " PayPal: "; - $name = urlencode($this->title . " " . $myrow['reference']); - $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" . - $name . "&amount=" . $amt . "¤cy_code=" . $myrow['curr_code']; - $this->fontSize -= 2; - $this->Text($ccol, $txt); - $this->NewLine(); - $this->SetTextColor(0, 0, 255); - $this->Text($ccol, $url); - $this->SetTextColor(0, 0, 0); - $this->addLink($url, $ccol, $this->row, $this->pageWidth - $this->rightMargin, $this->row + $this->lineHeight); - $this->fontSize += 2; - } + $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(); } - if ($this->params['comments'] != '') + + // Add page numbering to header if footer is turned off + if (!$this->footerEnable) { - $this->NewLine(); - $this->Font('bold'); - $this->Text($ccol, $this->params['comments']); - $this->Font(); + $str = _("Page") . ' ' . $this->pageNumber . '/' . $this->getAliasNbPages(); + $this->Text($pageNumCol, $str, 0, 0, 0, 'right', 0, 0, NULL, 1); } - $this->row = $lower - 5; - $this->fontSize -= 4; - - $this->Text($ccol, $doc_Address, $ccol2 + 40); - $this->Text($ccol2 + 30, $doc_Phone_Fax_Email, $mcol); - $this->Text($mcol , $doc_Bank, $mcol2); - $this->Text($mcol2, $doc_Bank_Account); - $this->fontSize += 4; - $this->NewLine(); - $adrline = $this->row; - - $adr = explode("\n", $this->company['postal_address']); - for ($i = 0; $i < count($adr); $i++) - { - $this->Text($ccol, $adr[$i], $ccol2 + 40); + // 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(); } - $this->row = $adrline; - $this->Text($ccol2 + 30, $this->company['phone'], $mcol); - $this->NewLine(); - $this->Text($ccol2 + 30, $this->company['fax'], $mcol); - $this->NewLine(); - $this->Text($ccol2 + 30, $this->company['email'], $mcol); - // fetch this later - $this->row = $adrline; - $this->Text($mcol, $bankaccount['bank_name'], $mcol2); - $adr = explode("\n", $bankaccount['bank_address']); - for ($i = 0; $i < count($adr); $i++) - { - $this->NewLine(); - $this->Text($mcol, $adr[$i], $mcol2); + $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: + * @param int $output_format Format of the output string. Possible values are: + * @access public + */ + function DatePrettyPrint($date, $input_format = 0, $output_format = 0) + { + if ($date != '') + { + $date = date2sql($date); + $year = (int) (substr($date, 0, 4)); + $month = (int) (substr($date, 5, 2)); + $day = (int) (substr($date, 8, 2)); + if ($output_format == 0) + return(date('F j, Y', mktime(12, 0, 0, $month, $day, $year))); + elseif ($output_format == 1) + return(date('F Y', mktime(12, 0, 0, $month, $day, $year))); + elseif ($output_format == 2) + return(date('M Y', mktime(12, 0, 0, $month, $day, $year))); } - - $this->row = $adrline; - $this->Text($mcol2, $bankaccount['bank_account_name']); - $this->NewLine(); - $this->Text($mcol2, $bankaccount['bank_account_number']); - $this->row = $temp; + else + return $date; } function AddImage($logo, $x, $y, $w, $h) { - if ($this->rtl) - $x = $this->pageWidth - $x - $w; if (strpos($logo, ".png") || strpos($logo, ".PNG")) $this->addPngFromFile($logo, $x, $y, $w, $h); else $this->addJpegFromFile($logo, $x, $y, $w, $h); - } + } - function SetDrawColor($r, $g, $b) + // Get current draw color setting from TCPDF object; returns array of RGB numbers + function GetDrawColor() { - $this->setStrokeColor($r / 255, $g / 255, $b / 255); + // Convert the TCPDF stored DrawColor string into an array of strings + $colorFields = explode(' ', $this->DrawColor); + + // Test last value: G == grayscale, single number; RG == RGB, 3 numbers + if ($colorFields[count($colorFields) - 1] == 'G') + // Convert a grayscale string value to the equivalent RGB value + $drawColor = array((float) $colorFields[0], (float) $colorFields[0], (float) $colorFields[0]); + else + // Convert RGB string values to the a numeric array + $drawColor = array((float) $colorFields[0], (float) $colorFields[1], (float) $colorFields[2]); + + return $drawColor; } + function SetDrawColor($r, $g, $b) + { + parent::SetDrawColor($r, $g, $b); + } + function SetTextColor($r, $g, $b) { - $this->setColor($r / 255, $g / 255, $b / 255); + parent::SetTextColor($r, $g, $b); + } + + /** + * Set the fill color for table cells. + * @see reporting/includes/TCPDF#SetFillColor($col1, $col2, $col3, $col4) + */ + function SetFillColor($r, $g, $b) + { + parent::SetFillColor($r, $g, $b); + } + + // Get current cell padding setting from TCPDF object + function GetCellPadding() + { + return $this->cMargin; } - function Text($c, $txt, $n=0, $corr=0, $r=0) + // Set desired cell padding (aka "cell margin") + // Seems to be just left and right margins... + function SetCellPadding($pad) + { + parent::SetCellPadding($pad); + } + + function Text($c, $txt, $n=0, $corr=0, $r=0, $align='left', $border=0, $fill=0, $link=NULL, $stretch=0) { if ($n == 0) $n = $this->pageWidth - $this->rightMargin; - return $this->TextWrap($c, $this->row - $r, $n - $c + $corr, $txt, 'left'); + return $this->TextWrap($c, $this->row - $r, $n - $c + $corr, $txt, $align, $border, $fill, $link, $stretch); } - - function TextWrap($xpos, $ypos, $len, $str, $align = 'left') + + function TextWrap($xpos, $ypos, $len, $str, $align = 'left', $border = 0, $fill = 0, $link = NULL, $stretch = 0) { - if ($this->rtl) + if ($this->fontSize != $this->oldFontSize) { - $str = strrev($str); - $xpos = $this->pageWidth - $xpos - $len; - if ($align == 'left') - $align = 'right'; - elseif ($align == 'right') - $align = 'left'; - } - return $this->addTextWrap($xpos, $ypos, $len, $this->fontSize, $str, $align); + $this->SetFontSize($this->fontSize); + $this->oldFontSize = $this->fontSize; + } + return $this->addTextWrap($xpos, $ypos, $len, $this->fontSize, $str, $align, $border, $fill, $link, $stretch); } - function TextCol($c, $n, $txt, $corr=0, $r=0) + function TextCol($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0) { - return $this->TextWrap($this->cols[$c], $this->row - $r, $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c]); + return $this->TextWrap($this->cols[$c], $this->row - $r, $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c], $border, $fill, $link, $stretch); } - function TextCol2($c, $n, $txt, $corr=0, $r=0) + function AmountCol($c, $n, $txt, $dec=0, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0) + { + return $this->TextCol($c, $n, number_format2($txt, $dec), $corr, $r, $border, $fill, $link, $stretch); + } + + function AmountCol2($c, $n, $txt, $dec=0, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0, $amount_locale = 'en_US.UTF-8', $amount_format = '%(!.2n') { - return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c]); + setlocale(LC_MONETARY, $amount_locale); + return $this->TextCol($c, $n, money_format($amount_format, $txt), $corr, $r, $border, $fill, $link, $stretch); } - function TextColLines($c, $n, $txt, $corr=0) + function DateCol($c, $n, $txt, $conv=false, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0) + { + if ($conv) + $txt = sql2date($txt); + return $this->TextCol($c, $n, $txt, $corr, $r, $border, $fill, $link, $stretch); + } + + function TextCol2($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0) + { + return $this->TextWrap($this->cols2[$c], $this->row - $r, $this->cols2[$n] - $this->cols2[$c] + $corr, $txt, $this->aligns2[$c], $border, $fill, $link, $stretch); + } + + function TextColLines($c, $n, $txt, $corr=0, $r=0, $border=0, $fill=0, $link=NULL, $stretch=0) + { + $this->row -= $r; + $this->TextWrapLines($this->cols[$c], $this->cols[$n] - $this->cols[$c] + $corr, $txt, $this->aligns[$c], $border, $fill, $link, $stretch); + } + + function TextWrapLines($c, $width, $txt, $align='left', $border=0, $fill=0, $link=NULL, $stretch=0) { $str = Explode("\n", $txt); for ($i = 0; $i < count($str); $i++) { $l = $str[$i]; - do + do { - $l = $this->TextCol($c, $n, $l, $corr); - $this->NewLine(); + $l = $this->TextWrap($c, $this->row , $width, $l, $align, $border, $fill, $link, $stretch); + $this->row -= $this->lineHeight; } while ($l != ''); - } + } + } + + /** + * Expose the underlying calcTextWrap() function in this API. + */ + function TextWrapCalc($txt, $width, $spacebreak=false) + { + return $this->calcTextWrap($txt, $width, $spacebreak); + } + + /** + * Sets the line drawing style. + * + * Takes an associative array as arg so you don't need to specify all values. + * + * Array keys: + * width (float) - the thickness of the line in user units + * cap (string) - the type of cap to put on the line, values can be 'butt','round','square' + * where the diffference between 'square' and 'butt' is that 'square' projects a flat end past the + * end of the line. + * join (string) - can be 'miter', 'round', 'bevel' + * dash (mixed) - Dash pattern. Is 0 (without dash) or string with series of length values, which are the + * lengths of the on and off dashes. For example: "2" represents 2 on, 2 off, 2 on, 2 off, ...; + * "2,1" is 2 on, 1 off, 2 on, 1 off, ... + * phase (integer) - a modifier on the dash pattern which is used to shift the point at which the pattern starts. + * color (array) - draw color. Format: array(GREY), or array(R,G,B) or array(C,M,Y,K). + */ + function SetLineStyle($style) + { + parent::SetLineStyle($style); } - function LineTo($from, $row, $to, $row2) + /** + * Sets the line drawing width. + */ + function SetLineWidth($width) + { + parent::SetLineWidth($width); + } + + function LineTo($from, $row, $to, $row2) { - Cpdf::line($from, $row, $to, $row2); + parent::line($from, $row, $to, $row2); } - function Line($row, $height = 0) + function Line($row, $height = 0) { - $this->setLineStyle($height + 1); - Cpdf::line($this->pageWidth - $this->rightMargin, $row ,$this->leftMargin, $row); + $oldLineWidth = $this->GetLineWidth(); + $this->SetLineWidth($height + 1); + parent::line($this->pageWidth - $this->rightMargin, $row ,$this->leftMargin, $row); + $this->SetLineWidth($oldLineWidth); } - function NewLine($l=1, $np=0) + /** + * Underlines the contents of a cell, but not the cell padding area. + * Primarily useful for the last line before a "totals" line. + * @param int $c Column number to underline. + * @param int $r Print the underline(s) this number of rows below the current position. Can be negative in order to go up. + * @param int $type Type of underlining to draw. Possible values are: + * @param int $linewidth Thickness of the line to draw. Default value of zero will use the current line width defined for this document. + * @param array $style Line style. Array like for {@link SetLineStyle SetLineStyle}. Default value: default line style (empty array). + * @access public + * @see SetLineWidth(), SetDrawColor(), SetLineStyle() + */ + function UnderlineCell($c, $r = 0, $type = 1, $linewidth = 0, $style = array()) { - $this->row -= ($l * $this->lineHeight); - if ($np > 0 && $this->row < $this->bottomMargin + ($np * $this->lineHeight)) - $this->Header(); - } + // If line width was specified, save current setting so we can reset it + if ($linewidth != 0) + { + $oldLineWidth = $this->GetLineWidth(); + $this->SetLineWidth($linewidth); + } + + // Figure out how far down to move the line based on current font size + // Calculate this because printing underline directly at $this->row goes on top + // of the parts of characters that "hang down", like the bottom of commas & + // lowercase letter 'g', etc. + if ($this->fontSize < 10) + $y_adj = 2; + else + $y_adj = 3; + parent::line($this->cols[$c] + $this->cMargin, $this->row - $r - $y_adj, $this->cols[$c + 1] - $this->cMargin, $this->row - $r - $y_adj, $style); + + // Double underline, far enough below the first underline so as not to overlap + // the first underline (depends on current line thickness (aka "line width") + if ($type == 2) + 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); + + // If line width was specified, reset it back to the original setting + if ($linewidth != 0) + $this->SetLineWidth($oldLineWidth); + } + + function NewLine($l=1, $np=0, $h = NULL) + { + // If the line height wasn't specified, use the current setting + if ($h == NULL) + $h = $this->lineHeight; + + // Move one line down the page + $this->row -= ($l * $h); + // Reset the "current line height" for the new line + $this->curLineHeight = $this->fontSize; + // Check to see if we're at the bottom and should insert a page break + if ($this->row < $this->bottomMargin + ($np * $h)) + $this->{$this->headerFunc}(); // call header template chosen by current report + } function End($email=0, $subject=null, $myrow=null, $doctype = 0) { - global $go_debug, $path_to_root; + global $pdf_debug, $path_to_root, $comp_path; - // this is no good in IE so has been replaced, see down under - // but good for debugging purposes in IE - //session_write_close(); - - if ($go_debug == 1) + if ($pdf_debug == 1) { - $buf = $this->output(1); - $len = strlen($buf); - $pdfcode = $buf; + $pdfcode = $this->Output('','S'); $pdfcode = str_replace("\n", "\n
", htmlspecialchars($pdfcode)); echo ''; echo trim($pdfcode); @@ -621,104 +884,121 @@ class FrontReport extends Cpdf //header('Pragma: public'); //$this->pdf->stream(); - } - else + } + else { - $buf = $this->output(); - $len = strlen($buf); - $dir = './pdf_files'; + + $dir = $comp_path.'/'.user_company(). '/pdf_files'; //save the file if (!file_exists($dir)) { mkdir ($dir,0777); } - $fname = $dir . '/' . $this->filename; - $fp = fopen($fname,'w'); - fwrite($fp,$buf); - fclose($fp); + // do not use standard filenames or your sensitive company data + // are world readable + if ($email == 1) + $fname = $dir.'/'.$this->filename; + else + $fname = $dir.'/'.uniqid('').'.pdf'; + $this->Output($fname, 'F'); if ($email == 1) { $emailtype = true; if ($this->currency != $myrow['curr_code']) { - include("doctext2.inc"); - } + include("doctext2.inc"); + } else { - include("doctext.inc"); - } - require_once($path_to_root . "reporting/includes/class.mail.inc"); + include("doctext.inc"); + } + require_once($path_to_root . "/reporting/includes/class.mail.inc"); $mail = new email($this->company['coy_name'], $this->company['email']); - $from = $this->company['coy_name'] . " <" . $this->company['email'] . ">"; + if (!isset($myrow['email']) || $myrow['email'] == '') + $myrow['email'] = isset($myrow['contact_email']) ? $myrow['contact_email'] : ''; $to = $myrow['DebtorName'] . " <" . $myrow['email'] . ">"; - $msg = $doc_Dear_Sirs . ",\n\n" . $doc_AttachedFile . " " . $subject . + $msg = $doc_Dear_Sirs . " " . $myrow['DebtorName'] . ",\n\n" . $doc_AttachedFile . " " . $subject . "\n\n"; - if ($myrow['dimension_id'] > 0 && $doctype == 10) // helper for payment links + if (isset($myrow['dimension_id']) && $myrow['dimension_id'] > 0 && $doctype == 10) // helper for payment links { if ($myrow['dimension_id'] == 1) { $amt = number_format($myrow["ov_freight"] + $myrow["ov_gst"] + $myrow["ov_amount"], user_price_dec()); - $txt = $doc_Payment_Link . " PayPal: "; + $txt = $doc_Payment_Link . " PayPal: "; $nn = urlencode($this->title . " " . $myrow['reference']); $url = "https://www.paypal.com/xclick/business=" . $this->company['email'] . "&item_name=" . $nn . "&amount=" . $amt . "¤cy_code=" . $myrow['curr_code']; - $msg .= $txt . $url . "\n\n"; - } + $msg .= $txt . $url . "\n\n"; + } } $msg .= $doc_Kindest_regards . "\n\n"; - $sender = $this->user . "\n" . $this->company['coy_name']; + $sender = $this->user . "\n" . $this->company['coy_name'] . "\n" . $this->company['postal_address'] . "\n" . $this->company['email'] . "\n" . $this->company['phone']; $mail->to($to); $mail->subject($subject); $mail->text($msg . $sender); $mail->attachment($fname); $ret = $mail->send(); - if (1 == 1) // just for fun and for debugging purposes!! - { - $from = str_replace("<", "(", $from); - $from = str_replace(">", ")", $from); - $to = str_replace("<", "(", $to); - $to = str_replace(">", ")", $to); - $msg2 = "
From: " . $from; - $msg2 .= "
To: " . $to; - $msg2 .= "
Subject: " . $subject; - $msg2 .= "
Msg: " . nl2br($msg); - $msg2 .= nl2br($sender) . "
"; - $msg2 .= "
Filepath: " . $fname . "
Filename: " . $this->filename . "
"; - } - if ($ret) - $str = "
" . $this->title . " " . $myrow['reference'] . " " . _("sent to") . " "; - else - $str = "
" . $this->title . " " . $myrow['reference'] . " " . _("NOT sent to") . " "; - $msg2 .= $str . $myrow['DebtorName'] . " - " . $myrow['email']; - echo ""; - echo $msg2; - echo ""; + if (!$ret) + display_error(_("Sending document by email failed")); + else + display_notification($this->title . " " . $myrow['reference'] . " " + . _("has been sent by email.")); + unlink($fname); } else { - echo ' - - - - - click here if you are not re-directed. - - '; - } - // also have a look through the directory, and remove the files that are older than a week - // rather want to save 'em - /*if ($d = @opendir($dir)) { + $printer = get_report_printer(user_print_profile(), $_POST['REP_ID']); + if ($printer == false) { + if(in_ajax()) { + global $Ajax; + + if (user_rep_popup()) + $Ajax->popup($fname); // when embeded pdf viewer used + else + $Ajax->redirect($fname); // otherwise use faster method + } else { + //echo ' + // + // + // + // + // click here if you are not re-directed. + // + // '; + header('Content-type: application/pdf'); + header("Content-Disposition: inline; filename=$this->filename"); + header('Expires: 0'); + header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); + header('Pragma: public'); + + $this->Stream(); + } + } else { // send report to network printer + $prn = new remote_printer($printer['queue'],$printer['host'], + $printer['port'], $printer['timeout']); + $error = $prn->print_file($fname); + if ($error) + display_error($error); + else + display_notification(_('Report has been sent to network printer ').$printer['name']); + } + } + // first have a look through the directory, + // and remove old temporary pdfs + if ($d = @opendir($dir)) { while (($file = readdir($d)) !== false) { - // then check to see if this one is too old + if (!is_file($dir.'/'.$file) || $file == 'index.php') continue; + // then check to see if this one is too old $ftime = filemtime($dir.'/'.$file); - if (time()-$ftime > 3600*24*7){ + // seems 3 min is enough for any report download, isn't it? + if (time()-$ftime > 180){ unlink($dir.'/'.$file); } } closedir($d); - }*/ + } } } }