From 8f0eee2446150353ee2cc7093292d199536f378e Mon Sep 17 00:00:00 2001 From: Tiavina Date: Fri, 20 Dec 2024 15:55:10 +0300 Subject: [PATCH] finish automatically create devis , WIP automatically create trajet , update calendar --- calendar/lib/AppInfo/Application.php | 1 - gestion/lib/AppInfo/Application.php | 4 +- gestion/lib/Db/Bdd.php | 122 ++++++++++++++++++ .../CalendarObjectCreatedListener.php | 16 +-- gestion/lib/Service/GestionService.php | 108 ++++++++++++++++ 5 files changed, 240 insertions(+), 11 deletions(-) rename {calendar => gestion}/lib/Listener/CalendarObjectCreatedListener.php (73%) create mode 100644 gestion/lib/Service/GestionService.php diff --git a/calendar/lib/AppInfo/Application.php b/calendar/lib/AppInfo/Application.php index 3f35365..9f20d30 100644 --- a/calendar/lib/AppInfo/Application.php +++ b/calendar/lib/AppInfo/Application.php @@ -75,7 +75,6 @@ class Application extends App implements IBootstrap { $context->registerEventListener(BeforeAppointmentBookedEvent::class, AppointmentBookedListener::class); $context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class); $context->registerEventListener(RenderReferenceEvent::class, CalendarReferenceListener::class); - $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarObjectCreatedListener::class); $context->registerNotifierService(Notifier::class); } diff --git a/gestion/lib/AppInfo/Application.php b/gestion/lib/AppInfo/Application.php index 7913968..6ed5a6d 100644 --- a/gestion/lib/AppInfo/Application.php +++ b/gestion/lib/AppInfo/Application.php @@ -4,6 +4,8 @@ namespace OCA\Gestion\AppInfo; +use OCA\DAV\Events\CalendarObjectCreatedEvent; +use OCA\Gestion\Listener\CalendarObjectCreatedListener; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; @@ -18,7 +20,7 @@ class Application extends App implements IBootstrap { } public function register(IRegistrationContext $context): void { - + $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarObjectCreatedListener::class); } public function boot(IBootContext $context): void { diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index 8770c79..7385e5f 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -672,6 +672,51 @@ class Bdd { return true; } + public function insertDefuntByNameAndReturnId($name) { + $this->insertDefuntByName($name); + $sql = "SELECT max(id) as LAST_INSERT_ID + FROM ".$this->tableprefix."defunt + WHERE nom = ?"; + $res = $this->execSQLNoJsonReturn($sql,array($name)); + if($res){ + return $res[0]['LAST_INSERT_ID']; + } + return null; + } + + public function getLastClientIdByName($name) { + $sql = "SELECT max(id) as LAST_INSERT_ID + FROM ".$this->tableprefix."client + WHERE nom = ?"; + $res = $this->execSQLNoJsonReturn($sql,array($name)); + if($res){ + return $res[0]['LAST_INSERT_ID']; + } + return null; + } + + public function getLastLocationIdByName($name) { + $sql = "SELECT max(id) as LAST_INSERT_ID + FROM ".$this->tableprefix."lieu + WHERE nom = ?"; + $res = $this->execSQLNoJsonReturn($sql,array($name)); + if($res){ + return $res[0]['LAST_INSERT_ID']; + } + return null; + } + + public function getLastThanatoIdByName($name) { + $sql = "SELECT max(id) as LAST_INSERT_ID + FROM ".$this->tableprefix."thanato + WHERE nom = ?"; + $res = $this->execSQLNoJsonReturn($sql,array($name)); + if($res){ + return $res[0]['LAST_INSERT_ID']; + } + return null; + } + /** * Insert lieu */ @@ -728,6 +773,83 @@ class Bdd { return true; } + public function insertDevisFromVCalendarAndReturnId($thanatoId,$clientId,$locationId,$defuntId){ + $idNextcloud = "admin"; + $last=0; + $last = $this->lastinsertid("devis", "admin") + 1; + + $sql = "INSERT INTO `".$this->tableprefix."devis` ( `date`, + `id_nextcloud`, + `num`, + `id_defunt`, + `id_client`, + `id_thanato`, + `version`, + `id_lieu`, + `mentions`, + `comment`, + `user_id` + ) + VALUES (NOW(),?,?,?,?,?,?,?,?,?,?);"; + $this->execSQLNoData($sql, array( + $idNextcloud, + "Nom du defunt", + $defuntId, + $clientId, + $thanatoId, + $this->l->t('New'), + $locationId, + $this->l->t('Mention'), + $this->l->t('Comment'), + $last)); + + return $this->getLastDevisIdFromVCalendarProperty($thanatoId,$clientId,$locationId,$defuntId); + } + + private function getLastDevisIdFromVCalendarProperty($thanatoId,$clientId,$locationId,$defuntId){ + $sql = "SELECT max(id) as LAST_INSERT_ID + FROM ".$this->tableprefix."devis + WHERE id_defunt = ? AND id_lieu = ? AND id_client = ? AND id_thanato = ?"; + $res = $this->execSQLNoJsonReturn($sql,array( + $defuntId, + $locationId, + $clientId, + $thanatoId + )); + if($res){ + return $res[0]['LAST_INSERT_ID']; + } + return null; + } + + public function getArticlesIdFromArticlesNameArray(array $articles): array { + $articleIds = []; + foreach ($articles as $article) { + $sql = "SELECT id FROM ".$this->tableprefix."article WHERE description = ?"; + $res = $this->execSQLNoJsonReturn($sql, array($article)); + if ($res) { + $articleIds[] = $res[0]['id']; + } + } + return $articleIds; + } + + public function insertDevisArticleFromDevisIdAndArticlesIdArray($devisId, $articleIds) { + if (!empty($articleIds)) { + $idNextcloud = "admin"; + foreach ($articleIds as $articleId) { + $this->insertDevisArticle(devisId: $devisId, articleId: $articleId,idNextcloud: $idNextcloud); + } + } + return true; + } + + private function insertDevisArticle($devisId,$articleId,$idNextcloud){ + $sql = "INSERT INTO ".$this->tableprefix."article_devis (devis_id, article_id, quantite, discount, comment,id_nextcloud) VALUES (?, ?, 1, 0, '-',?)"; + $this->execSQLNoData($sql, array($devisId, $articleId,$idNextcloud)); + return true; + } + /** * Insert invoice */ diff --git a/calendar/lib/Listener/CalendarObjectCreatedListener.php b/gestion/lib/Listener/CalendarObjectCreatedListener.php similarity index 73% rename from calendar/lib/Listener/CalendarObjectCreatedListener.php rename to gestion/lib/Listener/CalendarObjectCreatedListener.php index 0365b1e..973a67d 100644 --- a/calendar/lib/Listener/CalendarObjectCreatedListener.php +++ b/gestion/lib/Listener/CalendarObjectCreatedListener.php @@ -23,11 +23,10 @@ declare(strict_types=1); * along with this program. If not, see . */ -namespace OCA\Calendar\Listener; +namespace OCA\Gestion\Listener; -use OCA\Calendar\Service\Appointments\BookingService; -use OCA\Calendar\Service\Calendar\CalendarService; use OCA\DAV\Events\CalendarObjectCreatedEvent; +use OCA\Gestion\Service\GestionService; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use Psr\Log\LoggerInterface; @@ -37,13 +36,13 @@ class CalendarObjectCreatedListener implements IEventListener { /** @var LoggerInterface */ private $logger; - /** @var \OCA\Calendar\Service\Calendar\CalendarService* */ - private $calendarService; + /** @var GestionService */ + private $gestionService; public function __construct( - LoggerInterface $logger,CalendarService $calendarService) { + LoggerInterface $logger,GestionService $gestionService) { $this->logger = $logger; - $this->calendarService = $calendarService; + $this->gestionService = $gestionService; } public function handle(Event $event): void { @@ -52,8 +51,7 @@ class CalendarObjectCreatedListener implements IEventListener { } $calendarData = $event->getObjectData(); $vCalendarString = $calendarData["calendardata"]; - $calendarSummary = $this->calendarService->GetCalendarSummaryFromVCalendarString($vCalendarString); - $this->calendarService->createDefuntFromCalendarSummary($calendarSummary); + $this->gestionService->HandleCreatedCalendarObject($vCalendarString); } } diff --git a/gestion/lib/Service/GestionService.php b/gestion/lib/Service/GestionService.php new file mode 100644 index 0000000..59f9045 --- /dev/null +++ b/gestion/lib/Service/GestionService.php @@ -0,0 +1,108 @@ + + * + * @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 OCA\Gestion\Db\Bdd; +use Psr\Log\LoggerInterface; + +class GestionService { + /** @var Bdd */ + private $gestionBdd; + + /** @var LoggerInterface */ + private $logger; + + public function __construct( + Bdd $gestionBdd, + LoggerInterface $logger) { + $this->logger = $logger; + $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); + if($value !== ""){ + $summaryValue = trim($value); + } + return $summaryValue; + } + + + public function HandleCreatedCalendarObject(string $vCalendarString){ + $calendarSummary = $this->GetCalendarSummaryFromVCalendarString($vCalendarString); + $defuntId = $this->gestionBdd->insertDefuntByNameAndReturnId($calendarSummary); + $clientId = $this->GetClientIdFromVCalendarString($vCalendarString); + $locationId = $this->GetLocationIdFromVCalendarString($vCalendarString); + $thanatoId = $this->GetThanatoIdFromVCalendarString($vCalendarString);key: + + $devisId = $this->gestionBdd->insertDevisFromVCalendarAndReturnId($thanatoId,$clientId,$locationId,$defuntId); + $articlesValue = $this->GetArticlesNameFromVCalendarString($vCalendarString); + if(!empty($articlesValue)){ + $articlesId = $this->gestionBdd->getArticlesIdFromArticlesNameArray($articlesValue); + $this->gestionBdd->insertDevisArticleFromDevisIdAndArticlesIdArray($devisId, $articlesId); + } + } + + private function GetClientIdFromVCalendarString(string $vCalendarString){ + $clientValue = $this->GetValueFromKeyInVCalendarString("CLIENT", $vCalendarString); + $clientId = $this->gestionBdd->getLastClientIdByName(name: $clientValue) ?? 0; + return $clientId; + } + + private function GetLocationIdFromVCalendarString(string $vCalendarString){ + $locationValue = $this->GetValueFromKeyInVCalendarString("LOCATION", $vCalendarString); + $locationId = $this->gestionBdd->getLastLocationIdByName($locationValue)?? 0; + return $locationId; + } + + private function GetThanatoIdFromVCalendarString(string $vCalendarString){ + $thanatoValue = $this->GetValueFromKeyInVCalendarString("EMBALMER", $vCalendarString); + $thanatoId = $this->gestionBdd->getLastThanatoIdByName($thanatoValue) ?? 0; + return $thanatoId; + } + + private function GetArticlesNameFromVCalendarString(string $vCalendarString): array { + $devisArticleValue = $this->GetValueFromKeyInVCalendarString("DESCRIPTION", $vCalendarString); + $this->logger->debug('LIST OF ARTICLES',["" => $devisArticleValue]); + $articles = explode('\;', $devisArticleValue); + $mapped = array_map('trim', $articles); + $this->logger->debug('LIST OF ARTICLES',$mapped); + return $mapped; + } +}