2025-03-04 17:31:39 +03:00

313 lines
12 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\Order\PdfHandler;
use DateTime;
use \FPDF;
use OCA\Gestion\Constants\OrderTypeConstant;
use OCA\Gestion\Helpers\FileExportHelpers;
use OCA\Gestion\Helpers\PriceHelpers;
class OrderPdfHandler extends FPDF {
private $orderData = [];
private $logoExist = false;
private $logoPath = "/var/www/html/data/admin/files/.gestion/";
private $orderProducts = [];
private $totalPrices = [];
private $tvaDefault = 20;
private $currentIndexPosition = 0;
private $initialIndexPosition = 0;
private $articlesCountToGet = 0;
private const MAX_ARTICLES_PER_PAGE = 18;
private const DEFAULT_SUBCONTRACTOR_NAME = "SOUS TRAITANT";
function Header()
{
if($this->logoExist){
$this->Image($this->logoPath."logo.png", 10, 10, 50, 25);
}
else{
$this->Cell(55,30,'');
}
$this->DrawOrderCompanyAndClientInfo();
$this->DrawOrderInfoTable();
}
function Footer()
{
$this->DrawTotalPriceInfo($this->totalPrices);
$this->SetY(-40);
$this->SetY(-18);
$this->SetFont('Arial', 'B', 8);
$this->Cell(0, 4, utf8_decode(html_entity_decode($this->orderData['configuration']->legal_one)), 0, 1, 'C');
$this->Cell(0, 4, utf8_decode(html_entity_decode($this->orderData['configuration']->adresse)), 0, 1, 'C');
$this->Cell(0, 4, utf8_decode(html_entity_decode($this->orderData['configuration']->telephone)), 0, 1, 'C');
}
public function OrderPdfFactory(array $factureData,$logoExist = false){
$this->orderData = $factureData;
$this->logoExist = $logoExist;
$this->orderProducts = $this->orderData["products"];
$this->tvaDefault = $this->orderData["configuration"]->tva_default;
$this->totalPrices = $this->orderData["totalPrices"];
$orderProductsCount = sizeof($this->orderProducts);
$this->articlesCountToGet = ($orderProductsCount <= self::MAX_ARTICLES_PER_PAGE ) ? $orderProductsCount : self::MAX_ARTICLES_PER_PAGE;
}
private function DrawOrderCompanyAndClientInfo(){
$this->SetY(40);
$this->SetFont('Arial', '', 12);
$this->Cell(0, 7, FileExportHelpers::FormatTextForExport($this->orderData['configuration']->entreprise), 0, 0);
$this->Cell(0, 7, FileExportHelpers::FormatTextForExport(
$this->orderData['company_name']),
0, 1,'R');
$this->Cell(0, 7, FileExportHelpers::FormatTextForExport($this->orderData['configuration_adresse']), 0, 0);
$this->Cell(0, 7, trim(FileExportHelpers::FormatTextForExport($this->orderData['client_name'])), 0, 1,'R');
$this->Cell(0, 7, FileExportHelpers::FormatTextForExport($this->orderData['configuration_adresse_city']), 0, 0);border:
$this->Cell(0, 7, trim(FileExportHelpers::FormatTextForExport($this->orderData['client_real_adress'])), 0, 1,'R');
$this->Cell(0, 7, FileExportHelpers::FormatTextForExport('Tél : ') . FileExportHelpers::FormatTextForExport($this->orderData['configuration']->telephone),0,0);
$this->Cell(0, 7, trim(FileExportHelpers::FormatTextForExport($this->orderData['client_adress_city'])), 0, 1,'R');
$this->Cell(0, 7, 'Mail : ' . $this->orderData['configuration']->mail, 0, 0);
$this->Cell(0, 7, FileExportHelpers::FormatTextForExport('Numéro') . ' Siret: ' . $this->orderData['client_legal_one'], 0, 1,'R');
$this->Cell(0, 7, 'Mail : ' . FileExportHelpers::FormatTextForExport($this->orderData['client_mail']), 0, 1,'R');
$this->Ln(3);
}
private function DrawOrderInfoTable(){
$this->SetFont('Arial', 'B', 11);
$this->Cell(45, 7, 'DATE DU COMMANDE', 1, 0, 'C');
$this->Cell(65, 7, 'LIEU', 1, 0, 'C');
$this->Cell(40, 7, 'COMMANDE', 1, 0, 'C');
$this->Cell(40, 7, 'DATE DE SOINS', 1, 1, 'C');
$this->SetFont('Arial', '', 10);
$this->Cell(45, 7, $this->orderData["order_date"], 1, 0, 'C');
$this->Cell(65, 7, FileExportHelpers::FormatTextForExport($this->orderData['lieu_nom'] ?? ""), 1, 0, 'C');
$this->Cell(40, 7, $this->orderData['order_full_number'], 1, 0, 'C');
$this->Cell(40, 7, $this->orderData["devis_date"], 1, 1, 'C');
$this->Ln(4);
}
private function DrawArticlesTableRect(){
$this->SetLineWidth(0.1);
$this->Rect(10, 105, 190, 120, "D");
// cadre titre des colonnes
$this->Line(10, 115, 200,115);
// les traits verticaux colonnes
$this->Line(35, 105, 35, 225);
$this->Line(135, 105, 135, 225);
$this->Line(155, 105, 155, 225);
$this->Line(175, 105, 175, 225);
}
private function DrawArticlesTableHeader(){
$tvaValue = $this->orderData["configuration"]->tva_default;
$this->SetFont('Arial','',10);
$this->SetXY( 10,106 );
$this->Cell( 24, 8, "Date", 0, 0, 'C');
$this->SetXY( 35,106 );
$this->Cell( 100, 8, "Description", 0, 0, 'C');
$this->SetXY( 135,106 );
$this->Cell( 20, 8, "Prix Uni. HT", 0, 0, 'C');
$this->SetXY( 155,106 );
$this->Cell( 20, 8, 'TVA ' . $tvaValue . '%', 0, 0, 'C');
$this->SetXY( 175,106 );
$this->Cell( 25, 8, "Prix Uni. TTC", 0, 0, 'C');
}
public function DrawArticlesTableValue(){
$this->SetFont('Arial','',10);
$tvaValue = $this->orderData["configuration"]->tva_default;
$totalHt = 0;
$totalTtc = 0;
$totalTva = 0;
$products = $this->orderData["products"];
// var_dump($products);
$yValue = 116;
$currentIndexPosition = $this->currentIndexPosition;
for($currentIndexPosition;$currentIndexPosition<($this->initialIndexPosition + $this->articlesCountToGet);$currentIndexPosition++){
$product = $products[$currentIndexPosition];
$valueHt = $product['produit_price'] * $product['quantite'];
$valueTtc = PriceHelpers::calculPriceWithVatValue($valueHt,$tvaValue);
$totalHt+=$valueHt;
$totalTtc+=$valueTtc;
$productDescription = $product["produit_reference"];
$dateValue = "";
if($product === end($products) ){
$dateValue = $this->orderData["devis_date"];
if($this->orderData["fk_order_type_key"] == OrderTypeConstant::ORDER_TYPE_DEVIS){
$productDescription .= " de " . $this->orderData["defunt_name_with_sexe"];
}
}
$tvaAmount = $valueTtc - $valueHt;
$this->SetXY( 10,$yValue );
$this->Cell(25, 6, $dateValue, 0,0,'C');
$this->SetXY( 35,$yValue );
$this->MultiAlignCell(100, 6, FileExportHelpers::FormatTextForExport($productDescription),0);
$this->SetXY( 135,$yValue );
$this->Cell(20, 6, number_format($valueHt,2,'.','').chr(128), 0, 0, 'C');
$this->SetXY( 155,$yValue );
$this->Cell(20, 6, number_format($tvaAmount,2,'.','').chr(128), 0, 0, 'C');
$this->SetXY( 175,$yValue );
$this->Cell(25, 6, number_format($valueTtc,2,'.','').chr(128), 0, 1, 'C');
$yValue += 6;
$totalTva += $tvaAmount;
}
$this->currentIndexPosition = $currentIndexPosition;
$this->initialIndexPosition += $this->currentIndexPosition;
$chargedOrderCount = $this->currentIndexPosition + 1;
$articlesLeftToGet = sizeof($this->orderProducts) - $chargedOrderCount;
$this->articlesCountToGet = ($articlesLeftToGet <= self::MAX_ARTICLES_PER_PAGE) ? $articlesLeftToGet + 1 : self::MAX_ARTICLES_PER_PAGE;
}
private function DrawTotalPriceInfo($totalPriceArray){
$this->SetY(230);
$this->SetFont('Arial', '', 11);
$startOfArrayX = 120;
$startOfArrayY = 230;
foreach($totalPriceArray as $label => $price){
$this->SetXY($startOfArrayX,$startOfArrayY);
$this->Cell(40, 7, $label, 1, 1, 'C');
$this->SetXY($startOfArrayX + 40,$startOfArrayY);
$this->Cell(40, 7, number_format($price,2,'.','').chr(128), 1, 1, 'C');
$startOfArrayY += 7;
}
}
private function DrawPageNumbersText($pageNumber,$pageCount){
$this->SetXY( 120, 5 );
$this->Cell( 160, 8, $pageNumber . '/' . $pageCount, 0, 0, 'C');
}
private function DrawArticlesTable(){
$pageCount = ceil(sizeof($this->orderProducts) / self::MAX_ARTICLES_PER_PAGE);
$pageNumber = 1;
while($pageNumber <= $pageCount){
if($pageNumber > 1){
$this->AddPage();
$this->DrawPageNumbersText($pageNumber,$pageCount);
}
$this->DrawArticlesTableRect();
$this->DrawArticlesTableHeader();
$this->DrawArticlesTableValue();
$pageNumber++;
}
}
public function SetOrderContent(){
$this->AddPage();
$this->SetMargins(10,0,right: 10);
$this->DrawArticlesTable();
}
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;
}
}