THANASOFT-HFC/gestion/lib/Service/ExportThanatoStatisticService.php
2024-12-30 18:44:06 +03:00

137 lines
4.4 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 ExportThanatoStatisticService {
/** @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 $thanatoIds){
$thanatoList = $this->gestionBdd->getThanatoByIds($thanatoIds);
$currentYear = date('Y');
$currentMonth = date('m');
$filename = "$currentYear-$currentMonth-";
foreach($thanatoList as $thanato){
$filename .= $thanato['thanato_nom'] . '-' . $thanato['thanato_prenom'] . '--';
}
$filename = rtrim($filename, '-');
$filename = str_replace(' ','-', $filename);
$filename = str_replace('&nbsp;','-', $filename);
return $filename;
}
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)'.';'.
"\n";
return $fileHeader;
}
public function populateExportDataIntoFileContent(array $exportData,string $fileContent): string{
foreach($exportData as $thanatoId => $devisPerThanato){
foreach($devisPerThanato as $devisDate => $devisData){
$distanceTotal = $this->gestionBdd->getDistanceTotalByDevisIdList($devisData["devisId"]);
$devisList = $devisData["devis"];
if(!empty($devisList)){
foreach($devisList as $devis){
$fileContent = $this->populateDevisDataIntoThanatoExportFileContent($fileContent,$devis);
}
$fileContent = $this->populateDistanceTotalIntoThanatoExportFileContent($fileContent,$distanceTotal);
}
}
}
return $fileContent;
}
private function populateDistanceTotalIntoThanatoExportFileContent(string $fileContent,$distance){
$fileContent = $fileContent.
''.';'.
''.';'.
''.';'.
''.';'.
''.';'.
''.';'.
''.';'.
''.';'.
''.';'.
''.';'.
utf8_decode(html_entity_decode("$distance"))."\n";
return $fileContent;
}
private function getFormatDevisProduitsAsString($devisProduits){
$result = '';
foreach ($devisProduits as $produit) {
$result .= $produit['produit_reference'] . '-' . $produit['produit_description'] . '--';
}
// Remove the trailing "--" at the end
$result = rtrim($result, '-');
return $result;
}
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";
return $fileContent;
}
}