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.

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