finish automatically create devis , WIP automatically create trajet , update calendar

This commit is contained in:
Tiavina 2024-12-20 15:55:10 +03:00
parent 4ce4a2ab05
commit 8f0eee2446
5 changed files with 240 additions and 11 deletions

View File

@ -75,7 +75,6 @@ class Application extends App implements IBootstrap {
$context->registerEventListener(BeforeAppointmentBookedEvent::class, AppointmentBookedListener::class); $context->registerEventListener(BeforeAppointmentBookedEvent::class, AppointmentBookedListener::class);
$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class); $context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
$context->registerEventListener(RenderReferenceEvent::class, CalendarReferenceListener::class); $context->registerEventListener(RenderReferenceEvent::class, CalendarReferenceListener::class);
$context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarObjectCreatedListener::class);
$context->registerNotifierService(Notifier::class); $context->registerNotifierService(Notifier::class);
} }

View File

@ -4,6 +4,8 @@
namespace OCA\Gestion\AppInfo; namespace OCA\Gestion\AppInfo;
use OCA\DAV\Events\CalendarObjectCreatedEvent;
use OCA\Gestion\Listener\CalendarObjectCreatedListener;
use OCP\AppFramework\App; use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\AppFramework\Bootstrap\IRegistrationContext;
@ -18,7 +20,7 @@ class Application extends App implements IBootstrap {
} }
public function register(IRegistrationContext $context): void { public function register(IRegistrationContext $context): void {
$context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarObjectCreatedListener::class);
} }
public function boot(IBootContext $context): void { public function boot(IBootContext $context): void {

View File

@ -672,6 +672,51 @@ class Bdd {
return true; 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 * Insert lieu
*/ */
@ -728,6 +773,83 @@ class Bdd {
return true; 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 * Insert invoice
*/ */

View File

@ -23,11 +23,10 @@ declare(strict_types=1);
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
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\DAV\Events\CalendarObjectCreatedEvent;
use OCA\Gestion\Service\GestionService;
use OCP\EventDispatcher\Event; use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener; use OCP\EventDispatcher\IEventListener;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -37,13 +36,13 @@ class CalendarObjectCreatedListener implements IEventListener {
/** @var LoggerInterface */ /** @var LoggerInterface */
private $logger; private $logger;
/** @var \OCA\Calendar\Service\Calendar\CalendarService* */ /** @var GestionService */
private $calendarService; private $gestionService;
public function __construct( public function __construct(
LoggerInterface $logger,CalendarService $calendarService) { LoggerInterface $logger,GestionService $gestionService) {
$this->logger = $logger; $this->logger = $logger;
$this->calendarService = $calendarService; $this->gestionService = $gestionService;
} }
public function handle(Event $event): void { public function handle(Event $event): void {
@ -52,8 +51,7 @@ class CalendarObjectCreatedListener implements IEventListener {
} }
$calendarData = $event->getObjectData(); $calendarData = $event->getObjectData();
$vCalendarString = $calendarData["calendardata"]; $vCalendarString = $calendarData["calendardata"];
$calendarSummary = $this->calendarService->GetCalendarSummaryFromVCalendarString($vCalendarString); $this->gestionService->HandleCreatedCalendarObject($vCalendarString);
$this->calendarService->createDefuntFromCalendarSummary($calendarSummary);
} }
} }

View File

@ -0,0 +1,108 @@
<?php
declare(strict_types=1);
/**
* Calendar App
*
* @copyright 2021 Anna Larch <anna.larch@gmx.net>
*
* @author Anna Larch <anna.larch@gmx.net>
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* 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 <http://www.gnu.org/licenses/>.
*
*/
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;
}
}