fix statistique client calcul total HT

This commit is contained in:
Tolotsoa 2025-11-17 17:27:40 +03:00
parent 34717b772d
commit 411cc2d5a8
2 changed files with 84 additions and 74 deletions

View File

@ -3208,7 +3208,7 @@ class Bdd
return $data; return $data;
} }
private function getClientFactureStatisticPerMonth($clientId, array $produitList) private function getClientFactureStatisticPerMonth($clientId, array $produitList, $clientGroupId = null)
{ {
$currentYear = date('Y'); $currentYear = date('Y');
$monthLists = range(1, 12); $monthLists = range(1, 12);
@ -3245,7 +3245,8 @@ class Bdd
$statisticForeachProductPerMonth[$produit['id']] = 0; $statisticForeachProductPerMonth[$produit['id']] = 0;
} }
$productTotalCount = $this->getDevisProductsQuantityByDevisListAndProductId($factureDevisIds, $produit['id']); $productTotalCount = $this->getDevisProductsQuantityByDevisListAndProductId($factureDevisIds, $produit['id']);
$totalWithoutVat = $productTotalCount * $produit["prix_unitaire"]; $prixUnitaire = is_null($clientGroupId) ? $produit["prix_unitaire"] : $this->getProductPriceByClientGroupId($clientGroupId, $produit['id']);
$totalWithoutVat = $productTotalCount * $prixUnitaire;
$statisticForeachProductPerMonth[$produit['id']] += $productTotalCount; $statisticForeachProductPerMonth[$produit['id']] += $productTotalCount;
$produitsPrice += $totalWithoutVat; $produitsPrice += $totalWithoutVat;
} }
@ -3272,9 +3273,10 @@ class Bdd
$client = $this->getClientById($clientId); $client = $this->getClientById($clientId);
if($client != null) { if($client != null) {
$clientName = trim($client["client_nom"]) . '-' .trim($client['client_entreprise']); $clientName = trim($client["client_nom"]) . '-' .trim($client['client_entreprise']);
$clientGroupId = $client['fk_client_group_id'];
} }
$data[$clientId]["client_name"] = $clientName; $data[$clientId]["client_name"] = $clientName;
$data[$clientId]["client_data"] = $this->getClientFactureStatisticPerMonth($clientId, $produitList); $data[$clientId]["client_data"] = $this->getClientFactureStatisticPerMonth($clientId, $produitList, $clientGroupId);
} }
return $data; return $data;
} }

View File

