src/Controller/CartController.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Cart;
  4. use App\Entity\Setting;
  5. use App\Form\CartType;
  6. use App\Repository\AddonRepository;
  7. use App\Repository\CartRepository;
  8. use App\Repository\ProductRepository;
  9. use App\Repository\SettingRepository;
  10. use App\Repository\SubscriptionPlanPriceRepository;
  11. use App\Service\CartService;
  12. use DateTime;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpFoundation\Session\Session;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. class CartController extends AbstractController
  20. {
  21.     private string $sessionId;
  22.     private Session $session;
  23.     public function __construct()
  24.     {
  25.         $session         = new Session();
  26.         $this->session   $session;
  27.         $this->sessionId $session->getId();
  28.     }
  29.     #[Route(path: [
  30.         'de' => '/de/mein-einkaufswagen',
  31.         'en' => '/en/my-cart',
  32.         'es' => '/es/mi-carreton'
  33.     ], name'app_cart_index'methods: ['GET'])]
  34.     public function index(CartRepository $cartRepositorySettingRepository $settingRepository): Response
  35.     {
  36.         $session = new Session();
  37.         $session->start();
  38.         $this->sessionId $session->getId();
  39.         $itemsInCart $cartRepository->findBy(['session_id' => $this->sessionId]);
  40.         if ($this->getUser()) {
  41.             $itemsInCart $cartRepository->findBy(['client' => $this->getUser()]);
  42.         }
  43.         /**
  44.          * @var Setting $settings
  45.          */
  46.         $settings $settingRepository->findSettings();
  47.         return $this->render('shop/cart/index.html.twig', [
  48.             'carts'      => $itemsInCart,
  49.             'session_id' => $this->sessionId,
  50.             'vat_value'  => $settings->getVat(),
  51.         ]);
  52.     }
  53.     #[Route('/cart/new'name'app_cart_new'methods: ['GET''POST'])]
  54.     public function new(
  55.         Request $request,
  56.         CartRepository $cartRepository,
  57.         ProductRepository $productRepository,
  58.         SubscriptionPlanPriceRepository $subscriptionPlanPriceRepository,
  59.         AddonRepository $addonRepository,
  60.         EntityManagerInterface $entityManager
  61.     ): Response {
  62.         $cart      = new Cart();
  63.         $productId $_REQUEST['product_id'];
  64.         $planId    $_REQUEST['plan'];
  65.         $plan    $subscriptionPlanPriceRepository->findOneBy(['subscription_plan' => $planId'product' => $productId]);
  66.         $product $productRepository->find($productId);
  67.         $subscriptionPlanPrices       $subscriptionPlanPriceRepository->findBy(['product' => $product], []);
  68.         $amountSubscriptionPlanPrices count($subscriptionPlanPrices);
  69.         $addons                       $addonRepository->findBy(['product' => $product]);
  70.         $amountAddons                 count($addons);
  71.         $form $this->createForm(CartType::class, $cart);
  72.         $form->handleRequest($request);
  73.         if ($form->isSubmitted() && $form->isValid()) {
  74.             //dd($request->request);
  75.             $plan       $subscriptionPlanPriceRepository->find($request->request->get('plan'));
  76.             $basePrice  $plan->getSinglePriceMonthUser();
  77.             $totalPrice $basePrice $form['amount_users']->getData();
  78.             $cart->setProduct($product);
  79.             $cart->setAmountUsers($form['amount_users']->getData());
  80.             $cart->setTotalPriceMonth($totalPrice);
  81.             $cart->setSessionId($this->sessionId);
  82.             $cart->setClient($this->getUser() ?? null);
  83.             $cart->setRegDatetime(new DateTime());
  84.             $cartRepository->add($carttrue);
  85.             //Addons
  86.             $addons $request->request->get('order');
  87.             if ($addons) {
  88.                 foreach ($addons as $addon) {
  89.                     $cart        = new Cart();
  90.                     $addonObject $addonRepository->find($addon['addon']);
  91.                     $basePrice   $addonObject->getSinglePriceMonthUser();
  92.                     $totalPrice  $basePrice $form['amount_users']->getData();
  93.                     $cart->setAmountUsers($form['amount_users']->getData());
  94.                     $cart->setTotalPriceMonth($totalPrice);
  95.                     $cart->setProduct($product);
  96.                     $cart->setAddon($addonObject);
  97.                     $cart->setSessionId($this->sessionId);
  98.                     $cart->setClient($this->getUser() ?? null);
  99.                     $cart->setRegDatetime(new DateTime());
  100.                     $entityManager->persist($cart);
  101.                     $entityManager->flush();
  102.                 }
  103.             }
  104.             $this->session->set('oldSessionId'$this->sessionId);
  105.             return $this->redirectToRoute('app_cart_index', [], Response::HTTP_SEE_OTHER);
  106.         }
  107.         $basePrice  $plan->getSinglePriceMonthUser();
  108.         $totalPrice $basePrice $plan->getMinUser();
  109.         return $this->renderForm('shop/cart/new.html.twig', [
  110.             'cart'          => $cart,
  111.             'form'          => $form,
  112.             'min'           => $plan->getMinUser(),
  113.             'max'           => $plan->getMaxUser(),
  114.             'base_price'    => $basePrice,
  115.             'total_price'   => $totalPrice,
  116.             'session_id'    => $this->sessionId,
  117.             'product'       => $product->getProductName(),
  118.             'product_id'    => $product->getId(),
  119.             'plan'          => $plan->getId(),
  120.             'addons'        => $addons,
  121.             'amount_addons' => $amountAddons,
  122.             'amount_plans'  => $amountSubscriptionPlanPrices,
  123.         ]);
  124.     }
  125.     #[Route('/cart/{id}'name'app_cart_show'methods: ['GET'])]
  126.     public function show(Cart $cart): Response
  127.     {
  128.         return $this->render('shop/cart/show.html.twig', [
  129.             'cart' => $cart,
  130.         ]);
  131.     }
  132.     #[Route('/cart/{id}/edit'name'app_cart_edit'methods: ['GET''POST'])]
  133.     public function edit(Request $requestCart $cartCartRepository $cartRepositoryEntityManagerInterface $entityManagerCartService $cartService): Response
  134.     {
  135.         $form $this->createForm(CartType::class, $cart);
  136.         $form->handleRequest($request);
  137.         if ($form->isSubmitted() && $form->isValid()) {
  138.             $amountUsers               $form['amount_users']->getData();
  139.             $basePriceProductMonthUser $cartService->getBasePriceMonthUser($cart->getProduct(), $amountUsers);
  140.             $productsAndAddons         $cartRepository->findBy(['session_id' => $cart->getSessionId(), 'product' => $cart->getProduct()], ['id' => 'ASC']);
  141.             if ($cart->getClient()) {
  142.                 $productsAndAddons $cartRepository->findBy(['client' => $cart->getClient(), 'product' => $cart->getProduct()], ['id' => 'ASC']);
  143.             }
  144.             foreach ($productsAndAddons as $item) {
  145.                 $basePrice $basePriceProductMonthUser;
  146.                 if ($item->getAddon()) {
  147.                     $basePrice $cartService->getBasePriceAddonMonthUser($item->getAddon());
  148.                 }
  149.                 $totalPriceMonth $basePrice $amountUsers;
  150.                 $item->setAmountUsers($amountUsers);
  151.                 $item->setTotalPriceMonth($totalPriceMonth);
  152.                 $entityManager->persist($item);
  153.             }
  154.             $entityManager->flush();
  155.             return $this->redirectToRoute('app_cart_index', [], Response::HTTP_SEE_OTHER);
  156.         }
  157.         return $this->renderForm('shop/cart/edit.html.twig', [
  158.             'cart' => $cart,
  159.             'form' => $form,
  160.         ]);
  161.     }
  162.     #[Route('/cart/{id}/delete'name'app_cart_delete'methods: ['GET''POST'])]
  163.     public function delete($idCartRepository $cartRepository): Response
  164.     {
  165.         $productId $_REQUEST['product'];
  166.         $cartData  $cartRepository->findBy(['session_id' => $id'product' => $productId]);
  167.         foreach ($cartData as $cart) {
  168.             $cartRepository->remove($carttrue);
  169.         }
  170.         return $this->redirectToRoute('app_cart_index', [], Response::HTTP_SEE_OTHER);
  171.     }
  172.     #[Route('/cart/{id}/delete-addon'name'app_cart_delete_addon'methods: ['GET''POST'])]
  173.     public function deleteAddon($idCartRepository $cartRepository): Response
  174.     {
  175.         $cart $cartRepository->find($id);
  176.         $cartRepository->remove($carttrue);
  177.         return $this->redirectToRoute('app_cart_index', [], Response::HTTP_SEE_OTHER);
  178.     }
  179. }