Thanasoft-H2F/gestion/lib/Service/ExportThanatoStatisticService.php
2025-02-21 09:56:32 +03:00

167 lines
5.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\Constants\OrderTypeConstant;
use OCA\Gestion\Constants\ThanatoTypeConstant;
use OCA\Gestion\Db\Bdd;
use OCA\Gestion\Helpers\FileExportHelpers;
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 getThanatoStatisticFolders($thanatoList,$year){
$thanatoStatFolders = ['STATISTIQUES/THANATOS/'];
foreach($thanatoList as $thanato){
if($thanato['fk_thanato_type_key'] == ThanatoTypeConstant::THANATO_TYPE_SUBCONTRACTOR){
$thanatoStatFolders[] = 'STATISTIQUES/FOURNISSEURS/'.$year.'/';
break;
}
}
return $thanatoStatFolders;
}
public function getFilename(array $thanatoList,$month,$year){
$filename = "$year-$month-";
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 =
'FACTURE'.';'.
'THANATOPRACTEUR'.';'.
'DATE'.';'.
'HEURE DE DEBUT'.';'.
'HEURE DE FIN'.';'.
'SOINS'.';'.
'JOUR/FERIE'.';'.
'NOM ET PRENOM'.';'.
'LIEU'.';'.
'POMPES FUNEBRES'.';'.
'ADRESSE'.';'.
'BON DE COMMANDE'.';'.
'TOTAL ACHAT'.';'.
'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"]);
$orderFullNumber = "";
$totalOrderPrice = "";
$isDevisDoneBySubContractor = $devis['fk_order_type_key'] == OrderTypeConstant::ORDER_TYPE_DEVIS &&
$devis['order_id'] != null && $devis["fk_thanato_type_key"] == ThanatoTypeConstant::THANATO_TYPE_SUBCONTRACTOR;
if($isDevisDoneBySubContractor){
$orderFullNumber = $devis["order_full_number"];
$totalOrderPrice = $devis["total_price"];
}
$fileContent = $fileContent.
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"] ?? "").';'.
FileExportHelpers::FormatTextForExport($orderFullNumber).';'.
"$totalOrderPrice".';'.
''.';'."\n";
return $fileContent;
}
}