@ -30,94 +30,102 @@ use OCA\Gestion\Db\Bdd;
use OCA\Gestion\Helpers\FileExportHelpers; use OCA\Gestion\Helpers\FileExportHelpers;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
class ExportClientStatisticService { class ExportClientStatisticService
/** @var Bdd */ {
private $gestionBdd; /** @var Bdd */
private $gestionBdd;
/** @var LoggerInterface */ /** @var LoggerInterface */
private $logger; private $logger;
public function __construct( public function __construct(
Bdd $gestionBdd, Bdd $gestionBdd,
LoggerInterface $logger) { LoggerInterface $logger
$this->logger = $logger; ) {
$this->gestionBdd = $gestionBdd; $this->logger = $logger;
} $this->gestionBdd = $gestionBdd;
}
public function getFileName(array $clientIds){ public function getFileName(array $clientIds)
{
$filename = ""; $filename = "";
$clients = $this->gestionBdd->getClientsByClientsID($clientIds); $clients = $this->gestionBdd->getClientsByClientsID($clientIds);
foreach($clients as $client){ foreach($clients as $client) {
$filename .= $client['client_nom'] . '-' . $client['client_entreprise'] . '--'; $filename .= $client['client_nom'] . '-' . $client['client_entreprise'] . '--';
} }
$filename = rtrim($filename, '-'); $filename = rtrim($filename, '-');
$filename = str_replace(' ','-', $filename); $filename = str_replace(' ', '-', $filename);
$filename = str_replace(' ','-', $filename); $filename = str_replace(' ', '-', $filename);
return $filename; return $filename;
} }
public function getExportClientFileHeader(): string{ public function getExportClientFileHeader(): string
$fileHeader = {
'CLIENT'.';'. $fileHeader =
'CLIENT'.';'.
'MOIS'.';'. 'MOIS'.';'.
'ANNEE'.';'. 'ANNEE'.';'.
'NB DE DEFUNTS'.';'; 'NB DE DEFUNTS'.';';
$produitList = $this->gestionBdd->getProduitsListAsArray(); $produitList = $this->gestionBdd->getProduitsListAsArray();
foreach($produitList as $produit){ foreach($produitList as $produit) {
$fileHeader .= FileExportHelpers::FormatTextForExport($produit['reference']).';'; $fileHeader .= FileExportHelpers::FormatTextForExport($produit['reference']).';';
} }
$fileHeader .= 'TOTAL HT'.';'."\n"; $fileHeader .= 'TOTAL HT'.';'."\n";
return $fileHeader; return $fileHeader;
} }
public function populateExportDataIntoFileContent(array $exportData,string $fileContent): string{ public function populateExportDataIntoFileContent(array $exportData, string $fileContent): string
foreach($exportData as $clientId => $clientData){ {
$clientName = $clientData["client_name"]; foreach($exportData as $clientId => $clientData) {
$clientStatPerMonth = $clientData["client_data"]; $clientName = $clientData["client_name"];
$totalPrice = 0; $clientStatPerMonth = $clientData["client_data"];
if(!empty($clientStatPerMonth)){ $totalPrice = 0;
foreach($clientStatPerMonth as $month => $stat){ if(!empty($clientStatPerMonth)) {
$stat["client_name"] = $clientName; foreach($clientStatPerMonth as $month => $stat) {
$totalPrice+=$stat["total_price"]; $stat["client_name"] = $clientName;
$fileContent = $this->populateClientStatDataIntoFileContent($fileContent,$month,$stat); $totalPrice += $stat["total_price"];
} $fileContent = $this->populateClientStatDataIntoFileContent($fileContent, $month, $stat);
$fileContent = $this->populateTotalPriceIntoFileContent($fileContent,$totalPrice,count($stat["products"])); }
} $fileContent = $this->populateTotalPriceIntoFileContent($fileContent, $totalPrice, count($stat["products"]));
} }
return $fileContent; }
} return $fileContent;
}
private function populateTotalPriceIntoFileContent(string $fileContent,$totalPrice,$productsCount){ private function populateTotalPriceIntoFileContent(string $fileContent, $totalPrice, $productsCount)
$fileContent = $fileContent. {
''.';'. $fileContent = $fileContent.
''.';'. ''.';'.
''.';'. ''.';'.
''.';'; ''.';'.
while($productsCount > 0){ ''.';';
$fileContent .= ''.';'; while($productsCount > 0) {
$productsCount--; $fileContent .= ''.';';
} $productsCount--;
$fileContent .= "$totalPrice".";"."\n"; }
return $fileContent; $fileContent .= "$totalPrice".";"."\n";
} return $fileContent;
}
private function populateClientStatDataIntoFileContent(string $fileContent,$month,array $statPerMonth){ private function populateClientStatDataIntoFileContent(string $fileContent, $month, array $statPerMonth)
$yearValue = $statPerMonth["year"]; {
$defuntCount = $statPerMonth["defunt_count"]; $yearValue = $statPerMonth["year"];
$products = $statPerMonth["products"]; $defuntCount = $statPerMonth["defunt_count"];
$products = $statPerMonth["products"];
$fileContent = $fileContent. $priceTotal = $statPerMonth["total_price"];
FileExportHelpers::FormatTextForExport($statPerMonth['client_name']).';'.
"$month".';'.
"$yearValue".';'.
"$defuntCount".';';
foreach($products as $productCount){ $fileContent = $fileContent.
$fileContent .= "$productCount".";"; FileExportHelpers::FormatTextForExport($statPerMonth['client_name']).';'.
} "$month".';'.
$fileContent .= "\n"; "$yearValue".';'.
return $fileContent; "$defuntCount".';';
} foreach($products as $productCount) {
$fileContent .= "$productCount".";";
}
$fileContent .= "$priceTotal".';'."\n";
return $fileContent;
}
} }