From 30e5c7852ad59694de7d818432bfa861112fd372 Mon Sep 17 00:00:00 2001 From: Tiavina Date: Mon, 30 Dec 2024 18:44:06 +0300 Subject: [PATCH 1/2] WIP export client with articles --- gestion/lib/Controller/PageController.php | 2 +- gestion/lib/Db/Bdd.php | 70 ++++++++++++++----- .../Service/ExportClientStatisticService.php | 11 ++- .../Service/ExportThanatoStatisticService.php | 2 +- 4 files changed, 64 insertions(+), 21 deletions(-) diff --git a/gestion/lib/Controller/PageController.php b/gestion/lib/Controller/PageController.php index 86c2a55..c534649 100644 --- a/gestion/lib/Controller/PageController.php +++ b/gestion/lib/Controller/PageController.php @@ -2618,7 +2618,7 @@ class PageController extends Controller { catch(\OCP\Files\NotPermittedException $e) { } - $fileHeader = $this->exportClientStatisticService->getExportClientFileHeader(); + $fileHeader = $this->exportClientStatisticService->getExportClientFileHeader($exportData); $fileContent = $this->exportClientStatisticService->populateExportDataIntoFileContent($exportData,$fileHeader); $fileName = $this->exportClientStatisticService->getFileName($clientIdsToExport); $fileNamePath = $_clean_folder."STAT-CLIENTS-" . $fileName .'.csv'; diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index ed13455..83c5056 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -1919,7 +1919,8 @@ class Bdd { lieu.latitude as lieu_latitude, lieu.longitude as lieu_longitude, client.nom as nom_client, - client.entreprise as client_entreprise + client.entreprise as client_entreprise, + client.adresse as client_adresse FROM ".$this->tableprefix."devis as devis LEFT JOIN ".$this->tableprefix."thanato as thanato on devis.id_thanato = thanato.id LEFT JOIN ".$this->tableprefix."lieu as lieu on devis.id_lieu = lieu.id @@ -1980,6 +1981,31 @@ class Bdd { return null; } + private function getDevisProduitByDevisAndProduit($devisId,$produitId){ + $sql = "SELECT + produit_devis.id, + produit_devis.produit_id, + produit_devis.quantite, + produit_devis.discount, + produit.prix_unitaire as produit_price, + produit.reference as produit_reference, + produit.description as produit_description, + produit.vat as produit_vat + FROM ".$this->tableprefix ."produit_devis as produit_devis + LEFT JOIN ".$this->tableprefix."produit as produit on produit_devis.produit_id = produit.id + WHERE produit_devis.devis_id = ? AND + produit.id = ?;"; + + $produitList = $this->execSQLNoJsonReturn( + $sql, + [$devisId,$produitId]); + + if(!empty($produitList)){ + return $produitList[0]; + } + return null; + } + private function getDevisProduits($devisId){ $sql = "SELECT produit_devis.id, @@ -2001,22 +2027,16 @@ class Bdd { return $produitList; } - private function getProduitsDevisStatistic($devisId){ - $produitList = $this->getDevisProduits($devisId); - $productsCount = count($produitList); - $productsPrice = 0; - - foreach($produitList as $produit){ - $productsPrice += $produit["quantite"] * $produit["produit_price"]; + private function getProduitsDevisStatistic($devisId,$produitId){ + $produitQuantity = 0; + $devisProduit = $this->getDevisProduitByDevisAndProduit($devisId,$produitId); + if($devisProduit){ + $produitQuantity = $devisProduit["quantite"]; } - - return [ - "count" => $productsCount, - "total_price" => $productsPrice - ]; + return $produitQuantity; } - private function getClientFactureStatisticPerMonth($clientId){ + private function getClientFactureStatisticPerMonth($clientId,array $produitList){ $currentYear = date('Y'); $monthLists = range(1,12); $data = [] ; @@ -2044,8 +2064,15 @@ class Bdd { $defuntCount = count($factureList); $produitsCount = 0; $produitsPrice = 0; + $statisticForeachProduct = []; foreach($factureList as $facture){ - $devisProduitStat = $this->getProduitsDevisStatistic($facture["devis_id"]); + foreach($produitList as $produit){ + $devisProduitStat = $this->getProduitsDevisStatistic($facture["devis_id"],$produit['id']); + if(!isset($statisticForeachProduct[$produit['reference']])){ + $statisticForeachProduct[$produit['reference']] = []; + } + $statisticForeachProduct[$produit['reference']] + } $produitsCount+= $devisProduitStat["count"]; $produitsPrice+= $devisProduitStat["total_price"]; } @@ -2061,6 +2088,7 @@ class Bdd { public function getExportClientStatData(array $clientIds){ $data = []; + $produitList = $this->getProduitsListAsArray(); foreach($clientIds as $clientId){ if(!isset($data[$clientId])){ $data[$clientId] = []; @@ -2072,7 +2100,7 @@ class Bdd { $clientName = trim($client["client_nom"]) . '-' .trim($client['client_entreprise']); } $data[$clientId]["client_name"] = $clientName; - $data[$clientId]["client_data"] = $this->getClientFactureStatisticPerMonth($clientId); + $data[$clientId]["client_data"] = $this->getClientFactureStatisticPerMonth($clientId,$produitList); } return $data; } @@ -2163,4 +2191,14 @@ class Bdd { return null; } + public function getProduitsListAsArray(){ + $sql = "SELECT * FROM ".$this->tableprefix."produit as produit;"; + + $produitList = $this->execSQLNoJsonReturn( + $sql, + []); + + return $produitList; + } + } \ No newline at end of file diff --git a/gestion/lib/Service/ExportClientStatisticService.php b/gestion/lib/Service/ExportClientStatisticService.php index 5ee2c0e..f4c8f82 100644 --- a/gestion/lib/Service/ExportClientStatisticService.php +++ b/gestion/lib/Service/ExportClientStatisticService.php @@ -55,15 +55,20 @@ class ExportClientStatisticService { return $filename; } - public function getExportClientFileHeader(): string{ + public function getExportClientFileHeader($exportData): string{ $fileHeader = 'Client'.';'. 'Mois'.';'. utf8_decode(html_entity_decode('Année')).';'. utf8_decode(html_entity_decode('Nb défunts')).';'. 'Nb articles'.';'. - 'Total HT'.';'. - "\n"; + 'Total HT'.';'; + + $produitList = $this->gestionBdd->getProduitsListAsArray(); + foreach($produitList as $produit){ + $fileHeader .= $produit['reference'].';'; + } + $fileHeader .= "\n"; return $fileHeader; } diff --git a/gestion/lib/Service/ExportThanatoStatisticService.php b/gestion/lib/Service/ExportThanatoStatisticService.php index f8ccdcf..61f815e 100644 --- a/gestion/lib/Service/ExportThanatoStatisticService.php +++ b/gestion/lib/Service/ExportThanatoStatisticService.php @@ -128,7 +128,7 @@ class ExportThanatoStatisticService { utf8_decode(html_entity_decode($devis["nom_defunt"])).';'. utf8_decode(html_entity_decode($devis["nom_lieu"] ?? "")).';'. utf8_decode(html_entity_decode($devis["nom_client"] ?? "")).';'. - utf8_decode(html_entity_decode($devis["client_entreprise"] ?? ""))."\n"; + utf8_decode(html_entity_decode($devis["client_adresse"] ?? ""))."\n"; return $fileContent; From 1a254879d9880c7459c814eaa3fdd1ad42984985 Mon Sep 17 00:00:00 2001 From: Tiavina Date: Tue, 31 Dec 2024 00:27:28 +0300 Subject: [PATCH 2/2] fix export thanato header, add articles on export client --- gestion/lib/Controller/PageController.php | 2 +- gestion/lib/Db/Bdd.php | 79 ++++++++----------- gestion/lib/Helpers/FileExportHelpers.php | 14 ++++ .../Service/ExportClientStatisticService.php | 49 ++++++------ .../Service/ExportThanatoStatisticService.php | 43 +++++----- 5 files changed, 97 insertions(+), 90 deletions(-) create mode 100644 gestion/lib/Helpers/FileExportHelpers.php diff --git a/gestion/lib/Controller/PageController.php b/gestion/lib/Controller/PageController.php index c534649..86c2a55 100644 --- a/gestion/lib/Controller/PageController.php +++ b/gestion/lib/Controller/PageController.php @@ -2618,7 +2618,7 @@ class PageController extends Controller { catch(\OCP\Files\NotPermittedException $e) { } - $fileHeader = $this->exportClientStatisticService->getExportClientFileHeader($exportData); + $fileHeader = $this->exportClientStatisticService->getExportClientFileHeader(); $fileContent = $this->exportClientStatisticService->populateExportDataIntoFileContent($exportData,$fileHeader); $fileName = $this->exportClientStatisticService->getFileName($clientIdsToExport); $fileNamePath = $_clean_folder."STAT-CLIENTS-" . $fileName .'.csv'; diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index 83c5056..224c989 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -1981,31 +1981,6 @@ class Bdd { return null; } - private function getDevisProduitByDevisAndProduit($devisId,$produitId){ - $sql = "SELECT - produit_devis.id, - produit_devis.produit_id, - produit_devis.quantite, - produit_devis.discount, - produit.prix_unitaire as produit_price, - produit.reference as produit_reference, - produit.description as produit_description, - produit.vat as produit_vat - FROM ".$this->tableprefix ."produit_devis as produit_devis - LEFT JOIN ".$this->tableprefix."produit as produit on produit_devis.produit_id = produit.id - WHERE produit_devis.devis_id = ? AND - produit.id = ?;"; - - $produitList = $this->execSQLNoJsonReturn( - $sql, - [$devisId,$produitId]); - - if(!empty($produitList)){ - return $produitList[0]; - } - return null; - } - private function getDevisProduits($devisId){ $sql = "SELECT produit_devis.id, @@ -2027,13 +2002,25 @@ class Bdd { return $produitList; } - private function getProduitsDevisStatistic($devisId,$produitId){ - $produitQuantity = 0; - $devisProduit = $this->getDevisProduitByDevisAndProduit($devisId,$produitId); - if($devisProduit){ - $produitQuantity = $devisProduit["quantite"]; + private function getDevisProductsQuantityByDevisListAndProductId($devisList,$productId){ + if(empty($devisList)){ + return 0; } - return $produitQuantity; + $sqlConditionsPlaceholder = implode(',', array_fill(0, count($devisList), '?')); + $sql = "SELECT + SUM(produit_devis.quantite) as total_quantity + FROM ".$this->tableprefix ."produit_devis as produit_devis + WHERE produit_devis.devis_id IN ($sqlConditionsPlaceholder) AND + produit_devis.produit_id = ?;"; + + $produitList = $this->execSQLNoJsonReturn( + $sql, + array_merge($devisList,array($productId))); + + if(!empty($produitList)){ + return $produitList[0]['total_quantity']; + } + return 0; } private function getClientFactureStatisticPerMonth($clientId,array $produitList){ @@ -2061,26 +2048,28 @@ class Bdd { $sql, [$currentYear,$monthValue,$clientId]); - $defuntCount = count($factureList); - $produitsCount = 0; - $produitsPrice = 0; - $statisticForeachProduct = []; + $factureDevisIds = []; foreach($factureList as $facture){ - foreach($produitList as $produit){ - $devisProduitStat = $this->getProduitsDevisStatistic($facture["devis_id"],$produit['id']); - if(!isset($statisticForeachProduct[$produit['reference']])){ - $statisticForeachProduct[$produit['reference']] = []; - } - $statisticForeachProduct[$produit['reference']] + $factureDevisIds[] = $facture['devis_id']; + } + + $defuntCount = count($factureList); + $produitsPrice = 0; + $statisticForeachProductPerMonth = []; + foreach($produitList as $produit){ + if(!isset($statisticForeachProductPerMonth[$produit['id']])){ + $statisticForeachProductPerMonth[$produit['id']] = 0; } - $produitsCount+= $devisProduitStat["count"]; - $produitsPrice+= $devisProduitStat["total_price"]; + $productTotalCount = $this->getDevisProductsQuantityByDevisListAndProductId($factureDevisIds,$produit['id']); + $totalWithoutVat = $productTotalCount * $produit["prix_unitaire"]; + $statisticForeachProductPerMonth[$produit['id']] += $productTotalCount; + $produitsPrice += $totalWithoutVat; } $data[$monthValue] = [ "defunt_count" => $defuntCount, - "produit_count" => $produitsCount, "total_price" => $produitsPrice, - "year" => $currentYear + "year" => $currentYear, + "products" => $statisticForeachProductPerMonth ]; } return $data; diff --git a/gestion/lib/Helpers/FileExportHelpers.php b/gestion/lib/Helpers/FileExportHelpers.php new file mode 100644 index 0000000..160f54d --- /dev/null +++ b/gestion/lib/Helpers/FileExportHelpers.php @@ -0,0 +1,14 @@ +gestionBdd->getProduitsListAsArray(); foreach($produitList as $produit){ - $fileHeader .= $produit['reference'].';'; + $fileHeader .= FileExportHelpers::FormatTextForExport($produit['reference']).';'; } - $fileHeader .= "\n"; + $fileHeader .= 'TOTAL HT'.';'."\n"; return $fileHeader; } @@ -83,37 +82,41 @@ class ExportClientStatisticService { $totalPrice+=$stat["total_price"]; $fileContent = $this->populateClientStatDataIntoFileContent($fileContent,$month,$stat); } - $fileContent = $this->populateTotalPriceIntoFileContent($fileContent,$totalPrice); + $fileContent = $this->populateTotalPriceIntoFileContent($fileContent,$totalPrice,count($stat["products"])); } } return $fileContent; } - private function populateTotalPriceIntoFileContent(string $fileContent,$price){ + private function populateTotalPriceIntoFileContent(string $fileContent,$totalPrice,$productsCount){ $fileContent = $fileContent. ''.';'. ''.';'. ''.';'. - ''.';'. - ''.';'. - utf8_decode(html_entity_decode("$price"))."\n"; + ''.';'; + while($productsCount > 0){ + $fileContent .= ''.';'; + $productsCount--; + } + $fileContent .= "$totalPrice".";"."\n"; return $fileContent; } private function populateClientStatDataIntoFileContent(string $fileContent,$month,array $statPerMonth){ $yearValue = $statPerMonth["year"]; $defuntCount = $statPerMonth["defunt_count"]; - $productCount = $statPerMonth["produit_count"]; - $totalPrice = $statPerMonth["total_price"]; + $products = $statPerMonth["products"]; $fileContent = $fileContent. - utf8_decode(html_entity_decode($statPerMonth['client_name'])).';'. - utf8_decode(html_entity_decode("$month")).';'. - utf8_decode(html_entity_decode("$yearValue")).';'. - utf8_decode(html_entity_decode("$defuntCount")).';'. - utf8_decode(html_entity_decode("$productCount")).';'. - utf8_decode(html_entity_decode("$totalPrice")).';'."\n"; - + FileExportHelpers::FormatTextForExport($statPerMonth['client_name']).';'. + "$month".';'. + "$yearValue".';'. + "$defuntCount".';'; + + foreach($products as $productCount){ + $fileContent .= "$productCount".";"; + } + $fileContent .= "\n"; return $fileContent; } diff --git a/gestion/lib/Service/ExportThanatoStatisticService.php b/gestion/lib/Service/ExportThanatoStatisticService.php index 61f815e..9aaaa0f 100644 --- a/gestion/lib/Service/ExportThanatoStatisticService.php +++ b/gestion/lib/Service/ExportThanatoStatisticService.php @@ -27,6 +27,7 @@ declare(strict_types=1); namespace OCA\Gestion\Service; use OCA\Gestion\Db\Bdd; +use OCA\Gestion\Helpers\FileExportHelpers; use Psr\Log\LoggerInterface; class ExportThanatoStatisticService { @@ -59,17 +60,17 @@ class ExportThanatoStatisticService { public function getExportThanatoFileHeader(): string{ $fileHeader = - 'Thanatopracteur'.';'. - 'Date'.';'. - utf8_decode(html_entity_decode('Heure de début')).';'. - 'Heure de fin'.';'. - 'Soins'.';'. - utf8_decode(html_entity_decode('Jour/Férié')).';'. - utf8_decode(html_entity_decode('Nom et Prénom')).';'. - 'Lieu'.';'. - utf8_decode(html_entity_decode('Pompe funèbre')).';'. - utf8_decode(html_entity_decode('Pompe funèbre adresse')).';'. - 'Distance Totale (km)'.';'. + 'THANATOPRACTEUR'.';'. + 'DATE'.';'. + 'HEURE DE DEBUT'.';'. + 'HEURE DE FIN'.';'. + 'SOINS'.';'. + 'JOUR/FERIE'.';'. + 'NOM ET PRENOM'.';'. + 'LIEU'.';'. + 'POMPES FUNEBRES'.';'. + 'ADRESSE'.';'. + 'DISTANCE TOTALE KM'.';'. "\n"; return $fileHeader; } @@ -119,16 +120,16 @@ class ExportThanatoStatisticService { private function populateDevisDataIntoThanatoExportFileContent(string $fileContent,array $devis){ $produitAsString = $this->getFormatDevisProduitsAsString($devis["produits"]); $fileContent = $fileContent. - utf8_decode(html_entity_decode($devis['nom_thanato'] . ' ' . $devis['prenom_thanatho'])).';'. - utf8_decode(html_entity_decode($devis["date"])).';'. - utf8_decode(html_entity_decode($devis["startTime"])).';'. - utf8_decode(html_entity_decode($devis["endTime"])).';'. - utf8_decode(html_entity_decode($produitAsString)).';'. - utf8_decode(html_entity_decode($devis["dayType"])).';'. - utf8_decode(html_entity_decode($devis["nom_defunt"])).';'. - utf8_decode(html_entity_decode($devis["nom_lieu"] ?? "")).';'. - utf8_decode(html_entity_decode($devis["nom_client"] ?? "")).';'. - utf8_decode(html_entity_decode($devis["client_adresse"] ?? ""))."\n"; + FileExportHelpers::FormatTextForExport($devis['nom_thanato'] . ' ' . $devis['prenom_thanatho']).';'. + FileExportHelpers::FormatTextForExport($devis["date"]).';'. + FileExportHelpers::FormatTextForExport($devis["startTime"]).';'. + FileExportHelpers::FormatTextForExport($devis["endTime"]).';'. + FileExportHelpers::FormatTextForExport($produitAsString).';'. + FileExportHelpers::FormatTextForExport($devis["dayType"]).';'. + FileExportHelpers::FormatTextForExport($devis["nom_defunt"]).';'. + FileExportHelpers::FormatTextForExport($devis["nom_lieu"] ?? "").';'. + FileExportHelpers::FormatTextForExport($devis["nom_client"] ?? "").';'. + FileExportHelpers::FormatTextForExport($devis["client_adresse"] ?? "")."\n"; return $fileContent;