From d499696964989cc0fefcb3abd48f02b86073bbe4 Mon Sep 17 00:00:00 2001 From: Tiavina Date: Wed, 8 Jan 2025 10:40:41 +0300 Subject: [PATCH] finish api to set defunt cover , WIP frontend part --- gestion/appinfo/routes.php | 3 + gestion/lib/Constants/BddConstant.php | 10 ++ gestion/lib/Constants/ProductConstant.php | 13 +++ gestion/lib/Controller/PageController.php | 20 +++- gestion/lib/Db/Bdd.php | 131 +++++++++++++++++++++- 5 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 gestion/lib/Constants/BddConstant.php create mode 100644 gestion/lib/Constants/ProductConstant.php diff --git a/gestion/appinfo/routes.php b/gestion/appinfo/routes.php index 226fd04..4dc18be 100644 --- a/gestion/appinfo/routes.php +++ b/gestion/appinfo/routes.php @@ -136,5 +136,8 @@ return [ //certificate ['name' => 'page#exportCareCertificate', 'url' => '/defunt/exportCareCertificate', 'verb' => 'POST'], + + //defuntCover + ['name' => 'page#setDefuntCover', 'url' => '/defunt/setDefuntCover', 'verb' => 'POST'], ] ]; diff --git a/gestion/lib/Constants/BddConstant.php b/gestion/lib/Constants/BddConstant.php new file mode 100644 index 0000000..46b39fd --- /dev/null +++ b/gestion/lib/Constants/BddConstant.php @@ -0,0 +1,10 @@ +json_decode($defunt), 'path' => $this->idNextcloud, 'url' => $this->getNavigationLink(), - 'logo' => $this->getLogo() + 'logo' => $this->getLogo(), + 'coverProducts' =>$this->myDb->getCoverProducts() )); } @@ -2906,7 +2907,7 @@ class PageController extends Controller { * @param int $defuntId */ - public function exportCareCertificate($defuntId){ + public function exportCareCertificate($defuntId){ try{ $careCertificateFilename = $this->certificateService->generateCareCertificate($defuntId,$this->idNextcloud); return $careCertificateFilename; @@ -2914,4 +2915,19 @@ class PageController extends Controller { catch(\OCP\Files\NotFoundException $e) { } } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @param int $defuntId + */ + + public function setDefuntCover($defuntId,$productId){ + try{ + $response = $this->myDb->setDefuntCover($defuntId,$productId); + return $response; + } + catch(\OCP\Files\NotFoundException $e) { } + + } } diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index cf23a64..127f870 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -5,6 +5,7 @@ use OCA\Gestion\Helpers\DateHelpers; use OCP\IDBConnection; use OCP\IL10N; use \Datetime; +use OCA\Gestion\Constants\ProductConstant; use OCA\Gestion\Helpers\FileExportHelpers; use Psr\Log\LoggerInterface; use OCA\Gestion\Helpers\VCalendarHelpers; @@ -14,7 +15,6 @@ class Bdd { public const DEFAULT_CALENDAR_UUID_FOR_DEVIS = "not-related"; public const CALENDAR_TABLE_PREFIX = "*PREFIX*"; - public const DEFAULT_CLIENT_GROUP_NAME = "Nom du groupe"; private IDbConnection $pdo; private array $whiteColumn; @@ -2707,5 +2707,134 @@ class Bdd { return null; } + private function getDefuntById($defuntId){ + $sql = "SELECT + defunt.id as id, + defunt.nom as defunt_nom, + devis.id as devis_id + FROM ".$this->tableprefix."defunt as defunt + LEFT JOIN ".$this->tableprefix."devis as devis on defunt.id = devis.id_defunt + WHERE defunt.id = ? + LIMIT 1;"; + + $defunt = $this->execSQLNoJsonReturn($sql,[$defuntId]); + if(!empty($defunt)){ + return $defunt[0]; + } + return null; + } + + private function getProductAsDefuntCoverProduct($productCoverId){ + $sqlConditionsPlaceholder = implode(',', array_fill(0, count(ProductConstant::PRODUCT_COVER_REFERENCE_LIST), '?')); + $sql = "SELECT + produit.id, + produit.reference + FROM ".$this->tableprefix."produit as produit + WHERE produit.id = ? AND + produit.reference IN ($sqlConditionsPlaceholder) + LIMIT 1;"; + + $product = $this->execSQLNoJsonReturn( + $sql, + array_merge( + [$productCoverId], + ProductConstant::PRODUCT_COVER_REFERENCE_LIST + ) + ); + if(!empty($product)){ + return $product[0]; + } + return $product; + } + + private function isProductAlreadyExistInDevis($productId,$devisId){ + $sql = "SELECT + produit_devis.id + FROM ".$this->tableprefix."produit_devis as produit_devis + WHERE produit_devis.produit_id = ? AND + produit_devis.devis_id = ? + LIMIT 1;"; + + $product = $this->execSQLNoJsonReturn($sql,[$productId,$devisId]); + if(!empty($product)){ + return true; + } + return false; + } + + private function removeProductFromDevis($productId,$devisId){ + $sql = "DELETE + FROM ". + $this->tableprefix."produit_devis + WHERE devis_id = ? AND produit_id = ? + "; + $this->execSQLNoData($sql,[$devisId,$productId]); + } + + private function getProductByReference($productReference){ + $sql = "SELECT + produit.id, + produit.reference + FROM ".$this->tableprefix."produit as produit + WHERE produit.reference = ? + LIMIT 1;"; + + $product = $this->execSQLNoJsonReturn($sql,[$productReference]); + if(!empty($product)){ + return $product[0]; + } + return null; + } + + private function removeProductFromDevisByProductReference($productReference,$devisId){ + $product = $this->getProductByReference($productReference); + if($product != null){ + $this->removeProductFromDevis($product["id"],$devisId); + } + } + + private function createProductDevisIfNotExist($productId,$devisId){ + $isProductDevisAlreadyExist = $this->isProductAlreadyExistInDevis($productId,$devisId); + if(!$isProductDevisAlreadyExist){ + $idNextcloud = "admin"; + $this->insertDevisArticle(devisId: $devisId, articleId: $productId,idNextcloud: $idNextcloud); + } + } + public function setDefuntCover($defuntId, $productCoverId){ + $defunt = $this->getDefuntById($defuntId); + if($defunt == null){ + return null; + } + if($defunt["devis_id"] == null){ + return null; + } + $product = $this->getProductAsDefuntCoverProduct($productCoverId); + if($product == null){ + return null; + } + + $productCoverReferencesList = ProductConstant::PRODUCT_COVER_REFERENCE_LIST; + foreach($productCoverReferencesList as $currentProductCoverReference){ + if($currentProductCoverReference === $product["reference"]){ + $this->createProductDevisIfNotExist($product["id"],$defunt["devis_id"]); + } + else{ + $this->removeProductFromDevisByProductReference($currentProductCoverReference,$defunt["devis_id"]); + } + } + return true; + } + + public function getCoverProducts(){ + $sqlConditionsPlaceholder = implode(',', array_fill(0, count(ProductConstant::PRODUCT_COVER_REFERENCE_LIST), '?')); + $sql = "SELECT + produit.id, + produit.reference + FROM ".$this->tableprefix."produit as produit + WHERE produit.reference IN ($sqlConditionsPlaceholder)"; + + return $this->execSQL($sql,ProductConstant::PRODUCT_COVER_REFERENCE_LIST); + } + } \ No newline at end of file