327 lines
14 KiB
PHP
327 lines
14 KiB
PHP
<?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\Constants\DevisMentionConstant;
|
|
use OCA\Gestion\Constants\OrderStatusConstant;
|
|
use OCA\Gestion\Constants\ThanatoTypeConstant;
|
|
use OCA\Gestion\Db\Bdd;
|
|
use OCA\Gestion\Db\OrderBdd;
|
|
use Psr\Log\LoggerInterface;
|
|
use OCA\Gestion\Helpers\VCalendarHelpers;
|
|
|
|
class GestionService {
|
|
/** @var Bdd */
|
|
private $gestionBdd;
|
|
|
|
/** @var LoggerInterface */
|
|
private $logger;
|
|
|
|
/** @var \OCA\Gestion\Db\OrderBdd */
|
|
private $orderBdd;
|
|
|
|
public function __construct(
|
|
Bdd $gestionBdd,
|
|
OrderBdd $orderBdd,
|
|
LoggerInterface $logger) {
|
|
$this->orderBdd = $orderBdd;
|
|
$this->logger = $logger;
|
|
$this->gestionBdd = $gestionBdd;
|
|
}
|
|
|
|
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 GetThanatoFromVCalendarString(string $vCalendarString){
|
|
$thanato = null;
|
|
$thanatoNames = $this->GetAttendeesNameFromVCalendarString($vCalendarString);
|
|
if(count($thanatoNames) > 0){
|
|
$thanatoName = $thanatoNames[0];
|
|
$thanatoFromDb = $this->gestionBdd->getThanatoByUserUuid($thanatoName);
|
|
if($thanatoFromDb != null){
|
|
$thanato = $thanatoFromDb;
|
|
}
|
|
}
|
|
else{
|
|
//get from calendar object
|
|
$organizerName = $this->getPrincipalUsernameFromVCalendarString($vCalendarString);
|
|
if($organizerName != null){
|
|
$thanatoFromDb = $this->gestionBdd->getThanatoByUserUuid($organizerName);
|
|
if($thanatoFromDb != null){
|
|
$thanato = $thanatoFromDb;
|
|
}
|
|
}
|
|
}
|
|
return $thanato;
|
|
}
|
|
|
|
private function GetThanatoIdFromVCalendarString(string $vCalendarString)
|
|
{
|
|
$thanatoId = 0;
|
|
$thanatoNames = $this->GetAttendeesNameFromVCalendarString($vCalendarString);
|
|
if(count($thanatoNames) > 0){
|
|
$thanatoName = $thanatoNames[0];
|
|
$thanatoIdFromDb = $this->gestionBdd->getThanatoIdByUserUuid($thanatoName);
|
|
if($thanatoIdFromDb != null){
|
|
$thanatoId = $thanatoIdFromDb;
|
|
}
|
|
}
|
|
else{
|
|
//get from calendar object
|
|
$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 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 IsOrderAlreadyCreated($calendarUuid){
|
|
$order = $this->orderBdd->getOrderByCalendarUuid($calendarUuid);
|
|
return $order != null;
|
|
}
|
|
|
|
private function CreateOrderFromVCalendarString($vCalendarString,$thanatoId){
|
|
$calendarUuid = $this->GetCalendarUuidFromVCalendarString($vCalendarString);
|
|
$orderAlreadyCreated = $this->IsOrderAlreadyCreated($calendarUuid);
|
|
if($orderAlreadyCreated){
|
|
return;
|
|
}
|
|
$nextcloudUser = $this->GetThanatoNameFromVCalendarString($vCalendarString);
|
|
$calendarSummary = $this->GetCalendarSummaryFromVCalendarString($vCalendarString);
|
|
$clientId = $this->GetClientIdFromVCalendarString($vCalendarString);
|
|
$locationId = $this->GetLocationIdFromVCalendarString($vCalendarString);
|
|
$calendarStartDate = $this->GetCalendarDateFromVCalendarString($vCalendarString);
|
|
$defuntId = $this->gestionBdd->insertDefuntByNameAndReturnId($calendarSummary);
|
|
$orderId = $this->orderBdd->insertOrderFromVCalendarPropertyAndReturnId(
|
|
$thanatoId,
|
|
$clientId,
|
|
$locationId,
|
|
$defuntId,
|
|
$calendarUuid,
|
|
$calendarStartDate,
|
|
$nextcloudUser);
|
|
$productsValue = $this->GetArticlesNameFromVCalendarString($vCalendarString);
|
|
if(!empty($productsValue)){
|
|
$productIds = $this->gestionBdd->getArticleIdsByArticleReferences($productsValue);
|
|
$this->orderBdd->insertOrderProductsByProductIds($orderId, $productIds);
|
|
}
|
|
|
|
}
|
|
|
|
public function HandleCreatedCalendarObject(string $vCalendarString){
|
|
$thanato = $this->GetThanatoFromVCalendarString($vCalendarString);
|
|
if($thanato != null){
|
|
$thanatoId = $thanato["id"];
|
|
$thanatoIsSubcontractor = $thanato["fk_thanato_type_key"] === ThanatoTypeConstant::THANATO_TYPE_SUBCONTRACTOR;
|
|
if($thanatoIsSubcontractor){
|
|
$this->CreateOrderFromVCalendarString($vCalendarString,$thanatoId);
|
|
return;
|
|
}
|
|
}
|
|
else{
|
|
$thanatoId = 0;
|
|
}
|
|
$calendarSummary = $this->GetCalendarSummaryFromVCalendarString($vCalendarString);
|
|
$clientId = $this->GetClientIdFromVCalendarString($vCalendarString);
|
|
$locationId = $this->GetLocationIdFromVCalendarString($vCalendarString);
|
|
$calendarUuid = $this->GetCalendarUuidFromVCalendarString($vCalendarString);
|
|
$userName = $this->GetThanatoNameFromVCalendarString($vCalendarString);
|
|
$devisAlreadyCreated = $this->IsDevisAlreadyCreated($clientId,$locationId,$thanatoId,$calendarSummary,$calendarUuid);
|
|
if($devisAlreadyCreated){
|
|
return;
|
|
}
|
|
$defuntId = $this->gestionBdd->insertDefuntByNameAndReturnId($calendarSummary);
|
|
$calendarStartDate = $this->GetCalendarDateFromVCalendarString($vCalendarString);
|
|
$devisId = $this->gestionBdd->insertDevisFromVCalendarAndReturnId($thanatoId,$clientId,$locationId,$defuntId,$calendarUuid,$calendarStartDate,$userName);
|
|
$articlesValue = $this->GetArticlesNameFromVCalendarString($vCalendarString);
|
|
if(!empty($articlesValue)){
|
|
$articleIds = $this->gestionBdd->getArticleIdsByArticleReferences($articlesValue);
|
|
$this->gestionBdd->insertDevisArticleFromDevisIdAndArticlesIdArray($devisId, $articleIds);
|
|
}
|
|
$this->gestionBdd->createDevisTrajetFromVCalendar($devisId,$userName);
|
|
}
|
|
|
|
private function GetThanatoNameFromVCalendarString($vCalendarString){
|
|
$thanatoName = null;
|
|
$thanatoNames = $this->GetAttendeesNameFromVCalendarString($vCalendarString);
|
|
if(count($thanatoNames) > 0){
|
|
$thanatoName = $thanatoNames[0];
|
|
}
|
|
else{
|
|
//get from calendar object
|
|
$thanatoName = $this->getPrincipalUsernameFromVCalendarString($vCalendarString);
|
|
}
|
|
return $thanatoName;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public function HandleCalendarObjectMovedToTrash(string $vCalendarString){
|
|
$thanato = $this->GetThanatoFromVCalendarString($vCalendarString);
|
|
if($thanato == null){
|
|
return;
|
|
}
|
|
$calendarUuid = $this->GetCalendarUuidFromVCalendarString($vCalendarString);
|
|
$thanatoIsSubcontractor = $thanato["fk_thanato_type_key"] === ThanatoTypeConstant::THANATO_TYPE_SUBCONTRACTOR;
|
|
if($thanatoIsSubcontractor){
|
|
$order = $this->orderBdd->getOrderByCalendarUuid($calendarUuid);
|
|
if($order != null){
|
|
$this->orderBdd->updateOrderStatus($order['id'],OrderStatusConstant::CANCELED_KEY);
|
|
}
|
|
}
|
|
else{
|
|
$devis = $this->gestionBdd->getDevisByCalendarUuid($calendarUuid);
|
|
if($devis != null){
|
|
$this->gestionBdd->updateDevisMentionToCanceled($devis['id']);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private function CheckIfDevisIsAlreadyUpdated($devis,$vCalendarString){
|
|
$requestedDefuntName = $this->GetCalendarSummaryFromVCalendarString($vCalendarString);
|
|
$requestedClientId = $this->GetClientIdFromVCalendarString($vCalendarString);
|
|
$requestLocationId = $this->GetLocationIdFromVCalendarString($vCalendarString);
|
|
$requestedArticleReferences = $this->GetArticlesNameFromVCalendarString($vCalendarString);
|
|
$requestedArticleIds = $this->gestionBdd->getArticleIdsByArticleReferences($requestedArticleReferences);
|
|
$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 &&
|
|
$requestedArticleIds == $articleDevisIds;
|
|
}
|
|
|
|
private function UpdateDevisDataByVCalendarString($devis,$vCalendarString){
|
|
$requestedDefuntName = $this->GetCalendarSummaryFromVCalendarString($vCalendarString);
|
|
$defuntId = $this->gestionBdd->createOrUpdateDefuntByNameAndReturnDefuntId($devis['defunt_id'],$devis['defunt_nom'],$requestedDefuntName);
|
|
$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']);
|
|
|
|
$articlesValue = $this->GetArticlesNameFromVCalendarString($vCalendarString);
|
|
if(!empty($articlesValue)){
|
|
$articleIds = $this->gestionBdd->getArticleIdsByArticleReferences($articlesValue);
|
|
$this->gestionBdd->updateDevisArticles($devis['id'],$articleIds);
|
|
}
|
|
}
|
|
|
|
public function HandleUpdatedCalendarObject(string $vCalendarString){
|
|
$calendarUuid = $this->GetCalendarUuidFromVCalendarString($vCalendarString);
|
|
$devis = $this->gestionBdd->getDevisByCalendarUuid($calendarUuid);
|
|
if($devis != null){
|
|
$this->gestionBdd->updateDevisMention($devis['id'],DevisMentionConstant::NEW);
|
|
$isDevisAlreadyUpdated = $this->CheckIfDevisIsAlreadyUpdated($devis,$vCalendarString);
|
|
if($isDevisAlreadyUpdated){
|
|
return true;
|
|
}
|
|
$this->UpdateDevisDataByVCalendarString($devis,$vCalendarString);
|
|
}
|
|
return true;
|
|
}
|
|
}
|