From 1a254879d9880c7459c814eaa3fdd1ad42984985 Mon Sep 17 00:00:00 2001 From: Tiavina Date: Tue, 31 Dec 2024 00:27:28 +0300 Subject: [PATCH] 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;