src/Controller/CartController.php line 40

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Config\CartConfig;
  4. use App\Entity\Speaker;
  5. use App\Entity\SpeakerConsultation;
  6. use App\Entity\SpeakerCourse;
  7. use App\Form\SpeakerOrderType;
  8. use App\Repository\PromoCodeRepository;
  9. use App\Repository\SpeakerConsultationRepository;
  10. use App\Service\CartService;
  11. use App\Service\DiscountService;
  12. use App\Service\OrderService;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. /**
  21.  * @Route("/cart")
  22.  */
  23. class CartController extends AbstractController
  24. {
  25.     /**
  26.      * @Route("/", name="cart.index")
  27.      */
  28.     public function index(
  29.         CartService                   $cartService,
  30.         SpeakerConsultationRepository $consultationRepository,
  31.         PromoCodeRepository           $promoCodeRepository,
  32.         OrderService                  $orderService
  33.     ): Response
  34.     {
  35.         $now = new \DateTime();
  36.         if ($cartService->getExpired() < $now) {
  37.             $cartService->clearConsultations();
  38.         }
  39.         $promoCodes $cartService->getPromoCodes();
  40.         $data = [];
  41.         $cartConsultations $cartService->getConsultations();
  42.         foreach ($consultationRepository->findBy(['id' => $cartService->getConsultationsIds()], ['startDate' => 'asc']) as $consultation) {
  43.             $id $consultation->getSpeaker()->getId();
  44.             $data[$id]['speaker'] = $consultation->getSpeaker();
  45.             $data[$id]['consultations'][] = array_merge($cartConsultations[$id][$consultation->getId()], [
  46.                 'entity' => $consultation,
  47.             ]);
  48.         }
  49.         // Get services
  50.         foreach ($cartService->getServices() as $idSpeaker => $service) {
  51.             $data[$idSpeaker]['speaker'] = $this->getDoctrine()->getRepository(Speaker::class)->find($idSpeaker);
  52.             $data[$idSpeaker]['services'] = $service;
  53.         }
  54.         // Get premium package
  55.         foreach ($cartService->getPremiumPackages() as $idSpeaker => $premiumPackage) {
  56.             $data[$idSpeaker]['speaker'] = $this->getDoctrine()->getRepository(Speaker::class)->find($idSpeaker);
  57.             $data[$idSpeaker]['packages'] = $premiumPackage;
  58.         }
  59.         // Get courses
  60.         foreach ($cartService->getCourses() as $idSpeaker => $courses) {
  61.             $data[$idSpeaker]['speaker'] = $this->getDoctrine()->getRepository(Speaker::class)->find($idSpeaker);
  62.             $data[$idSpeaker]['courses'] = $this->getDoctrine()->getRepository(SpeakerCourse::class)->findBy(['id' => $courses]);
  63.         }
  64.         ksort($data);
  65.         if (empty($data)) {
  66.             return $this->render('cart/empty.html.twig', []);
  67.         }
  68.         // Add discount service
  69.         foreach ($data as $id => $item) {
  70.             $discountService = new DiscountService();
  71.             $discountService->setConsultationsCount(count($item['consultations'] ?? []));
  72.             if (isset($promoCodes[$id]) && $promo $promoCodeRepository->find($promoCodes[$id])) {
  73.                 $discountService->setPromoCode($promo);
  74.             }
  75.             $data[$id]['discountService'] = $discountService;
  76.         }
  77.         return $this->render('cart/index.html.twig', ['data' => $data'timezone' => $cartService->getTimeZone(), 'total' => $orderService->getTotal()]);
  78.     }
  79.     /**
  80.      * @Route("/add-cons", name="cart.add")
  81.      */
  82.     public function addConsultation(TranslatorInterface $translatorRequest $requestCartService $cartService): JsonResponse
  83.     {
  84.         //if (!$cartService->freezeConsultation($request->get('id'), $request->get('type'))) {
  85.         if (!$cartService->freezeConsultation($request->get('id'), 'price')) {
  86.             return $this->json([
  87.                 'success' => false,
  88.                 'message' => $translator->trans('message.cart_info_consultation_ordered'),
  89.             ]);
  90.         }
  91.         return $this->json([
  92.             'success' => true,
  93.             'min'     => CartConfig::FREEZE_MIN,
  94.             'message' => sprintf($translator->trans('message.cart_info_consultation'), CartConfig::FREEZE_MIN),
  95.         ]);
  96.     }
  97.     /**
  98.      * @Route("/info", name="cart.info")
  99.      */
  100.     public function info(CartService $cartService): JsonResponse
  101.     {
  102.         $now = new \DateTime();
  103.         $expired $cartService->getExpired();
  104.         if ($expired $now) {
  105.             $cartService->clearConsultations();
  106.         }
  107.         return $this->json([
  108.             'count'   => $cartService->getItemsCount(),
  109.             'expired' => $expired $expired->getTimestamp() - $now->getTimestamp() : 0,
  110.         ]);
  111.     }
  112.     /**
  113.      * @Route("/add-promo", name="cart.add_promo")
  114.      */
  115.     public function addPromoCode(
  116.         Request             $request,
  117.         CartService         $cartService,
  118.         PromoCodeRepository $repository,
  119.         TranslatorInterface $translator
  120.     ): JsonResponse
  121.     {
  122.         $promo $repository->findOneBy(['code' => $request->get('code'), 'speaker' => (int)$request->get('speaker')]);
  123.         if ($promo) {
  124.             $cartService->addPromoCode($promo);
  125.             return $this->json([
  126.                 'success' => true,
  127.                 'message' => sprintf($translator->trans('message.promo_code_applied'), $promo->getAmount() . '%')
  128.             ]);
  129.         }
  130.         // Try to remove previews promo code
  131.         $cartService->removePromoCodeBySpeakerId((int)$request->get('speaker'));
  132.         return $this->json([
  133.             'success' => false,
  134.             'message' => sprintf($translator->trans('message.promo_code_not_found'), $request->get('code'))
  135.         ]);
  136.     }
  137.     /**
  138.      * @Route("/delete/{consultation}", name="cart.remove_cons")
  139.      * @param SpeakerConsultation $consultation
  140.      * @param CartService $cartService
  141.      * @return RedirectResponse
  142.      */
  143.     public function removeConsultation(SpeakerConsultation $consultationCartService $cartService): RedirectResponse
  144.     {
  145.         $cartService->removeConsultation($consultation);
  146.         return $this->redirectToRoute('cart.index');
  147.     }
  148.     /**
  149.      * @Route("/quick-add-consultation/{speaker}", name="cart.quick_add_cons")
  150.      * @param Speaker $speaker
  151.      * @param CartService $cartService
  152.      * @param PromoCodeRepository $repository
  153.      * @return Response
  154.      */
  155.     public function quickAddConsultation(Speaker $speakerCartService $cartServicePromoCodeRepository $repository): Response
  156.     {
  157.         $promo $cartService->getPromoCodes();
  158.         return $this->render('cart/add_cons.html.twig', [
  159.             'speaker'   => $speaker,
  160.             'orderForm' => $this->createForm(SpeakerOrderType::class)->createView(),
  161.             'promoVal'  => isset($promo[$speaker->getId()]) ? $repository->find($promo[$speaker->getId()]) : '',
  162.         ]);
  163.     }
  164.     /**
  165.      * @Route("/quick-edit-consultation/{consultation}", name="cart.quick_edit_cons")
  166.      * @param SpeakerConsultation $consultation
  167.      * @param CartService $cartService
  168.      * @return RedirectResponse
  169.      */
  170.     public function changeConsultation(SpeakerConsultation $consultationCartService $cartService): RedirectResponse
  171.     {
  172.         $cartService->removeConsultation($consultation);
  173.         return $this->redirectToRoute('cart.quick_add_cons', ['speaker' => $consultation->getSpeaker()->getId()]);
  174.     }
  175.     /**
  176.      * @Route("/add-service", name="cart.add_service")
  177.      * @param Request $request
  178.      * @param CartService $cartService
  179.      * @return RedirectResponse
  180.      */
  181.     public function addService(Request $requestCartService $cartService): RedirectResponse
  182.     {
  183.         $code $request->get('service');
  184.         $const strtoupper($code);
  185.         if (!defined('App\Config\PremiumServiceConfig::' $const)) {
  186.             $this->addFlash('error''message.service_not_found');
  187.             return $this->redirectToRoute('cabinet.premium.index');
  188.         }
  189.         $cartService->setTimeZone($this->getUser()->getTimezone())->addService($code$this->getUser()->getSpeaker());
  190.         return $this->redirectToRoute('cart.index');
  191.     }
  192.     /**
  193.      * @Route("/remove-service/{code}", name="cart.remove_service")
  194.      * @param $code
  195.      * @param CartService $cartService
  196.      * @return RedirectResponse
  197.      */
  198.     public function removeService($codeCartService $cartService): RedirectResponse
  199.     {
  200.         $cartService->removeService($code$this->getUser()->getSpeaker());
  201.         return $this->redirectToRoute('cart.index');
  202.     }
  203.     /**
  204.      * @Route("/add-premium", name="cart.add_premium_package")
  205.      * @param Request $request
  206.      * @param CartService $cartService
  207.      * @return RedirectResponse
  208.      */
  209.     public function addPremiumPackage(Request $requestCartService $cartService): RedirectResponse
  210.     {
  211.         $code $request->get('package');
  212.         $const strtoupper($code);
  213.         if (!defined('App\Config\PremiumPackageConfig::' $const)) {
  214.             $this->addFlash('error''message.premium_package_not_found');
  215.             return $this->redirectToRoute('cabinet.premium.index');
  216.         }
  217.         $cartService->setTimeZone($this->getUser()->getTimezone())->addPremiumPackage($code$this->getUser()->getSpeaker());
  218.         return $this->redirectToRoute('cart.index');
  219.     }
  220.     /**
  221.      * @Route("/remove-premium/{code}", name="cart.remove_premium_package")
  222.      * @param $code
  223.      * @param CartService $cartService
  224.      * @return RedirectResponse
  225.      */
  226.     public function removePremiumPackage($codeCartService $cartService): RedirectResponse
  227.     {
  228.         $cartService->removePremiumPackage($code$this->getUser()->getSpeaker());
  229.         return $this->redirectToRoute('cart.index');
  230.     }
  231.     /**
  232.      * @Route("/add-course/{course}", name="cart.add_course")
  233.      * @param SpeakerCourse $course
  234.      * @param CartService $cartService
  235.      * @param TranslatorInterface $translator
  236.      * @return JsonResponse
  237.      */
  238.     public function addCourse(SpeakerCourse $courseCartService $cartServiceTranslatorInterface $translator): JsonResponse
  239.     {
  240.         $courses $cartService->getCourses();
  241.         if (!empty($courses[$course->getSpeaker()->getId()]) && in_array($course->getId(), $courses[$course->getSpeaker()->getId()])) {
  242.             return $this->json([
  243.                 'success' => false,
  244.                 'message' => $translator->trans('message.cart_course_ordered'),
  245.             ]);
  246.         }
  247.         $cartService->addCourse($course);
  248.         return $this->json([
  249.             'success' => true,
  250.             'message' => $translator->trans('message.cart_course_added'),
  251.         ]);
  252.     }
  253.     /**
  254.      * @Route("/remove-course/{course}", name="cart.remove_course")
  255.      * @param SpeakerCourse $course
  256.      * @param CartService $cartService
  257.      * @return RedirectResponse
  258.      */
  259.     public function removeCourse(SpeakerCourse $courseCartService $cartService): RedirectResponse
  260.     {
  261.         $cartService->removeCourse($course);
  262.         return $this->redirectToRoute('cart.index');
  263.     }
  264. }