* * @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 Exception; use OCA\Gestion\Constants\BddConstant; use OCA\Gestion\Constants\DevisMentionConstant; use OCA\Gestion\Constants\VCalendarPropertyConstant; use OCA\Gestion\Db\Bdd; use OCP\IUserSession; use Psr\Log\LoggerInterface; use OCA\Gestion\Helpers\VCalendarHelpers; use OCA\Gestion\Service\Devis\Pdf\DevisPdfService; class GestionService { /** @var Bdd */ private $gestionBdd; /** @var LoggerInterface */ private $logger; /** @var TalkService */ private $talkService; private $userConnectedUuid; /** @var DevisPdfService */ private $devisPdfService; public function __construct( Bdd $gestionBdd, LoggerInterface $logger, TalkService $talkService, IUserSession $userSession, DevisPdfService $devisPdfService ) { $this->logger = $logger; $this->gestionBdd = $gestionBdd; $this->talkService = $talkService; $this->devisPdfService = $devisPdfService; try { $this->userConnectedUuid = $userSession->getUser()->getUID(); } catch (Exception) { $this->userConnectedUuid = BddConstant::DEFAULT_ADMIN_APP_ID_NEXTCLOUD; } } private function GetIsCalendarPendingFromVCalendarString(string $vCalendarString): bool { $isCalendarPending = false; $isCalendarPendingValue = VCalendarHelpers::GetValueFromKeyInVCalendarString(VCalendarPropertyConstant::PROPERTY_IS_CALENDAR_PENDING, $vCalendarString); if ($isCalendarPendingValue == "1") { $isCalendarPending = true; } return $isCalendarPending; } private function GetIsLeaveFromVCalendarString(string $vCalendarString): bool { $isLeave = false; $isLeaveValue = VCalendarHelpers::GetValueFromKeyInVCalendarString(VCalendarPropertyConstant::PROPERTY_IS_LEAVE, $vCalendarString); if ($isLeaveValue == "1") { $isLeave = true; } return $isLeave; } private function GetCalendarSummaryFromVCalendarString(string $vCalendarString): string { $summaryValue = "Nom du défunt"; $value = VCalendarHelpers::GetValueFromKeyInVCalendarString("SUMMARY", $vCalendarString); if ($value !== "") { $summaryValue = trim($value); } return $summaryValue; } private function GetThanatoIdFromVCalendarString(string $vCalendarString) { $thanatoId = 0; $organizerName = $this->getPrincipalUsernameFromVCalendarString($vCalendarString); if ($organizerName != null) { $thanatoIdFromDb = $this->gestionBdd->getThanatoIdByUserUuid($organizerName); if ($thanatoIdFromDb != null) { $thanatoId = $thanatoIdFromDb; } } return $thanatoId; } private function getPrincipalUsernameFromVCalendarString(string $vCalendarString) { $calendarUuid = VCalendarHelpers::GetValueFromKeyInVCalendarString("UID", $vCalendarString); $principalUsername = $this->gestionBdd->getCalendarOrganizerNameByCalendarObjectUuid($calendarUuid); return $principalUsername; } private function GetAttendeesNameFromVCalendarString(string $vCalendarString): array { $names = []; preg_match_all('/CN=([^;]+)/', $vCalendarString, $matches); if (isset($matches[1])) { $names = $matches[1]; } return $names; } private function IsDevisAlreadyCreated($clientId, $locationId, $thanatoId, $defuntName, $calendarUuid = "not-related") { $defuntId = $this->gestionBdd->getLastDefuntIdByName($defuntName); $devisId = $this->gestionBdd->getLastDevisIdFromVCalendarProperty($thanatoId, $clientId, $locationId, $defuntId, $calendarUuid); return $devisId != null; } private function GetDevisCommentFromVCalendarString(string $vCalendarString) { $commentValue = VCalendarHelpers::GetValueFromKeyInVCalendarString("COMMENT", $vCalendarString); if ($commentValue == "") { $commentValue = "Commentaire"; } return $commentValue; } 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; } private function GetIsPivateFromVCalendarString(string $vCalendarString): bool { $isPrivateValue = VCalendarHelpers::GetValueFromKeyInVCalendarString(VCalendarPropertyConstant::PROPERTY_IS_PRIVATE, $vCalendarString); return $isPrivateValue === "1" ? true : false; } public function HandleCalendarObjectMovedToTrash(string $vCalendarString) { $calendarUuid = $this->GetCalendarUuidFromVCalendarString($vCalendarString); $devis = $this->gestionBdd->getDevisByCalendarUuid($calendarUuid); if ($devis != null) { $this->gestionBdd->updateDevisMentionToCanceled($devis['id']); } return true; } private function UpdateDevisDataByVCalendarString($devis, $vCalendarString) { $requestedDefuntName = $this->GetCalendarSummaryFromVCalendarString($vCalendarString); $lieuDeces = $this->getLieuDecesFromVCalendarString($vCalendarString); $dateNaissance = $this->getDateNaissanceFromVCalendarString($vCalendarString); $defuntId = $this->gestionBdd->createOrUpdateDefuntByNameAndReturnDefuntId($devis['defunt_id'], $devis['defunt_nom'], $requestedDefuntName, $lieuDeces, $dateNaissance); file_put_contents( '/var/www/html/data/nextcloud_calendar_debug.log', date('Y-m-d H:i:s') . " - defuntId: " . $defuntId . "\n", FILE_APPEND ); $this->gestionBdd->updateDevisDefunt($devis['id'], $defuntId, $devis['defunt_id']); $requestedClientId = $this->GetClientIdFromVCalendarString($vCalendarString); $this->gestionBdd->updateDevisClient($devis['id'], $requestedClientId, $devis['client_id']); $requestLocationId = $this->GetLocationIdFromVCalendarString($vCalendarString); $this->gestionBdd->updateDevisLieu($devis['id'], $requestLocationId, $devis['lieu_id']); $requestedDevisComment = $this->GetDevisCommentFromVCalendarString($vCalendarString); $this->gestionBdd->updateDevisComment($devis['id'], $requestedDevisComment, $devis['comment']); $requestedDevisDate = $this->GetCalendarDateFromVCalendarString($vCalendarString); $this->gestionBdd->updateDevisDate($devis['id'], $requestedDevisDate); $articlesValue = $this->GetArticlesNameFromVCalendarString($vCalendarString); if (!empty($articlesValue)) { $articleIds = $this->gestionBdd->getArticleIdsByArticleReferences($articlesValue); $this->gestionBdd->updateDevisArticles($devis['id'], $articleIds); } } private function CheckIfDevisIsAlreadyUpdated($devis, $vCalendarString) { $requestedDefuntName = $this->GetCalendarSummaryFromVCalendarString($vCalendarString); $requestedLieuDeces = $this->getLieuDecesFromVCalendarString($vCalendarString); $requestedDateNaissance = $this->getDateNaissanceFromVCalendarString($vCalendarString); $currentDefunt = $this->gestionBdd->getDefuntById($devis['defunt_id']); $requestedClientId = $this->GetClientIdFromVCalendarString($vCalendarString); $requestLocationId = $this->GetLocationIdFromVCalendarString($vCalendarString); $requestedDevisComment = $this->GetDevisCommentFromVCalendarString($vCalendarString); $requestedArticleReferences = $this->GetArticlesNameFromVCalendarString($vCalendarString); $requestedArticleIds = $this->gestionBdd->getArticleIdsByArticleReferences($requestedArticleReferences); $requestedDevisDate = $this->GetCalendarDateFromVCalendarString($vCalendarString); $articleDevis = $this->gestionBdd->getProduitDevisByDevisId($devis['id']); $articleDevisIds = []; foreach ($articleDevis as $currentArticleDevis) { $articleDevisIds[] = $currentArticleDevis['produit_id']; } sort($requestedArticleIds); sort($articleDevisIds); return $devis['defunt_nom'] == $requestedDefuntName && $devis['client_id'] == $requestedClientId && $devis['lieu_id'] == $requestLocationId && $devis['comment'] == $requestedDevisComment && $requestedArticleIds == $articleDevisIds && $devis['date'] == $requestedDevisDate && ($currentDefunt['lieu_deces'] ?? null) == $requestedLieuDeces && ($currentDefunt['date_naissance'] ?? null) == $requestedDateNaissance; } public function HandleUpdatedCalendarObject(string $vCalendarString) { try { $isPrivate = $this->GetIsPivateFromVCalendarString($vCalendarString); $absenceType = VCalendarHelpers::GetValueFromKeyInVCalendarString('ABSENCETYPE', $vCalendarString); if ($isPrivate || $absenceType) { //from devis calendar to leave calendar $calendarUuid = $this->GetCalendarUuidFromVCalendarString($vCalendarString); $devis = $this->gestionBdd->getDevisByCalendarUuid($calendarUuid); if ($devis != null) { $this->gestionBdd->updateDevisMentionToCanceled($devis['id']); } return; } $calendarUuid = $this->GetCalendarUuidFromVCalendarString($vCalendarString); $devis = $this->gestionBdd->getDevisByCalendarUuid($calendarUuid); if ($devis != null) { $isDevisAlreadyUpdated = $this->CheckIfDevisIsAlreadyUpdated($devis, $vCalendarString); if ($isDevisAlreadyUpdated) { return true; } $devisIsAlreadyFactured = $devis['mentions'] == DevisMentionConstant::FACTURED || $devis['mentions'] == DevisMentionConstant::FACTURED_FORMATTED; $this->UpdateDevisDataByVCalendarString($devis, $vCalendarString); if ($devisIsAlreadyFactured == false) { $userName = $this->GetThanatoNameFromVCalendarString($vCalendarString); $devisTalkMessage = $this->gestionBdd->getDevisTalkRoomMessage($devis['id'], $userName); $this->talkService->sendDevisTalkNotifications($devisTalkMessage, $userName, $this->userConnectedUuid); } } else { //update from calendar leave or calendar pending to calendar devis $this->HandleCreatedCalendarObject($vCalendarString); } } catch (\OC\OCS\Exception $e) { $this->logger->debug("Error while handling created calendar object: " . $e->getMessage()); } catch (\Throwable $e) { $this->logger->debug("Error while handling created calendar object: " . $e->getMessage()); } return true; } public function HandleCalendarObjectMoved(string $vCalendarString, $targetCalendarId) { try { $calendarUuid = $this->GetCalendarUuidFromVCalendarString($vCalendarString); $isCalendarForLeave = $this->GetIsLeaveFromVCalendarString($vCalendarString); $isCalendarDevis = $isCalendarForLeave == false; if ($isCalendarDevis) { $devis = $this->gestionBdd->getDevisByCalendarUuid($calendarUuid); if ($devis != null) { $userPrincipalName = $this->gestionBdd->getCalendarPrincipalNameByCalendarId($targetCalendarId); if ($userPrincipalName != null) { $thanato = $this->gestionBdd->getThanatoByUserUuid($userPrincipalName); if ($thanato != null) { $thanatoHasBeenChanged = $thanato['id'] != $devis["id_thanato"]; if ($thanatoHasBeenChanged) { $oldThanato = $this->gestionBdd->getThanatoByThanatoId($devis['id_thanato']); $oldThanatoUserUuid = $oldThanato['fk_user_uuid']; //delete ligne trajet from old devis $this->gestionBdd->deleteLigneTrajetByDevisId($devis['id']); //delete from home and to home trajet if there is no devis at all for this date $month = explode('-', $devis['date'])[1]; $year = explode('-', $devis['date'])[0]; $oldThanatoTrajet = $this->gestionBdd->getTrajetByThanatoAndMonthYear( $devis['id_thanato'], $month, $year ); if($oldThanatoTrajet != null) { $thereIsThanatoDevisRattachedToLigneTrajetForADate = $this->gestionBdd->thereIsThanatoDevisRattachedToLigneTrajetForADate($devis['date'], $oldThanatoTrajet['id']); if(!$thereIsThanatoDevisRattachedToLigneTrajetForADate) { $this->gestionBdd->deleteThanatoLigneTrajetForADateByIdTrajet($devis['date'], $oldThanatoTrajet['id']); } } $this->gestionBdd->updateDevisThanato($devis['id'], $thanato['id']); $this->gestionBdd->reArrangeLigneTrajetyByDevisId($devis['id'], $userPrincipalName); } } } } } } catch (\OC\OCS\Exception $e) { $this->logger->debug("Error while handling calendar object moved event: " . $e->getMessage()); } catch (\Throwable $e) { $this->logger->debug("Error while handling calendar object moved event: " . $e->getMessage()); } } public function HandleCreatedCalendarObject(string $vCalendarString) { try { $isPrivate = $this->GetIsPivateFromVCalendarString($vCalendarString); $absenceType = VCalendarHelpers::GetValueFromKeyInVCalendarString('ABSENCETYPE', $vCalendarString); if($isPrivate || $absenceType) { //Nothing to do manage fo a private calendar return; } $isCalendarPending = $this->GetIsCalendarPendingFromVCalendarString($vCalendarString); if ($isCalendarPending) { return; } $calendarSummary = $this->GetCalendarSummaryFromVCalendarString($vCalendarString); $clientId = $this->GetClientIdFromVCalendarString($vCalendarString); $locationId = $this->GetLocationIdFromVCalendarString($vCalendarString); $thanatoId = $this->GetThanatoIdFromVCalendarString($vCalendarString); $calendarUuid = $this->GetCalendarUuidFromVCalendarString($vCalendarString); $userName = $this->GetThanatoNameFromVCalendarString($vCalendarString); $lieuDeces = $this->getLieuDecesFromVCalendarString($vCalendarString); $dateNaissance = $this->getDateNaissanceFromVCalendarString($vCalendarString); $devisAlreadyCreated = $this->IsDevisAlreadyCreated($clientId, $locationId, $thanatoId, $calendarSummary, $calendarUuid); if ($devisAlreadyCreated) { return; } $defuntId = $this->gestionBdd->insertDefuntByNameAndReturnId($calendarSummary, $lieuDeces, $dateNaissance); $calendarStartDate = $this->GetCalendarDateFromVCalendarString($vCalendarString); $devisComment = $this->GetDevisCommentFromVCalendarString($vCalendarString); $devisId = $this->gestionBdd->insertDevisFromVCalendarAndReturnId($thanatoId, $clientId, $locationId, $defuntId, $calendarUuid, $calendarStartDate, $devisComment); $articlesValue = $this->GetArticlesNameFromVCalendarString($vCalendarString); if (!empty($articlesValue)) { $articleIds = $this->gestionBdd->getArticleIdsByArticleReferences($articlesValue); $this->gestionBdd->insertDevisArticleFromDevisIdAndArticlesIdArray($devisId, $articleIds); } $devisTalkMessage = $this->gestionBdd->getDevisTalkRoomMessage($devisId, $userName); $this->talkService->sendDevisTalkNotifications($devisTalkMessage, $userName, $this->userConnectedUuid); $this->gestionBdd->createDevisTrajetFromVCalendar($devisId, $userName); $this->devisPdfService->generateDevisPdfByDevisId($devisId, $this->userConnectedUuid); } catch (\OC\OCS\Exception $e) { $this->logger->debug("Error while handling created calendar object: " . $e->getMessage()); } catch (\Throwable $e) { $this->logger->debug("Error while handling created calendar object: " . $e->getMessage()); } } private function GetThanatoNameFromVCalendarString($vCalendarString) { $thanatoName = $this->getPrincipalUsernameFromVCalendarString($vCalendarString); return $thanatoName ?? BddConstant::DEFAULT_ADMIN_APP_ID_NEXTCLOUD; } private function GetClientIdFromVCalendarString(string $vCalendarString) { $this->logger->debug($vCalendarString); $clientValue = VCalendarHelpers::GetValueFromKeyInVCalendarString("CLIENT", $vCalendarString); if ($clientValue == null || $clientValue == "") { $clientValue = 0; } return (int) $clientValue; } private function GetLocationIdFromVCalendarString(string $vCalendarString) { $locationValue = VCalendarHelpers::GetValueFromKeyInVCalendarString("LOCATION", $vCalendarString); if ($locationValue == null || $locationValue == "") { $locationValue = 0; } return (int) $locationValue; } private function GetArticlesNameFromVCalendarString(string $vCalendarString): array { $devisArticleValue = VCalendarHelpers::GetValueFromKeyInVCalendarString("DESCRIPTION", $vCalendarString); $articles = explode('\;', $devisArticleValue); $mapped = array_map('trim', $articles); return $mapped; } private function getLieuDecesFromVCalendarString(string $vCalendarString): ?string { $lieuDecesValue = VCalendarHelpers::GetValueFromKeyInVCalendarString("LIEUDECES", $vCalendarString); return !empty($lieuDecesValue) ? trim($lieuDecesValue) : null; } private function getDateNaissanceFromVCalendarString(string $vCalendarString): ?string { $dateNaissanceValue = VCalendarHelpers::GetValueFromKeyInVCalendarString("DATENAISSANCE", $vCalendarString); return !empty($dateNaissanceValue) ? trim($dateNaissanceValue) : null; } }