THANASOFT-DV/gestion/lib/Service/Certificate/CertificateService.php
2025-01-20 19:52:07 +03:00

189 lines
6.7 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 OCA\Gestion\Helpers\DateHelpers;
use OCA\Gestion\Service\Certificate\PdfHandler\CareCertificatePdfHandler;
use OCA\Gestion\Service\Certificate\PdfHandler\PacemakerCertificatePdfHandler;
use OCP\Files\IRootFolder;
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 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_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();
$pdf->AddFont('Corsiva','','Monotype-Corsiva-Regular.php');
$pdf->AddFont('Corsiva Bold','','Monotype-Corsiva-Bold.php');
$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;
}
private function getPacemakerCertificateFolder($devisOfDefunt){
$folder = 'CLIENTS/'
.strtoupper($devisOfDefunt["client_entreprise"])
.'/DEFUNTS/'
.strtoupper($devisOfDefunt["defunt_nom"]).'/'
.'ATTESTATION/';
return $folder;
}
private function getPacemakerCertificateFilename($devisOfDefunt){
$filename = 'ATTESTATION_PACEMAKER_'.strtoupper($devisOfDefunt['defunt_nom']);
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["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).'/';
$pacemakerCertificateFolder = $this->getPacemakerCertificateFolder($devisOfDefunt);
$folderDestination = $clean_folder.$pacemakerCertificateFolder;
$pdfFilename = $this->getPacemakerCertificateFilename($devisOfDefunt);
$filenamePath = $clean_folder.$pacemakerCertificateFolder.$pdfFilename.'.pdf';
$pdf = new PacemakerCertificatePdfHandler();
$signatureImageExist = $this->signatureImageExists();
$pdf->SetPacemakerCertificateData($devisOfDefunt,$logo,$signatureImageExist);
$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;
}
}