THANASOFT-DV/gestion/lib/Service/ExportThanatoStatisticService.php

356 lines
11 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 OCP\Files\IRootFolder;
use Psr\Log\LoggerInterface;
use OCA\Gestion\Helpers\DateHelpers;
use OCA\Gestion\Constants\BddConstant;
use OCA\Gestion\Helpers\FileExportHelpers;
use OCA\Gestion\Constants\AbsenceTypeConstant;
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('&nbsp;','-', $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_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'.';'.
'NOMBRE DE SOINS ET TOILETTES'.';'.
"\n";
return $fileHeader;
}
private function populateNoDevisDataInADay(string $fileContent,$leave){
$startTimeValue = "";
$endTimeValue = "";
$leaveValue = "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;
}
public function populateExportDataIntoFileContent(array $exportData,string $fileContent): string{
$g_totalDistance = 0;
$g_totalDevisHours = 0;
$g_totalWorkedHours = 0;
$g_totalLeaveHours = 0;
$g_totalTravelingHoursBetweenDevisLocation = 0;
$g_totalDiseaseHours = 0;
$g_totalRestHours = 0;
$g_totalDevisCount = 0;
foreach($exportData as $devisDate => $devisData){
$totalDevisHours = 0;
$totalWorkedHours = 8;
$totalLeaveHours = 0;
$totalDiseaseHours = 0;
$totalRestHours = 0;
$totalDistance = 0;
$totalDevisCount = 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{
$totalDevisCount += count($devisData["devisId"]);
$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);
$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,$totalDevisCount
);
$g_totalDistance += $totalDistance;
$g_totalDevisHours += $totalDevisHours;
$g_totalWorkedHours += $totalWorkedHours;
$g_totalLeaveHours += $totalLeaveHours;
$g_totalTravelingHoursBetweenDevisLocation += $totalTravelingHoursBetweenDevisLocation;
$g_totalDiseaseHours += $totalDiseaseHours;
$g_totalRestHours += $totalRestHours;
$g_totalDevisCount += $totalDevisCount;
}
$fileContent = $this->populateLastRecapForTheLine(
$fileContent,
$g_totalDistance,
$g_totalDevisHours,
$g_totalWorkedHours,
$g_totalLeaveHours,
$g_totalTravelingHoursBetweenDevisLocation,
$g_totalDiseaseHours,
$g_totalRestHours,$g_totalDevisCount
);
return $fileContent;
}
private function populateLastRecapForTheLine(string $fileContent,$distance,$totalDevisHours,$totalWorkedHours,$totalLeaveHours,$totalTravelingHours ,$totalDiseaseHours = 0,$totalRestHours = 0,$totalDevisCount = 0){
$fileContent = $fileContent.
''.';'.
''.';'.
''.';'.
''.';'.
''.';'.
''.';'.
''.';'.
''.';'.
''.';'.
''.';'.
''.';'.
''.';'.
''.';'.
''.';'.
"$distance".';'.
"$totalDevisHours".';'.
"$totalLeaveHours".';'.
"$totalRestHours".';'.
"$totalDiseaseHours".';'.
"$totalWorkedHours".';'.
"$totalTravelingHours".';'.
"$totalDevisCount"."\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"]);
$factureNum = $devis["facture_num"] ?? $devis["facture_on_group_num"] ?? "";
$fileContent = $fileContent.
FileExportHelpers::FormatTextForExport($factureNum).';'.
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;
}
}