Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

191 lines
7.3KB

  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 = null;
  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. $orderProductArray = $this->getOrderShopContainer()->getBuilder()->addOrderProduct(
  69. $orderShop,
  70. $orderProduct
  71. );
  72. if (is_array($orderProductArray)) {
  73. foreach($orderProductArray as $orderProduct) {
  74. $this->orderProducts[] = $orderProduct;
  75. }
  76. }
  77. }
  78. /**
  79. * @Route("/order-reduction-cart/delete/{id}", name="delete_reduction_cart")
  80. */
  81. public function deleteReductionCart(Request $request): RedirectResponse
  82. {
  83. $entityManager = $this->getEntityManager();
  84. $id = $request->get('id');
  85. $orderReductionCart = $this->getOrderReductionCartContainer()->getStore()->getOneById((int) $id);
  86. $orderShop = $this->getCartCurrent();
  87. if ($orderReductionCart && $orderShop->getOrderReductionCarts() && $orderShop->getOrderReductionCarts(
  88. )->contains($orderReductionCart)) {
  89. $entityManager->remove($orderReductionCart);
  90. $entityManager->flush();
  91. $this->addFlash('success', 'La réduction a bien été supprimée de votre panier.');
  92. } else {
  93. $this->addFlash('error', 'Une erreur est survenue lors de la suppression de la réduction. ');
  94. }
  95. return $this->redirectToReferer($request);
  96. }
  97. /**
  98. * @Route("/reduction-credit/add/{id}", name="order_reduction_credit")
  99. */
  100. public function addReductionCredit(Request $request): RedirectResponse
  101. {
  102. $id = $request->get('id');
  103. $orderShop = $this->getCartCurrent();
  104. $user = $this->getUserCurrent();
  105. $orderShopContainer = $this->getOrderShopContainer();
  106. $reductionCredit = $this->getReductionCreditContainer()->getStore()->getOneById($id);
  107. if ($orderShop && $user && $reductionCredit
  108. && $orderShopContainer->getStore()->isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)
  109. && !$orderShopContainer->getSolver()->isReductionCreditAddedToOrder($orderShop, $reductionCredit)) {
  110. $return = $orderShopContainer->getBuilder()->addReductionCredit($orderShop, $reductionCredit);
  111. if ($return) {
  112. $this->addFlash('success', 'Votre avoir a bien été ajouté à votre panier.');
  113. } else {
  114. $this->addFlash(
  115. 'error',
  116. 'Vous ne pouvez pas effectuer cette action. Le montant de la commande est insuffisant.'
  117. );
  118. }
  119. } else {
  120. $this->addFlash('error', "Impossible d'effectuer cette action");
  121. }
  122. return $this->redirectToReferer($request);
  123. }
  124. /**
  125. * @Route("/order-reduction-credit/delete/{id}", name="delete_reduction_credit")
  126. */
  127. public function deleteReductionCredit(Request $request): RedirectResponse
  128. {
  129. $entityManager = $this->getEntityManager();
  130. $id = $request->get('id');
  131. $orderReductionCredit = $this->getOrderReductionCreditContainer()->getStore()->getOneById((int)$id);
  132. $orderShop = $this->getCartCurrent();
  133. if ($orderReductionCredit && $orderShop->getOrderReductionCredits() && $orderShop->getOrderReductionCredits(
  134. )->contains($orderReductionCredit)) {
  135. $entityManager->remove($orderReductionCredit);
  136. $entityManager->flush();
  137. $this->addFlash('success', 'Votre avoir a bien été supprimé de votre panier.');
  138. } else {
  139. $this->addFlash('error', 'Une erreur est survenue lors de la suppression de votre avoir. ');
  140. }
  141. $referer = $this->getReferer($request);
  142. if ($referer) {
  143. return $this->redirect($referer);
  144. } else {
  145. return $this->redirectToRoute('frontend_order_cart', [
  146. 'section' => $this->getSectionCurrentSlug()
  147. ]);
  148. }
  149. }
  150. protected function redirectToReferer(Request $request): RedirectResponse
  151. {
  152. $referer = $this->getReferer($request);
  153. if ($referer) {
  154. return $this->redirect($referer);
  155. } else {
  156. return $this->redirectToRoute('frontend_order_cart', [
  157. 'section' => $this->getSectionCurrentSlug()
  158. ]);
  159. }
  160. }
  161. }