diff --git a/gestion/appinfo/routes.php b/gestion/appinfo/routes.php index 074a919..5a7f30d 100644 --- a/gestion/appinfo/routes.php +++ b/gestion/appinfo/routes.php @@ -67,6 +67,7 @@ return [ ['name' => 'page#insertFacture', 'url' => '/facture/insert', 'verb' => 'POST'], ['name' => 'page#exportDevisToFacture', 'url' => '/exportDevisToFacture', 'verb' => 'POST'], ['name' => 'page#exportFactureToPdf', 'url' => '/facture/exportFactureToPdf', 'verb' => 'POST'], + ['name' => 'page#exportFactureByClientAndMonthYearToPdf', 'url' => '/facture/exportFactureByClientAndMonthYearToPdf', 'verb' => 'POST'], ['name' => 'page#getProduits', 'url' => '/getProduits', 'verb' => 'PROPFIND'], ['name' => 'page#getProduitsById', 'url' => '/getProduitsById', 'verb' => 'POST'], diff --git a/gestion/lib/Controller/PageController.php b/gestion/lib/Controller/PageController.php index bf66d67..667d624 100644 --- a/gestion/lib/Controller/PageController.php +++ b/gestion/lib/Controller/PageController.php @@ -2777,6 +2777,24 @@ class PageController extends Controller { } + /** + * @NoAdminRequired + * @NoCSRFRequired + * @param string $clientId + * @param string $month + * @param string $year + * + */ + + public function exportFactureByClientAndMonthYearToPdf($clientId,$month,$year){ + try{ + $factureFilename = $this->invoicePdfService->generateMultipleInvoicePdfByClientAndMonthYear($clientId,$month,$year,$this->idNextcloud); + return $factureFilename; + } + catch(\OCP\Files\NotFoundException $e) { } + + } + /** * @NoAdminRequired * @NoCSRFRequired diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index 12e39ac..70fcb75 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -2302,6 +2302,68 @@ class Bdd { return $factureData; } + public function getInvoicePdfDataByClientAndMonthYear($clientId,$month,$year,$configuration){ + $invoices = $this->getInvoiceByClientAndMonthYear($clientId,$month,$year); + $firstClient = $this->getFirstClient(); + foreach($invoices as &$invoice){ + $invoice["siret"] = $firstClient != null ? $firstClient['legal_one'] : ''; + $products = $this->getDevisProduits($invoice["devis_id"]); + $invoice["products"] = $products; + $invoice["configuration"] = $configuration; + + $clientAdresses = FileExportHelpers::GetAddressAndCityFromAddress($invoice["client_adresse"]); + $invoice["client_real_adress"] = $clientAdresses["address"]; + $invoice["client_adress_city"] = $clientAdresses["city"]; + + $configurationAdresses = FileExportHelpers::GetAddressAndCityFromAddress($configuration->adresse); + $invoice["configuration_adresse"] = $configurationAdresses["address"]; + $invoice["configuration_adresse_city"] = $configurationAdresses["city"]; + } + return $invoices; + } + + private function getInvoiceByClientAndMonthYear($clientId,$month,$year){ + $sql = "SELECT + facture.id, + facture.date, + facture.date_paiement, + facture.num, + devis.id as devis_id, + devis.date as devis_date, + devis.num as calendar_uuid, + devis.comment as devis_comment, + devis.id_client as devis_id_client, + client.nom as client_nom, + client.entreprise as client_entreprise, + client.adresse as client_adresse, + defunt.nom as defunt_nom, + defunt.sexe as defunt_sexe, + lieu.nom as lieu_nom, + lieu.adresse as lieu_adresse, + thanato.nom as thanato_nom, + thanato.prenom as thanato_prenom + FROM ".$this->tableprefix."facture as facture + LEFT JOIN ".$this->tableprefix."devis as devis on facture.id_devis = devis.id + LEFT JOIN ".$this->tableprefix."client as client on devis.id_client = client.id + LEFT JOIN ".$this->tableprefix."defunt as defunt on devis.id_defunt = defunt.id + LEFT JOIN ".$this->tableprefix."lieu as lieu on devis.id_lieu = lieu.id + LEFT JOIN ".$this->tableprefix."thanato as thanato on devis.id_thanato = thanato.id + WHERE devis.id_client = ? AND + YEAR(facture.date_paiement) = ?"; + + $conditions = [$clientId,$year]; + if($month != 0){ + $conditions[] = $month; + $sql .= " AND MONTH(facture.date_paiement) = ?"; + } + $sql .= ";"; + $factures = $this->execSQLNoJsonReturn( + $sql, + $conditions); + + return $factures; + } + private function getFactureByIdWithDevis($factureId){ $sql = "SELECT facture.id, diff --git a/gestion/lib/Helpers/DateHelpers.php b/gestion/lib/Helpers/DateHelpers.php index 1a9f379..19961a7 100644 --- a/gestion/lib/Helpers/DateHelpers.php +++ b/gestion/lib/Helpers/DateHelpers.php @@ -5,6 +5,7 @@ namespace OCA\Gestion\Helpers; use DateTime; use DateTimeZone; use Exception; +use IntlDateFormatter; class DateHelpers { @@ -20,6 +21,23 @@ class DateHelpers } } + public static function GetMonthPlainString($month){ + $formatter = new IntlDateFormatter( + 'fr_FR', + IntlDateFormatter::FULL, + IntlDateFormatter::NONE, + null, + null, + 'MMMM' + ); + + $date = DateTime::createFromFormat('!m', $month); + $monthString = strtoupper($formatter->format($date)); + $normalizedMonth = mb_convert_case($monthString, MB_CASE_UPPER, "UTF-8"); + + return $normalizedMonth; + } + public static function GetDateWithFormatDayAndMonthPlainString(string $date){ $dateTime = new DateTime($date); $formattedDate = $dateTime->format('d F'); diff --git a/gestion/lib/Service/InvoicePdfHandler.php b/gestion/lib/Service/InvoicePdfHandler.php index be2e2c4..9153381 100644 --- a/gestion/lib/Service/InvoicePdfHandler.php +++ b/gestion/lib/Service/InvoicePdfHandler.php @@ -28,11 +28,13 @@ namespace OCA\Gestion\Service; use DateTime; use \FPDF; +use OCA\Gestion\Helpers\DateHelpers; use OCA\Gestion\Helpers\FileExportHelpers; use OCA\Gestion\Helpers\PriceHelpers; class InvoicePdfHandler extends FPDF { + private $multipleFactureData = []; private $factureData = []; private $logo = null; private $logoPath = "/var/www/html/data/admin/files/.gestion/"; @@ -44,7 +46,6 @@ class InvoicePdfHandler extends FPDF { else{ $this->Cell(55,30,''); } - $this->Ln(30); } function Footer() { @@ -65,7 +66,23 @@ class InvoicePdfHandler extends FPDF { $this->logo = $logo; } - public function GetFilename(){ + public function MutlipleInvoicePdfFactory(array $multipleInvoiceData,$logo = null){ + $this->multipleFactureData = $multipleInvoiceData; + $this->logo = $logo; + } + + public function GetMultipleInvoiceFilename($month,$year){ + $filename = ""; + foreach($this->multipleFactureData as $factureData){ + $filename = strtoupper($factureData["client_entreprise"]); + $filename .= $month != 0 ? '_'.DateHelpers::GetMonthPlainString($month) :''; + $filename .= "_".$year; + break; + } + return $filename; + } + + public function GetInvoiceFilename(){ $factureNum = $this->factureData['num']; $factureNum = str_replace('/','-',$factureNum); $defuntNom = str_replace(' ',' ',$this->factureData['defunt_nom']); @@ -73,6 +90,7 @@ class InvoicePdfHandler extends FPDF { } private function DrawInvoiceCompanyAndClientInfo(){ + $this->SetY(40); $this->SetFont('Arial', '', 12); $this->Cell(0, 7, FileExportHelpers::FormatTextForExport($this->factureData['configuration']->entreprise), 0, 0); $this->Cell(0, 7, FileExportHelpers::FormatTextForExport($this->factureData['client_nom']), 0, 1,'R'); @@ -218,7 +236,14 @@ class InvoicePdfHandler extends FPDF { } } - public function GetFactureContent(){ + public function SetMultipleFactureContent(){ + foreach($this->multipleFactureData as $factureData){ + $this->factureData = $factureData; + $this->SetFactureContent(); + } + } + + public function SetFactureContent(){ $this->AddPage(); $this->SetMargins(10,0,10); $this->DrawInvoiceCompanyAndClientInfo(); @@ -227,8 +252,6 @@ class InvoicePdfHandler extends FPDF { $this->DrawArticlesTableHeader(); $totalPriceValue = $this->DrawArticlesTableValueAndReturnTotalPrice(); $this->DrawBankAndTotalPriceInfo($totalPriceValue); - $pdfContent = $this->Output('','S'); - return $pdfContent; } function MultiAlignCell($w,$h,$text,$border=0,$ln=0,$align='L',$fill=false) diff --git a/gestion/lib/Service/InvoicePdfService.php b/gestion/lib/Service/InvoicePdfService.php index 332f8d6..f729062 100644 --- a/gestion/lib/Service/InvoicePdfService.php +++ b/gestion/lib/Service/InvoicePdfService.php @@ -75,12 +75,13 @@ class InvoicePdfService { if($invoicePdfData == null){ return ""; } - $clean_folder = html_entity_decode($currentConfig->path).'/'; + $clean_folder = html_entity_decode(string: $currentConfig->path).'/'; $factureFolders = $this->getFacturesFolder($invoicePdfData,$clean_folder); $pdf = new InvoicePdfHandler(); $pdf->InvoicePdfFactory($invoicePdfData,$logo); - $pdfContent = $pdf->GetFactureContent(); - $pdfFilename = $pdf->GetFilename(); + $pdf->SetFactureContent(); + $pdfContent = $pdf->Output('','S'); + $pdfFilename = $pdf->GetInvoiceFilename(); $filenames = []; foreach($factureFolders as $folder){ try { @@ -116,4 +117,32 @@ class InvoicePdfService { $this->generateFacturePdfByFactureId($factureId,$idNextCloud); } } + + public function generateMultipleInvoicePdfByClientAndMonthYear($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(); + $invoiceData = $this->gestionBdd->getInvoicePdfDataByClientAndMonthYear($clientId,$month,$year,$currentConfig); + if($invoiceData == null){ + return ""; + } + $pdf = new InvoicePdfHandler(); + $pdf->MutlipleInvoicePdfFactory($invoiceData,$logo); + $pdf->SetMultipleFactureContent(); + $racinePath = html_entity_decode(string: $currentConfig->path).'/'; + $clientRacineFolder = $racinePath.'CLIENTS/'.strtoupper($invoiceData[0]["client_entreprise"]).'/'; + $filename = $currentConfig->facture_prefixe.'_'.$pdf->GetMultipleInvoiceFilename($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; + } }