THANASOFT-DV/gestion/lib/Service/Devis/Pdf/DevisPdfService.php

217 lines
8.6 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\Devis\Pdf;
use DateTime;
use OCA\Gestion\Constants\DevisExportTypeConstant;
use OCA\Gestion\Db\Bdd;
use OCA\Gestion\Helpers\DateHelpers;
use OCA\Gestion\Helpers\FileExportHelpers;
use OCP\Files\IRootFolder;
class DevisPdfService {
/** @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 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 getDevisPdfFilename($devisData){
$filename = "DEVIS-";
$defuntNom = str_replace('&nbsp;',' ',$devisData['defunt_nom']);
$devisLocation = str_replace('&nbsp;',' ',$devisData['lieu_nom']);
return $filename.$defuntNom.'-'.$devisLocation;
}
private function formatMultipleDevisToPdfFormat($multipleDevisData,$configuration){
foreach($multipleDevisData as &$devis){
$devis = $this->formatDevisDataToPdfDataFormat($devis,$configuration);
}
return $multipleDevisData;
}
private function formatDevisDataToPdfDataFormat($devis,$configuration){
$devisDate = $devis['devis_date'];
$devisDate = DateTime::createFromFormat('Y-m-d',$devisDate);
$devisDate = $devisDate->format('d-m-Y');
$devis['devis_date'] = $devisDate;
$devis['configuration'] = $configuration;
$products = $this->gestionBdd->getDevisProduits($devis["devis_id"]);
$devis = $this->gestionBdd->setDevisStartAndEndTime($devis);
$firstClient = $this->gestionBdd->getFirstClient();
$devis["siret"] = $firstClient != null ? $firstClient['legal_one'] : '';
$devis["products"] = $products;
$clientAdresses = FileExportHelpers::GetAddressAndCityFromAddress($devis["client_adresse"]);
$devis["client_real_adress"] = $clientAdresses["address"];
$devis["client_adress_city"] = $clientAdresses["city"];
$configurationAdresses = FileExportHelpers::GetAddressAndCityFromAddress($configuration->adresse);
$devis["configuration_adresse"] = $configurationAdresses["address"];
$devis["configuration_adresse_city"] = $configurationAdresses["city"];
return $devis;
}
public function generateDevisPdfByDevisId($devisId,$idNextCloud){
$storage = $this->rootFolder->getUserFolder($idNextCloud);
$configs = json_decode($this->gestionBdd->getConfiguration(self::DEFAULT_NEXTCLOUD_ADMIN));
$currentConfig = $configs[0];
$logo = $this->getLogo();
$devisPdfData = $this->gestionBdd->getDevisPdfDataByDevisId($devisId);
if($devisPdfData == null){
return null;
}
$devisPdfDataFormatted = $this->formatDevisDataToPdfDataFormat($devisPdfData,$currentConfig);
$clean_folder = html_entity_decode(string: $currentConfig->path).'/';
$devisPdfFolders = $this->getDevisPdfFolder($devisPdfDataFormatted,$clean_folder);
$pdf = new DevisPdfHandler();
$pdf->AddFont('ComicSans','','Comic Sans MS.php');
$pdf->AddFont('ComicSans','B','comic-sans-bold.php');
$pdf->SetDevisPdfData($devisPdfDataFormatted,$logo);
$pdf->SetDevisContent();
$pdfContent = $pdf->Output('','S');
$pdfFilename = $this->getDevisPdfFilename($devisPdfDataFormatted);
$filenames = [];
foreach($devisPdfFolders as $folder){
try {
$storage->newFolder($folder);
}
catch(\OCP\Files\NotPermittedException $e) {
}
$ff_pdf = $folder.$pdfFilename.'.pdf';
$storage->newFile($ff_pdf);
$file_pdf = $storage->get($ff_pdf);
$file_pdf->putContent($pdfContent);
$filenames[] = $ff_pdf;
}
return $filenames;
}
private function getDevisPdfFolder(array $devisPdfData,$racinePath){
$clientRacineFolder = $racinePath.'CLIENTS/'.strtoupper($devisPdfData["client_nom"]).'/';
$defuntsFolder = $clientRacineFolder.'DEFUNTS/'.strtoupper($devisPdfData['defunt_nom']).'/'.'DEVIS'.'/';
$devisDate = $devisPdfData['devis_date'];
$devisDatetime = new DateTime($devisDate);
$devisDateYear = $devisDatetime->format('Y');
$devisMonth = DateHelpers::GetDateWithFormatDayAndMonthPlainString($devisPdfData['devis_date']);
$devisByYearFolder = $clientRacineFolder."$devisDateYear".'/'.$devisMonth.'/'.'DEVIS'.'/';
return [
$defuntsFolder,
$devisByYearFolder
];
}
private function GetMultipleDevisFilename($multipleDevisData,$month,$year,$type = DevisExportTypeConstant::TYPE_SINGLE){
$filename = "";
foreach($multipleDevisData as $devis){
if($type == DevisExportTypeConstant::TYPE_SINGLE){
$filename = mb_strtoupper($devis["client_nom"],'UTF-8');
}
else{
$filename = mb_strtoupper($devis["group_name"],'UTF-8');
}
$filename .= $month != 0 ? '_'.DateHelpers::GetMonthPlainString($month) :'';
$filename .= "_".$year;
break;
}
return $filename;
}
public function generateMultipleDevisPdfByClientAndMonthYear($clientId,$month,$year,$type,$idNextCloud){
$storage = $this->rootFolder->getUserFolder($idNextCloud);
$configs = json_decode($this->gestionBdd->getConfiguration(self::DEFAULT_NEXTCLOUD_ADMIN));
$currentConfig = $configs[0];
$logo = $this->getLogo();
if($type == DevisExportTypeConstant::TYPE_SINGLE){
$multipleDevisData = $this->gestionBdd->getDevisPdfDataByClientAndMonthYear($clientId,$month,$year,$currentConfig);
}
else{
$multipleDevisData = $this->gestionBdd->getDevisPdfDataByClientGroupFacturationAndMonthYear(
$clientId,
$month,
$year,
$currentConfig
);
}
if(empty($multipleDevisData)){
return null;
}
$multipleDevisDataFormatted = $this->formatMultipleDevisToPdfFormat($multipleDevisData,$currentConfig);
if($type == DevisExportTypeConstant::TYPE_SINGLE){
$clientFolderName = mb_strtoupper($multipleDevisDataFormatted[0]["client_nom"] ?? "-","UTF-8");
}
else{
$clientFolderName = mb_strtoupper($multipleDevisDataFormatted[0]["group_name"] ?? "-","UTF-8");
}
$pdf = new DevisPdfHandler();
$pdf->AddFont('ComicSans','','Comic Sans MS.php');
$pdf->AddFont('ComicSans','B','comic-sans-bold.php');
$pdf->SetMultipleDevisPdfData($multipleDevisDataFormatted,$logo);
$pdf->SetMultipleDevisContent();
$racinePath = html_entity_decode(string: $currentConfig->path).'/';
$clientRacineFolder = $racinePath.'CLIENTS/'.$clientFolderName.'/';
$filename = 'DEVIS'.'_'.$this->GetMultipleDevisFilename($multipleDevisDataFormatted,$month,$year,$type);
$filenamePath = $clientRacineFolder.$filename.'.pdf';
$pdfContent = $pdf->Output('','S');
try {
$storage->newFolder($clientRacineFolder);
}
catch(\OCP\Files\NotPermittedException $e) {
}
$storage->newFile($filenamePath);
$file_pdf = $storage->get($filenamePath);
$file_pdf->putContent($pdfContent);
return $filenamePath;
}
}