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.

189 lines
7.2KB

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