From d556f779671db032ed243c16f3f949f4be43500e Mon Sep 17 00:00:00 2001 From: Didier 'OdyX' Raboud Date: Fri, 17 Mar 2023 16:16:01 +0100 Subject: [PATCH] SwissQR: Get QR height in a single function --- .../modules/facture/doc/pdf_crabe.modules.php | 22 +++++++-------- .../facture/doc/pdf_sponge.modules.php | 22 +++++---------- .../core/modules/facture/modules_facture.php | 27 +++++++++++++++++++ 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/htdocs/core/modules/facture/doc/pdf_crabe.modules.php b/htdocs/core/modules/facture/doc/pdf_crabe.modules.php index 96276df046d..a971123f648 100644 --- a/htdocs/core/modules/facture/doc/pdf_crabe.modules.php +++ b/htdocs/core/modules/facture/doc/pdf_crabe.modules.php @@ -354,12 +354,10 @@ class pdf_crabe extends ModelePDFFactures $heightforfooter += 6; } - $heightforqrinvoice = 0; - if (!empty($conf->global->INVOICE_ADD_SWISS_QR_CODE)) { + $heightforqrinvoice_firstpage = $this->getHeightForQRInvoice(1, $object, $langs); + if ($heightforqrinvoice_firstpage > 0) { // Shrink infotot to a base 30 $heightforinfotot = 30 + (4 * $nbpayments); // Height reserved to output the info and total part and payment part - // SWIFT's requirement; the SwissQR is explicitly that size, from the bottom of the page - $heightforqrinvoice = 105; } if (class_exists('TCPDF')) { @@ -604,7 +602,7 @@ class pdf_crabe extends ModelePDFFactures $pdf->setTopMargin($tab_top_newpage); $pageposbefore = $pdf->getPage(); - $page_bottom_margin = $heightforfooter + $heightforfreetext + $heightforinfotot + ($pageposbefore == 1 ? $heightforqrinvoice : 0); + $page_bottom_margin = $heightforfooter + $heightforfreetext + $heightforinfotot + $this->getHeightForQRInvoice($pageposbefore, $object, $langs); $pdf->setPageOrientation('', 1, $page_bottom_margin); $showpricebeforepagebreak = 1; @@ -839,11 +837,11 @@ class pdf_crabe extends ModelePDFFactures while ($pagenb < $pageposafter) { $pdf->setPage($pagenb); if ($pagenb == 1) { - $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforqrinvoice, 0, $outputlangs, 0, 1, $object->multicurrency_code); + $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforqrinvoice_firstpage, 0, $outputlangs, 0, 1, $object->multicurrency_code); } else { $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1, $object->multicurrency_code); } - $this->_pagefoot($pdf, $object, $outputlangs, 1, ($pagenb == 1 ? $heightforqrinvoice : 0)); + $this->_pagefoot($pdf, $object, $outputlangs, 1, $this->getHeightForQRInvoice($pagenb, $object, $langs)); $pagenb++; $pdf->setPage($pagenb); $pdf->setPageOrientation('', 1, 0); // The only function to edit the bottom margin of current page to set it. @@ -857,11 +855,11 @@ class pdf_crabe extends ModelePDFFactures } if (isset($object->lines[$i + 1]->pagebreak) && $object->lines[$i + 1]->pagebreak) { if ($pagenb == 1) { - $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforqrinvoice, 0, $outputlangs, 0, 1, $object->multicurrency_code); + $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforfooter - $heightforqrinvoice_firstpage, 0, $outputlangs, 0, 1, $object->multicurrency_code); } else { $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforfooter, 0, $outputlangs, 1, 1, $object->multicurrency_code); } - $this->_pagefoot($pdf, $object, $outputlangs, 1, ($pagenb == 1 ? $heightforqrinvoice : 0)); + $this->_pagefoot($pdf, $object, $outputlangs, 1, $this->getHeightForQRInvoice($pagenb, $object, $langs)); // New page $pdf->AddPage(); if (!empty($tplidx)) { @@ -877,8 +875,8 @@ class pdf_crabe extends ModelePDFFactures // Show square if ($pagenb == 1) { - $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter - $heightforqrinvoice, 0, $outputlangs, 0, 0, $object->multicurrency_code); - $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter - $heightforqrinvoice + 1; + $this->_tableau($pdf, $tab_top, $this->page_hauteur - $tab_top - $heightforinfotot - $heightforfreetext - $heightforfooter - $heightforqrinvoice_firstpage, 0, $outputlangs, 0, 0, $object->multicurrency_code); + $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter - $heightforqrinvoice_firstpage + 1; } else { $this->_tableau($pdf, $tab_top_newpage, $this->page_hauteur - $tab_top_newpage - $heightforinfotot - $heightforfreetext - $heightforfooter, 0, $outputlangs, 1, 0, $object->multicurrency_code); $bottomlasttab = $this->page_hauteur - $heightforinfotot - $heightforfreetext - $heightforfooter + 1; @@ -897,7 +895,7 @@ class pdf_crabe extends ModelePDFFactures } // Pagefoot - $this->_pagefoot($pdf, $object, $outputlangs, 0, ($pagenb == 1 ? $heightforqrinvoice : 0)); + $this->_pagefoot($pdf, $object, $outputlangs, 0, $this->getHeightForQRInvoice($pageposbefore, $object, $langs)); if (method_exists($pdf, 'AliasNbPages')) { $pdf->AliasNbPages(); } diff --git a/htdocs/core/modules/facture/doc/pdf_sponge.modules.php b/htdocs/core/modules/facture/doc/pdf_sponge.modules.php index eee76cc6ce1..d758bd3e8d2 100644 --- a/htdocs/core/modules/facture/doc/pdf_sponge.modules.php +++ b/htdocs/core/modules/facture/doc/pdf_sponge.modules.php @@ -133,11 +133,6 @@ class pdf_sponge extends ModelePDFFactures */ public $heightforfooter; - /** - * @var int heightforfooter - */ - public $heightforqrinvoice; - /** * @var int tab_top */ @@ -387,12 +382,9 @@ class pdf_sponge extends ModelePDFFactures $this->heightforfreetext = (isset($conf->global->MAIN_PDF_FREETEXT_HEIGHT) ? $conf->global->MAIN_PDF_FREETEXT_HEIGHT : 5); // Height reserved to output the free text on last page $this->heightforfooter = $this->marge_basse + (empty($conf->global->MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS) ? 12 : 22); // Height reserved to output the footer (value include bottom margin) - $this->heightforqrinvoice = 0; - if (!empty($conf->global->INVOICE_ADD_SWISS_QR_CODE)) { + if ($this->getHeightForQRInvoice(1, $object, $langs) > 0) { // Shrink infotot to a base 30 $this->heightforinfotot = 30 + (4 * $nbpayments); // Height reserved to output the info and total part and payment part - // SWIFT's requirement; the SwissQR is explicitly that size, from the bottom of the page - $this->heightforqrinvoice = 105; } if (class_exists('TCPDF')) { $pdf->setPrintHeader(false); @@ -544,7 +536,7 @@ class pdf_sponge extends ModelePDFFactures // Define heigth of table for lines (for first page) - $tab_height = $this->page_hauteur - $this->tab_top - $this->heightforfooter - $this->heightforfreetext - $this->heightforqrinvoice; + $tab_height = $this->page_hauteur - $this->tab_top - $this->heightforfooter - $this->heightforfreetext - $this->getHeightForQRInvoice(1, $object, $langs); $nexY = $this->tab_top - 1; @@ -739,7 +731,7 @@ class pdf_sponge extends ModelePDFFactures $pdf->setTopMargin($this->tab_top_newpage); $pageposbefore = $pdf->getPage(); - $page_bottom_margin = $this->heightforfooter + $this->heightforfreetext + $this->heightforinfotot + ($pageposbefore == 1 ? $this->heightforqrinvoice : 0); + $page_bottom_margin = $this->heightforfooter + $this->heightforfreetext + $this->heightforinfotot + $this->getHeightForQRInvoice($pageposbefore, $object, $langs); $pdf->setPageOrientation('', 1, $page_bottom_margin); $showpricebeforepagebreak = 1; @@ -994,7 +986,7 @@ class pdf_sponge extends ModelePDFFactures // Detect if some page were added automatically and output _tableau for past pages while ($pagenb < $pageposafter) { $pdf->setPage($pagenb); - $heightforqrinvoice = $pagenb == 1 ? $this->heightforqrinvoice : 0; + $heightforqrinvoice = $this->getHeightForQRInvoice($pagenb, $object, $langs); if ($pagenb == $pageposbeforeprintlines) { $this->_tableau($pdf, $this->tab_top, $this->page_hauteur - $this->tab_top - $this->heightforfooter - $heightforqrinvoice, 0, $outputlangs, $hidetop, 1, $object->multicurrency_code, $outputlangsbis); } else { @@ -1013,7 +1005,7 @@ class pdf_sponge extends ModelePDFFactures } if (isset($object->lines[$i + 1]->pagebreak) && $object->lines[$i + 1]->pagebreak) { - $heightforqrinvoice = $pagenb == 1 ? $this->heightforqrinvoice : 0; + $heightforqrinvoice = $this->getHeightForQRInvoice($pagenb, $object, $langs); if ($pagenb == $pageposafter) { $this->_tableau($pdf, $this->tab_top, $this->page_hauteur - $this->tab_top - $this->heightforfooter - $heightforqrinvoice, 0, $outputlangs, $hidetop, 1, $object->multicurrency_code, $outputlangsbis); } else { @@ -1033,7 +1025,7 @@ class pdf_sponge extends ModelePDFFactures } // Show square - $heightforqrinvoice = $pagenb == 1 ? $this->heightforqrinvoice : 0; + $heightforqrinvoice = $this->getHeightForQRInvoice($pagenb, $object, $langs); if ($pagenb == $pageposbeforeprintlines) { $this->_tableau($pdf, $this->tab_top, $this->page_hauteur - $this->tab_top - $this->heightforinfotot - $this->heightforfreetext - $this->heightforfooter - $heightforqrinvoice, 0, $outputlangs, $hidetop, 0, $object->multicurrency_code, $outputlangsbis); $bottomlasttab = $this->page_hauteur - $this->heightforinfotot - $this->heightforfreetext - $this->heightforfooter - $heightforqrinvoice + 1; @@ -2452,7 +2444,7 @@ class pdf_sponge extends ModelePDFFactures protected function _pagefoot(&$pdf, $object, $outputlangs, $hidefreetext = 0) { $showdetails = getDolGlobalInt('MAIN_GENERATE_DOCUMENTS_SHOW_FOOT_DETAILS', 0); - return pdf_pagefoot($pdf, $outputlangs, 'INVOICE_FREE_TEXT', $this->emetteur, ($pdf->getPage() == 1 ? $this->heightforqrinvoice : 0) + $this->marge_basse, $this->marge_gauche, $this->page_hauteur, $object, $showdetails, $hidefreetext, $this->page_largeur, $this->watermark); + return pdf_pagefoot($pdf, $outputlangs, 'INVOICE_FREE_TEXT', $this->emetteur, $this->getHeightForQRInvoice($pdf->getPage(), $object, $outputlangs) + $this->marge_basse, $this->marge_gauche, $this->page_hauteur, $object, $showdetails, $hidefreetext, $this->page_largeur, $this->watermark); } /** diff --git a/htdocs/core/modules/facture/modules_facture.php b/htdocs/core/modules/facture/modules_facture.php index 8f8e842129d..bdfe5687f9a 100644 --- a/htdocs/core/modules/facture/modules_facture.php +++ b/htdocs/core/modules/facture/modules_facture.php @@ -166,6 +166,33 @@ abstract class ModelePDFFactures extends CommonDocGenerator return $qrBill; } + /** + * Get the height for bottom-page QR invoice in mm, depending on the page number. + * + * @param int $pagenbr Page number + * @param Facture $object Invoice object + * @param Translate $langs Translation object + * @return int Height in mm of the bottom-page QR invoice. Can be zero if not on right page; not enabled + */ + protected function getHeightForQRInvoice(int $pagenbr, \Facture $object, \Translate $langs) : int + { + global $conf; + + // Keep it, reset it after QRinvoice getter + $error = $this->error; + + if (!empty($conf->global->INVOICE_ADD_SWISS_QR_CODE)) { + if (!$this->getSwissQrBill($object, $langs)) { + // Reset error to previous one if exists + $this->error = $error; + return 0; + } + // SWIFT's requirementis 105, but we get more room with 100 and the page nuber is in a nice place. + return $pagenbr == 1 ? 100 : 0; + } + return 0; + } + /** * Add SwissQR invoice at bottom of page 1 *