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.

CartController.php 6.9KB

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