diff --git a/gestion/lib/Constants/OrderTypeConstant.php b/gestion/lib/Constants/OrderTypeConstant.php new file mode 100644 index 0000000..256d4cf --- /dev/null +++ b/gestion/lib/Constants/OrderTypeConstant.php @@ -0,0 +1,9 @@ +tableprefix ."produit_devis as produit_devis + LEFT JOIN ".$this->tableprefix."produit as produit on produit_devis.produit_id = produit.id + WHERE produit_devis.devis_id = ?;"; + + $produitList = $this->execSQLNoJsonReturn( + $sql, + [$devisId]); + + $produitReferences = []; + foreach($produitList as $produit){ + if($produit["produit_reference"] != null){ + $produitReferences[] = $produit["produit_reference"]; + } + } + $produitReferences = array_unique($produitReferences); + return $produitReferences; + } + public function getDevisProduits($devisId){ $sql = "SELECT produit_devis.id, @@ -2341,10 +2365,13 @@ class Bdd { produit.reference as produit_reference, produit.description as produit_description, produit.vat as produit_vat, - devis.id_client as devis_client_id + devis.id_client as devis_client_id, + thanato.id as fk_thanato_id, + thanato.fk_thanato_type_key as fk_thanato_type_key FROM ".$this->tableprefix ."produit_devis as produit_devis LEFT JOIN ".$this->tableprefix."produit as produit on produit_devis.produit_id = produit.id LEFT JOIN ".$this->tableprefix."devis as devis on produit_devis.devis_id = devis.id + LEFT JOIN ".$this->tableprefix."thanato as thanato on devis.id_thanato = thanato.id WHERE produit_devis.devis_id = ?;"; $produitList = $this->execSQLNoJsonReturn( @@ -2352,11 +2379,24 @@ class Bdd { [$devisId]); if(!empty($produitList)){ - $clientId = $produitList[0]["devis_client_id"]; - $client = $this->getClientById($clientId); - foreach($produitList as &$produit){ - $productPrice = $this->getProductPriceByClientGroupId($client['fk_client_group_id'],$produit['produit_id']); - $produit['produit_price'] = $productPrice ?? $produit['produit_price']; + $thanatoTypeKey = $produitList[0]["thanato_type_key"]; + $thanatoId = $produitList[0]["fk_thanato_id"]; + $needToApplyThanatoFee = $thanatoTypeKey == ThanatoTypeConstant::THANATO_TYPE_SUBCONTRACTOR; + if($needToApplyThanatoFee && $thanatoId != null){ + foreach($produitList as &$produit){ + $productPrice = $this->getProductPriceByThanatoId($thanatoId,$produit['id']); + $productPrice = $productPrice ?? $produit['produit_price']; + $produit['produit_price'] = $productPrice * $produit["quantite"]; + } + } + else{ + $clientId = $produitList[0]["devis_client_id"]; + $client = $this->getClientById($clientId); + foreach($produitList as &$produit){ + $productPrice = $this->getProductPriceByClientGroupId($client['fk_client_group_id'],$produit['produit_id']); + $productPrice = $productPrice ?? $produit['produit_price']; + $produit['produit_price'] = $productPrice * $produit["quantite"]; + } } } @@ -2509,7 +2549,7 @@ class Bdd { } return $invoices; } - public function getDevisPdfDataByDevisId($devisId){ + public function getDevisByDevisId($devisId){ $sql = "SELECT devis.id as devis_id, devis.date as devis_date, @@ -3008,4 +3048,19 @@ class Bdd { return null; } + private function getProductPriceByThanatoId($thanatoId,$productId){ + $sql = "SELECT * + FROM ".$this->tableprefix ."thanato_product_discount as thanato_product_discount + WHERE thanato_product_discount.fk_thanato_id = ? AND + thanato_product_discount.fk_product_id = ?; + "; + $thanatoProductDiscount = $this->execSQLNoJsonReturn( + $sql, + [$thanatoId,$productId]); + if(!empty($thanatoProductDiscount)){ + return $thanatoProductDiscount[0]['ht_price']; + } + return null; + } + } diff --git a/gestion/lib/Db/OrderBdd.php b/gestion/lib/Db/OrderBdd.php index fc35bee..88e435b 100644 --- a/gestion/lib/Db/OrderBdd.php +++ b/gestion/lib/Db/OrderBdd.php @@ -2,6 +2,7 @@ namespace OCA\Gestion\Db; use OCA\Gestion\Constants\OrderStatusConstant; +use OCA\Gestion\Constants\OrderTypeConstant; use OCA\Gestion\Constants\ThanatoTypeConstant; use OCA\Gestion\Helpers\OrderHelpers; use OCP\IDBConnection; @@ -162,6 +163,51 @@ class OrderBdd { $this->execSQLNoData($sql,[$statusKey,$orderId]); } + + public function getOrderItemsReferenceByOrderId($orderId){ + $sql = "SELECT + order_item.id as order_item_id, + order_product.reference as order_product_reference, + FROM ".$this->orderTablePrefix."order_item as order_item + LEFT JOIN ".$this->orderTablePrefix."order_product as order_product on order_item.fk_order_item_id = order_product.id + WHERE order_item.fk_order_id = ? + GROUP BY order_product.reference;"; + $itemList = $this->execSQLNoJsonReturn( + $sql, + [$orderId]); + + $itemReferences = []; + foreach($itemList as &$item){ + if($item['order_product_reference'] != null){ + $itemReferences[] = $item['order_product_reference']; + } + } + return $itemReferences; + } + + public function getOrderItemsByOrderId($orderId){ + $sql = "SELECT + order_item.id as order_item_id, + order_item.quantity as order_item_quantity, + order_product.id as order_product_id, + order_product.reference as order_product_reference, + order_product.description as order_product_description, + order_product.prix_unitaire as order_product_ht_price + FROM ".$this->orderTablePrefix."order_item as order_item + LEFT JOIN ".$this->orderTablePrefix."order_product as order_product on order_item.fk_order_item_id = order_product.id + WHERE order_item.fk_order_id = ?;"; + $itemList = $this->execSQLNoJsonReturn( + $sql, + [$orderId]); + + $finalItemList = []; + foreach($itemList as &$item){ + if($item['order_product_id'] != null){ + $finalItemList[] = $item; + } + } + return $finalItemList; + } public function getOrderProductsByOrderId($orderId){ $sql = "SELECT order_product.id as order_product_id, @@ -202,10 +248,12 @@ class OrderBdd { orders.order_number, orders.order_full_number, orders.order_comment, - orders.fk_defunt_id, - orders.fk_lieu_id, - orders.fk_client_id, - orders.fk_thanato_id, + orders.fk_order_type_key, + orders.fk_devis_id, + devis.id_defunt as fk_defunt_id, + devis.id_lieu as fk_lieu_id, + devis.id_client as fk_client_id, + devis.id_thanato as fk_thanato_id, orders.fk_order_status_key, orders.fk_provider_id, thanato.id as thanato_id, @@ -226,12 +274,14 @@ class OrderBdd { provider.provider_company_name as provider_company_name FROM " .$this->orderTablePrefix."orders as orders - LEFT JOIN ".$this->orderTablePrefix."thanato as thanato on orders.fk_thanato_id = thanato.id - LEFT JOIN ".$this->orderTablePrefix."client as client on orders.fk_client_id = client.id - LEFT JOIN ".$this->orderTablePrefix."defunt as defunt on orders.fk_defunt_id = defunt.id - LEFT JOIN ".$this->orderTablePrefix."lieu as lieu on orders.fk_lieu_id = lieu.id + LEFT JOIN ".$this->orderTablePrefix."devis as devis on orders.fk_devis_id = devis.id + LEFT JOIN ".$this->orderTablePrefix."thanato as thanato on devis.id_thanato = thanato.id + LEFT JOIN ".$this->orderTablePrefix."client as client on devis.id_client = client.id + LEFT JOIN ".$this->orderTablePrefix."defunt as defunt on devis.id_defunt = defunt.id + LEFT JOIN ".$this->orderTablePrefix."lieu as lieu on devis.id_lieu = lieu.id LEFT JOIN ".$this->orderTablePrefix."provider as provider on orders.fk_provider_id = provider.id LEFT JOIN ".$this->orderTablePrefix."order_status as order_status on orders.fk_order_status_key = order_status.status_key + LEFT JOIN ".$this->orderTablePrefix."order_type as order_type on orders.fk_order_type_key = order_type.order_type_key ORDER BY orders.id DESC; " ; @@ -387,4 +437,30 @@ class OrderBdd { ]; } + public function createOrderFromDevisIdAndDate(int $devisId,Datetime $devisDate,string $idNextCloud = BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD){ + $lastOrderNumber = $this->getLastOrderNumber(); + $currentOrderNumber = $lastOrderNumber + 1; + $currentOrderFullNumber = OrderHelpers::GetFullOrderNumberByDateAndOrderNumber($devisDate,$currentOrderNumber); + $orderDate = $devisDate->format("Y-m-d"); + $sql = "INSERT INTO `".$this->orderTablePrefix."orders` ( + `order_date`, + `order_number`, + `order_full_number`, + `fk_order_status_key`, + `id_nextcloud`, + `fk_devis_id`, + `fk_order_type_key` + ) + VALUES (?,?,?,?,?,?,?);"; + $this->execSQLNoData($sql, array( + $orderDate, + $currentOrderNumber, + $currentOrderFullNumber, + OrderStatusConstant::ORDERED_KEY, + $idNextCloud, + $devisId, + OrderTypeConstant::ORDER_TYPE_DEVIS + ) + ); + } } \ No newline at end of file diff --git a/gestion/lib/Service/Devis/Pdf/DevisPdfService.php b/gestion/lib/Service/Devis/Pdf/DevisPdfService.php index 43d4c46..97e5498 100644 --- a/gestion/lib/Service/Devis/Pdf/DevisPdfService.php +++ b/gestion/lib/Service/Devis/Pdf/DevisPdfService.php @@ -107,7 +107,7 @@ class DevisPdfService { $configs = json_decode($this->gestionBdd->getConfiguration(self::DEFAULT_NEXTCLOUD_H2F_ADMIN)); $currentConfig = $configs[0]; $logo = $this->getLogo(); - $devisPdfData = $this->gestionBdd->getDevisPdfDataByDevisId($devisId); + $devisPdfData = $this->gestionBdd->getDevisByDevisId($devisId); if($devisPdfData == null){ return null; } diff --git a/gestion/lib/Service/GestionService.php b/gestion/lib/Service/GestionService.php index 0e10736..187ca0a 100644 --- a/gestion/lib/Service/GestionService.php +++ b/gestion/lib/Service/GestionService.php @@ -142,52 +142,13 @@ class GestionService { 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; @@ -203,7 +164,12 @@ class GestionService { } $defuntId = $this->gestionBdd->insertDefuntByNameAndReturnId($calendarSummary); $calendarStartDate = $this->GetCalendarDateFromVCalendarString($vCalendarString); - $devisId = $this->gestionBdd->insertDevisFromVCalendarAndReturnId($thanatoId,$clientId,$locationId,$defuntId,$calendarUuid,$calendarStartDate,$userName); + $devisDate = $calendarStartDate->format('Y-m-d'); + $devisId = $this->gestionBdd->insertDevisFromVCalendarAndReturnId($thanatoId,$clientId,$locationId,$defuntId,$calendarUuid,$devisDate,$userName); + $thanatoIsSubcontractor = $thanato["fk_thanato_type_key"] === ThanatoTypeConstant::THANATO_TYPE_SUBCONTRACTOR; + if($thanatoIsSubcontractor){ + $this->orderBdd->createOrderFromDevisIdAndDate($devisId,$calendarStartDate,$userName); + } $articlesValue = $this->GetArticlesNameFromVCalendarString($vCalendarString); if(!empty($articlesValue)){ $articleIds = $this->gestionBdd->getArticleIdsByArticleReferences($articlesValue); diff --git a/gestion/lib/Service/Order/OrderService.php b/gestion/lib/Service/Order/OrderService.php index 3d56121..3fa672c 100644 --- a/gestion/lib/Service/Order/OrderService.php +++ b/gestion/lib/Service/Order/OrderService.php @@ -26,6 +26,10 @@ declare(strict_types=1); namespace OCA\Gestion\Service\Order; +use DateTime; +use OCA\Gestion\Constants\BddConstant; +use OCA\Gestion\Constants\OrderTypeConstant; +use OCA\Gestion\Db\Bdd; use OCA\Gestion\Db\OrderBdd; use OCA\Gestion\Helpers\FileExportHelpers; use Psr\Log\LoggerInterface; @@ -33,30 +37,40 @@ use Psr\Log\LoggerInterface; class OrderService { /** @var \OCA\Gestion\Db\OrderBdd */ private $orderBdd; + private $gestionBdd; /** @var LoggerInterface */ private $logger; public function __construct( OrderBdd $orderBdd, - LoggerInterface $logger) { + LoggerInterface $logger, + Bdd $gestionBdd) { $this->logger = $logger; $this->orderBdd = $orderBdd; + $this->gestionBdd = $gestionBdd; } public function getOrdersWithDetailsAsArray(){ $orders = $this->orderBdd->getOrdersWithDetails(); foreach($orders as &$order){ $currentOrderProductsReferenceAsString = ""; - $currentOrderProductsReferences = []; - $currentOrderProducts = $this->orderBdd->getOrderProductsByOrderId($order['id']); - foreach($currentOrderProducts as $currentProduct){ - $currentOrderProductsReferences[] = $currentProduct["produit_reference"]; + if($order["fk_order_type_key"] == OrderTypeConstant::ORDER_TYPE_DEVIS){ + if($order["fk_devis_id"] != null){ + $produitReferences = $this->gestionBdd->getDevisProduitsReferences($order["fk_devis_id"]); + } + if(!empty($produitReferences)){ + $currentOrderProductsReferenceAsString = implode('-',$produitReferences); + } + $order["product_references"] = $currentOrderProductsReferenceAsString; } - if(!empty($currentOrderProductsReferences)){ - $currentOrderProductsReferenceAsString = implode('-',$currentOrderProductsReferences); + else{ + $orderItemReferences = $this->orderBdd->getOrderItemsReferenceByOrderId($order['id']); + if(!empty($orderItemReferences)){ + $currentOrderProductsReferenceAsString = implode('-',$orderItemReferences); + } + $order["product_references"] = $currentOrderProductsReferenceAsString; } - $order["product_references"] = $currentOrderProductsReferenceAsString; } return $orders; } @@ -109,4 +123,8 @@ class OrderService { $orderDetails["client_adress_city"] = $clientAdresses["city"]; return $orderDetails; } + + public function createOrderFromDevisIdAndDate(int $devisId,Datetime $devisDate,string $idNextCloud = BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD ){ + $this->orderBdd->createOrderFromDevisIdAndDate($devisId,$devisDate,$idNextCloud); + } }