437 lines
18 KiB
PHP
437 lines
18 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
/**
|
|
* Calendar App
|
|
*
|
|
* @copyright 2021 Anna Larch <anna.larch@gmx.net>
|
|
*
|
|
* @author Anna Larch <anna.larch@gmx.net>
|
|
* @author Richard Steinmetz <richard@steinmetz.cloud>
|
|
*
|
|
* This library is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
* License as published by the Free Software Foundation; either
|
|
* version 3 of the License, or any later version.
|
|
*
|
|
* This library is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
*/
|
|
|
|
namespace OCA\Gestion\Service\InvoiceGroupPdfHandler;
|
|
|
|
use DateTime;
|
|
use \FPDF;
|
|
use OCA\Gestion\Helpers\FileExportHelpers;
|
|
use OCA\Gestion\Helpers\PriceHelpers;
|
|
|
|
class InvoiceGroupPdfHandler extends FPDF {
|
|
public $factureData = [];
|
|
public $logo = null;
|
|
|
|
public $productsCount = 0;
|
|
|
|
public $totalPrices = [];
|
|
public $devisCount = 0;
|
|
public $logoPath = "/var/www/html/data/admin/files/.gestion/";
|
|
|
|
public $currentIndexPosition = 0;
|
|
public $initialIndexPosition = 0;
|
|
|
|
public $devisCountToGet = 0;
|
|
public $devisList = [];
|
|
|
|
public $thereIsOrderOrCaseNumber = false;
|
|
public $startingYOfArticlesTable = 95;
|
|
public int $maxArticlePerPage = 19;
|
|
public $additionalArticlesLineBasedOnMultiline = 0;
|
|
function Header()
|
|
{
|
|
if($this->logo != "nothing"){
|
|
$this->Image($this->logoPath."logo.png", 2, 2, 75, 25);
|
|
$this->AddWatermark();
|
|
}
|
|
else{
|
|
$this->Cell(55,30,'');
|
|
}
|
|
$this->SetMargins(3,0,3);
|
|
$this->DrawInvoiceCompanyAndClientInfo();
|
|
$this->DrawInvoiceInfoTable();
|
|
}
|
|
function AddWatermark()
|
|
{
|
|
$this->SetAlpha(0.2);
|
|
|
|
$imagePath = $this->logoPath . "filigrane_pdf.png";
|
|
list($originalWidth, $originalHeight) = getimagesize($imagePath);
|
|
|
|
// Convertir les dimensions de pixels à mm (1 pixel = 0.264583 mm)
|
|
// Convertir les dimensions de pixels à mm (1 pixel = 0.264583 mm)
|
|
$originalWidth = $originalWidth * 0.264583;
|
|
$originalHeight = $originalHeight * 0.264583;
|
|
|
|
// Augmenter l'échelle, par exemple, 1.5 pour 150% de la taille d'origine
|
|
$scale = 2;
|
|
$width = $originalWidth * $scale;
|
|
$height = $originalHeight * $scale;
|
|
// Calculer la position pour centrer l'image
|
|
$x = (210 - $width) / 2 + 15; // Décalage à droite de 15 mm
|
|
$y = (297 - $height) / 2; // 297 mm est la hauteur d'une page A4
|
|
|
|
// Ajouter l'image en filigrane
|
|
$this->Image($imagePath, $x, $y, $width, $height); // Chemin, position x, position y, largeur, hauteur
|
|
$this->SetAlpha(0.1); // Définir l'opacité
|
|
}
|
|
|
|
function SetAlpha($alpha)
|
|
{
|
|
// Appliquer la transparence au document
|
|
$this->SetFillColor(255, 255, 255, $alpha * 255);
|
|
$this->SetTextColor(0, 0, 0, $alpha * 255);
|
|
$this->SetDrawColor(0, 0, 0, $alpha * 255);
|
|
}
|
|
|
|
function Footer()
|
|
{
|
|
$this->SetY(-34);
|
|
$this->SetFont('ComicSans', '', size: 7);
|
|
$this->MultiCell(0,4,utf8_decode(html_entity_decode('Tout retard de paiement entraînera de plein droit une pénalité de retard de 3 fois le taux légal ( Loi 2008-776 du 4 août 2008) et une indemnité forfaitaire de 40 EUR pour frais de recouvrement sera appliquée.')));
|
|
$this->Ln(1);
|
|
$this->MultiCell(0,4,utf8_decode(html_entity_decode('Si les frais de recouvrement sont supérieurs à ce montant forfaitaire, une indemnisation complémentaire sera due sur présentation de justificatifs ( articles L.441-3 et L.441-6 du code de commerce ).
|
|
')));
|
|
|
|
$this->SetY(-10);
|
|
$this->SetFont('ComicSans', '', 7);
|
|
$this->Cell(0, 10, utf8_decode(html_entity_decode($this->factureData['configuration']->legal_one)), 0, 0, 'C');
|
|
}
|
|
|
|
public function InvoicePdfFactory(array $factureData,$logo = null){
|
|
$this->factureData = $factureData;
|
|
$this->productsCount = $this->factureData["productsCount"];
|
|
$this->devisCount = count($this->factureData['devis']);
|
|
$this->totalPrices = $this->factureData["totalPrices"];
|
|
$this->devisCountToGet = ($this->devisCount <= $this->maxArticlePerPage ) ? $this->devisCount : $this->maxArticlePerPage;
|
|
$this->devisList = $this->factureData['devis'];
|
|
$this->logo = $logo;
|
|
}
|
|
|
|
public function GetInvoiceFilename(){
|
|
$factureNum = $this->factureData['num'];
|
|
$factureNum = str_replace('/','-',$factureNum);
|
|
$clientName = str_replace(' ',' ',$this->factureData['group_name'] ?? '');
|
|
return 'FACTURE'.'_'.$factureNum.'_'.mb_strtoupper($clientName,'UTF-8');
|
|
}
|
|
|
|
public function DrawPageNumbersText($pageNumber,$pageCount){
|
|
$this->SetXY( 120, 5 );
|
|
$this->Cell( 160, 8, $pageNumber . '/' . $pageCount, 0, 0, 'C');
|
|
}
|
|
|
|
public function DrawArticlesTable(){
|
|
$pageCount = ceil($this->devisCount / $this->maxArticlePerPage);
|
|
$pageNumber = 1;
|
|
while($pageNumber <= $pageCount){
|
|
if($pageNumber > 1){
|
|
$this->AddPage();
|
|
$this->DrawPageNumbersText($pageNumber,$pageCount);
|
|
}
|
|
$this->DrawArticlesTableRect();
|
|
$this->DrawArticlesTableHeader();
|
|
$this->DrawArticlesTableValue();
|
|
$pageNumber++;
|
|
}
|
|
}
|
|
|
|
public function DrawInvoiceCompanyInfo(){
|
|
$this->SetY(40);
|
|
$this->SetFont('ComicSans', '', 10);
|
|
$this->Cell(0, 4, FileExportHelpers::FormatTextForExport($this->factureData['configuration']->entreprise), 0, 1);
|
|
$this->Cell(0, 4, FileExportHelpers::FormatTextForExport($this->factureData['configuration_adresse']), 0, 1);
|
|
$this->Cell(0, 4, FileExportHelpers::FormatTextForExport($this->factureData['configuration_adresse_city']), 0, 1);
|
|
$this->Cell(0, 4, FileExportHelpers::FormatTextForExport('Tél : ') . FileExportHelpers::FormatTextForExport($this->factureData['configuration']->telephone),0,1);
|
|
$this->Cell(0, 4, 'Mail : ' . $this->factureData['configuration']->mail, 0, 1);
|
|
}
|
|
public function DrawInvoiceClientInfo(){
|
|
$this->SetFont('ComicSans', '', 10);
|
|
$clientName = $this->factureData['group_name'];
|
|
$clientInfoXAxis = 125;
|
|
|
|
$clientAddress = $this->factureData['client_real_adress'];
|
|
$clientAdressWidth = $this->GetStringWidth($clientAddress);
|
|
$maxWidth = $this->GetPageWidth();
|
|
$availableWidhtForClientInfo = $maxWidth - 10 - $clientInfoXAxis;
|
|
$clientAdressIsMultiline = $clientAdressWidth > $availableWidhtForClientInfo;
|
|
|
|
$clientInfoYAxis = $clientAdressIsMultiline ? 35 : 40;
|
|
$this->SetXY($clientInfoXAxis,$clientInfoYAxis);
|
|
$this->Cell(0, 4, FileExportHelpers::FormatTextForExport($clientName));
|
|
$clientInfoYAxis += 4;
|
|
$this->SetXY($clientInfoXAxis,$clientInfoYAxis);
|
|
$this->MultiCell( 0, 4, trim(FileExportHelpers::FormatTextForExport($clientAddress)));
|
|
if($clientAdressIsMultiline){
|
|
$clientInfoYAxis += 4;
|
|
}
|
|
$clientInfoYAxis += 4;
|
|
$this->SetXY($clientInfoXAxis,$clientInfoYAxis);
|
|
$this->Cell(0, 4, trim(FileExportHelpers::FormatTextForExport($this->factureData['client_adress_city'])));
|
|
}
|
|
|
|
public function DrawInvoiceCompanyAndClientInfo(){
|
|
$this->DrawInvoiceCompanyInfo();
|
|
$this->DrawInvoiceClientInfo();
|
|
|
|
}
|
|
|
|
public function DrawInvoiceInfoTable(){
|
|
$this->setY(67);
|
|
$factureDatePaiement = $this->factureData['date_paiement'];
|
|
$factureDatePaiement = DateTime::createFromFormat('Y-m-d',$factureDatePaiement);
|
|
$factureDateEcheance = $factureDatePaiement;
|
|
$factureDatePaiement = $factureDatePaiement->format('d-m-Y');
|
|
$factureDateEcheance->modify('last day of next month');
|
|
$factureDateEcheance = $factureDateEcheance->format('d-m-Y');
|
|
$this->SetFont('ComicSans', 'B', 11);
|
|
$this->Cell(25, 7, 'DATE', 1, 0, 'C');
|
|
$this->Cell(104, 7, 'CLIENT', 1, 0, 'C');
|
|
$this->Cell(39, 7, 'FACTURE', 1, 0, 'C');
|
|
$this->Cell(36, 7, 'ECHEANCE', 1, 1, 'C');
|
|
|
|
$this->SetFont('ComicSans', '', 10);
|
|
$this->Cell(25, 11, $factureDatePaiement, 1, 0, 'C');
|
|
$this->Cell(104, 11, utf8_decode(html_entity_decode($this->factureData['group_name'])), 1, 0, 'C');
|
|
$this->Cell(39, 11, $this->factureData['num'], 1, 0, 'C');
|
|
$this->Cell(36, 11, $factureDateEcheance, 1, 1, 'C');
|
|
|
|
$this->startingYOfArticlesTable = 88;
|
|
}
|
|
|
|
public function DrawArticlesTableRect(){
|
|
$this->SetLineWidth(0.2);
|
|
$gapBetweenStartingOfArticlesTableAndColumnName = 10;
|
|
$tableHeight = $this->thereIsOrderOrCaseNumber ? 137 : 137 + 10;
|
|
$this->Rect(3, $this->startingYOfArticlesTable, 204, $tableHeight, "D");
|
|
// cadre titre des colonnes
|
|
$this->Line(3, $this->startingYOfArticlesTable + $gapBetweenStartingOfArticlesTableAndColumnName, 207,$this->startingYOfArticlesTable + $gapBetweenStartingOfArticlesTableAndColumnName);
|
|
// les traits verticaux colonnes
|
|
$this->Line(28, $this->startingYOfArticlesTable, 28, 235);
|
|
$this->Line(132, $this->startingYOfArticlesTable, 132, 235);
|
|
$this->Line(157, $this->startingYOfArticlesTable, 157, 235);
|
|
$this->Line(182, $this->startingYOfArticlesTable, 182, 235);
|
|
}
|
|
|
|
public function DrawArticlesTableHeader(){
|
|
$tvaValue = $this->factureData["configuration"]->tva_default;
|
|
$columnNameY = $this->startingYOfArticlesTable + 1;
|
|
$this->SetFont('ComicSans', '', 10);
|
|
$this->SetXY(12, $columnNameY);
|
|
$this->Cell(7, 8, "Date", 0, 0, 'C');
|
|
|
|
$this->SetXY(28, $columnNameY);
|
|
$this->Cell(100, 8, "Description", 0, 0, 'C');
|
|
|
|
$this->SetXY(135, $columnNameY);
|
|
$this->Cell(20, 8, "Prix Uni. HT", 0, 0, 'C');
|
|
|
|
$this->SetXY(159, $columnNameY);
|
|
$this->Cell(20, 8, 'TVA ' . $tvaValue . '%', 0, 0, 'C');
|
|
|
|
$this->SetXY(185, $columnNameY);
|
|
$this->Cell(20, 8, "Prix Uni. TTC", 0, 0, 'C');
|
|
}
|
|
|
|
public function DrawArticlesTableValue(){
|
|
// Set espacement avant de continue
|
|
|
|
$this->SetFont('ComicSans','',10);
|
|
$devisData = $this->factureData['devis'];
|
|
$tvaValue = $this->factureData["configuration"]->tva_default;
|
|
$totalHt = 0;
|
|
$totalTtc = 0;
|
|
$totalTva = 0;
|
|
$yValue = $this->startingYOfArticlesTable + 13;
|
|
$maxDescriptionWidth = 102;
|
|
$currentIndexPosition = $this->currentIndexPosition;
|
|
for($currentIndexPosition;$currentIndexPosition<($this->initialIndexPosition + $this->devisCountToGet);$currentIndexPosition++){
|
|
$currentDevis = $devisData[$currentIndexPosition];
|
|
$devisDate = $currentDevis['devis_date'];
|
|
$devisDate = DateTime::createFromFormat('Y-m-d',$devisDate);
|
|
$devisDate = $devisDate->format('d-m-Y');
|
|
$products = $currentDevis["products"];
|
|
$productIncrement = 0;
|
|
foreach($products as $product){
|
|
$this->SetFont('ComicSans', '', 8);
|
|
|
|
$valueHt = $product['produit_price'] * $product["quantite"];
|
|
$valueTtc = PriceHelpers::calculPriceWithVatValue($valueHt,$tvaValue);
|
|
$totalHt+=$valueHt;
|
|
$totalTtc+=$valueTtc;
|
|
$productDescription = $product["produit_description"] ?? "";
|
|
$dateValue = "";
|
|
if($productIncrement == 0){
|
|
$dateValue = $devisDate;
|
|
$productDescription .= " de " . $currentDevis["defunt_nom"] ?? "";
|
|
}
|
|
$tvaAmount = $valueTtc - $valueHt;
|
|
$this->SetXY( 5,$yValue );
|
|
$this->Cell(5, 6, $dateValue, 0,0);
|
|
|
|
$this->SetXY( 29,$yValue );
|
|
$productDescription = FileExportHelpers::FormatTextForExport($productDescription);
|
|
$productDescriptionWidth = $this->GetStringWidth($productDescription);
|
|
$productDescriptionWidthIsGreaterThanMaxWidth = $productDescriptionWidth > $maxDescriptionWidth;
|
|
$productDescriptionIsMultiline = false;
|
|
if ($productDescriptionWidthIsGreaterThanMaxWidth) {
|
|
$yBefore = $this->GetY();
|
|
$this->MultiCell($maxDescriptionWidth, 6, $productDescription,0,'L');
|
|
$yAfter = $this->GetY();
|
|
$productDescriptionIsMultiline = ($yAfter - $yBefore) > 6;
|
|
if($productDescriptionIsMultiline){
|
|
$this->SetXY($this->GetX(),$yBefore);
|
|
}
|
|
} else {
|
|
$this->Cell($maxDescriptionWidth, 6, $productDescription);
|
|
}
|
|
|
|
|
|
$this->SetXY( 135,$yValue );
|
|
$this->Cell(20, 6, number_format($valueHt,2,'.','').chr(128), 0, 0, 'C');
|
|
|
|
$this->SetXY( 159,$yValue );
|
|
$this->Cell(20, 6, number_format($tvaAmount,2,'.','').chr(128), 0, 0, 'C');
|
|
|
|
$this->SetXY( 182,$yValue );
|
|
$this->Cell(25, 6, number_format($valueTtc,2,'.','').chr(128), 0, 1, 'C');
|
|
$yValue += 6;
|
|
$totalTva += $tvaAmount;
|
|
if($productDescriptionWidthIsGreaterThanMaxWidth){
|
|
$yValue += 6;
|
|
}
|
|
$productIncrement++;
|
|
}
|
|
}
|
|
$this->currentIndexPosition = $currentIndexPosition;
|
|
$this->initialIndexPosition = $this->currentIndexPosition;
|
|
$chargedDevisCount = $this->currentIndexPosition + 1;
|
|
$devisLeftToGet = $this->devisCount - $chargedDevisCount;
|
|
$this->devisCountToGet = ($devisLeftToGet <= $this->maxArticlePerPage) ? $devisLeftToGet + 1 : $this->maxArticlePerPage;
|
|
}
|
|
|
|
public function DrawBankAndTotalPriceInfo(){
|
|
$this->SetY(238);
|
|
$this->SetFont('ComicSans', '', 7);
|
|
$this->MultiCell(0,4,utf8_decode(html_entity_decode("Paiement à votre convenance par chèque à l'ordre de ". $this->factureData['configuration']->entreprise)));
|
|
$this->MultiCell(0,4,utf8_decode(html_entity_decode("en indiquant le numéro de facture, ou par virement :")));
|
|
|
|
$this->Ln(1);
|
|
|
|
//Table IBAN
|
|
$this->SetFont('ComicSans', '', 8);
|
|
$ibanWidth = 62;
|
|
$ibanCursorY = $this->GetY();
|
|
$this->Cell($ibanWidth, 5, 'IBAN : FR76 1360 6000 1436 5418 1800 038', 1, 1, 'C');
|
|
$ibanCursorX = $this->GetX();
|
|
$this->Cell($ibanWidth, 5, 'Code SWIFT : AGRI FR PP 836', 1, 1, 'C');
|
|
|
|
//TABLE HT
|
|
$tableWidth = 40; // Largeur totale de la 2e table (20+20)
|
|
$marginRight = 3; // Marge par rapport au bord droit
|
|
$pageWidth = 210; // Largeur d'une page A4 en mm (portrait)
|
|
|
|
// Position correcte de la 2e table
|
|
$startOfArrayX = $pageWidth - $tableWidth - $marginRight;
|
|
$startOfArrayY = $ibanCursorY - 5;
|
|
|
|
|
|
$totalPriceArray = $this->totalPrices;
|
|
foreach($totalPriceArray as $label => $price){
|
|
$this->SetXY($startOfArrayX,$startOfArrayY);
|
|
$this->Cell(20, 5, $label, 1, 1, 'C');
|
|
$this->SetXY($startOfArrayX + 20,$startOfArrayY);
|
|
$this->Cell(20, 5, number_format($price,2,'.','').chr(128), 1, 1, 'C');
|
|
$startOfArrayY += 5;
|
|
}
|
|
}
|
|
|
|
public function SetFactureContent(){
|
|
$this->AddPage();
|
|
$this->SetMargins(3,0,3);
|
|
$this->DrawArticlesTable();
|
|
$this->DrawBankAndTotalPriceInfo();
|
|
}
|
|
|
|
function MultiAlignCell($w,$h,$text,$border=0,$ln=0,$align='L',$fill=false)
|
|
{
|
|
// Store reset values for (x,y) positions
|
|
$x = $this->GetX() + $w;
|
|
$y = $this->GetY();
|
|
|
|
// Make a call to FPDF's MultiCell
|
|
$this->MultiCell($w,$h,$text,$border,$align,$fill);
|
|
|
|
// Reset the line position to the right, like in Cell
|
|
if( $ln==0 )
|
|
{
|
|
$this->SetXY($x,$y);
|
|
}
|
|
}
|
|
|
|
function NbLines($w, $txt)
|
|
{
|
|
// Compute the number of lines a MultiCell of width w will take
|
|
if(!isset($this->CurrentFont))
|
|
$this->Error('No font has been set');
|
|
$cw = $this->CurrentFont['cw'];
|
|
if($w==0)
|
|
$w = $this->w-$this->rMargin-$this->x;
|
|
$wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
|
|
$s = str_replace("\r",'',(string)$txt);
|
|
$nb = strlen($s);
|
|
if($nb>0 && $s[$nb-1]=="\n")
|
|
$nb--;
|
|
$sep = -1;
|
|
$i = 0;
|
|
$j = 0;
|
|
$l = 0;
|
|
$nl = 1;
|
|
while($i<$nb)
|
|
{
|
|
$c = $s[$i];
|
|
if($c=="\n")
|
|
{
|
|
$i++;
|
|
$sep = -1;
|
|
$j = $i;
|
|
$l = 0;
|
|
$nl++;
|
|
continue;
|
|
}
|
|
if($c==' ')
|
|
$sep = $i;
|
|
$l += $cw[$c];
|
|
if($l>$wmax)
|
|
{
|
|
if($sep==-1)
|
|
{
|
|
if($i==$j)
|
|
$i++;
|
|
}
|
|
else
|
|
$i = $sep+1;
|
|
$sep = -1;
|
|
$j = $i;
|
|
$l = 0;
|
|
$nl++;
|
|
}
|
|
else
|
|
$i++;
|
|
}
|
|
return $nl;
|
|
}
|
|
}
|