Merge branch 'fixes/fix-relate-devis-and-calendar' into staging

This commit is contained in:
Tiavina 2024-12-27 11:23:24 +03:00
commit cf9bd434b6
5 changed files with 182 additions and 27 deletions

View File

@ -1,13 +1,19 @@
<?php
namespace OCA\Gestion\Db;
use OCA\Gestion\Helpers\DateHelpers;
use OCP\IDBConnection;
use OCP\IL10N;
use \Datetime;
use Psr\Log\LoggerInterface;
use OCA\Gestion\Helpers\VCalendarHelpers;
class Bdd {
private String $charset = 'utf8mb4';
public const DEFAULT_CALENDAR_UUID_FOR_DEVIS = "not-related";
public const CALENDAR_TABLE_PREFIX = "*PREFIX*";
private IDbConnection $pdo;
private array $whiteColumn;
private array $whiteTable;
@ -814,8 +820,12 @@ class Bdd {
return true;
}
public function insertDevisFromVCalendarAndReturnId($thanatoId,$clientId,$locationId,$defuntId){
public function insertDevisFromVCalendarAndReturnId($thanatoId,$clientId,$locationId,$defuntId,$calendarUuid = self::DEFAULT_CALENDAR_UUID_FOR_DEVIS,$date = null){
if($date == null){
$date = new Datetime();
$date = $date->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;
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace OCA\Gestion\Helpers;
use DateTime;
use DateTimeZone;
use Exception;
class DateHelpers
{
public static function isWeekend(string $dateString): bool
{
try {
$date = new DateTime($dateString);
$dayOfWeek = $date->format('N');
return in_array($dayOfWeek, [6, 7]);
} catch (Exception $e) {
// Handle invalid date strings
return false;
}
}
}

View File

@ -0,0 +1,64 @@
<?php
namespace OCA\Gestion\Helpers;
use DateTime;
use DateTimeZone;
use Exception;
class VCalendarHelpers
{
public static 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;
}
public static function GetDateStartOrDateEndFromVCalendarString(string $key, string $vCalendarString)
{
preg_match("/$key;TZID=([^:]+):(\d+T\d+)/", $vCalendarString, $matches);
if(!$matches){
return null;
}
try{
$dateTz = $matches[1];
$datetimeString = $matches[2];
$datetimeValue = new DateTime($datetimeString, new DateTimeZone($dateTz));
return $datetimeValue;
}
catch(Exception $e){
return null;
}
}
public static function GetStartAndEndTimeFromVCalendarString(string $vCalendarString){
$startTimeValue = "";
$endTimeValue = "";
if($vCalendarString != ""){
$dateStart = self::GetDateStartOrDateEndFromVCalendarString("DTSTART", $vCalendarString);
if($dateStart != null){
$startTimeValue = $dateStart->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;
}
}

View File

@ -47,6 +47,9 @@ class ExportThanatoStatisticService {
$fileHeader =
'Thanatopracteur'.';'.
'Date'.';'.
'Heure Debut'.';'.
'Heure Fin'.';'.
utf8_decode(html_entity_decode('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"] ?? "")).';'.

View File

@ -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;
}
}