THANASOFT-HFC/gestion/lib/Service/Certificate/CertificateService.php

286 lines
11 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\Certificate;
use DateTime;
use DateTimeImmutable;
use OCA\Gestion\Db\Bdd;
use OCP\Files\IRootFolder;
use OCA\Gestion\Helpers\DateHelpers;
use OCA\Gestion\Service\Certificate\PdfHandler\CareCertificatePdfHandler;
use OCA\Gestion\Service\Certificate\PdfHandler\PacemakerCertificatePdfHandler;
use OCA\Gestion\Service\Certificate\PdfHandler\PacemakerAbsentCertificatePdfHandler;
class CertificateService
{
/** @var Bdd */
private $gestionBdd;
/** @var IRootFolder */
private $rootFolder;
private const DEFAULT_NEXTCLOUD_ADMIN = "admin";
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 tamponImageExist()
{
$storage = $this->rootFolder->getUserFolder(self::DEFAULT_NEXTCLOUD_ADMIN);
try {
if(isset($storage)) {
$storage->get("/.gestion/sign.jpg"); // tampon image
$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());
}
private function setDevisOfDefuntDefaultValue($devisOfDefunt)
{
$devisOfDefunt["devis_date"] = new DateTimeImmutable($devisOfDefunt["devis_date"]);
$locationOfDevis = "";
if($devisOfDefunt['lieu_nom'] != null) {
$locationOfDevis .= $devisOfDefunt['lieu_nom'];
}
if($devisOfDefunt['lieu_adresse'] != null) {
$locationOfDevis .= " ". $devisOfDefunt['lieu_adresse'];
}
$devisOfDefunt['location_of_devis'] = $locationOfDevis;
if($devisOfDefunt['thanato_nom'] == null) {
$devisOfDefunt['thanato_nom'] = "";
}
if($devisOfDefunt['thanato_prenom'] == null) {
$devisOfDefunt['thanato_prenom'] = "";
}
if($devisOfDefunt['thanato_reference'] == null) {
$devisOfDefunt['thanato_reference'] = "";
}
if($devisOfDefunt['client_nom'] == null) {
$devisOfDefunt['client_nom'] = "";
}
if($devisOfDefunt['client_prenom'] == null) {
$devisOfDefunt['client_prenom'] = "";
}
if($devisOfDefunt['client_entreprise'] == null) {
$devisOfDefunt['client_entreprise'] = "";
}
if($devisOfDefunt['client_adresse'] == null) {
$devisOfDefunt['client_adresse'] = "";
}
return $devisOfDefunt;
}
public function generateCareCertificate($defuntId, $idNextCloud)
{
$storage = $this->rootFolder->getUserFolder($idNextCloud);
$configs = json_decode($this->gestionBdd->getConfiguration(self::DEFAULT_NEXTCLOUD_ADMIN));
$currentConfig = $configs[0];
$logo = $this->getLogo();
$devisOfDefunt = $this->gestionBdd->getDevisOfDefunt($defuntId);
if($devisOfDefunt == null) {
return null;
}
$devisOfDefunt["configuration"] = $currentConfig;
$devisOfDefunt = $this->setDevisOfDefuntDefaultValue($devisOfDefunt);
$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();
$pdf->AddFont('ComicSans', '', 'Comic Sans MS.php');
$pdf->AddFont('ComicSans', 'B', 'comic-sans-bold.php');
$signatureImageExist = $this->signatureImageExists();
$tamponImageExist = $this->tamponImageExist();
$pdf->SetCareCertificateData($devisOfDefunt, $logo, $signatureImageExist, $tamponImageExist);
$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/'
.mb_strtoupper($devisOfDefunt["client_nom"], 'UTF-8')
.'/DEFUNTS/'
.mb_strtoupper($devisOfDefunt["defunt_nom"], 'UTF-8').'/'
.'ATTESTATION/';
return $careCertificateFolder;
}
private function GetCareCertificateFilename($devisOfDefunt)
{
$filename = 'ATTESTATION_SOIN_'.mb_strtoupper($devisOfDefunt['defunt_nom'], 'UTF-8');
return $filename;
}
private function getPacemakerCertificateFolder($devisOfDefunt)
{
$folder = 'CLIENTS/'
.mb_strtoupper($devisOfDefunt["client_nom"], 'UTF-8')
.'/DEFUNTS/'
.mb_strtoupper($devisOfDefunt["defunt_nom"], 'UTF-8').'/'
.'ATTESTATION/';
return $folder;
}
private function getPacemakerCertificateFilename($devisOfDefunt)
{
$filename = 'ATTESTATION_PACEMAKER_'.mb_strtoupper($devisOfDefunt['defunt_nom'], 'UTF-8');
return $filename;
}
public function generatePacemakerCertificate($defuntId, $idNextCloud)
{
$storage = $this->rootFolder->getUserFolder($idNextCloud);
$configs = json_decode($this->gestionBdd->getConfiguration(self::DEFAULT_NEXTCLOUD_ADMIN));
$currentConfig = $configs[0];
$logo = $this->getLogo();
$devisOfDefunt = $this->gestionBdd->getDevisOfDefunt($defuntId);
if($devisOfDefunt == null) {
return null;
}
$devisOfDefunt["configuration"] = $currentConfig;
$devisOfDefunt = $this->setDevisOfDefuntDefaultValue($devisOfDefunt);
$clean_folder = html_entity_decode(string: $currentConfig->path).'/';
$pacemakerCertificateFolder = $this->getPacemakerCertificateFolder($devisOfDefunt);
$folderDestination = $clean_folder.$pacemakerCertificateFolder;
$pdfFilename = $this->getPacemakerCertificateFilename($devisOfDefunt);
$filenamePath = $clean_folder.$pacemakerCertificateFolder.$pdfFilename.'.pdf';
$pdf = new PacemakerCertificatePdfHandler();
$pdf->AddFont('ComicSans', '', 'Comic Sans MS.php');
$pdf->AddFont('ComicSans', 'B', 'comic-sans-bold.php');
$signatureImageExist = $this->signatureImageExists();
$tamponImageExist = $this->tamponImageExist();
$pdf->SetPacemakerCertificateData($devisOfDefunt, $logo, $signatureImageExist, $tamponImageExist);
$pdf->SetPacemakerCertificate();
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 getPacemakerAbsentCertificateFilename($devisOfDefunt)
{
$filename = 'ATTESTATION_ABSENCE_PACEMAKER_'.mb_strtoupper($devisOfDefunt['defunt_nom'], 'UTF-8');
return $filename;
}
public function generatePacemakerAbsentCertificate($defuntId, $idNextCloud)
{
$storage = $this->rootFolder->getUserFolder($idNextCloud);
$configs = json_decode($this->gestionBdd->getConfiguration(self::DEFAULT_NEXTCLOUD_ADMIN));
$currentConfig = $configs[0];
$logo = $this->getLogo();
$devisOfDefunt = $this->gestionBdd->getDevisOfDefunt($defuntId);
if($devisOfDefunt == null) {
return null;
}
$devisOfDefunt["configuration"] = $currentConfig;
$devisOfDefunt = $this->setDevisOfDefuntDefaultValue($devisOfDefunt);
$clean_folder = html_entity_decode(string: $currentConfig->path).'/';
$pacemakerCertificateFolder = $this->getPacemakerCertificateFolder($devisOfDefunt);
$folderDestination = $clean_folder.$pacemakerCertificateFolder;
$pdfFilename = $this->getPacemakerAbsentCertificateFilename($devisOfDefunt);
$filenamePath = $clean_folder.$pacemakerCertificateFolder.$pdfFilename.'.pdf';
$pdf = new PacemakerAbsentCertificatePdfHandler();
$pdf->AddFont('ComicSans', '', 'Comic Sans MS.php');
$pdf->AddFont('ComicSans', 'B', 'comic-sans-bold.php');
$signatureImageExist = $this->signatureImageExists();
$tamponImageExist = $this->tamponImageExist();
$pdf->SetPacemakerCertificateData($devisOfDefunt, $logo, $signatureImageExist, $tamponImageExist);
$pdf->SetPacemakerCertificate();
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;
}
}