diff --git a/gestion/appinfo/routes.php b/gestion/appinfo/routes.php index e42eafc..f7187b8 100644 --- a/gestion/appinfo/routes.php +++ b/gestion/appinfo/routes.php @@ -135,5 +135,10 @@ return [ //devis pdf ['name' => 'page#exportDevisToPdf', 'url' => '/devis/exportDevisToPdf', 'verb' => 'POST'], ['name' => 'page#exportDevisByClientAndMonthYearToPdf', 'url' => '/devis/exportDevisByClientAndMonthYearToPdf', 'verb' => 'POST'], + + + //certificate + ['name' => 'page#exportCareCertificate', 'url' => '/defunt/exportCareCertificate', 'verb' => 'POST'], + ] ]; \ No newline at end of file diff --git a/gestion/lib/Controller/PageController.php b/gestion/lib/Controller/PageController.php index e19cb80..b77a61c 100644 --- a/gestion/lib/Controller/PageController.php +++ b/gestion/lib/Controller/PageController.php @@ -17,6 +17,7 @@ use \Datetime; use \DatetimeImmutable; use \IntlDateFormatter; use \FPDF; +use OCA\Gestion\Service\Certificate\CertificateService; use OCA\Gestion\Service\Devis\Pdf\DevisPdfService; use OCA\Gestion\Service\ExportClientStatisticService; use OCA\Gestion\Service\ExportThanatoStatisticService; @@ -52,6 +53,9 @@ class PageController extends Controller { /** @var DevisPdfService */ private $devisPdfService; + /** @var CertificateService */ + private $certificateService; + /** * Constructor */ @@ -68,7 +72,8 @@ class PageController extends Controller { ExportThanatoStatisticService $exportThanatoStatisticService, ExportClientStatisticService $exportClientStatisticService, InvoicePdfService $invoicePdfService, - DevisPdfService $devisPdfService){ + DevisPdfService $devisPdfService, + CertificateService $certificateService){ parent::__construct($AppName, $request); @@ -81,6 +86,7 @@ class PageController extends Controller { $this->exportClientStatisticService = $exportClientStatisticService; $this->invoicePdfService = $invoicePdfService; $this->devisPdfService = $devisPdfService; + $this->certificateService = $certificateService; //$this->fpdf = $fpdf; if ($userSession->isLoggedIn()) { @@ -2734,4 +2740,19 @@ class PageController extends Controller { catch(\OCP\Files\NotFoundException $e) { } } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @param int $defuntId + */ + + public function exportCareCertificate($defuntId){ + try{ + $careCertificateFilename = $this->certificateService->generateCareCertificate($defuntId,$this->idNextcloud); + return $careCertificateFilename; + } + catch(\OCP\Files\NotFoundException $e) { } + + } } diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index 9b5c4c2..0df96b1 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -2378,4 +2378,38 @@ class Bdd { return $devisList; } + public function getDevisOfDefunt($defuntId){ + $sql = "SELECT + devis.id as id, + devis.date as devis_date, + devis.id_defunt as defunt_id, + devis.id_lieu as lieu_id, + lieu.nom as lieu_nom, + lieu.adresse as lieu_adresse, + defunt.nom as defunt_nom, + defunt.sexe as defunt_sexe, + client.nom as client_nom, + client.prenom as client_prenom, + client.entreprise as client_entreprise, + client.adresse as client_adresse, + thanato.nom as thanato_nom, + thanato.prenom as thanato_prenom, + thanato.reference as thanato_reference, + thanato.date_habilitation as thanato_date_habilitation + FROM ".$this->tableprefix."devis as devis + LEFT JOIN ".$this->tableprefix."lieu as lieu on devis.id_lieu = lieu.id + LEFT JOIN ".$this->tableprefix."defunt as defunt on devis.id_defunt = defunt.id + LEFT JOIN ".$this->tableprefix."client as client on devis.id_client = client.id + LEFT JOIN ".$this->tableprefix."thanato as thanato on devis.id_thanato = thanato.id + WHERE devis.id_defunt = ? ;"; + + $devisOfDefunt = $this->execSQLNoJsonReturn($sql, [$defuntId]); + + if(!empty( $devisOfDefunt)){ + return $devisOfDefunt[0]; + } + + return null; + } + } diff --git a/gestion/lib/Service/Certificate/CertificateService.php b/gestion/lib/Service/Certificate/CertificateService.php new file mode 100644 index 0000000..38c1590 --- /dev/null +++ b/gestion/lib/Service/Certificate/CertificateService.php @@ -0,0 +1,136 @@ + + * + * @author Anna Larch + * @author Richard Steinmetz + * + * 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 . + * + */ + +namespace OCA\Gestion\Service\Certificate; + +use DateTime; +use DateTimeImmutable; +use OCA\Gestion\Db\Bdd; +use OCA\Gestion\Helpers\DateHelpers; +use OCA\Gestion\Service\Certificate\PdfHandler\CareCertificatePdfHandler; +use OCP\Files\IRootFolder; + +class CertificateService { + /** @var Bdd */ + private $gestionBdd; + + /** @var IRootFolder */ + private $rootFolder; + + private const DEFAULT_NEXTCLOUD_ADMIN = "admin"; + private const DEFAULT_NEXTCLOUD_H2F_ADMIN = "Emmanuelle"; + public function __construct( + Bdd $gestionBdd, + IRootFolder $rootFolder) { + $this->gestionBdd = $gestionBdd; + $this->rootFolder = $rootFolder; + } + + private function signatureImageExists(){ + $storage = $this->rootFolder->getUserFolder(self::DEFAULT_NEXTCLOUD_ADMIN); + try{ + if(isset($storage)){ + $storage->get("/.gestion/sign.png"); + $signatureExist = true; + }else{ + $signatureExist = false; + } + } + catch(\OCP\Files\NotFoundException $e) { + $signatureExist = false; + } + return $signatureExist; + } + + private function getLogo(){ + $storage = $this->rootFolder->getUserFolder(self::DEFAULT_NEXTCLOUD_ADMIN); + try{ + try { + if(isset($storage)){ + $file = $storage->get('/.gestion/logo.png'); + }else{ + return "nothing"; + } + } catch(\OCP\Files\NotFoundException $e) { + $file = $storage->get('/.gestion/logo.jpeg'); + } + } + catch(\OCP\Files\NotFoundException $e) { + return "nothing"; + } + + return base64_encode($file->getContent()); + } + + public function generateCareCertificate($defuntId,$idNextCloud){ + $storage = $this->rootFolder->getUserFolder($idNextCloud); + $configs = json_decode($this->gestionBdd->getConfiguration(self::DEFAULT_NEXTCLOUD_H2F_ADMIN)); + $currentConfig = $configs[0]; + $logo = $this->getLogo(); + $devisOfDefunt = $this->gestionBdd->getDevisOfDefunt($defuntId); + if($devisOfDefunt == null){ + return null; + } + $devisOfDefunt["configuration"] = $currentConfig; + $devisOfDefunt["thanato_date_habilitation"] = new DateTimeImmutable($devisOfDefunt["thanato_date_habilitation"]); + $devisOfDefunt["devis_date"] = new DateTimeImmutable($devisOfDefunt["devis_date"]); + $clean_folder = html_entity_decode(string: $currentConfig->path).'/'; + $careCertificateFolder = $this->getCareCertificateFolder($devisOfDefunt); + $folderDestination = $clean_folder.$careCertificateFolder; + $pdfFilename = $this->GetCareCertificateFilename($devisOfDefunt); + $filenamePath = $clean_folder.$careCertificateFolder.$pdfFilename.'.pdf'; + $pdf = new CareCertificatePdfHandler(); + $signatureImageExist = $this->signatureImageExists(); + $pdf->SetCareCertificateData($devisOfDefunt,$logo,$signatureImageExist); + $pdf->SetCareCertificate(); + try { + $storage->newFolder($folderDestination); + } + catch(\OCP\Files\NotPermittedException $e) { + } + $pdfContent = $pdf->Output('','S'); + $storage->newFile($filenamePath); + $pdfFile = $storage->get($filenamePath); + $pdfFile->putContent($pdfContent); + return $filenamePath; + } + + private function getCareCertificateFolder($devisOfDefunt){ + $careCertificateFolder = 'CLIENTS/' + .strtoupper($devisOfDefunt["client_entreprise"]) + .'/DEFUNTS/' + .strtoupper($devisOfDefunt["defunt_nom"]).'/' + .'ATTESTATION/'; + + return $careCertificateFolder; + } + + private function GetCareCertificateFilename($devisOfDefunt){ + $filename = 'ATTESTATION_SOIN_'.strtoupper($devisOfDefunt['defunt_nom']); + return $filename; + } + +} diff --git a/gestion/lib/Service/Certificate/PdfHandler/CareCertificatePdfHandler.php b/gestion/lib/Service/Certificate/PdfHandler/CareCertificatePdfHandler.php new file mode 100644 index 0000000..e5a2b15 --- /dev/null +++ b/gestion/lib/Service/Certificate/PdfHandler/CareCertificatePdfHandler.php @@ -0,0 +1,178 @@ + + * + * @author Anna Larch + * @author Richard Steinmetz + * + * 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 . + * + */ + +namespace OCA\Gestion\Service\Certificate\PdfHandler; + +use DateTime; +use \FPDF; +use OCA\Gestion\Helpers\DateHelpers; +use OCA\Gestion\Helpers\FileExportHelpers; +use OCA\Gestion\Helpers\PriceHelpers; + +class CareCertificatePdfHandler extends FPDF { + + private $devisOfDefunt = []; + private $logo = null; + private $signatureImageExist = false; + private $imagePath = "/var/www/html/data/admin/files/.gestion/"; + + function Header() + { + if($this->logo != "nothing"){ + $this->Image($this->imagePath."logo.png", 10, 10, 75, 25); + } + else{ + $this->Cell(55,30,''); + } + } + function Footer() + { + $this->SetY(-18); + $this->SetFont('Arial', '', 10); + $this->MultiCell(0, 5, utf8_decode(html_entity_decode($this->devisOfDefunt['configuration']->legal_one)), 0, 'C'); + $this->MultiCell(0, 5, utf8_decode(html_entity_decode($this->devisOfDefunt['configuration']->adresse)), 0,'C'); + } + + public function SetCareCertificateData(array $devisOfDefunt,$logo = null,$signatureImageExist = false){ + $this->devisOfDefunt = $devisOfDefunt; + $this->logo = $logo; + $this->signatureImageExist = $signatureImageExist; + } + + public function SetCareCertificate(){ + $this->AddPage(); + $this->SetMargins(left:20,top:0,right:20); + $this->SetCareCertificateTitle(); + $this->SetCareCertificateContent(); + $this->SetSigning(); + } + + private function SetSigning(){ + $this->SetXY(140,$this->GetY() + 30); + $this->Cell(0,10,'Cachet et signature'); + + if($this->signatureImageExist){ + $this->Image($this->imagePath."sign.png", 135, $this->GetY() + 12, 55, 30); + } + } + + private function SetCareCertificateContent(){ + $this->SetFont('Arial', '', 14); + $this->MultiCell(0,7,FileExportHelpers::FormatTextForExport('La Société '. $this->devisOfDefunt['configuration']->entreprise. ' habilitée sous le numéro ' . $this->devisOfDefunt['thanato_reference'] . ' certifie par la présente que: ')); + $this->SetFont('Arial', 'B', 14); + $this->Cell(0,12, 'Mr/Mlle ' . FileExportHelpers::FormatTextForExport($this->devisOfDefunt['thanato_nom'] . ' ' . $this->devisOfDefunt['thanato_prenom']),0,1); + $this->SetFont('Arial', '', 14); + $this->MultiCell(0,7, FileExportHelpers::FormatTextForExport('Employé au sein de notre société et titulaire du diplôme national de Thanatopracteur a effectué des soins de conservation sur le corps du défunt:')); + $this->SetFont('Arial', 'B', 14); + $this->Cell(0,12, 'M./Mme '.$this->devisOfDefunt['defunt_nom'],0,1); + $this->SetFont('Arial', '', 14); + $this->Cell(0,12, FileExportHelpers::FormatTextForExport("Qui reposait à l'adresse suivante") . ': ',0,1); + $this->SetFont('Arial', 'B', 14); + $this->Cell(0,12, FileExportHelpers::FormatTextForExport($this->devisOfDefunt['configuration']->adresse),0,1); + $this->SetFont('Arial', '', 14); + $this->Cell(0,12, FileExportHelpers::FormatTextForExport("La présente attestion est établie pour faire valoir ce que de droit."),0,5); + $this->Ln(5); + $this->Cell(0,12,FileExportHelpers::FormatTextForExport('Fait à '). FileExportHelpers::FormatTextForExport($this->devisOfDefunt['configuration']->adresse)); + $this->SetX(140); + $this->Cell(0,12,'le '. $this->devisOfDefunt['devis_date']->format('d/m/Y'),0); + } + + private function SetCareCertificateTitle(){ + $this->SetY(60); + $this->SetFont('Arial', 'B', 20); + $this->Cell(0, 10, 'ATTESTATION DE SOINS DE CONSERVATION', 0, 1,'C'); + $this->Ln(20); + } + + 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; + } +}