em = $em; $this->security = $security; $this->userUtils = $userUtils; $this->merchantUtils = $merchantUtils; $this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName()); $this->priceUtils = $priceUtils ; $this->productFamilyUtils = $productFamilyUtils ; $this->session = $session ; } public function getCartCurrent() { $paramsSearchOrderShop = []; $user = $this->security->getUser(); $visitor = $this->userUtils->getVisitorCurrent(); $orderShop = null ; $orderShopUser = null ; $orderShopVisitor = null ; if ($user) { $orderShopUser = $this->orderShopRepo->findCartCurrent([ 'user' => $user ]); } if ($visitor) { $orderShopVisitor = $this->orderShopRepo->findCartCurrent([ 'visitor' => $visitor ]); } if($orderShopUser || $orderShopVisitor) { if($orderShopUser && $orderShopVisitor && $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts())) { $this->session->getFlashBag()->add('success', "Votre panier visiteur vient d'être fusionné avec votre panier client.") ; } $orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor) ; } if(!$user && !$visitor) { $this->session->getFlashBag()->add('error', 'Vous devez accepter les cookies ou vous connecter pour créer un panier.') ; } else { if (!$orderShop) { $orderShop = $this->createOrderShop([ 'user' => $user, 'visitor' => $visitor, 'merchant' => $this->merchantUtils->getMerchantCurrent() ]); } } return $orderShop; } public function createOrderShop($params) { $orderShop = new OrderShop(); $orderShopBelongTo = false; if (isset($params['user']) && $params['user']) { $orderShopBelongTo = true; $orderShop->setUser($params['user']); } if (isset($params['visitor']) && $params['visitor']) { $orderShopBelongTo = true; $orderShop->setVisitor($params['visitor']); } if (!$orderShopBelongTo) { throw new \ErrorException('La commande doit être liée à un utilisateur ou à un visiteur.'); } if (isset($params['merchant']) && $params['merchant']) { $orderShop->setMerchant($params['merchant']); } else { throw new \ErrorException('La commande doit être liée à un merchant.'); } if($this->security->getUser()) { $orderShop->setCreatedBy($this->security->getUser()) ; $orderShop->setUpdatedBy($this->security->getUser()) ; } else { // createdBy doit pouvoir être NULL pour OrderShop, en attendant qu'on en discute, j'assigne ça au premier de la base $userRepository = $this->em->getRepository($this->em->getClassMetadata(UserInterface::class)->getName()) ; $user = $userRepository->find(1) ; $orderShop->setCreatedBy($user) ; $orderShop->setUpdatedBy($user) ; } $this->em->persist($orderShop); $this->em->flush(); return $orderShop; } public function addOrderProduct($orderShop, $orderProductAdd) { if ($orderProductAdd->getQuantityOrder() > 0) { $updated = false; $orderProductAdd->setTitle($orderProductAdd->getTitleOrderShop()); $orderProductAdd->setPrice($this->priceUtils->getPrice($orderProductAdd->getProduct())); $orderProductAdd->setUnit($orderProductAdd->getProduct()->getUnitInherited()); $orderProductAdd->setTaxRate($orderProductAdd->getProduct()->getTaxRateInherited()); $orderProductAdd->setQuantityProduct($orderProductAdd->getProduct()->getQuantityInherited()); $productFamily = $this->productFamilyUtils->getProductFamilyBySlug($orderProductAdd->getProduct()->getProductFamily()->getSlug()) ; $reductionCatalog = $productFamily->getReductionCatalog() ; if($reductionCatalog) { $orderProductReductionCatalog = new OrderProductReductionCatalog() ; $orderProductReductionCatalog->setTitle($reductionCatalog->getTitle()) ; $orderProductReductionCatalog->setValue($reductionCatalog->getValue()) ; $orderProductReductionCatalog->setUnit($reductionCatalog->getUnit()) ; $orderProductReductionCatalog->setBehaviorTaxRate($reductionCatalog->getBehaviorTaxRate()) ; $orderProductAdd->setOrderProductReductionCatalog($orderProductReductionCatalog); } foreach($orderShop->getOrderProducts() as $orderProduct) { if ($orderProduct->getProduct()->getId() == $orderProductAdd->getProduct()->getId() && $this->priceUtils->getPrice($orderProduct) == $this->priceUtils->getPrice($orderProductAdd) && $this->compareOrderProductReductionCatalog($orderProduct->getOrderProductReductionCatalog(), $orderProductAdd->getOrderProductReductionCatalog())) { $updated = true; $orderProduct->setQuantityOrder($orderProduct->getQuantityOrder() + $orderProductAdd->getQuantityOrder()); $this->em->persist($orderProduct); } } if (!$updated) { $orderShop->addOrderProduct($orderProductAdd); if(isset($orderProductReductionCatalog)) { $this->em->persist($orderProductReductionCatalog); } $this->em->persist($orderProductAdd); $this->em->persist($orderShop); } $this->em->flush(); } } public function compareOrderProductReductionCatalog($orderProductReductionCatalog1, $orderProductReductionCatalog2) { return $orderProductReductionCatalog1 && $orderProductReductionCatalog2 && $orderProductReductionCatalog1->getUnit() == $orderProductReductionCatalog2->getUnit() && $orderProductReductionCatalog1->getValue() == $orderProductReductionCatalog2->getValue() && $orderProductReductionCatalog1->getBehaviorTaxRate() == $orderProductReductionCatalog2->getBehaviorTaxRate() ; } public function countQuantities($orderShop) { return $this->countQuantitiesByOrderProducts($orderShop->getOrderProducts()); } public function countQuantitiesByOrderProducts($orderProducts = []) { $count = 0; foreach ($orderProducts as $orderProduct) { $count += $orderProduct->getQuantityOrder(); } return $count; } public function getOrderProductsByParentCategory($orderShop = null) { $categoriesArray = []; foreach ($orderShop->getOrderProducts() as $orderProduct) { $productCategories = $orderProduct->getProduct()->getProductFamily()->getProductCategories(); $category = $productCategories[0]->getParentCategory(); $labelCategory = $category->getTitle() ; if (!isset($categoriesArray[$labelCategory])) { $categoriesArray[$labelCategory] = [] ; } $categoriesArray[$labelCategory][] = $orderProduct; } return $categoriesArray; } public function getOrderDatas($order = null) { if(!$order) { $order = $this->getCartCurrent() ; } $data = [] ; $data['order'] = $order ; $data['count'] = $this->countQuantities($order) ; $data['total_with_tax'] = $this->priceUtils->getTotalWithTaxAndReduction($order) ; $data['order_products_by_category'] = $this->getOrderProductsByParentCategory($order) ; return $data ; } public function getOrderAsJsonObject(OrderShopInterface $order) { $data['id'] = $order->getId(); $data['user'] = $order->getUser()->getSummary(); $data['deliveryAddress'] = $order->getDeliveryAddress()->getSummary(); $data['invoiceAddress'] = $order->getInvoiceAddress()->getSummary(); $data['total'] = $this->priceUtils->getTotal($order); $data['totalWithTax'] = $this->priceUtils->getTotalWithTax($order); $data['totalWithTaxAndReduction'] = $this->priceUtils->getTotalWithTax($order); $i=0; foreach ($this->getOrderProductsByParentCategory($order) as $labelCategory => $orderProducts) { foreach ($orderProducts as $orderProduct) { $data['orderProducts'][$i]['id'] = $orderProduct->getId(); $data['orderProducts'][$i]['product'] = $orderProduct->getProduct()->getId(); $data['orderProducts'][$i]['quantityOrder'] = $orderProduct->getQuantityOrder(); $data['orderProducts'][$i]['labelCategory'] = $labelCategory; $data['orderProducts'][$i]['title'] = $orderProduct->getTitle(); $data['orderProducts'][$i]['price'] = $this->priceUtils->getPrice($orderProduct); $data['orderProducts'][$i]['priceWithTax'] = $this->priceUtils->getPriceWithTax($orderProduct); $data['orderProducts'][$i]['priceWithTaxAndReduction'] = $this->priceUtils->getPriceWithTaxAndReduction($orderProduct); $data['orderProducts'][$i]['quantity'] = $orderProduct->getQuantityOrder(); $data['orderProducts'][$i]['totalWithTaxAndReduction'] = $this->priceUtils->getTotalOrderProductsWithTaxAndReduction(array($orderProduct)); $i++; } } return $data; } public function getSummaryOrderProductReductionCatalog($orderProductReductionCatalog) { $text = '' ; if($orderProductReductionCatalog) { if($orderProductReductionCatalog->getUnit() == 'amount') { $text .= '- '.$orderProductReductionCatalog->getValue().' €' ; } if($orderProductReductionCatalog->getUnit() == 'percent') { $text .= '- '.$orderProductReductionCatalog->getValue().' %' ; } } return $text ; } public function mergeOrderShops($orderShop1, $orderShop2) { if($orderShop1 && $orderShop2) { foreach($orderShop2->getOrderProducts() as $orderProduct) { $this->addOrderProduct($orderShop1, $orderProduct); $this->em->remove($orderProduct) ; } $this->em->remove($orderShop2) ; $this->em->persist($orderShop1); $this->em->flush() ; return $orderShop1 ; } else { return ($orderShop1) ? $orderShop1 : $orderShop2 ; } } }