* * @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; require_once __DIR__ . '/../../../../vendor/autoload.php'; 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(' ', ' ', $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']; $tva = isset($devis["tva"]) ? $devis["tva"] : 1; $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; $devis["tva"] = $tva; $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 ]; } /** * Genere simplement le dossier de defunt et client pour le devis pour le user connecter */ public function generateClientAndDefuntFolder($devisId, $idNextCloud) { $storage = $this->rootFolder->getUserFolder($idNextCloud); $configs = json_decode($this->gestionBdd->getConfiguration(self::DEFAULT_NEXTCLOUD_ADMIN)); $currentConfig = $configs[0]; $devisPdfData = $this->gestionBdd->getDevisPdfDataByDevisId($devisId); if ($devisPdfData == null) { return null; } $clean_folder = html_entity_decode(string: $currentConfig->path) . '/'; $devisPdfDataFormatted = $this->formatDevisDataToPdfDataFormat($devisPdfData, $currentConfig); $devisPdfFolders = $this->getDevisPdfFolder($devisPdfDataFormatted, $clean_folder); foreach ($devisPdfFolders as $folder) { try { $storage->newFolder($folder); } catch (\OCP\Files\NotPermittedException $e) { } } } 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; } }