From a28b7f6ea6739080b5f4bf5407ad15f3cb2642aa Mon Sep 17 00:00:00 2001 From: Tolotsoa Date: Thu, 20 Nov 2025 09:45:34 +0300 Subject: [PATCH] fix erreur stats thanato --- .../Service/ExportThanatoStatisticService.php | 19 +-- gestion/lib/Service/GeoService.php | 126 +++++++++++++----- 2 files changed, 105 insertions(+), 40 deletions(-) diff --git a/gestion/lib/Service/ExportThanatoStatisticService.php b/gestion/lib/Service/ExportThanatoStatisticService.php index e0907cb..51897ce 100644 --- a/gestion/lib/Service/ExportThanatoStatisticService.php +++ b/gestion/lib/Service/ExportThanatoStatisticService.php @@ -369,22 +369,23 @@ class ExportThanatoStatisticService private function populateDevisDataIntoThanatoExportFileContent(string $fileContent, array $devis) { $produitAsString = $this->getFormatDevisProduitsAsString($devis["produits"]); - $devisNum = (string)$devis["calendar_uuid"]; + $devisNum = (string)($devis["calendar_uuid"] ?? ""); + $fileContent = $fileContent. FileExportHelpers::FormatTextForExport($devisNum).';'. - FileExportHelpers::FormatTextForExport($devis['nom_thanato'] . ' ' . $devis['prenom_thanato']).';'. - FileExportHelpers::FormatTextForExport($devis["date"]).';'. - FileExportHelpers::FormatTextForExport($devis["startTime"]).';'. - FileExportHelpers::FormatTextForExport($devis["endTime"]).';'. + FileExportHelpers::FormatTextForExport(($devis['nom_thanato'] ?? '') . ' ' . ($devis['prenom_thanato'] ?? '')).';'. + 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["dayType"] ?? "").';'. + 'Non'.';'. ''.';'. ''.';'. - FileExportHelpers::FormatTextForExport($devis["nom_defunt"]).';'. + FileExportHelpers::FormatTextForExport($devis["nom_defunt"] ?? "").';'. // ICI FileExportHelpers::FormatTextForExport($devis["nom_lieu"] ?? "").';'. FileExportHelpers::FormatTextForExport($devis["nom_client"] ?? "").';'. - FileExportHelpers::FormatTextForExport($devis["client_adresse"] ?? ""). + FileExportHelpers::FormatTextForExport($devis["client_adresse"] ?? "").';'. ''.';'. ''.';'. ''.';'. diff --git a/gestion/lib/Service/GeoService.php b/gestion/lib/Service/GeoService.php index 34acd1a..270cc35 100644 --- a/gestion/lib/Service/GeoService.php +++ b/gestion/lib/Service/GeoService.php @@ -30,28 +30,31 @@ use Exception; use OCA\Gestion\Constants\GeoConstant; use OCA\Gestion\Helpers\GeoHelpers; -class GeoService { - - public function __construct() { - } +class GeoService +{ + public function __construct() + { + } /** * Calcul la distance entre les deux points à vol d'oiseau */ - private function getDistanceInKmBetweenTwoPoints($lat1, $lon1, $lat2, $lon2) { + private function getDistanceInKmBetweenTwoPoints($lat1, $lon1, $lat2, $lon2) + { $R = 6371; // Rayon moyen de la Terre en kilomètres $dLat = deg2rad($lat2 - $lat1); $dLon = deg2rad($lon2 - $lon1); - $a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin($dLon/2) * sin($dLon/2); - $c = 2 * atan2(sqrt($a), sqrt(1-$a)); + $a = sin($dLat / 2) * sin($dLat / 2) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin($dLon / 2) * sin($dLon / 2); + $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); $d = $R * $c; return round($d, 2); } - private function getTravelingHourBetweenTwoPoints(array $origin,array $destination,$mode = "driving"){ + private function getTravelingHourBetweenTwoPoints(array $origin, array $destination, $mode = "driving") + { $baseUrl = "https://api.geoapify.com/v1/routing"; - $originPoints = GeoHelpers::getPointsTextFromLatitudeAndLongitude($origin["latitude"],$origin["longitude"]); - $destinationPoints = GeoHelpers::getPointsTextFromLatitudeAndLongitude($destination["latitude"],$destination["longitude"]); + $originPoints = GeoHelpers::getPointsTextFromLatitudeAndLongitude($origin["latitude"], $origin["longitude"]); + $destinationPoints = GeoHelpers::getPointsTextFromLatitudeAndLongitude($destination["latitude"], $destination["longitude"]); $fullUrl = $baseUrl."?waypoints=$originPoints|$destinationPoints&mode=$mode&apiKey=9e23d93e7f454c988344f9171bf867aa"; $curl = curl_init(); @@ -66,11 +69,11 @@ class GeoService { CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'GET', )); - + $response = curl_exec($curl); - + curl_close($curl); - + if ($response === false) { return 0; } else { @@ -79,45 +82,45 @@ class GeoService { $travelTimeHours = round($travelTimeHours, 2); return $travelTimeHours; } - } - catch(Exception $e){ + } catch(Exception $e) { return 0; } } - public function getTotalDistanceAndTotalTravelingHoursBetweenDevisLocationByRouteLines(array $routeLines){ + public function getTotalDistanceAndTotalTravelingHoursBetweenDevisLocationByRouteLinesSave(array $routeLines) + { $distanceCumul = 0; $totalTravelingHoursBetweenTwoDevisLocation = 0; - $lastPoint = NULL; - for ($i=0; $i < sizeof($routeLines); $i++) { + $lastPoint = null; + for ($i = 0; $i < sizeof($routeLines); $i++) { $currentDistance = 0; - if($routeLines[$i]['lieu_id'] != NULL){ + if($routeLines[$i]['lieu_id'] != null) { $lastPoint = $routeLines[$i]; - } - if($lastPoint['lieu_id'] != NULL && $routeLines[$i+1]['lieu_id'] != NULL){ + } + if($lastPoint['lieu_id'] != null && $routeLines[$i + 1]['lieu_id'] != null) { $currentDistance = $this->getDistanceInKmBetweenTwoPoints( - floatval(value: $lastPoint['latitude']), - floatval($lastPoint['longitude']), - floatval($routeLines[$i+1]['latitude']), - floatval($routeLines[$i+1]['longitude']) + floatval(value: $lastPoint['latitude']), + floatval($lastPoint['longitude']), + floatval($routeLines[$i + 1]['latitude']), + floatval($routeLines[$i + 1]['longitude']) ); - $targetIsBetweenTwoDevisLocation = $lastPoint['source'] != "siege" && $routeLines[$i+1]["source"] != "siege"; - if($targetIsBetweenTwoDevisLocation){ + $targetIsBetweenTwoDevisLocation = $lastPoint['source'] != "siege" && $routeLines[$i + 1]["source"] != "siege"; + if($targetIsBetweenTwoDevisLocation) { $originPoints = [ "latitude" => $lastPoint["latitude"], "longitude" => $lastPoint["longitude"] ]; $destinationPoints = [ - "latitude" => $routeLines[$i+1]["latitude"], - "longitude" => $routeLines[$i+1]["longitude"] + "latitude" => $routeLines[$i + 1]["latitude"], + "longitude" => $routeLines[$i + 1]["longitude"] ]; - $totalTravelingHoursBetweenTwoDevisLocation+= $this->getTravelingHourBetweenTwoPoints( + $totalTravelingHoursBetweenTwoDevisLocation += $this->getTravelingHourBetweenTwoPoints( $originPoints, $destinationPoints, GeoConstant::DRIVING_MODE ); } - } + } $distanceCumul += $currentDistance; } return [ @@ -125,4 +128,65 @@ class GeoService { "totalTravelingHours" => $totalTravelingHoursBetweenTwoDevisLocation ]; } + + public function getTotalDistanceAndTotalTravelingHoursBetweenDevisLocationByRouteLines(array $routeLines) + { + $distanceCumul = 0; + $totalTravelingHoursBetweenTwoDevisLocation = 0; + $lastPoint = null; + + for ($i = 0; $i < sizeof($routeLines); $i++) { + $currentDistance = 0; + + // Vérifier que l'élément actuel existe + if(!isset($routeLines[$i])) { + continue; + } + + if($routeLines[$i]['lieu_id'] != null) { + $lastPoint = $routeLines[$i]; + } + + // Vérifier que $i+1 existe ET que $lastPoint n'est pas null + if($lastPoint !== null && + isset($routeLines[$i + 1]) && + isset($lastPoint['lieu_id']) && + isset($routeLines[$i + 1]['lieu_id']) && + $lastPoint['lieu_id'] != null && + $routeLines[$i + 1]['lieu_id'] != null) { + + $currentDistance = $this->getDistanceInKmBetweenTwoPoints( + floatval($lastPoint['latitude']), + floatval($lastPoint['longitude']), + floatval($routeLines[$i + 1]['latitude']), + floatval($routeLines[$i + 1]['longitude']) + ); + + $targetIsBetweenTwoDevisLocation = $lastPoint['source'] != "siege" && $routeLines[$i + 1]["source"] != "siege"; + + if($targetIsBetweenTwoDevisLocation) { + $originPoints = [ + "latitude" => $lastPoint["latitude"], + "longitude" => $lastPoint["longitude"] + ]; + $destinationPoints = [ + "latitude" => $routeLines[$i + 1]["latitude"], + "longitude" => $routeLines[$i + 1]["longitude"] + ]; + $totalTravelingHoursBetweenTwoDevisLocation += $this->getTravelingHourBetweenTwoPoints( + $originPoints, + $destinationPoints, + GeoConstant::DRIVING_MODE + ); + } + } + + $distanceCumul += $currentDistance; + } + + return [ + "totalDistance" => round($distanceCumul, 2), + "totalTravelingHours" => round($totalTravelingHoursBetweenTwoDevisLocation, 2) + ]; + } }