From 078172f2d5d01caa2148dfc70f912549ca969ce7 Mon Sep 17 00:00:00 2001 From: Tiavina Date: Thu, 20 Feb 2025 09:47:45 +0300 Subject: [PATCH] fix defunt cover business logic --- gestion/lib/Db/Bdd.php | 52 +++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index b816fa2..a103496 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -3321,26 +3321,15 @@ class Bdd { } 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]; + $product = $this->getProductByProductId($productCoverId); + if($product == null){ + return null; } - return $product; + $productCoverType = $this->getProductTypeByKey(ProductConstant::PRODUCT_COVER_TYPE_KEY); + if($product["fk_product_type_id"] == $productCoverType["id"]){ + return $product; + } + return null; } private function isProductAlreadyExistInDevis($productId,$devisId){ @@ -3396,6 +3385,22 @@ class Bdd { $this->insertDevisArticle(devisId: $devisId, articleId: $productId,idNextcloud: $idNextcloud); } } + + private function getProductByProductId($productId){ + $sql = "SELECT + produit.id, + produit.reference, + produit.fk_product_type_id + FROM ".$this->tableprefix."produit as produit + WHERE produit.id = ? + LIMIT 1;"; + + $result = $this->execSQLNoJsonReturn($sql,[$productId]); + if(!empty($result)){ + return $result[0]; + } + return null; + } public function setDefuntCover($defuntId, $productCoverId){ $defunt = $this->getDefuntById($defuntId); if($defunt == null){ @@ -3409,13 +3414,14 @@ class Bdd { return null; } - $productCoverReferencesList = ProductConstant::PRODUCT_COVER_REFERENCE_LIST; - foreach($productCoverReferencesList as $currentProductCoverReference){ - if($currentProductCoverReference === $product["reference"]){ + $coverProductList = $this->getProductsByProductTypeKey(ProductConstant::PRODUCT_COVER_TYPE_KEY); + $coverProductList = json_decode($coverProductList); + foreach($coverProductList as $currentCoverProduct){ + if($currentCoverProduct->id == $product["id"]){ $this->createProductDevisIfNotExist($product["id"],$defunt["devis_id"]); } else{ - $this->removeProductFromDevisByProductReference($currentProductCoverReference,$defunt["devis_id"]); + $this->removeProductFromDevis($currentCoverProduct->id,$defunt["devis_id"]); } } return true;