From ba5dc3230d64604c5d8537562b55c6eb863abf23 Mon Sep 17 00:00:00 2001 From: Tiavina Date: Mon, 27 Jan 2025 14:12:44 +0300 Subject: [PATCH] wip thanato statistique --- gestion/lib/Controller/PageController.php | 8 +- gestion/lib/Db/Bdd.php | 137 ++++++++++++++---- gestion/lib/Helpers/DateHelpers.php | 16 ++ .../Service/ExportThanatoStatisticService.php | 37 ++++- 4 files changed, 165 insertions(+), 33 deletions(-) diff --git a/gestion/lib/Controller/PageController.php b/gestion/lib/Controller/PageController.php index 1f0d898..33aa2ee 100644 --- a/gestion/lib/Controller/PageController.php +++ b/gestion/lib/Controller/PageController.php @@ -2333,14 +2333,18 @@ class PageController extends Controller { * @NoAdminRequired * @NoCSRFRequired * @param array $thanatoIdsToExport + * @param string $month + * @param string $year * */ - public function exportThanatoStatistic($thanatoIdsToExport){ + public function exportThanatoStatistic($thanatoIdsToExport,$month,$year){ if(empty($thanatoIdsToExport)){ return ""; } - $exportData = $this->myDb->getExportThanatoStatisticData($thanatoIdsToExport); + $month = $month ?? date('m'); + $year = $year ?? date('Y'); + $exportData = $this->myDb->getExportThanatoStatisticData($thanatoIdsToExport,$month,$year); try{ $current_config = json_decode($this->myDb->getConfiguration($this->idNextcloud)); $clean_folder = html_entity_decode($current_config[0]->path).'/'; diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index a734486..5df71d9 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -2148,10 +2148,95 @@ class Bdd { return $data; } - public function getExportThanatoStatisticData(array $thanatoIds){ - $devisList = $this->getDevisListByThanatoIds($thanatoIds); - $devisListGroupedByDateAndThenByThanato = $this->getDevisListGroupedByDateAndThenByThanato($devisList); - return $devisListGroupedByDateAndThenByThanato; + private function getThanatosDevisListByDate(array $thanatoIds,$date){ + if(empty($thanatoIds)){ + return []; + } + + $dateFormatted = $date->format('Y-m-d'); + + $sqlConditionsPlaceholder = implode(',', array_fill(0, count($thanatoIds), '?')); + $sql = "SELECT + devis.id, + devis.date, + devis.mentions, + devis.num as calendar_uuid, + devis.id_defunt as id_defunt, + devis.id_lieu as id_lieu, + devis.id_client as id_client, + devis.id_thanato as id_thanato, + thanato.nom as nom_thanato, + thanato.prenom as prenom_thanato, + defunt.nom as nom_defunt, + lieu.nom as nom_lieu, + lieu.latitude as lieu_latitude, + lieu.longitude as lieu_longitude, + client.nom as nom_client, + client.entreprise as client_entreprise, + client.adresse as client_adresse, + facture.num as facture_num + FROM ".$this->tableprefix."devis as devis + LEFT JOIN ".$this->tableprefix."thanato as thanato on devis.id_thanato = thanato.id + LEFT JOIN ".$this->tableprefix."lieu as lieu on devis.id_lieu = lieu.id + LEFT JOIN ".$this->tableprefix."defunt as defunt on devis.id_defunt = defunt.id + LEFT JOIN ".$this->tableprefix."client as client on devis.id_client = client.id + LEFT JOIN ".$this->tableprefix."facture as facture on devis.id = facture.id_devis + WHERE devis.date = ? AND + devis.id_thanato IN ($sqlConditionsPlaceholder) AND + (devis.mentions = ? OR devis.mentions = ?) + ORDER BY devis.date ASC;"; + $devisList = $this->execSQLNoJsonReturn( + $sql, + array_merge([$dateFormatted],$thanatoIds,[DevisMentionConstant::FACTURED,DevisMentionConstant::FACTURED_FORMATTED])); + return $devisList; + } + + public function getThanatoDevisPerDateInAMonthYear(array $thanatoIds,$month,$year){ + $dateOfMonths = DateHelpers::getDatesOfMonth($year,$month); + $devisListPerThanatoPerDate = []; + foreach($dateOfMonths as $currentDate){ + $currentDateFormatted = $currentDate->format('Y-m-d'); + $devisList = $this->getThanatosDevisListByDate($thanatoIds,$currentDate); + if(empty($devisList)){ + //get if on leave + //if on leave set hours + //else set + foreach($thanatoIds as $thanatoId){ + $devisListPerThanatoPerDate[$thanatoId][$currentDateFormatted] = [ + "onLeave" => false, + "leaveTimeStart" => null, + "leaveTimeEnd" => null + ]; + } + } + else{ + foreach($devisList as $devis){ + $devisThanatoId = $devis["id_thanato"]; + $devis = $this->setDevisStartAndEndTime($devis); + $devis = $this->setDevisIsWeekendOrNotText($devis); + $devis = $this->setDevisProduitsList($devis); + //set devis articles list into devis data + if (!isset($devisListPerThanatoPerDate[$devisThanatoId])) { + $devisListPerThanatoPerDate[$devisThanatoId] = []; + } + if (!isset($devisListPerThanatoPerDate[$devisThanatoId][$currentDateFormatted])) { + $devisListPerThanatoPerDate[$devisThanatoId][$currentDateFormatted] = [ + 'total_distance' => 0, + "devis" => [], + "devisId" => [] + ]; + } + $devisListPerThanatoPerDate[$devisThanatoId][$currentDateFormatted]["devis"][] = $devis; + $devisListPerThanatoPerDate[$devisThanatoId][$currentDateFormatted]["devisId"][] = $devis['id']; + } + } + } + return $devisListPerThanatoPerDate; + } + + public function getExportThanatoStatisticData(array $thanatoIds,$month,$year){ + $devisList = $this->getThanatoDevisPerDateInAMonthYear($thanatoIds,$month,$year); + return $devisList; } public function getProduitsDevisByDevisId($devisId){ @@ -2169,29 +2254,31 @@ class Bdd { return $produitsList; } - private function getDevisListGroupedByDateAndThenByThanato(array $devisList){ - $devisListGroupedByDateAndThenByThanato = []; - foreach($devisList as $devis){ - $devisDate = $devis["date"]; - $devisThanatoId = $devis["id_thanato"]; - $devis = $this->setDevisStartAndEndTime($devis); - $devis = $this->setDevisIsWeekendOrNotText($devis); - $devis = $this->setDevisProduitsList($devis); - //set devis articles list into devis data - if (!isset($devisListGroupedByDateAndThenByThanato[$devisThanatoId])) { - $devisListGroupedByDateAndThenByThanato[$devisThanatoId] = []; + private function getDevisListGroupedByDateAndThenByThanato(array $devisListPerDate){ + $devisListGroupedByThanatoAndThenByDate = []; + foreach($devisListPerDate as $date => $devisList){ + foreach($devisList as $devis){ + $devisDate = $devis["date"]; + $devisThanatoId = $devis["id_thanato"]; + $devis = $this->setDevisStartAndEndTime($devis); + $devis = $this->setDevisIsWeekendOrNotText($devis); + $devis = $this->setDevisProduitsList($devis); + //set devis articles list into devis data + if (!isset($devisListGroupedByThanatoAndThenByDate[$devisThanatoId])) { + $devisListGroupedByThanatoAndThenByDate[$devisThanatoId] = []; + } + if (!isset($devisListGroupedByThanatoAndThenByDate[$devisThanatoId][$devisDate])) { + $devisListGroupedByThanatoAndThenByDate[$devisThanatoId][$devisDate] = [ + 'total_distance' => 0, + "devis" => [], + "devisId" => [] + ]; + } + $devisListGroupedByThanatoAndThenByDate[$devisThanatoId][$date]["devis"][] = $devis; + $devisListGroupedByThanatoAndThenByDate[$devisThanatoId][$date]["devisId"][] = $devis['id']; } - if (!isset($devisListGroupedByDateAndThenByThanato[$devisThanatoId][$devisDate])) { - $devisListGroupedByDateAndThenByThanato[$devisThanatoId][$devisDate] = [ - 'total_distance' => 0, - "devis" => [], - "devisId" => [] - ]; - } - $devisListGroupedByDateAndThenByThanato[$devisThanatoId][$devisDate]["devis"][] = $devis; - $devisListGroupedByDateAndThenByThanato[$devisThanatoId][$devisDate]["devisId"][] = $devis['id']; } - return $devisListGroupedByDateAndThenByThanato; + return $devisListGroupedByThanatoAndThenByDate; } private function setDevisProduitsList($devis){ diff --git a/gestion/lib/Helpers/DateHelpers.php b/gestion/lib/Helpers/DateHelpers.php index 19961a7..d42b152 100644 --- a/gestion/lib/Helpers/DateHelpers.php +++ b/gestion/lib/Helpers/DateHelpers.php @@ -64,4 +64,20 @@ class DateHelpers return $lastDay; } + public static function getDatesOfMonth($year, $month) { + $dates = []; + $daysInMonth = self::getDaysCountInAMonthAndYear($month, $year); + + for ($day = 1; $day <= $daysInMonth; $day++) { + $dateString = sprintf("%04d-%02d-%02d", $year, $month, $day); + $dates[] = new DateTime($dateString); + } + + return $dates; + } + + public static function getDaysCountInAMonthAndYear($month,$year){ + return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31); + } + } diff --git a/gestion/lib/Service/ExportThanatoStatisticService.php b/gestion/lib/Service/ExportThanatoStatisticService.php index f8b4dcf..e562f0a 100644 --- a/gestion/lib/Service/ExportThanatoStatisticService.php +++ b/gestion/lib/Service/ExportThanatoStatisticService.php @@ -76,16 +76,41 @@ class ExportThanatoStatisticService { return $fileHeader; } + private function populateNoDevisDataInADay(string $fileContent,string $dateAsString){ + $fileContent = $fileContent. + ''.';'. + ''.';'. + $dateAsString.';'. + ''.';'. + ''.';'. + ''.';'. + ''.';'. + ''.';'. + ''.';'. + ''.';'. + ''.';'. + ''.';'."\n"; + return $fileContent; + } + public function populateExportDataIntoFileContent(array $exportData,string $fileContent): string{ + // var_dump($exportData); + // die; 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); + $currentDevisIsNotDevis = array_key_exists('onLeave',$devisData); + if($currentDevisIsNotDevis){ + $fileContent = $this->populateNoDevisDataInADay($fileContent,$devisDate); + } + else{ + $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); } - $fileContent = $this->populateDistanceTotalIntoThanatoExportFileContent($fileContent,$distanceTotal); } } }