You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
7.1KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Controller\Order;
  3. use Lc\CaracoleBundle\Controller\AbstractController;
  4. use Lc\CaracoleBundle\Form\Order\OrderProductsType;
  5. use Lc\CaracoleBundle\Model\Order\OrderProductInterface;
  6. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. /**
  12. * @Route("/{section}/panier", name="frontend_cart_")
  13. */
  14. class CartController extends AbstractController
  15. {
  16. protected ProductFamilyInterface $productFamily;
  17. protected array $orderProducts = [];
  18. public function addProductFamily(Request $request): JsonResponse
  19. {
  20. $user = $this->getUserCurrent();
  21. $visitor = $this->getVisitorCurrent();
  22. $return = [];
  23. $data = $request->request->all();
  24. if (isset($data['order_products']['id_product_family'])) {
  25. $idProductFamily = $data['order_products']['id_product_family'];
  26. $this->productFamily = $this->getProductFamilyContainer()->getStore()->getOneById($idProductFamily);
  27. // alerte si cookies non acceptés
  28. if (!$user && !$visitor) {
  29. $this->addFlash(
  30. 'error',
  31. 'Vous devez <a href="' . $this->getRouter()->generate(
  32. 'frontend_page',
  33. ['devAlias' => 'politique-de-confidentialite']
  34. ) . '">accepter les cookies</a> ou vous <a href="' . $this->getRouter()->generate(
  35. 'fos_user_security_login'
  36. ) . '">connecter</a> pour ajouter un produit.'
  37. );
  38. return false;
  39. }
  40. if ($this->productFamily) {
  41. $form = $this->createForm(
  42. OrderProductsType::class,
  43. ['id_product_family' => $this->productFamily->getId()]
  44. );
  45. $form->handleRequest($request);
  46. if ($form->isSubmitted() && $form->isValid()) {
  47. $orderShop = $this->getOrderShopContainer()->getBuilder()->createIfNotExist(
  48. $this->getSectionCurrent(),
  49. $this->getUserCurrent(),
  50. $this->getVisitorCurrent()
  51. );
  52. $data = $form->getData();
  53. foreach ($data as $orderProduct) {
  54. if ($orderProduct instanceof OrderProductInterface) {
  55. if ($orderProduct->getQuantityOrder() > 0) {
  56. $addOrderProduct = $this->getOrderShopContainer()->getBuilder()->addOrderProduct(
  57. $orderShop,
  58. $orderProduct
  59. );
  60. }
  61. if (isset($addOrderProduct) && $addOrderProduct && $orderProduct->getQuantityOrder() > 0) {
  62. $this->orderProducts[] = $orderProduct;
  63. }
  64. }
  65. }
  66. }
  67. }
  68. }
  69. return new JsonResponse($return);
  70. }
  71. /**
  72. * @Route("/order-reduction-cart/delete/{id}", name="delete_reduction_cart")
  73. */
  74. public function deleteReductionCart(Request $request): RedirectResponse
  75. {
  76. $entityManager = $this->getEntityManager();
  77. $id = $request->get('id');
  78. $orderReductionCart = $this->getOrderReductionCartContainer()->getStore()->getOneById((int) $id);
  79. $orderShop = $this->getCartCurrent();
  80. if ($orderReductionCart && $orderShop->getOrderReductionCarts() && $orderShop->getOrderReductionCarts(
  81. )->contains($orderReductionCart)) {
  82. $entityManager->remove($orderReductionCart);
  83. $entityManager->flush();
  84. $this->addFlash('success', 'La réduction a bien été supprimée de votre panier.');
  85. } else {
  86. $this->addFlash('error', 'Une erreur est survenue lors de la suppression de la réduction. ');
  87. }
  88. return $this->redirectToReferer($request);
  89. }
  90. /**
  91. * @Route("/reduction-credit/add/{id}", name="order_reduction_credit")
  92. */
  93. public function addReductionCredit(Request $request): RedirectResponse
  94. {
  95. $id = $request->get('id');
  96. $orderShop = $this->getCartCurrent();
  97. $user = $this->getUserCurrent();
  98. $orderShopContainer = $this->getOrderShopContainer();
  99. $reductionCredit = $this->getReductionCreditContainer()->getStore()->getOneById($id);
  100. if ($orderShop && $user && $reductionCredit
  101. && $orderShopContainer->getStore()->isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)
  102. && !$orderShopContainer->getSolver()->isReductionCreditAddedToOrder($orderShop, $reductionCredit)) {
  103. $return = $orderShopContainer->getBuilder()->addReductionCredit($orderShop, $reductionCredit);
  104. if ($return) {
  105. $this->addFlash('success', 'Votre avoir a bien été ajouté à votre panier.');
  106. } else {
  107. $this->addFlash(
  108. 'error',
  109. 'Vous ne pouvez pas effectuer cette action. Le montant de la commande est insuffisant.'
  110. );
  111. }
  112. } else {
  113. $this->addFlash('error', "Impossible d'effectuer cette action");
  114. }
  115. return $this->redirectToReferer($request);
  116. }
  117. /**
  118. * @Route("/order-reduction-credit/delete/{id}", name="delete_reduction_credit")
  119. */
  120. public function deleteReductionCredit(Request $request): RedirectResponse
  121. {
  122. $entityManager = $this->getEntityManager();
  123. $id = $request->get('id');
  124. $orderReductionCredit = $this->getOrderReductionCreditContainer()->getStore()->getOneById((int)$id);
  125. $orderShop = $this->getCartCurrent();
  126. if ($orderReductionCredit && $orderShop->getOrderReductionCredits() && $orderShop->getOrderReductionCredits(
  127. )->contains($orderReductionCredit)) {
  128. $entityManager->remove($orderReductionCredit);
  129. $entityManager->flush();
  130. $this->addFlash('success', 'Votre avoir a bien été supprimé de votre panier.');
  131. } else {
  132. $this->addFlash('error', 'Une erreur est survenue lors de la suppression de votre avoir. ');
  133. }
  134. $referer = $this->getReferer($request);
  135. if ($referer) {
  136. return $this->redirect($referer);
  137. } else {
  138. return $this->redirectToRoute('frontend_order_cart', [
  139. 'section' => $this->getSectionCurrentSlug()
  140. ]);
  141. }
  142. }
  143. protected function redirectToReferer(Request $request): RedirectResponse
  144. {
  145. $referer = $this->getReferer($request);
  146. if ($referer) {
  147. return $this->redirect($referer);
  148. } else {
  149. return $this->redirectToRoute('frontend_order_cart', [
  150. 'section' => $this->getSectionCurrentSlug()
  151. ]);
  152. }
  153. }
  154. }