src/Controller/Main/CartController.php line 163

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Main;
  3. use App\Entity\Payment;
  4. use App\Entity\User;
  5. use App\Entity\Article;
  6. use App\Form\Payment1Type;
  7. use App\Form\RegistrationFormType;
  8. use App\Entity\ArticleSearch;
  9. use App\Entity\DeliverySpace;
  10. use App\Form\ArticleSearchType;
  11. use App\Form\DeliverySpaceType;
  12. use App\Service\Cart\CartService;
  13. use App\Repository\CityRepository;
  14. use App\Repository\StreetRepository;
  15. use App\Repository\ArticleRepository;
  16. use App\Repository\PaymentMethodRepository;
  17. use App\Repository\ShippingAmountRepository;
  18. use App\Service\Shipping\ShippingService;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  23. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  24. use Symfony\Component\HttpFoundation\JsonResponse;
  25. class CartController extends AbstractController
  26. {
  27.     private $cartService;
  28.     private $shippingService;
  29.     public function __construct(CartService $cartServiceShippingService $shippingService)
  30.     {
  31.         $this->cartService $cartService;
  32.         $this->shippingService $shippingService;
  33.     }
  34.     /**
  35.      * checkout
  36.      * @Route("/store/checkout", name="checkout_store")
  37.      * @return void
  38.      */
  39.     public function checkoutStore(): Response
  40.     {
  41.         return $this->render($this->getParameter('template') . '/cart/checkout.html.twig', []);
  42.     }
  43.     /**
  44.      * checkout
  45.      * @Route("/checkout", name="checkout")
  46.      * @return void
  47.      */
  48.     public function checkout(): Response
  49.     {
  50.         return $this->render($this->getParameter('template') . '/cart/checkout.html.twig', []);
  51.     }
  52.     /**
  53.      * cart
  54.      * @Route("/cart/order-step-1", name="cart_step_1")
  55.      * @return void
  56.      */
  57.     public function step1(SessionInterface $sessionInterfacePaymentMethodRepository $paymentMethodRepositoryStreetRepository $streetRepositoryShippingService $shippingServiceShippingAmountRepository $shippingAmountRepositoryRequest $request): Response
  58.     {
  59.         $user = new User();
  60.         $formRegistration $this->createForm(RegistrationFormType::class, $user);
  61.         // recupere la rue
  62.         $street $sessionInterface->get('shipping');
  63.         // on recuppere le montant corespondant
  64.         if ($request->request->count() > 0) {
  65.             $methodPaiement $request->request->get('method');
  66.             $sessionInterface->set('methodPayment'$methodPaiement);
  67.         }
  68.         if ($sessionInterface->get('methodPayment')) {
  69.             $methodPaiement $sessionInterface->get('methodPayment');
  70.         } else {
  71.             return $this->redirectToRoute('cart_index');
  72.         }
  73.         //on recupere la methode de paiment
  74.         // on recupere le total du panier
  75.         //on gener la nouvelle commande avec le prix de la livraion
  76.         return $this->renderForm($this->getParameter('template') . '/cart/order-step-1.html.twig', [
  77.             'items' => $this->cartService->getFullCart(),
  78.             'subtotal' => $this->cartService->getTotal(),
  79.             'street' => $street,
  80.             'registrationForm' => $formRegistration,
  81.             'methodPayment' => $paymentMethodRepository->find($methodPaiement)
  82.         ]);
  83.     }
  84.     /**
  85.      * @Route("/cart", name="cart_index")
  86.      */
  87.     public function index(PaymentMethodRepository $paymentMethodRepositoryArticleRepository $articleRepositoryStreetRepository $streetRepositoryCartService $cartServiceRequest $requestCityRepository $cityRepository): Response
  88.     {
  89.         $search = new ArticleSearch();
  90.         $form $this->createForm(ArticleSearchType::class, $search);
  91.         $payment = new Payment();
  92.         $formPayment $this->createForm(Payment1Type::class, $payment);
  93.         if ($request->isMethod('POST')) {
  94.             $articleIds $request->request->all()['article_id'];
  95.             $articleIds =  $request->request->all()['article_id'];
  96.             $quantities =  $request->request->all()['qty'];
  97.             // Faites ce que vous voulez avec les données récupérées
  98.             foreach ($articleIds as $index => $articleId) {
  99.                 $quantity $quantities[$index];
  100.                 // Traitez les données ici
  101.                 $cartService->addPost($articleId$quantity);
  102.             }
  103.             $this->addFlash('success'"Mise à jour du panier.");
  104.             return $this->redirectToRoute('cart_index');
  105.         }
  106.         $deliverySpace = new DeliverySpace();
  107.         $formDeliverySpace $this->createForm(DeliverySpaceType::class, $deliverySpace);
  108.         $formDeliverySpace->handleRequest($request);
  109.         return $this->renderForm($this->getParameter('template') . '/cart/index.html.twig', [
  110.             'items' => $cartService->getFullCart(),
  111.             'total' => $cartService->getTotal(),
  112.             'form' => $form,
  113.             'cities' => $cityRepository->findbyCountryName(),
  114.             'streets' => $streetRepository->findbyCity(),
  115.             'form_payment' => $formPayment,
  116.             'form_delivery_space' => $formDeliverySpace,
  117.             'methodPayment' => $paymentMethodRepository->findAll()
  118.         ]);
  119.     }
  120.     /**
  121.      * @Route("store/cart", name="store_cart_index")
  122.      */
  123.     public function indexStore(PaymentMethodRepository $paymentMethodRepositoryArticleRepository $articleRepositoryStreetRepository $streetRepositoryCartService $cartServiceRequest $requestCityRepository $cityRepository): Response
  124.     {
  125.         $search = new ArticleSearch();
  126.         $form $this->createForm(ArticleSearchType::class, $search);
  127.         $payment = new Payment();
  128.         $formPayment $this->createForm(Payment1Type::class, $payment);
  129.         if ($request->request->count() > 0) {
  130.             $cartService->addPost($request->request->get('article_id'), $request->request->get('qty'));
  131.             $this->addFlash('success'"Le produit a été modifié avec succès.");
  132.             return $this->redirectToRoute('cart_index');
  133.         }
  134.         $deliverySpace = new DeliverySpace();
  135.         $formDeliverySpace $this->createForm(DeliverySpaceType::class, $deliverySpace);
  136.         $formDeliverySpace->handleRequest($request);
  137.         return $this->renderForm($this->getParameter('template') . '/cart/index.html.twig', [
  138.             'items' => $cartService->getFullCart(),
  139.             'total' => $cartService->getTotal(),
  140.             'form' => $form,
  141.             'cities' => $cityRepository->findbyCountryName(),
  142.             'streets' => $streetRepository->findbyCity(),
  143.             'form_payment' => $formPayment,
  144.             'form_delivery_space' => $formDeliverySpace,
  145.             'methodPayment' => $paymentMethodRepository->findAll()
  146.         ]);
  147.     }
  148.     /**
  149.      * @Route("cart/add/{id}", name="cart_add")
  150.      */
  151.     public function add(int $idCartService $cartService)
  152.     {
  153.         $cartService->add($id);
  154.         return $this->redirectToRoute('cart_index');
  155.     }
  156.     /**
  157.      * @Route("cart/add-ajax", name="cart_add_ajax", methods={"GET","POST"})
  158.      */
  159.     public function addAjax(CartService $cartServiceRequest $request)
  160.     {
  161.         $id =  $request->get('_id');
  162.         $qty $request->get('_qty');
  163.         if (!empty($id)) {
  164.             //AJOUTE L'ARTICLE ET LA QUANTITTE AU PANIER
  165.             $cartService->addPost($id$qty);
  166.             return new JsonResponse(true);
  167.         } else {
  168.             return new JsonResponse(false);
  169.         }
  170.     }
  171.     /**
  172.      * @Route("cart/add-qty-ajax/{id}", name="cart_add_qty_ajax", methods={"GET","POST"})
  173.      */
  174.     public function addQtyAjax(Article $article nullCartService $cartServiceRequest $request)
  175.     {
  176.         if ($article) {
  177.             $response = [
  178.                 'code' => 1,
  179.                 'content' => $this->render($this->getParameter('template') . '/shop/_add_article.html.twig', [
  180.                     'article' => $article
  181.                 ])->getContent()
  182.             ];
  183.             return new JsonResponse($response200);
  184.         }
  185.         return new JsonResponse(['code' => 0], 200);
  186.     }
  187.     /**
  188.      * @Route("/cart/shipping/add/{id}", name="shipping_add")
  189.      */
  190.     public function addShipping(int $idShippingService $shippingService)
  191.     {
  192.         $shippingService->add($id);
  193.         return $this->redirectToRoute('cart_index');
  194.     }
  195.     /**
  196.      * @Route("/cart/remove/{id}", name="cart_remove", methods="GET")
  197.      */
  198.     public function remove($idCartService $cartService)
  199.     {
  200.         $cartService->remove($id);
  201.         $this->addFlash('success''Le produit a été retiré du panier');
  202.         return $this->redirectToRoute('cart_index');
  203.     }
  204.     /**
  205.      * @Route("/cart/delete/{id}", name="cart_delete", methods="GET")
  206.      */
  207.     public function delete($idCartService $cartService)
  208.     {
  209.         $cartService->delete($id);
  210.         $this->addFlash('success''Le produit a été retiré du panier');
  211.         return $this->redirectToRoute('cart_index');
  212.     }
  213.     /**
  214.      * @Route("/cart/delete-ajax", name="cart_delete_ajax", methods="POST")
  215.      */
  216.     public function deleteAjax(Request $requestCartService $cartService)
  217.     {
  218.         $id =  $request->get('id');
  219.         if (!empty($id)) {
  220.             $cartService->delete($id);
  221.             return new JsonResponse(true);
  222.         } else {
  223.             return new JsonResponse(false);
  224.         }
  225.     }
  226.     /**
  227.      * @Route("/cart/clear", name="cart_clear", methods="GET")
  228.      */
  229.     public function claer(CartService $cartService)
  230.     {
  231.         $cartService->clear();
  232.         return $this->redirectToRoute('cart_index');
  233.     }
  234.     /**
  235.      * RECHARGE LE PANIER EN AJAX
  236.      * @Route("/cart/load", name="cart_load")
  237.      */
  238.     public function loadCart()
  239.     {
  240.         $reponse = [
  241.             'count' => $this->render($this->getParameter('template') . '/cart/ajax/count.html.twig')->getContent(),
  242.             'cart' => $this->render($this->getParameter('template') . '/cart/ajax/load.html.twig')->getContent()
  243.         ];
  244.         return new JsonResponse($reponse);
  245.     }
  246. }