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.

193 lines
7.4KB

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