* * @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\Devis\Pdf; use DateTime; 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(' ',' ',$devisData['defunt_nom']); $devisLocation = str_replace(' ',' ',$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->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_entreprise"]).'/'; $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){ $filename = ""; foreach($multipleDevisData as $devis){ $filename = strtoupper($devis["client_entreprise"]); $filename .= $month != 0 ? '_'.DateHelpers::GetMonthPlainString($month) :''; $filename .= "_".$year; break; } return $filename; } public function generateMultipleDevisPdfByClientAndMonthYear($clientId,$month,$year,$idNextCloud){ $storage = $this->rootFolder->getUserFolder($idNextCloud); $configs = json_decode($this->gestionBdd->getConfiguration(self::DEFAULT_NEXTCLOUD_ADMIN)); $currentConfig = $configs[0]; $logo = $this->getLogo(); $mulitpleDevisData = $this->gestionBdd->getDevisPdfDataByClientAndMonthYear($clientId,$month,$year,$currentConfig); if(empty($mulitpleDevisData)){ return null; } $multipleDevisDataFormatted = $this->formatMultipleDevisToPdfFormat($mulitpleDevisData,$currentConfig); $pdf = new DevisPdfHandler(); $pdf->SetMultipleDevisPdfData($multipleDevisDataFormatted,$logo); $pdf->SetMultipleDevisContent(); $racinePath = html_entity_decode(string: $currentConfig->path).'/'; $clientRacineFolder = $racinePath.'CLIENTS/'.strtoupper($multipleDevisDataFormatted[0]["client_entreprise"]).'/'; $filename = 'DEVIS'.'_'.$this->GetMultipleDevisFilename($multipleDevisDataFormatted,$month,$year); $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; } }