From 9253a69ade84e1c1d97a778d9aafa174be9ecc79 Mon Sep 17 00:00:00 2001 From: Tiavina Date: Thu, 20 Feb 2025 15:48:39 +0300 Subject: [PATCH] when updating the thanato of the devis, update also the order (canceled or ordered), if the devis is not in order yet, create, wip thanato statistique --- gestion/lib/Controller/PageController.php | 4 +++ gestion/lib/Db/Bdd.php | 11 ++++++++ gestion/lib/Db/OrderBdd.php | 16 ++++++++++-- gestion/lib/Service/Order/OrderService.php | 30 ++++++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) diff --git a/gestion/lib/Controller/PageController.php b/gestion/lib/Controller/PageController.php index 596d5b0..989c318 100644 --- a/gestion/lib/Controller/PageController.php +++ b/gestion/lib/Controller/PageController.php @@ -1663,6 +1663,10 @@ class PageController extends Controller { $result = $this->myDb->gestion_update($table, $column, $data, $id, $this->idNextcloud); } else { + $isUpdateDevisThanato = (strcmp($table, 'devis')== 0) && (strcmp($column, 'id_thanato') ==0); + if($isUpdateDevisThanato){ + $this->orderService->createOrUpdateOrderByDevisAndThanato($id, $data); + } $facturecourant = json_decode($this->myDb->getOneFacture($id, $this->idNextcloud))[0]; if(strcmp($column, 'date_paiement')==0) { $datecourant = $facturecourant->date_paiement; diff --git a/gestion/lib/Db/Bdd.php b/gestion/lib/Db/Bdd.php index de33d4b..8188750 100644 --- a/gestion/lib/Db/Bdd.php +++ b/gestion/lib/Db/Bdd.php @@ -3066,4 +3066,15 @@ class Bdd { return null; } + public function getThanatoByThanatoId($thanatoId){ + $sql = "SELECT * + FROM ".$this->tableprefix."thanato as thanato + WHERE thanato.id = ?"; + $res = $this->execSQLNoJsonReturn($sql,array($thanatoId)); + if($res){ + return $res[0]; + } + return null; + } + } diff --git a/gestion/lib/Db/OrderBdd.php b/gestion/lib/Db/OrderBdd.php index 092aa09..8e88fa5 100644 --- a/gestion/lib/Db/OrderBdd.php +++ b/gestion/lib/Db/OrderBdd.php @@ -535,10 +535,11 @@ class OrderBdd { orders.fk_provider_id = ? AND MONTH(orders.order_date) = ? AND YEAR(orders.order_date) = ? AND - orders.fk_order_type_key = ? + orders.fk_order_type_key = ? AND + orders.fk_order_status_key != ? ORDER BY orders.order_date ASC;"; - $orders = $this->execSQLNoJsonReturn($sql,[ $providerId,$month,$year,OrderTypeConstant::ORDER_TYPE_PURCHASE]); + $orders = $this->execSQLNoJsonReturn($sql,[ $providerId,$month,$year,OrderTypeConstant::ORDER_TYPE_PURCHASE,OrderStatusConstant::CANCELED_KEY]); return $orders; } @@ -562,4 +563,15 @@ class OrderBdd { } return 0; } + + public function getOrderByDevisId($devisId){ + $sql = "SELECT * FROM ".$this->orderTablePrefix."orders as orders + WHERE orders.fk_devis_id = ? LIMIT 1"; + + $result = $this->execSQLNoJsonReturn($sql,[$devisId]); + if(!empty($result)){ + return $result[0]; + } + return null; + } } \ No newline at end of file diff --git a/gestion/lib/Service/Order/OrderService.php b/gestion/lib/Service/Order/OrderService.php index 77c4a31..897c7bb 100644 --- a/gestion/lib/Service/Order/OrderService.php +++ b/gestion/lib/Service/Order/OrderService.php @@ -29,7 +29,9 @@ namespace OCA\Gestion\Service\Order; use DateTime; use OCA\Gestion\Constants\BddConstant; use OCA\Gestion\Constants\OrderPdfConstant; +use OCA\Gestion\Constants\OrderStatusConstant; use OCA\Gestion\Constants\OrderTypeConstant; +use OCA\Gestion\Constants\ThanatoTypeConstant; use OCA\Gestion\Db\Bdd; use OCA\Gestion\Db\OrderBdd; use OCA\Gestion\Helpers\FileExportHelpers; @@ -168,4 +170,32 @@ class OrderService { public function addDefaultOrderItem($orderId){ $this->orderBdd->addDefaultOrderItem($orderId); } + + public function createOrUpdateOrderByDevisAndThanato($devisId,$thanatoId){ + $thanato = $this->gestionBdd->getThanatoByThanatoId($thanatoId); + if($thanato == null){ + return null; + } + $thanatoIsSubContractor = $thanato['fk_thanato_type_key'] == ThanatoTypeConstant::THANATO_TYPE_SUBCONTRACTOR; + $devis = $this->gestionBdd->getDevisByDevisId($devisId); + if($devis == null){ + return null; + } + $devisOrder = $this->orderBdd->getOrderByDevisId($devisId); + $thereIsAnOrderRelatedToThisDevis = $devisOrder != null && $devisOrder["fk_order_type_key"] == OrderTypeConstant::ORDER_TYPE_DEVIS; + if($thereIsAnOrderRelatedToThisDevis){ + $orderStatusForUpdate = OrderStatusConstant::ORDERED_KEY; + if(!$thanatoIsSubContractor){ + $orderStatusForUpdate = OrderStatusConstant::CANCELED_KEY; + } + $this->orderBdd->updateOrderStatus($devisOrder['id'],$orderStatusForUpdate); + } + else{ + if($thanatoIsSubContractor){ + $devisDate = new DateTime($devis['devis_date']); + $this->createOrderFromDevisIdAndDate((int)$devisId,$devisDate); + } + } + return true; + } }