* * @author Anna Larch * @author Richard Steinmetz * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE * License as published by the Free Software Foundation; either * version 3 of the License, or any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU AFFERO GENERAL PUBLIC LICENSE for more details. * * You should have received a copy of the GNU Affero General Public * License along with this library. If not, see . * */ namespace OCA\Gestion\Service; use OCA\Gestion\Db\Bdd; use OCA\Gestion\Helpers\FileExportHelpers; use Psr\Log\LoggerInterface; class ExportClientStatisticService { /** @var Bdd */ private $gestionBdd; /** @var LoggerInterface */ private $logger; public function __construct( Bdd $gestionBdd, LoggerInterface $logger ) { $this->logger = $logger; $this->gestionBdd = $gestionBdd; } public function getFileName(array $clientIds) { $filename = ""; $clients = $this->gestionBdd->getClientsByClientsID($clientIds); foreach($clients as $client) { $filename .= $client['client_nom'] . '-' . $client['client_entreprise'] . '--'; } $filename = rtrim($filename, '-'); $filename = str_replace(' ', '-', $filename); $filename = str_replace(' ', '-', $filename); return $filename; } public function getExportClientFileHeader(): string { $fileHeader = 'CLIENT'.';'. 'MOIS'.';'. 'ANNEE'.';'. 'NB DE DEFUNTS'.';'; $produitList = $this->gestionBdd->getProduitsListAsArray(); foreach($produitList as $produit) { $fileHeader .= FileExportHelpers::FormatTextForExport($produit['reference']).';'; } $fileHeader .= 'TOTAL HT'.';'."\n"; return $fileHeader; } public function populateExportDataIntoFileContent(array $exportData, string $fileContent): string { foreach($exportData as $clientId => $clientData) { $clientName = $clientData["client_name"]; $clientStatPerMonth = $clientData["client_data"]; $totalPrice = 0; if(!empty($clientStatPerMonth)) { foreach($clientStatPerMonth as $month => $stat) { $stat["client_name"] = $clientName; $totalPrice += $stat["total_price"]; $fileContent = $this->populateClientStatDataIntoFileContent($fileContent, $month, $stat); } $fileContent = $this->populateTotalPriceIntoFileContent($fileContent, $totalPrice, count($stat["products"])); } } return $fileContent; } private function populateTotalPriceIntoFileContent(string $fileContent, $totalPrice, $productsCount) { $fileContent = $fileContent. ''.';'. ''.';'. ''.';'. ''.';'; 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"]; $products = $statPerMonth["products"]; $priceTotal = $statPerMonth["total_price"]; $fileContent = $fileContent. FileExportHelpers::FormatTextForExport($statPerMonth['client_name']).';'. "$month".';'. "$yearValue".';'. "$defuntCount".';'; foreach($products as $productCount) { $fileContent .= "$productCount".";"; } $fileContent .= "$priceTotal".';'."\n"; return $fileContent; } }