* * @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\Constants\AbsenceTypeConstant; use OCA\Gestion\Constants\BddConstant; use OCA\Gestion\Db\Bdd; use OCA\Gestion\Helpers\DateHelpers; use OCA\Gestion\Helpers\FileExportHelpers; use Psr\Log\LoggerInterface; use OCP\Files\IRootFolder; class ExportThanatoStatisticService { /** @var Bdd */ private $gestionBdd; /** @var LoggerInterface */ private $logger; /** @var IRootFolder */ private $rootFolder; private $geoService; public function __construct( Bdd $gestionBdd, LoggerInterface $logger, IRootFolder $rootFolder, GeoService $geoService) { $this->geoService = $geoService; $this->rootFolder = $rootFolder; $this->logger = $logger; $this->gestionBdd = $gestionBdd; } private function getFilename($thanatoName,$thanatoLastName,$month,$year){ $filename = "$year-$month-"; $filename .= $thanatoName . '-' . $thanatoLastName; $filename = str_replace(' ','-', $filename); $filename = str_replace(' ','-', $filename); return $filename; } private function exportThanatoStatistic($thanatoId,$month,$year,$idNextcloud){ $thanato = $this->gestionBdd->getThanatoById($thanatoId); if($thanato == null){ return null; } $exportData = $this->gestionBdd->getExportThanatoStatisticData($thanatoId,$month,$year); if(empty($exportData)){ return null; } $defaultConfig = json_decode($this->gestionBdd->getConfiguration(BddConstant::DEFAULT_ADMIN_APP_ID_NEXTCLOUD)); $racineFolder = html_entity_decode($defaultConfig[0]->path).'/'; $thanatoFolder = $racineFolder.'STATISTIQUES/THANATOS/'; $fileHeader = $this->getExportThanatoFileHeader(); $fileContent = $this->populateExportDataIntoFileContent($exportData,$fileHeader); $storage = $this->rootFolder->getUserFolder($idNextcloud); try{ $storage->newFolder($thanatoFolder); } catch(\OCP\Files\NotPermittedException $e) { } $filename = $this->getFilename($thanato["nom"],$thanato["prenom"],$month,$year); $fileNamePath = $thanatoFolder."STAT-THANATO-" . $filename . '.csv'; $storage->newFile($fileNamePath); $file = $storage->get($fileNamePath); $file->putContent($fileContent); return $fileNamePath; } public function exportThanatosListStatistic(array $thanatoIds,$month,$year,$idNextcloud){ $filenames = []; foreach($thanatoIds as $thanatoId){ $filename = $this->exportThanatoStatistic($thanatoId,$month,$year,$idNextcloud); if($filename != null){ $filenames[] = $filename; } } return $filenames; } public function getExportThanatoFileHeader(): string{ $fileHeader = 'FACTURE'.';'. 'THANATOPRACTEUR'.';'. 'DATE'.';'. 'HEURE DE DEBUT'.';'. 'HEURE DE FIN'.';'. 'SOINS'.';'. 'JOUR/FERIE'.';'. 'CONGE'.';'. 'REPOS'.';'. 'MALADIE'.';'. 'NOM ET PRENOM'.';'. 'LIEU'.';'. 'POMPES FUNEBRES'.';'. 'ADRESSE'.';'. 'DISTANCE TOTALE KM'.';'. 'HEURES TOTAL DE SOIN'.';'. 'HEURES TOTAL DE CONGE'.';'. 'HEURES TOTAL DE REPOS'.';'. 'HEURES TOTAL DE MALADIE'.';'. 'HEURES TOTAL DE TRAVAIL'.';'. 'HEURES TOTAL DE PARCOURS ENTRE DEVIS'.';'. "\n"; return $fileHeader; } private function populateNoDevisDataInADay(string $fileContent,$leave){ $startTimeValue = ""; $endTimeValue = ""; $leaveValue = "Non"; $diseaseValue = "Non"; $restValue = "Non"; if($leave["onLeave"]){ $startTimeValue = $leave["startTime"]; $endTimeValue = $leave["endTime"]; if($leave["absenceTypeKey"] == AbsenceTypeConstant::LEAVE){ $leaveValue = "Oui"; } if($leave["absenceTypeKey"] == AbsenceTypeConstant::DISEASE){ $diseaseValue = "Oui"; } if($leave["absenceTypeKey"] == AbsenceTypeConstant::REST){ $restValue = "Oui"; } } $fileContent = $fileContent. ''.';'. FileExportHelpers::FormatTextForExport($leave['thanatoName']).';'. $leave['date'].';'. $startTimeValue.';'. $endTimeValue.';'. ''.';'. DateHelpers::getPublicHolidayText($leave['isPublicHoliday']).';'. $leaveValue.';'. $restValue.';'. $diseaseValue.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'."\n"; return $fileContent; } private function populateReturnToSiegeDataInToFileContent(string $fileContent , $devis){ $fileContent = $fileContent. ''.';'. FileExportHelpers::FormatTextForExport($devis['nom_thanato'] . ' ' . $devis['prenom_thanatho']).';'. FileExportHelpers::FormatTextForExport($devis["date"]).';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. FileExportHelpers::FormatTextForExport('H2F').';'. ''.';'. ''.';'. ''.';'. "0.5H".';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'."\n"; return $fileContent; } public function populateExportDataIntoFileContent(array $exportData,string $fileContent): string{ foreach($exportData as $devisDate => $devisData){ $totalDevisHours = 0; $totalWorkedHours = 8; $totalLeaveHours = 0; $totalDiseaseHours = 0; $totalRestHours = 0; $totalDistance = 0; $totalTravelingHoursBetweenDevisLocation = 0; $hasDevisInTheCurrentDate = $devisData['hasDevis']; if($hasDevisInTheCurrentDate === false){ $leaves = $devisData["leaves"]; foreach($leaves as $leave){ $fileContent = $this->populateNoDevisDataInADay($fileContent,$leave); if($leave["onLeave"]){ $totalLeaveHoursInsideWorkingHours = $leave["totalWorkedHours"]; if($leave["absenceTypeKey"] == AbsenceTypeConstant::LEAVE){ $totalLeaveHours += $totalLeaveHoursInsideWorkingHours; } if($leave["absenceTypeKey"] == AbsenceTypeConstant::REST){ $totalRestHours += $totalLeaveHoursInsideWorkingHours; } if($leave["absenceTypeKey"] == AbsenceTypeConstant::DISEASE){ $totalDiseaseHours += $totalLeaveHoursInsideWorkingHours; } } } $totalAbsenceHours = $totalLeaveHours + $totalRestHours + $totalDiseaseHours; $totalWorkedHours -= $totalAbsenceHours; } else{ $routeLines = $this->gestionBdd->getRouteLinesByDevisIdList($devisData["devisId"]); $totalDistanceAndTotalTravelingHoursBetweenDevis = $this->geoService->getTotalDistanceAndTotalTravelingHoursBetweenDevisLocationByRouteLines($routeLines); $totalDistance = $totalDistanceAndTotalTravelingHoursBetweenDevis["totalDistance"]; $totalTravelingHoursBetweenDevisLocation = $totalDistanceAndTotalTravelingHoursBetweenDevis["totalTravelingHours"]; $devisList = $devisData["devis"]; $leaves = $devisData["leaves"]; if(!empty($devisList)){ foreach($devisList as $devis){ $fileContent = $this->populateDevisDataIntoThanatoExportFileContent($fileContent,$devis); if ($devis['return_to_siege']) { $fileContent = $this->populateReturnToSiegeDataInToFileContent($fileContent , $devis); } $totalDevisHours += $devis["totalHours"]; } } foreach($leaves as $leave){ $fileContent = $this->populateNoDevisDataInADay($fileContent,$leave); if($leave["onLeave"]){ $totalLeaveHoursInsideWorkingHours = $leave["totalWorkedHours"]; if($leave["absenceTypeKey"] == AbsenceTypeConstant::LEAVE){ $totalLeaveHours += $totalLeaveHoursInsideWorkingHours; } if($leave["absenceTypeKey"] == AbsenceTypeConstant::REST){ $totalRestHours += $totalLeaveHoursInsideWorkingHours; } if($leave["absenceTypeKey"] == AbsenceTypeConstant::DISEASE){ $totalDiseaseHours += $totalLeaveHoursInsideWorkingHours; } } } $totalAbsenceHours = $totalLeaveHours + $totalRestHours + $totalDiseaseHours; $totalWorkedHours -= $totalAbsenceHours; } $fileContent = $this->populateLastRecapForTheLine( $fileContent, $totalDistance, $totalDevisHours, $totalWorkedHours, $totalLeaveHours, $totalTravelingHoursBetweenDevisLocation, $totalDiseaseHours, $totalRestHours ); } return $fileContent; } private function populateLastRecapForTheLine(string $fileContent,$distance,$totalDevisHours,$totalWorkedHours,$totalLeaveHours,$totalTravelingHours,$totalDiseaseHours = 0,$totalRestHours = 0){ $fileContent = $fileContent. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. "$distance"."KM".';'. "$totalDevisHours"."H".';'. "$totalLeaveHours"."H".';'. "$totalRestHours"."H".';'. "$totalDiseaseHours"."H".';'. "$totalWorkedHours"."H".';'. "$totalTravelingHours"."H"."\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. 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('Non').';'. ''.';'. ''.';'. FileExportHelpers::FormatTextForExport($devis["nom_defunt"]).';'. FileExportHelpers::FormatTextForExport($devis["nom_lieu"] ?? "").';'. FileExportHelpers::FormatTextForExport($devis["nom_client"] ?? "").';'. FileExportHelpers::FormatTextForExport($devis["client_adresse"] ?? ""). ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'. ''.';'."\n"; return $fileContent; } }