* * @author Anna Larch * @author Richard Steinmetz * * 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 . * */ 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; 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, Bdd $gestionBdd) { $this->logger = $logger; $this->orderBdd = $orderBdd; $this->gestionBdd = $gestionBdd; } public function getOrdersWithDetailsAsArray(){ $orders = $this->orderBdd->getOrdersWithDetails(); foreach($orders as &$order){ $currentOrderProductsReferenceAsString = ""; 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; } else{ $orderItemReferences = $this->orderBdd->getOrderItemsReferenceByOrderId($order['id']); if(!empty($orderItemReferences)){ $currentOrderProductsReferenceAsString = implode('-',$orderItemReferences); } $order["product_references"] = $currentOrderProductsReferenceAsString; } } return $orders; } public function createDefaultOrder($idNextcloud){ $this->orderBdd->createDefaultOrder($idNextcloud); } public function updateOrderDate($orderId,$date){ $this->orderBdd->updateOrderDateAndSetNewOrderNumber($orderId,$date); } public function getThanatoProductsFees(){ return $this->orderBdd->getThanatoProductDiscountList(); } public function createDefaultThanatoProductFee(){ $this->orderBdd->createDefaultThanatoProductFee(); } public function getOrderCount(){ return $this->orderBdd->getOrderCount(); } public function getThanatoProductFeeCount(){ return $this->orderBdd->getThanatoProductFeeCount(); } public function getOrderByIdWithDetails($orderId){ return $this->orderBdd->getOrderByIdWithDetails($orderId); } public function getOrderProductsById($orderId){ return $this->orderBdd->getOrderProductsByOrderId($orderId); } public function getOrderItemsByOrderId($orderId){ return $this->orderBdd->getOrderItemsByOrderId($orderId); } public function getOrderTotalAmount($orderId,$tva){ return $this->orderBdd->getOrderTotalAmount($orderId,$tva); } public function getOrderPdfData($orderId){ $orderDetails = $this->orderBdd->getOrderByIdWithDetails($orderId); if($orderDetails == null){ return null; } $orderProducts = []; if($orderDetails["fk_order_type_key"] == OrderTypeConstant::ORDER_TYPE_DEVIS){ $orderProducts = $this->gestionBdd->getOrderDevisProduits($orderDetails["fk_devis_id"]); $orderDetails["client_real_adress"] = $orderDetails["thanato_address"]; $orderDetails["client_adress_city"] = $orderDetails["thanato_city"]; $orderDetails["type"] = OrderPdfConstant::SUBCONTRACTOR_TYPE; $orderDetails["defunt_name_with_sexe"] = $orderDetails["defunt_nom"]; $orderDetails["client_phone"] = $orderDetails["thanato_phone"]; $orderDetails["client_legal_one"] = $orderDetails["thanato_siret"]; $orderDetails["company_name"] = $orderDetails["thanato_company_name"]; $orderDetails["client_name"] = $orderDetails["thanato_nom"] . ' ' . $orderDetails['thanato_prenom']; $orderDetails["client_name"] = trim($orderDetails["client_name"]); $orderDetails["client_mail"] = $orderDetails["thanato_email"] ?? ""; } else{ $orderItems = $this->orderBdd->getOrderItemsByOrderId($orderId); foreach($orderItems as $orderItem){ $orderProducts[] = [ "produit_id" => $orderItem["order_product_id"], "quantite" => $orderItem["order_item_quantity"], "produit_price" => $orderItem["order_product_ht_amount"], "produit_reference" => $orderItem["order_product_reference"], "produit_description" => $orderItem["order_product_label"], ]; } $orderDetails["client_real_adress"] = $orderDetails["provider_address"]; $orderDetails["client_adress_city"] = $orderDetails["provider_city"]; $orderDetails["client_legal_one"] = $orderDetails["provider_siret_number"];; $orderDetails["client_mail"] = $orderDetails["provider_email"]; $orderDetails["type"] = OrderPdfConstant::PROVIDER_TYPE; $orderDetails["devis_date"] = $orderDetails["order_date"]; $orderDetails["company_name"] = $orderDetails["provider_company_name"]; $orderDetails["client_name"] = $orderDetails["provider_name"] . ' ' . $orderDetails['provider_last_name']; $orderDetails["client_name"] = trim($orderDetails["client_name"]); } $orderDetails["products"] = $orderProducts; return $orderDetails; } public function createOrderFromDevisIdAndDate(int $devisId,Datetime $devisDate,string $idNextCloud = BddConstant::DEFAULT_ADMIN_ID_NEXTCLOUD ){ $this->orderBdd->createOrderFromDevisIdAndDate($devisId,$devisDate,$idNextCloud); } public function getOrderProducts(){ return $this->orderBdd->getOrderProducts(); } public function createDefaultOrderProduct($idNextCloud){ $this->orderBdd->createDefaultOrderProduct($idNextCloud); } 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; } public function getOrderDevisProduitsByDevisId($devisId){ return $this->gestionBdd->getOrderDevisProduitsByDevisId($devisId); } public function getTotalOrderDevisAmount($devisId){ return $this->gestionBdd->getTotalOrderDevisAmount($devisId); } }