Thanasoft-H2F/gestion/lib/Service/Order/OrderPdfService.php
2025-02-11 15:56:13 +03:00

140 lines
5.1 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\Order;
use DateTime;
use OCA\Gestion\Constants\BddConstant;
use OCA\Gestion\Db\Bdd;
use OCA\Gestion\Db\OrderBdd;
use OCA\Gestion\Helpers\DateHelpers;
use OCA\Gestion\Helpers\FileExportHelpers;
use OCA\Gestion\Service\Order\PdfHandler\OrderPdfHandler;
use OCP\Files\IRootFolder;
class OrderPdfService {
/** @var Bdd */
private $gestionBdd;
/** @var IRootFolder */
private $rootFolder;
private $orderBdd;
private $orderService;
public function __construct(
Bdd $gestionBdd,
IRootFolder $rootFolder,
OrderBdd $orderBdd,
OrderService $orderService) {
$this->orderBdd = $orderBdd;
$this->gestionBdd = $gestionBdd;
$this->rootFolder = $rootFolder;
$this->orderService = $orderService;
}
private function doesLogoExist(){
$storage = $this->rootFolder->getUserFolder(BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD);
try {
if(isset($storage)){
$storage->get('/.gestion/logo.png');
return true;
}else{
return false;
}
}
catch(\OCP\Files\NotFoundException $e) {
return false;
}
}
private function formatOrderDataForPdfExport($orderPdfData,$config){
$orderPdfData["configuration"] = $config;
$configurationAdresses = FileExportHelpers::GetAddressAndCityFromAddress($config->adresse);
$orderPdfData["configuration_adresse"] = $configurationAdresses["address"];
$orderPdfData["configuration_adresse_city"] = $configurationAdresses["city"];
$orderDate = $orderPdfData['order_date'];
$orderDate = DateTime::createFromFormat('Y-m-d',$orderDate);
$orderDate = $orderDate->format('d-m-Y');
$orderPdfData['order_date'] = $orderDate;
return $orderPdfData;
}
private function getCommandeFolders($orderPdfData,$racinePath){
$clientRacineFolder = $racinePath.'CLIENTS/'.mb_strtoupper($orderPdfData["client_entreprise"],'UTF-8').'/';
$defuntsFolder = $clientRacineFolder.'DEFUNTS/'.mb_strtoupper($orderPdfData['defunt_nom'],'UTF-8').'/'.'FOURNISSEUR'.'/';
$orderDate = $orderPdfData['order_date'];
$orderDatetime = new DateTime($orderDate);
$orderYear = $orderDatetime->format('Y');
$orderMonth = DateHelpers::GetDateWithFormatDayAndMonthPlainString($orderPdfData['order_date']);
$orderByYearFolder = $clientRacineFolder."$orderYear".'/'.$orderMonth.'/'.'FOURNISSEUR'.'/';
return [
$defuntsFolder,
$orderByYearFolder
];
}
private function GetOrderFilename($orderPdfData){
$orderFullNumber = $orderPdfData['order_full_number'];
$orderFullNumber = str_replace('/','-',$orderFullNumber);
$defuntNom = str_replace('&nbsp;',' ',$orderPdfData['defunt_nom']);
return "BDC".'_'.$orderFullNumber.'_'.mb_strtoupper($defuntNom,'UTF-8');
}
public function generateOrderPdfByOrderId($orderId,$idNextCloud){
$storage = $this->rootFolder->getUserFolder($idNextCloud);
$configs = json_decode($this->gestionBdd->getConfiguration(BddConstant::DEFAULT_ADMIN_APP_ID_NEXTCLOUD));
$currentConfig = $configs[0];
$logoExist = $this->doesLogoExist();
$orderPdfData = $this->orderService->getOrderPdfData($orderId);
if($orderPdfData == null){
return "";
}
$orderPdfData = $this->formatOrderDataForPdfExport($orderPdfData,$currentConfig);
$racineFolder = html_entity_decode(string: $currentConfig->path).'/';
$orderFolders = $this->getCommandeFolders($orderPdfData,$racineFolder);
$pdf = new OrderPdfHandler();
$pdf->OrderPdfFactory($orderPdfData,$logoExist);
$pdf->SetOrderContent();
$pdfContent = $pdf->Output('','S');
$pdfFilename = $this->GetOrderFilename($orderPdfData);
$filenames = [];
foreach($orderFolders 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;
}
}