選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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