From ab961fffd1801d21613385d7e8f8ce794d621374 Mon Sep 17 00:00:00 2001 From: Tiavina Date: Sun, 2 Mar 2025 11:23:34 +0300 Subject: [PATCH] finish export group devis into facture features on backend , wip frontend --- gestion/appinfo/routes.php | 3 + gestion/lib/Controller/PageController.php | 22 ++ gestion/lib/Db/Bdd.php | 245 +++++++++++++++++- .../InvoiceGroupPdfHandler.php | 2 - gestion/lib/Service/InvoicePdfService.php | 42 +++ 5 files changed, 302 insertions(+), 12 deletions(-) diff --git a/gestion/appinfo/routes.php b/gestion/appinfo/routes.php index 2595cfb..e55254f 100644 --- a/gestion/appinfo/routes.php +++ b/gestion/appinfo/routes.php @@ -169,5 +169,8 @@ return [ ['name' => 'page#payInvoices','url' => '/facture/payInvoices', 'verb' => 'POST'], //relation of user and thanato ['name' => 'page#getUsersNotLinkedToThanato','url' => '/user/getUsersNotLinkedToThanato', 'verb' => 'PROPFIND'], + + //group of devis and facture + ['name' => 'page#exportGroupOfDevisIntoFacture','url' => '/devis/exportGroupOfDevisIntoFacture', 'verb' => 'POST'], ] ]; diff --git a/gestion/lib/Controller/PageController.php b/gestion/lib/Controller/PageController.php index 54418d9..b3051ff 100644 --- a/gestion/lib/Controller/PageController.php +++ b/gestion/lib/Controller/PageController.php @@ -2926,4 +2926,26 @@ class PageController extends Controller { return json_encode([]); } } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * + */ + public function exportGroupOfDevisIntoFacture($clientId,$clientType,$month,$year,$facturationDate){ + try{ + $result = $this->invoicePdfService->exportGroupOfDevisIntoFacture( + $clientId, + $clientType, + $month, + $year, + $facturationDate, + $this->idNextcloud + ); + return json_encode($result); + } + catch(Exception $e) { + return json_encode([]); + } + } } diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index 3181084..a8f1343 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -2798,8 +2798,19 @@ class Bdd { public function getInvoiceGroupPdfData($factureId,$configuration){ $tvaValue = $configuration->tva_default; $factureData = $this->getFactureByFactureId($factureId); - $factureDevisList = $this->getInvoiceGroupDevisDatasByFactureId($factureId); - $factureData["group_name"] = ""; + $isFactureForSingleClient = $factureData['fk_client_id'] != null && $factureData['fk_client_id'] != 0; + if($isFactureForSingleClient){ + $factureDevisList = $this->getDevisDataByClientIdAndMonthYear( + $factureData['fk_client_id'], + $factureData['month'], + $factureData['year']); + } + else{ + $factureDevisList = $this->getDevisDataByClientGroupFacturationIdAndMonthYear( + $factureData['fk_client_group_facturation_id'], + $factureData['month'], + $factureData['year']); + } $factureIncrement = 0; $productsCount = 0; $totalHt = 0; @@ -2809,11 +2820,21 @@ class Bdd { $orderNumber = ""; foreach($factureDevisList as &$devis){ if($factureIncrement == 0){ - $factureData["client_real_adress"] = $devis["group_address"] ?? ""; - $factureData["client_adress_city"] = $devis["group_postal_code"]." ".$devis["group_city"]; - $factureData["client_mail"] = $devis["group_email"]; - $factureData["siret"] = $devis["group_siret_number"]; - $factureData["group_name"] = $devis["group_name"] ?? ""; + if($isFactureForSingleClient){ + $clientAddresses = FileExportHelpers::GetAddressAndCityFromAddress($devis['client_adresse']); + $factureData["client_real_adress"] = $clientAddresses['address']; + $factureData["client_adress_city"] = $clientAddresses['city']; + $factureData["client_mail"] = $devis["client_mail"]; + $factureData["siret"] = $devis["client_legal_one"]; + $factureData["group_name"] = $devis["client_nom"] ?? ""; + } + else{ + $factureData["client_real_adress"] = $devis["group_address"] ?? ""; + $factureData["client_adress_city"] = $devis["group_postal_code"]." ".$devis["group_city"]; + $factureData["client_mail"] = $devis["group_email"]; + $factureData["siret"] = $devis["group_siret_number"]; + $factureData["group_name"] = $devis["group_name"] ?? ""; + } } $caseNumber = empty($devis["case_number"]) ? $caseNumber : $devis["case_number"]; $orderNumber = empty($devis["order_number"]) ? $orderNumber : $devis["order_number"]; @@ -4076,8 +4097,10 @@ class Bdd { return null; } - private function createFactureAndReturnFactureId($facturationDate,$type){ + public function createFactureAndReturnFactureId($facturationDate,$type,$month = null,$year = null,$fkClientId = null,$fkClientGroupFacturationId = null){ $datetimeNow = new Datetime(); + $month = $month ?? $datetimeNow->format('m'); + $year = $year ?? $datetimeNow->format('Y'); if($facturationDate == null || $facturationDate == ""){ $facturationDate = DateHelpers::GetLastDayOfTheMonthOfADate($datetimeNow); $facturationDate = $facturationDate->format('Y-m-d'); @@ -4092,7 +4115,7 @@ class Bdd { $fullFactureNumber = DateHelpers::GetFullFactureOrDevisNumberByDate($datetimeNow,$factureNumber,"FAC"); $last = 0; $last = $this->lastNumFacture(BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD); - $sql = "INSERT INTO `".$this->tableprefix."facture` (`date`,`id_nextcloud`,`num`,`date_paiement`,`type_paiement`,`id_devis`,`user_id`, `version`,`facture_number`,`facture_type`) VALUES (?,?,?,?,?,?,?,?,?,?);"; + $sql = "INSERT INTO `".$this->tableprefix."facture` (`date`,`id_nextcloud`,`num`,`date_paiement`,`type_paiement`,`id_devis`,`user_id`, `version`,`facture_number`,`facture_type`,`month`,`year`,`fk_client_id`,`fk_client_group_facturation_id`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?);"; $this->execSQLNoData($sql, array( $factureDate , BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD, @@ -4103,7 +4126,11 @@ class Bdd { $last+1, "", $factureNumber, - $type) + $type, + $month, + $year, + $fkClientId, + $fkClientGroupFacturationId) ); return $this->getFactureIdByFactureNumber($fullFactureNumber); @@ -4133,4 +4160,202 @@ class Bdd { } return FactureTypeConstant::TYPE_SINGLE; } + + public function getDevisIdsByClientGroupFacturationIdAndMonthYear($clientGroupFacturationId,$month,$year){ + $sql = "SELECT devis.id + FROM ".$this->tableprefix."devis as devis + LEFT JOIN ".$this->tableprefix."client as client on devis.id_client = client.id + WHERE + client.fk_client_group_facturation_id = ? AND + YEAR(devis.date) = ? AND + MONTH(devis.date) = ? + "; + + $result = $this->execSQLNoJsonReturn($sql,[$clientGroupFacturationId,$year,$month]); + + $devisIds = []; + foreach($result as $currentResult){ + $devisIds[] = $currentResult['id']; + } + return $devisIds; + + } + + public function getDevisIdsByClientIdAndMonthYear($clientId,$month,$year){ + $sql = "SELECT devis.id + FROM ".$this->tableprefix."devis as devis + LEFT JOIN ".$this->tableprefix."client as client on devis.id_client = client.id + WHERE + client.id = ? AND + YEAR(devis.date) = ? AND + MONTH(devis.date) = ? + "; + + $result = $this->execSQLNoJsonReturn($sql,[$clientId,$year,$month]); + + $devisIds = []; + foreach($result as $currentResult){ + $devisIds[] = $currentResult['id']; + } + return $devisIds; + + } + + public function getFactureIdByClientIdAndMonthYear($clientId,$month,$year){ + $sql = "SELECT + facture.id as facture_id + FROM ".$this->tableprefix."facture as facture + WHERE + facture.fk_client_id = ? AND + facture.year = ? AND + facture.month = ? AND + facture.facture_type = ?"; + + $result = $this->execSQLNoJsonReturn($sql,[ + $clientId, + $year, + $month, + FactureTypeConstant::TYPE_GROUP + ]); + + if(!empty($result)){ + return $result[0]['facture_id']; + } + return null; + } + + public function getFactureIdByClientGroupFacturationIdAndMonthYear($clientId,$month,$year){ + $sql = "SELECT + facture.id as facture_id + FROM ".$this->tableprefix."facture as facture + WHERE + facture.fk_client_group_facturation_id = ? AND + facture.year = ? AND + facture.month = ? AND + facture.facture_type = ?"; + + $result = $this->execSQLNoJsonReturn($sql,[ + $clientId, + $year, + $month, + FactureTypeConstant::TYPE_GROUP + ]); + + if(!empty($result)){ + return $result[0]['facture_id']; + } + return null; + } + + public function invoiceListOfDevisIds($devisIds){ + $sqlConditionsPlaceholder = implode(',', array_fill(0, count($devisIds), '?')); + $sql = "UPDATE ".$this->tableprefix."devis as devis + SET devis.mentions = ? + WHERE devis.id IN ($sqlConditionsPlaceholder);"; + + $conditions = array_merge( + [DevisMentionConstant::FACTURED_FORMATTED], + $devisIds + ); + + $this->execSQLNoData($sql,$conditions); + } + + private function getDevisDataByClientGroupFacturationIdAndMonthYear($clientGroupFacturationId,$month,$year){ + $sql = "SELECT + devis.id as devis_id, + devis.date as devis_date, + devis.num as calendar_uuid, + devis.comment as devis_comment, + devis.order_number as order_number, + devis.case_number as case_number, + client.nom as client_nom, + client.entreprise as client_entreprise, + client.adresse as client_adresse, + client.mail as client_mail, + client.legal_one as client_legal_one, + 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, + client_group_facturation.id as group_id, + client_group_facturation.group_facturation_name as group_name, + client_group_facturation.phone_number as group_phone_number, + client_group_facturation.address as group_address, + client_group_facturation.postal_code as group_postal_code, + client_group_facturation.city as group_city, + client_group_facturation.email as group_email, + client_group_facturation.siret_number as group_siret_number, + client_group_facturation.tva_intracommu as group_tva_intracommu + FROM ".$this->tableprefix."devis as devis + 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 + LEFT JOIN ".$this->tableprefix."client_group_facturation as client_group_facturation on client.fk_client_group_facturation_id = client_group_facturation.id + WHERE + client.fk_client_group_facturation_id = ? AND + YEAR(devis.date) = ? AND + MONTH(devis.date) = ? + ORDER BY devis.date ASC;"; + + $devisList = $this->execSQLNoJsonReturn($sql,[ + $clientGroupFacturationId, + $year, + $month + ]); + + return $devisList; + } + + private function getDevisDataByClientIdAndMonthYear($clientId,$month,$year){ + $sql = "SELECT + devis.id as devis_id, + devis.date as devis_date, + devis.num as calendar_uuid, + devis.comment as devis_comment, + devis.order_number as order_number, + devis.case_number as case_number, + client.nom as client_nom, + client.entreprise as client_entreprise, + client.adresse as client_adresse, + client.mail as client_mail, + client.legal_one as client_legal_one, + 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, + client_group_facturation.id as group_id, + client_group_facturation.group_facturation_name as group_name, + client_group_facturation.phone_number as group_phone_number, + client_group_facturation.address as group_address, + client_group_facturation.postal_code as group_postal_code, + client_group_facturation.city as group_city, + client_group_facturation.email as group_email, + client_group_facturation.siret_number as group_siret_number, + client_group_facturation.tva_intracommu as group_tva_intracommu + FROM ".$this->tableprefix."devis as devis + 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 + LEFT JOIN ".$this->tableprefix."client_group_facturation as client_group_facturation on client.fk_client_group_facturation_id = client_group_facturation.id + WHERE + client.id = ? AND + YEAR(devis.date) = ? AND + MONTH(devis.date) = ? + ORDER BY devis.date ASC;"; + + $devisList = $this->execSQLNoJsonReturn($sql,[ + $clientId, + $year, + $month + ]); + + return $devisList; + } } diff --git a/gestion/lib/Service/InvoiceGroupPdfHandler/InvoiceGroupPdfHandler.php b/gestion/lib/Service/InvoiceGroupPdfHandler/InvoiceGroupPdfHandler.php index 70f2c3f..4785971 100644 --- a/gestion/lib/Service/InvoiceGroupPdfHandler/InvoiceGroupPdfHandler.php +++ b/gestion/lib/Service/InvoiceGroupPdfHandler/InvoiceGroupPdfHandler.php @@ -28,8 +28,6 @@ namespace OCA\Gestion\Service\InvoiceGroupPdfHandler; use DateTime; use \FPDF; -use OCA\Gestion\Constants\MultipleFactureTypeConstant; -use OCA\Gestion\Helpers\DateHelpers; use OCA\Gestion\Helpers\FileExportHelpers; use OCA\Gestion\Helpers\PriceHelpers; diff --git a/gestion/lib/Service/InvoicePdfService.php b/gestion/lib/Service/InvoicePdfService.php index ef9c92f..18e38ca 100644 --- a/gestion/lib/Service/InvoicePdfService.php +++ b/gestion/lib/Service/InvoicePdfService.php @@ -27,12 +27,14 @@ declare(strict_types=1); namespace OCA\Gestion\Service; use DateTime; +use OCA\Gestion\Constants\BddConstant; use OCA\Gestion\Constants\FactureTypeConstant; use OCA\Gestion\Constants\MultipleFactureTypeConstant; use OCA\Gestion\Db\Bdd; use OCA\Gestion\Helpers\DateHelpers; use OCA\Gestion\Service\InvoiceGroupPdfHandler\InvoiceGroupPdfHandler; use OCA\Gestion\Service\InvoiceRecap\InvoiceRecapService; +use OCP\DB\Exception; use OCP\Files\IRootFolder; class InvoicePdfService { @@ -224,4 +226,44 @@ class InvoicePdfService { public function generateInvoiceRecap($filter,$filterType,$date,$idNextCloud){ $this->invoiceRecapService->generateInvoiceRecap($filter,$filterType,$date,$idNextCloud); } + + public function exportGroupOfDevisIntoFacture($clientId,$clientType,$month,$year,$facturationDate,$idNextcloud = BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD){ + try{ + $datetime = new Datetime(); + $month = $month ?? $datetime->format('m'); + $year = $year ?? $datetime->format('Y'); + + $factureId = null; + $fkClientId = null; + $fkClientGroupFacturationId = null; + if($clientType == MultipleFactureTypeConstant::CLIENT_FILTER_TYPE){ + $factureId = $this->gestionBdd->getFactureIdByClientIdAndMonthYear($clientId,$month,$year); + $devisIds = $this->gestionBdd->getDevisIdsByClientIdAndMonthYear($clientId,$month,$year); + $fkClientId = $clientId; + } + else{ + $factureId = $this->gestionBdd->getFactureIdByClientGroupFacturationIdAndMonthYear($clientId,$month,$year); + $devisIds = $this->gestionBdd->getDevisIdsByClientGroupFacturationIdAndMonthYear($clientId,$month,$year); + $fkClientGroupFacturationId = $clientId; + } + $clientIsAlreadyFacturedForThisMonthAndYear = $factureId != null && $factureId != 0; + if($clientIsAlreadyFacturedForThisMonthAndYear == false){ + $factureId = $this->gestionBdd->createFactureAndReturnFactureId( + $facturationDate, + FactureTypeConstant::TYPE_GROUP, + $month, + $year, + $fkClientId, + $fkClientGroupFacturationId); + } + + $this->gestionBdd->invoiceListOfDevisIds($devisIds); + $filenames = $this->generateFactureGroupPdfByFactureId($factureId,$idNextcloud); + return $filenames; + } + catch(Exception){ + return null; + } + + } }