From ed78eab6d17be4ebf35cd65e7413015273ad5b9b Mon Sep 17 00:00:00 2001 From: Tiavina Date: Fri, 27 Dec 2024 11:10:15 +0300 Subject: [PATCH] wip relate devis and calendar --- gestion/lib/Db/Bdd.php | 67 +++++++++++++++++-- gestion/lib/Helpers/DateHelpers.php | 23 +++++++ gestion/lib/Helpers/VCalendarHelpers.php | 64 ++++++++++++++++++ .../Service/ExportThanatoStatisticService.php | 9 +++ gestion/lib/Service/GestionService.php | 46 +++++++------ 5 files changed, 182 insertions(+), 27 deletions(-) create mode 100644 gestion/lib/Helpers/DateHelpers.php create mode 100644 gestion/lib/Helpers/VCalendarHelpers.php diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index ae78f3e..561a014 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -1,13 +1,19 @@ format('Y-m-d'); + } $idNextcloud = "admin"; $last=0; $last = $this->lastinsertid("devis", "admin") + 1; @@ -832,10 +842,11 @@ class Bdd { `comment`, `user_id` ) - VALUES (NOW(),?,?,?,?,?,?,?,?,?,?);"; + VALUES (?,?,?,?,?,?,?,?,?,?,?);"; $this->execSQLNoData($sql, array( + $date, $idNextcloud, - "Nom du defunt", + $calendarUuid, $defuntId, $clientId, $thanatoId, @@ -845,18 +856,19 @@ class Bdd { $this->l->t('Comment'), $last)); - return $this->getLastDevisIdFromVCalendarProperty($thanatoId,$clientId,$locationId,$defuntId); + return $this->getLastDevisIdFromVCalendarProperty($thanatoId,$clientId,$locationId,$defuntId,$calendarUuid); } - public function getLastDevisIdFromVCalendarProperty($thanatoId,$clientId,$locationId,$defuntId){ + public function getLastDevisIdFromVCalendarProperty($thanatoId,$clientId,$locationId,$defuntId,$calendarUuid=self::DEFAULT_CALENDAR_UUID_FOR_DEVIS){ $sql = "SELECT max(id) as LAST_INSERT_ID FROM ".$this->tableprefix."devis - WHERE id_defunt = ? AND id_lieu = ? AND id_client = ? AND id_thanato = ?"; + WHERE id_defunt = ? AND id_lieu = ? AND id_client = ? AND id_thanato = ? AND num = ?"; $res = $this->execSQLNoJsonReturn($sql,array( $defuntId, $locationId, $clientId, - $thanatoId + $thanatoId, + $calendarUuid )); if($res){ return $res[0]['LAST_INSERT_ID']; @@ -1757,12 +1769,29 @@ class Bdd { "devisId" => [] ]; } + $devis = $this->setDevisStartAndEndTime($devis); + $devis = $this->setDevisIsWeekendOrNotText($devis); $devisListGroupedByDateAndThenByThanato[$devisThanatoId][$devisDate]["devis"][] = $devis; $devisListGroupedByDateAndThenByThanato[$devisThanatoId][$devisDate]["devisId"][] = $devis['id']; } return $devisListGroupedByDateAndThenByThanato; } + private function setDevisIsWeekendOrNotText($devis){ + $isWeekend = DateHelpers::isWeekend($devis['date']); + $value = $isWeekend ? "Férié" : "J"; + $devis["dayType"] = $value; + return $devis; + } + + private function setDevisStartAndEndTime($devis){ + $calendarData = $this->getCalendarDataByCalendarObjectUuid($devis["calendar_uuid"]); + $devisTimeValue = VCalendarHelpers::GetStartAndEndTimeFromVCalendarString($calendarData); + $devis["startTime"] = $devisTimeValue["startTime"]; + $devis["endTime"] = $devisTimeValue["endTime"]; + return $devis; + } + public function getDistanceTotalByDevisIdList(array $devisIdList){ $ligneTrajetList = $this->getLigneTrajetsByDevisIdList($devisIdList); $distanceTotal = $this->getLigneTrajetsListDistance($ligneTrajetList); @@ -1816,6 +1845,7 @@ class Bdd { 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, @@ -1843,4 +1873,27 @@ class Bdd { return $devisList; } + public function getCalendarDataByCalendarObjectUuid(string $calendarObjectUuid){ + $sql = "SELECT * FROM ".self::CALENDAR_TABLE_PREFIX."calendarobjects WHERE uid = ?;"; + $calendarObjectList = $this->execSQLNoJsonReturn($sql, [$calendarObjectUuid]); + if(!empty($calendarObjectList)){ + $calendarDataBlob = $calendarObjectList[0]['calendardata']; + $calendarDataString = VCalendarHelpers::ReadVCalendarDataBlob($calendarDataBlob); + return $calendarDataString; + } + return ""; + } + + /** + * @param $calendarData + * @return bool|string + */ + private function readBlob($calendarData) { + if (is_resource($calendarData)) { + return stream_get_contents($calendarData); + } + + return $calendarData; + } + } \ No newline at end of file diff --git a/gestion/lib/Helpers/DateHelpers.php b/gestion/lib/Helpers/DateHelpers.php new file mode 100644 index 0000000..b266531 --- /dev/null +++ b/gestion/lib/Helpers/DateHelpers.php @@ -0,0 +1,23 @@ +format('N'); + return in_array($dayOfWeek, [6, 7]); + } catch (Exception $e) { + // Handle invalid date strings + return false; + } + } + +} diff --git a/gestion/lib/Helpers/VCalendarHelpers.php b/gestion/lib/Helpers/VCalendarHelpers.php new file mode 100644 index 0000000..f36197e --- /dev/null +++ b/gestion/lib/Helpers/VCalendarHelpers.php @@ -0,0 +1,64 @@ +format("H") . "h"; + } + $dateEnd = self::GetDateStartOrDateEndFromVCalendarString("DTEND", $vCalendarString); + if($dateEnd != null){ + $endTimeValue = $dateEnd->format("H") . "h"; + } + } + return [ + "startTime" => $startTimeValue, + "endTime" => $endTimeValue + ]; + } + + public static function ReadVCalendarDataBlob($vCalendarData){ + if (is_resource($vCalendarData)) { + return stream_get_contents($vCalendarData); + } + return $vCalendarData; + } +} diff --git a/gestion/lib/Service/ExportThanatoStatisticService.php b/gestion/lib/Service/ExportThanatoStatisticService.php index 704ffc9..e8133b8 100644 --- a/gestion/lib/Service/ExportThanatoStatisticService.php +++ b/gestion/lib/Service/ExportThanatoStatisticService.php @@ -47,6 +47,9 @@ class ExportThanatoStatisticService { $fileHeader = 'Thanatopracteur'.';'. 'Date'.';'. + 'Heure Debut'.';'. + 'Heure Fin'.';'. + 'Jour/Férié'.';'. 'Nom et Prenoms'.';'. 'Lieu'.';'. 'Pompe funebre'.';'. @@ -80,6 +83,9 @@ class ExportThanatoStatisticService { ''.';'. ''.';'. ''.';'. + ''.';'. + ''.';'. + ''.';'. utf8_decode(html_entity_decode("$distance"))."\n"; return $fileContent; } @@ -88,6 +94,9 @@ class ExportThanatoStatisticService { $fileContent = $fileContent. utf8_decode(html_entity_decode($devis['nom_thanato'] . ' ' . $devis['prenom_thanatho'])).';'. utf8_decode(html_entity_decode($devis["date"])).';'. + utf8_decode(html_entity_decode($devis["startTime"])).';'. + utf8_decode(html_entity_decode($devis["endTime"])).';'. + utf8_decode(html_entity_decode($devis["dayType"])).';'. utf8_decode(html_entity_decode($devis["nom_defunt"])).';'. utf8_decode(html_entity_decode($devis["nom_lieu"] ?? "")).';'. utf8_decode(html_entity_decode($devis["nom_client"] ?? "")).';'. diff --git a/gestion/lib/Service/GestionService.php b/gestion/lib/Service/GestionService.php index da9af17..c7de3e4 100644 --- a/gestion/lib/Service/GestionService.php +++ b/gestion/lib/Service/GestionService.php @@ -28,6 +28,7 @@ namespace OCA\Gestion\Service; use OCA\Gestion\Db\Bdd; use Psr\Log\LoggerInterface; +use OCA\Gestion\Helpers\VCalendarHelpers; class GestionService { /** @var Bdd */ @@ -43,20 +44,10 @@ class GestionService { $this->gestionBdd = $gestionBdd; } - private function GetValueFromKeyInVCalendarString(string $key, string $vCalendarString): string - { - $value = ""; - preg_match("/$key:(.*)\r\n/", $vCalendarString, $matches); - if (isset($matches[1])) { - $value = trim($matches[1]); - } - return $value; - } - private function GetCalendarSummaryFromVCalendarString(string $vCalendarString): string { $summaryValue = "Nom du défunt"; - $value = $this->GetValueFromKeyInVCalendarString("SUMMARY", $vCalendarString); + $value = VCalendarHelpers::GetValueFromKeyInVCalendarString("SUMMARY", $vCalendarString); if($value !== ""){ $summaryValue = trim($value); } @@ -86,23 +77,40 @@ class GestionService { return $names; } - private function IsDevisAlreadyCreated($clientId,$locationId,$thanatoId,$defuntName){ + private function IsDevisAlreadyCreated($clientId,$locationId,$thanatoId,$defuntName,$calendarUuid="not-related"){ $defuntId = $this->gestionBdd->getLastDefuntIdByName($defuntName); - $devisId = $this->gestionBdd->getLastDevisIdFromVCalendarProperty($thanatoId,$clientId,$locationId,$defuntId); + $devisId = $this->gestionBdd->getLastDevisIdFromVCalendarProperty($thanatoId,$clientId,$locationId,$defuntId,$calendarUuid); return $devisId != null; } + private function GetCalendarUuidFromVCalendarString(string $vCalendarString): string + { + $calendarUuid = VCalendarHelpers::GetValueFromKeyInVCalendarString("UID", $vCalendarString); + if($calendarUuid == ""){ + $calendarUuid = $this->gestionBdd::DEFAULT_CALENDAR_UUID_FOR_DEVIS; + } + return $calendarUuid; + } + + private function GetCalendarDateFromVCalendarString(string $vCalendarString){ + $calendarStartDate = VCalendarHelpers::GetDateStartOrDateEndFromVCalendarString('DTSTART',$vCalendarString); + $calendarStartDate = $calendarStartDate->format('Y-m-d'); + return $calendarStartDate; + } + public function HandleCreatedCalendarObject(string $vCalendarString){ $calendarSummary = $this->GetCalendarSummaryFromVCalendarString($vCalendarString); $clientId = $this->GetClientIdFromVCalendarString($vCalendarString); $locationId = $this->GetLocationIdFromVCalendarString($vCalendarString); $thanatoId = $this->GetThanatoIdFromVCalendarString($vCalendarString); - $devisAlreadyCreated = $this->IsDevisAlreadyCreated($clientId,$locationId,$thanatoId,$calendarSummary); + $calendarUuid = $this->GetCalendarUuidFromVCalendarString($vCalendarString); + $devisAlreadyCreated = $this->IsDevisAlreadyCreated($clientId,$locationId,$thanatoId,$calendarSummary,$calendarUuid); if($devisAlreadyCreated){ return; } $defuntId = $this->gestionBdd->insertDefuntByNameAndReturnId($calendarSummary); - $devisId = $this->gestionBdd->insertDevisFromVCalendarAndReturnId($thanatoId,$clientId,$locationId,$defuntId); + $calendarStartDate = $this->GetCalendarDateFromVCalendarString($vCalendarString); + $devisId = $this->gestionBdd->insertDevisFromVCalendarAndReturnId($thanatoId,$clientId,$locationId,$defuntId,$calendarUuid,$calendarStartDate); $articlesValue = $this->GetArticlesNameFromVCalendarString($vCalendarString); if(!empty($articlesValue)){ $articlesId = $this->gestionBdd->getArticlesIdFromArticlesNameArray($articlesValue); @@ -112,23 +120,21 @@ class GestionService { } private function GetClientIdFromVCalendarString(string $vCalendarString){ - $clientValue = $this->GetValueFromKeyInVCalendarString("CLIENT", $vCalendarString); + $clientValue = VCalendarHelpers::GetValueFromKeyInVCalendarString("CLIENT", $vCalendarString); $clientId = $this->gestionBdd->getLastClientIdByName(name: $clientValue) ?? 0; return $clientId; } private function GetLocationIdFromVCalendarString(string $vCalendarString){ - $locationValue = $this->GetValueFromKeyInVCalendarString("LOCATION", $vCalendarString); + $locationValue = VCalendarHelpers::GetValueFromKeyInVCalendarString("LOCATION", $vCalendarString); $locationId = $this->gestionBdd->getLastLocationIdByName($locationValue)?? 0; return $locationId; } private function GetArticlesNameFromVCalendarString(string $vCalendarString): array { - $devisArticleValue = $this->GetValueFromKeyInVCalendarString("DESCRIPTION", $vCalendarString); - $this->logger->debug('LIST OF ARTICLES',["" => $devisArticleValue]); + $devisArticleValue = VCalendarHelpers::GetValueFromKeyInVCalendarString("DESCRIPTION", $vCalendarString); $articles = explode('\;', $devisArticleValue); $mapped = array_map('trim', $articles); - $this->logger->debug('LIST OF ARTICLES',$mapped); return $mapped; } }