diff --git a/htdocs/core/lib/pdf.lib.php b/htdocs/core/lib/pdf.lib.php index 43d02cbe8fe..7acc2b0990e 100644 --- a/htdocs/core/lib/pdf.lib.php +++ b/htdocs/core/lib/pdf.lib.php @@ -2511,7 +2511,7 @@ function pdf_getlinetotalexcltax($object, $i, $outputlangs, $hidedetails = 0) * * @param Commande|Facture|Propal|FactureFournisseur|CommandeFournisseur|SupplierProposal $object Object * @param int $i Current line number - * @param Translate $outputlangs Object langs for output + * @param Translate $outputlangs Object langs for output * @param int<0,2> $hidedetails Hide value (0 = no, 1 = yes, 2 = just special lines) * @return string Return total of line incl tax */ @@ -2838,3 +2838,74 @@ function pdfExtractMetadata($file, $field = 'Keywords') return "ERROR: FAILED TO READ PDF"; } } + +/** + * Render subtotals line with a colored background and adapted text color . + * + * @param TCPDF $pdf PDF instance + * @param CommonDocGenerator $generator Generator object + * @param float $curY Current Y position + * @param CommonObject $object Object containing lines + * @param int $i Current line number + * @param Translate $outputlangs Output language object + * @param int $hideref Hide reference + * @param int $hidedesc Hide description + * @param array $bgColor RGB color array [R,G,B] + * @param bool $isSubtotal Whether this is a subtotal line + * @param bool $applySubtotalLogic Whether to apply subtotal specific logic + * + * @return void + */ +function pdf_render_subtotals( + TCPDF $pdf, + CommonDocGenerator $generator, + float $curY, + CommonObject $object, + int $i, + Translate $outputlangs, + int $hideref, + int $hidedesc, + array $bgColor, + bool $isSubtotal = false, + bool $applySubtotalLogic = true +) { + $savePage = $pdf->getPage(); + $saveX = $pdf->GetX(); + $prevAlign = $generator->cols['desc']['content']['align']; + + if ($isSubtotal && $applySubtotalLogic && $object->lines[$i]->qty < 0) { + $outputlangs->load("subtotals"); + $object->lines[$i]->desc = $outputlangs->trans("SubtotalOf", $object->lines[$i]->desc); + $generator->cols['desc']['content']['align'] = ($prevAlign === 'L') ? 'R' : 'L'; + } + + $pdf->startTransaction(); + $pdf->SetXY($saveX, $curY); + $generator->printColDescContent($pdf, $curY, 'desc', $object, $i, $outputlangs, $hideref, $hidedesc); + $pageAfter = $pdf->getPage(); + $yAfter = $pdf->GetY(); + $pdf->rollbackTransaction(true); + + $pdf->SetFillColor($bgColor[0], $bgColor[1], $bgColor[2]); + $width = $generator->page_largeur - $generator->marge_droite - $generator->marge_gauche; + + $pdf->SetXY($generator->marge_gauche, $curY); + if ($pageAfter === $savePage) { + $pdf->MultiCell($width, max(0, $yAfter - $curY), '', 0, '', true); + } else { + $pdf->MultiCell($width, $pdf->getPageHeight() - $pdf->getBreakMargin() - $curY, '', 0, '', true); + + $pdf->setPage($pageAfter); + $pdf->SetXY($generator->marge_gauche, $pdf->getMargins()['top']); + $pdf->MultiCell($width, max(0, $yAfter - $pdf->getMargins()['top']), '', 0, '', true); + + $pdf->setPage($savePage); + } + + $pdf->SetTextColor(colorIsLight(implode(',', $bgColor))); + $pdf->SetXY($saveX, $curY); + $generator->printColDescContent($pdf, $curY, 'desc', $object, $i, $outputlangs, $hideref, $hidedesc); + $generator->setAfterColsLinePositionsData('desc', $pdf->GetY(), $pdf->getPage()); + + $generator->cols['desc']['content']['align'] = $prevAlign; +} diff --git a/htdocs/core/modules/commande/doc/pdf_eratosthene.modules.php b/htdocs/core/modules/commande/doc/pdf_eratosthene.modules.php index d0c51a38fa1..b3d4fc3e11b 100644 --- a/htdocs/core/modules/commande/doc/pdf_eratosthene.modules.php +++ b/htdocs/core/modules/commande/doc/pdf_eratosthene.modules.php @@ -662,23 +662,7 @@ class pdf_eratosthene extends ModelePDFCommandes $this->setAfterColsLinePositionsData('desc', $pdf->GetY(), $pdf->getPage()); } else { $bg_color = colorStringToArray(getDolGlobalString("SUBTOTAL_BACK_COLOR_LEVEL_".abs($object->lines[$i]->qty))); - $pdf->SetFillColor($bg_color[0], $bg_color[1], $bg_color[2]); - $pdf->SetXY($pdf->GetX(), $curY); - $pdf->MultiCell($this->page_largeur - $this->marge_droite - $this->marge_gauche, 6, '', 0, '', true); - $previous_align = array(); - $previous_align['align'] = $this->cols['desc']['content']['align']; - if ($object->lines[$i]->qty < 0) { - $langs->load("subtotals"); - $object->lines[$i]->desc = $langs->trans("SubtotalOf", $object->lines[$i]->desc); - if ($previous_align['align'] == 'L') { - $this->cols['desc']['content']['align'] = 'R'; - } elseif ($previous_align['align'] == 'R') { - $this->cols['desc']['content']['align'] = 'L'; - } - } - $this->printColDescContent($pdf, $curY, 'desc', $object, $i, $outputlangs, $hideref, $hidedesc); - $this->setAfterColsLinePositionsData('desc', $pdf->GetY(), $pdf->getPage()); - $this->cols['desc']['content']['align'] = $previous_align['align']; // Re align if we printed a subtotal ligne + pdf_render_subtotals($pdf, $this, $curY, $object, $i, $outputlangs, $hideref, $hidedesc, $bg_color, true, true); } } diff --git a/htdocs/core/modules/expedition/doc/pdf_espadon.modules.php b/htdocs/core/modules/expedition/doc/pdf_espadon.modules.php index a72b83cea76..51ea1be8f41 100644 --- a/htdocs/core/modules/expedition/doc/pdf_espadon.modules.php +++ b/htdocs/core/modules/expedition/doc/pdf_espadon.modules.php @@ -635,23 +635,7 @@ class pdf_espadon extends ModelePdfExpedition if ($this->getColumnStatus('desc')) { if ($object->lines[$i]->special_code == SUBTOTALS_SPECIAL_CODE) { $bg_color = colorStringToArray(getDolGlobalString("SUBTOTAL_BACK_COLOR_LEVEL_".abs($object->lines[$i]->qty))); - $pdf->SetFillColor($bg_color[0], $bg_color[1], $bg_color[2]); - $pdf->SetXY($pdf->GetX() + 1, $curY); - $pdf->MultiCell($this->page_largeur - $this->marge_droite - $this->marge_gauche - 2, 6, '', 0, '', true); - $previous_align = array(); - $previous_align['align'] = $this->cols['desc']['content']['align']; - if ($object->lines[$i]->qty < 0) { - $langs->load("subtotals"); - $object->lines[$i]->desc = $langs->trans("SubtotalOf", $object->lines[$i]->desc); - if ($previous_align['align'] == 'L') { - $this->cols['desc']['content']['align'] = 'R'; - } elseif ($previous_align['align'] == 'R') { - $this->cols['desc']['content']['align'] = 'L'; - } - } - $this->printColDescContent($pdf, $curY, 'desc', $object, $i, $outputlangs, $hideref, $hidedesc); - $this->setAfterColsLinePositionsData('desc', $pdf->GetY(), $pdf->getPage()); - $this->cols['desc']['content']['align'] = $previous_align['align']; // Re align if we printed a subtotal ligne + pdf_render_subtotals($pdf, $this, $curY, $object, $i, $outputlangs, $hideref, $hidedesc, $bg_color, true, true); } else { $pdf->startTransaction(); diff --git a/htdocs/core/modules/facture/doc/pdf_sponge.modules.php b/htdocs/core/modules/facture/doc/pdf_sponge.modules.php index 1d04cc5143d..dae2981db8e 100644 --- a/htdocs/core/modules/facture/doc/pdf_sponge.modules.php +++ b/htdocs/core/modules/facture/doc/pdf_sponge.modules.php @@ -805,23 +805,7 @@ class pdf_sponge extends ModelePDFFactures $this->setAfterColsLinePositionsData('desc', $pdf->GetY(), $pdf->getPage()); } else { $bg_color = colorStringToArray(getDolGlobalString("SUBTOTAL_BACK_COLOR_LEVEL_".abs($object->lines[$i]->qty))); - $pdf->SetFillColor($bg_color[0], $bg_color[1], $bg_color[2]); - $pdf->SetXY($pdf->GetX(), $curY); - $pdf->MultiCell($this->page_largeur - $this->marge_droite - $this->marge_gauche, 6, '', 0, '', true); - $previous_align = array(); - $previous_align['align'] = $this->cols['desc']['content']['align']; - if ($object->lines[$i]->qty < 0) { - $langs->load("subtotals"); - $object->lines[$i]->desc = $langs->trans("SubtotalOf", $object->lines[$i]->desc); - if ($previous_align['align'] == 'L') { - $this->cols['desc']['content']['align'] = 'R'; - } elseif ($previous_align['align'] == 'R') { - $this->cols['desc']['content']['align'] = 'L'; - } - } - $this->printColDescContent($pdf, $curY, 'desc', $object, $i, $outputlangs, $hideref, $hidedesc); - $this->setAfterColsLinePositionsData('desc', $pdf->GetY(), $pdf->getPage()); - $this->cols['desc']['content']['align'] = $previous_align['align']; // Re align if we printed a subtotal ligne + pdf_render_subtotals($pdf, $this, $curY, $object, $i, $outputlangs, $hideref, $hidedesc, $bg_color, true, true); } } diff --git a/htdocs/core/modules/propale/doc/pdf_cyan.modules.php b/htdocs/core/modules/propale/doc/pdf_cyan.modules.php index b6abb050e5c..651e28b9082 100644 --- a/htdocs/core/modules/propale/doc/pdf_cyan.modules.php +++ b/htdocs/core/modules/propale/doc/pdf_cyan.modules.php @@ -666,23 +666,7 @@ class pdf_cyan extends ModelePDFPropales $this->setAfterColsLinePositionsData('desc', $pdf->GetY(), $pdf->getPage()); } else { $bg_color = colorStringToArray(getDolGlobalString("SUBTOTAL_BACK_COLOR_LEVEL_".abs($object->lines[$i]->qty))); - $pdf->SetFillColor($bg_color[0], $bg_color[1], $bg_color[2]); - $pdf->SetXY($pdf->GetX(), $curY); - $pdf->MultiCell($this->page_largeur - $this->marge_droite - $this->marge_gauche, 6, '', 0, '', true); - $previous_align = array(); - $previous_align['align'] = $this->cols['desc']['content']['align']; - if ($object->lines[$i]->qty < 0) { - $langs->load("subtotals"); - $object->lines[$i]->desc = $langs->trans("SubtotalOf", $object->lines[$i]->desc); - if ($previous_align['align'] == 'L') { - $this->cols['desc']['content']['align'] = 'R'; - } elseif ($previous_align['align'] == 'R') { - $this->cols['desc']['content']['align'] = 'L'; - } - } - $this->printColDescContent($pdf, $curY, 'desc', $object, $i, $outputlangs, $hideref, $hidedesc); - $this->setAfterColsLinePositionsData('desc', $pdf->GetY(), $pdf->getPage()); - $this->cols['desc']['content']['align'] = $previous_align['align']; // Re align if we printed a subtotal ligne + pdf_render_subtotals($pdf, $this, $curY, $object, $i, $outputlangs, $hideref, $hidedesc, $bg_color, true, true); } }