* * @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; use DateTime; use OCA\Gestion\Db\Bdd; use OCA\Gestion\Helpers\DateHelpers; use OCP\Files\IRootFolder; class InvoicePdfService { /** @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()); } public function generateFacturePdfByFactureId($factureId,$idNextCloud){ $storage = $this->rootFolder->getUserFolder($idNextCloud); $configs = json_decode($this->gestionBdd->getConfiguration(self::DEFAULT_NEXTCLOUD_ADMIN)); $currentConfig = $configs[0]; $logo = $this->getLogo(); $invoicePdfData = $this->gestionBdd->getInvoicePdfData($factureId,$currentConfig); if($invoicePdfData == null){ return ""; } $clean_folder = html_entity_decode($currentConfig->path).'/'; $factureFolders = $this->getFacturesFolder($invoicePdfData,$clean_folder); $pdf = new InvoicePdfHandler(); $pdf->InvoicePdfFactory($invoicePdfData,$logo); $pdfContent = $pdf->GetFactureContent(); $pdfFilename = $pdf->GetFilename(); $filenames = []; foreach($factureFolders 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 getFacturesFolder(array $factureData,$racinePath){ $clientRacineFolder = $racinePath.'CLIENTS/'.strtoupper($factureData["client_entreprise"]).'/'; $defuntsFolder = $clientRacineFolder.'DEFUNTS/'.strtoupper($factureData['defunt_nom']).'/'.'FACTURES'.'/'; $devisDate = $factureData['devis_date']; $devisDatetime = new DateTime($devisDate); $devisDateYear = $devisDatetime->format('Y'); $devisMonth = DateHelpers::GetDateWithFormatDayAndMonthPlainString($factureData['devis_date']); $factureByYearFolder = $clientRacineFolder."$devisDateYear".'/'.$devisMonth.'/'.'FACTURES'.'/'; return [ $defuntsFolder, $factureByYearFolder ]; } public function generateFacturePdfByFactureIds(array $factureIds,$idNextCloud){ foreach( $factureIds as $factureId ){ $this->generateFacturePdfByFactureId($factureId,$idNextCloud); } } }