diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index 68d4504..9716adc 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -2005,22 +2005,28 @@ 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 getDevisProductsQuantityByDevisListAndProductId($devisList,$productId){ + if(empty($devisList)){ + return 0; } + $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 = ?;"; - return [ - "count" => $productsCount, - "total_price" => $productsPrice - ]; + $produitList = $this->execSQLNoJsonReturn( + $sql, + array_merge($devisList,array($productId))); + + if(!empty($produitList)){ + return $produitList[0]['total_quantity']; + } + return 0; } - private function getClientFactureStatisticPerMonth($clientId){ + private function getClientFactureStatisticPerMonth($clientId,array $produitList){ $currentYear = date('Y'); $monthLists = range(1,12); $data = [] ; @@ -2045,19 +2051,28 @@ class Bdd { $sql, [$currentYear,$monthValue,$clientId]); - $defuntCount = count($factureList); - $produitsCount = 0; - $produitsPrice = 0; + $factureDevisIds = []; foreach($factureList as $facture){ - $devisProduitStat = $this->getProduitsDevisStatistic($facture["devis_id"]); - $produitsCount+= $devisProduitStat["count"]; - $produitsPrice+= $devisProduitStat["total_price"]; + $factureDevisIds[] = $facture['devis_id']; + } + + $defuntCount = count($factureList); + $produitsPrice = 0; + $statisticForeachProductPerMonth = []; + foreach($produitList as $produit){ + if(!isset($statisticForeachProductPerMonth[$produit['id']])){ + $statisticForeachProductPerMonth[$produit['id']] = 0; + } + $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; @@ -2065,6 +2080,7 @@ class Bdd { public function getExportClientStatData(array $clientIds){ $data = []; + $produitList = $this->getProduitsListAsArray(); foreach($clientIds as $clientId){ if(!isset($data[$clientId])){ $data[$clientId] = []; @@ -2076,7 +2092,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; } @@ -2167,4 +2183,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/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 .= FileExportHelpers::FormatTextForExport($produit['reference']).';'; + } + $fileHeader .= 'TOTAL HT'.';'."\n"; return $fileHeader; } @@ -78,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 b99a4c2..f8b4dcf 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,18 +60,18 @@ class ExportThanatoStatisticService { public function getExportThanatoFileHeader(): string{ $fileHeader = - 'Facture'.';'. - '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)'.';'. + 'FACTURE'.';'. + 'THANATOPRACTEUR'.';'. + 'DATE'.';'. + 'HEURE DE DEBUT'.';'. + 'HEURE DE FIN'.';'. + 'SOINS'.';'. + 'JOUR/FERIE'.';'. + 'NOM ET PRENOM'.';'. + 'LIEU'.';'. + 'POMPES FUNEBRES'.';'. + 'ADRESSE'.';'. + 'DISTANCE TOTALE KM'.';'. "\n"; return $fileHeader; } @@ -121,17 +122,17 @@ class ExportThanatoStatisticService { private function populateDevisDataIntoThanatoExportFileContent(string $fileContent,array $devis){ $produitAsString = $this->getFormatDevisProduitsAsString($devis["produits"]); $fileContent = $fileContent. - utf8_decode(html_entity_decode($devis["facture_num"])).';'. - 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["facture_num"]).';'. + 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;