Thanasoft-H2F/gestion/lib/Service/ExportClientStatisticService.php
2024-12-29 17:21:18 +03:00

111 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
/**
* Calendar App
*
* @copyright 2021 Anna Larch <anna.larch@gmx.net>
*
* @author Anna Larch <anna.larch@gmx.net>
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* 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 <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Gestion\Service;
use OCA\Gestion\Db\Bdd;
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, '-');
return $filename;
}
public function getExportClientFileHeader(): string{
$fileHeader =
'Client'.';'.
'Mois'.';'.
'Année'.';'.
'Nb defunts'.';'.
'Nb articles'.';'.
'Total HT'.';'.
"\n";
return $fileHeader;
}
public function populateExportDataIntoFileContent(array $exportData,string $fileContent): string{
foreach($exportData as $clientId => $clientData){
foreach($clientData as $data){
$clientName = $data["client_name"];
$clientStatPerMonth = $data["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);
}
}
}
return $fileContent;
}
private function populateTotalPriceIntoFileContent(string $fileContent,$price){
$fileContent = $fileContent.
''.';'.
''.';'.
''.';'.
''.';'.
''.';'.
utf8_decode(html_entity_decode("$price"))."\n";
return $fileContent;
}
private function populateClientStatDataIntoFileContent(string $fileContent,$month,array $statPerMonth){
$fileContent = $fileContent.
utf8_decode(html_entity_decode($statPerMonth['client_name'])).';'.
utf8_decode(html_entity_decode("$month")).';'.
utf8_decode(html_entity_decode($statPerMonth["year"])).';'.
utf8_decode(html_entity_decode($statPerMonth["defunt_count"])).';'.
utf8_decode(html_entity_decode($statPerMonth["produit_count"])).';'.
utf8_decode(html_entity_decode($statPerMonth["total_price"])).';'."\n";
return $fileContent;
}
}