src/Controller/Front/CatalogController.php line 179

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\AttributValue;
  4. use App\Entity\Cart;
  5. use App\Entity\CartElement;
  6. use App\Entity\Category;
  7. use App\Entity\Creation;
  8. use App\Entity\FeatureFamily;
  9. use App\Entity\Model;
  10. use App\Entity\ModelProductMenuMaker;
  11. use App\Entity\Product;
  12. use App\Entity\ProductPrice;
  13. use App\Entity\User;
  14. use App\Service\CartService;
  15. use App\Service\CatalogService;
  16. use App\Service\ModuleService;
  17. use App\Service\ProductService;
  18. use App\Service\QrCodeService;
  19. use App\Service\Tools;
  20. use Doctrine\ORM\EntityManagerInterface;
  21. use Exception;
  22. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  23. use Symfony\Component\HttpFoundation\JsonResponse;
  24. use Symfony\Component\HttpFoundation\RedirectResponse;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpFoundation\Response;
  27. use Symfony\Component\HttpFoundation\Session\Session;
  28. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  29. use Symfony\Component\Routing\Annotation\Route;
  30. /**
  31.  * Class CatalogController
  32.  * @package App\Controller\Front
  33.  * @Route(path="/catalog", name="catalog_", options={"expose"=true})
  34.  */
  35. class CatalogController extends AbstractController
  36. {
  37.     public string $module;
  38.     private ModuleService $moduleService;
  39.     private CatalogService $catalogService;
  40.     private ProductService $productService;
  41.     private CartService $cartService;
  42.     private Tools $tools;
  43.     private EntityManagerInterface $em;
  44.     private QrCodeService $qrCodeService;
  45.     public function __construct(
  46.         ModuleService $moduleService,
  47.         CatalogService $catalogService,
  48.         ProductService $productService,
  49.         CartService $cartService,
  50.         Tools $tools,
  51.         EntityManagerInterface $em,
  52.         QrCodeService $qrCodeService,
  53.     ) {
  54.         $this->module 'catalog';
  55.         $this->moduleService $moduleService;
  56.         $this->catalogService $catalogService;
  57.         $this->productService $productService;
  58.         $this->cartService $cartService;
  59.         $this->tools $tools;
  60.         $this->em $em;
  61.         $this->qrCodeService $qrCodeService;
  62.     }
  63.     /**
  64.      * @return Response
  65.      * @Route("/cannot/order", name="cannot_order")
  66.      */
  67.     public function cannotOrder(): Response
  68.     {
  69.         return $this->redirectToRoute('app_index');
  70.         //                return $this->render('front/catalog/cannotOrder.html.twig');
  71.     }
  72.     /**
  73.      * @param User|null $customer
  74.      * @return Response
  75.      * @Route("/{customer}", name="index")
  76.      */
  77.     public function index(User $customer null): Response
  78.     {
  79.         if (!$this->moduleService->access($this->module) and !$this->moduleService->access('catalog_wallet''command')) {
  80.             return $this->redirectToRoute('app_index');
  81.         }
  82.         /** @var User $user */
  83.         $user $this->getUser();
  84.         if (!$user->getCanOrder()) {
  85.             return $this->redirectToRoute('catalog_cannot_order');
  86.         }
  87.         if (!is_null($customer)) {
  88.             if (!$customer->getCanOrder()) {
  89.                 return $this->redirectToRoute('catalog_cannot_order');
  90.             }
  91.         }
  92.         $categoriesTop $this->em->getRepository(Category::class)->getRootNodes('position');
  93.         if ($customer) {
  94.             $session = new Session();
  95.             $session->set('customer'$customer->getId());
  96.         }
  97.         return $this->render('front/catalog/index.html.twig', [
  98.             'categories' => $categoriesTop,
  99.         ]);
  100.     }
  101.     /**
  102.      * @param Category $category
  103.      * @return Response
  104.      * @Route("/category/{category}", name="category", requirements={"category"="\d+"})
  105.      */
  106.     public function category(Category $category): Response
  107.     {
  108.         if (!$this->catalogService->isCategoryValid($category)) {
  109.             return $this->redirectToRoute('app_index');
  110.         }
  111.         /** @var User $user */
  112.         $user $this->getUser();
  113.         if (!$user->getCanOrder()) {
  114.             return $this->redirectToRoute('catalog_cannot_order');
  115.         }
  116.         $marketingZone $this->catalogService->getMarketingZone($category);
  117.         //Si aucune zone marketing, on cherche une zone sur la catégorie parent
  118.         if (!$marketingZone && $category->getParent()) {
  119.             $marketingZone $this->catalogService->getMarketingZone($category->getParent());
  120.         }
  121.         return $this->render('front/catalog/category.html.twig', [
  122.             'category' => $category,
  123.             'marketingZone' => $marketingZone,
  124.         ]);
  125.     }
  126.     /**
  127.      * @param Product $product
  128.      * @return RedirectResponse|Response
  129.      * @Route("/product/{product}", name="product", requirements={"product"="\d+"})
  130.      */
  131.     public function product(Product $product): RedirectResponse|Response
  132.     {
  133.         if (!$this->productService->isCommandable($product)) {
  134.             return $this->redirectToRoute('app_index');
  135.         }
  136.         /** @var User $user */
  137.         $user $this->getUser();
  138.         if (!$user->getCanOrder()) {
  139.             return $this->redirectToRoute('catalog_cannot_order');
  140.         }
  141.         $session = new Session();
  142.         return $this->render('front/catalog/product.html.twig', [
  143.             'product' => $product,
  144.             'customerSession' => $session->get('customer'),
  145.             'economicalTurnover' => $this->tools->getSettingByName('ECONOMICAL_TURNOVER')
  146.         ]);
  147.     }
  148.     /**
  149.      * @param Product $product
  150.      * @param Request $request
  151.      * @return JsonResponse
  152.      * @throws Exception
  153.      * @Route("/product/{product}/feature-values", name="product_feature_values", requirements={"product"="\d+"}, options={"expose"=true})
  154.      */
  155.     public function productFeatureValues(Product $productRequest $request): JsonResponse
  156.     {
  157.         if (!$this->productService->isCommandable($product)) {
  158.             return new JsonResponse([
  159.                 'success' => false,
  160.                 'url' => $this->generateUrl('app_index'),
  161.             ]);
  162.         }
  163.         $productFeatureValues $this->productService->getProductFeatureValuesByProduct($product);
  164.         /** @var Model $model */
  165.         $model $this->em->getRepository(Model::class)->find($request->request->get('model'));
  166.         if (!is_null($model)) {
  167.             $modelProductFeatureValues = [];
  168.             /** @var ModelProductMenuMaker $modelProductMenuMaker */
  169.             foreach ($model->getModelProductMenuMakers() as $modelProductMenuMaker) {
  170.                 if ($modelProductMenuMaker->getProductMenuMaker()->getProduct() === $product) {
  171.                     if ($modelProductMenuMaker->isProductMenuMakerHaveFile() or $modelProductMenuMaker->isEquivalentProductMenuMakerHaveFile()) {
  172.                         $modelProductFeatureValues[] = $modelProductMenuMaker->getProductMenuMaker()->productFeatureValueFormat()->getPosition() . '~' $modelProductMenuMaker->getProductMenuMaker()->productFeatureValueFormat()->getId();
  173.                     }
  174.                 }
  175.             }
  176.             foreach ($productFeatureValues as $key => $productFeatureValue) {
  177.                 if (!in_array($key$modelProductFeatureValues)) {
  178.                     unset($productFeatureValues[$key]);
  179.                 }
  180.             }
  181.         }
  182.         return new JsonResponse([
  183.             'success' => true,
  184.             'productFeatureValues' => $productFeatureValues,
  185.         ]);
  186.     }
  187.     /**
  188.      * @param Product $product
  189.      * @param Request $request
  190.      * @return JsonResponse
  191.      * @Route("/product/{product}/prices", name="product_prices", requirements={"product"="\d+"}, options={"expose"=true})
  192.      * @throws Exception
  193.      */
  194.     public function productPrices(Product $productRequest $request): JsonResponse
  195.     {
  196.         if (!$this->productService->isCommandable($product)) {
  197.             return new JsonResponse([
  198.                 'success' => false,
  199.                 'url' => $this->generateUrl('app_index'),
  200.             ]);
  201.         }
  202.         $productFeatureValues = (!is_null($request->request->get('productFeatureValues')) ? $request->request->get('productFeatureValues') : []);
  203.         $attributValues = (!is_null($request->request->get('attributValues')) ? $request->request->get('attributValues') : []);
  204.         $isFormatVariable = (!is_null($request->request->get('isFormatVariable')) ? $request->request->get('isFormatVariable') : false);
  205.         $width = ($request->request->get('width') ? $request->request->get('width') : null);
  206.         $height = ($request->request->get('height') ? $request->request->get('height') : null);
  207.         return new JsonResponse([
  208.             'success' => true,
  209.             'template' => $this->renderView('front/catalog/product_price.html.twig', [
  210.                 'quantities' => $this->productService->calculPriceByProductPrices($product$productFeatureValues$attributValues$isFormatVariable$width$height),
  211.             ]),
  212.         ]);
  213.     }
  214.     /**
  215.      * @param Product $product
  216.      * @param User $customer
  217.      * @param Request $request
  218.      * @return JsonResponse
  219.      * @throws Exception|TransportExceptionInterface
  220.      * @Route("/product/{product}/add-cart/{customer}", name="product_add_cart", requirements={"product"="\d+", "customer"="\d+"})
  221.      */
  222.     public function addCart(Product $productUser $customerRequest $request): JsonResponse
  223.     {
  224.         if (!$this->productService->isCommandable($product)) {
  225.             return new JsonResponse([
  226.                 'success' => false,
  227.                 'url' => $this->generateUrl('app_index'),
  228.             ]);
  229.         }
  230.         $customerEmail $request->request->get('customerEmail');
  231.         if (!empty($customerEmail)) {
  232.             $customer->setEmail($customerEmail);
  233.             if (is_null($customer->getQrCodeMizogoo())) {
  234.                 $res $this->qrCodeService->mizogooQrCode($customer);
  235.                 if (!$res['success']) {
  236.                     return new JsonResponse($res);
  237.                 }
  238.             }
  239.             $this->em->persist($customer);
  240.             $this->em->flush();
  241.         }
  242.         $model $request->request->get('model');
  243.         if (!is_null($model)) {
  244.             /** @var Model $model */
  245.             $model $this->em->getRepository(Model::class)->find($model);
  246.         }
  247.         $attributValues = (!is_null($request->request->get('attributValues')) ? $request->request->get('attributValues') : []);
  248.         $quantity = (!is_null($request->request->get('quantity')) ? $request->request->get('quantity') : 1);
  249.         $productPriceId = (!is_null($request->request->get('productPriceId')) ? $request->request->get('productPriceId') : null);
  250.         $width $request->request->get('width');
  251.         $height $request->request->get('height');
  252.         /** @var ProductPrice $productPrice */
  253.         $productPrice $this->em->getRepository(ProductPrice::class)->findOneBy([
  254.             'id' => $productPriceId
  255.         ]);
  256.         if (!is_null($productPrice)) {
  257.             /** @var Cart $cart */
  258.             $cart $customer->getCurrentCart();
  259.             if (!$cart) {
  260.                 $cart $this->cartService->create($customer);
  261.             }
  262.             $cartElement = new CartElement();
  263.             $cartElement->setCart($cart);
  264.             $cartElement->setProduct($product);
  265.             $cartElement->setProductPrice($productPrice);
  266.             $cartElement->setQuantity($quantity);
  267.             if ($product->isCustomizable()) {
  268.                 $creation = new Creation();
  269.                 $creation->setUser($customer);
  270.                 $creation->setProduct($product);
  271.                 $creation->setModel($model);
  272.                 if ($width) {
  273.                     $creation->setWidth($width);
  274.                 }
  275.                 if ($height) {
  276.                     $creation->setHeight($height);
  277.                 }
  278.                 foreach ($productPrice->getProductFeatureValues() as $productFeatureValue) {
  279.                     if ($productFeatureValue->getProductFeature()->getFeature()->getFeatureFamily()->getConstName() == FeatureFamily::CUSTOMIZABLE) {
  280.                         $creation->addProductFeatureValue($productFeatureValue);
  281.                     }
  282.                 }
  283.                 $this->em->persist($creation);
  284.                 $cartElement->setCreation($creation);
  285.             } elseif ($product->isClassic()) {
  286.                 $creation = new Creation();
  287.                 $creation->setUser($customer);
  288.                 $creation->setProduct($product);
  289.                 foreach ($productPrice->getProductFeatureValues() as $productFeatureValue) {
  290.                     if ($productFeatureValue->getProductFeature()->getFeature()->getFeatureFamily()->getConstName() == FeatureFamily::CUSTOMIZABLE) {
  291.                         $creation->addProductFeatureValue($productFeatureValue);
  292.                     }
  293.                 }
  294.                 $this->em->persist($creation);
  295.                 $cartElement->setCreation($creation);
  296.             }
  297.             if ($attributValues) {
  298.                 foreach ($attributValues as $value) {
  299.                     /** @var AttributValue $attributValue */
  300.                     $attributValue $this->em->getRepository(AttributValue::class)->find($value);
  301.                     if (!is_null($attributValue)) {
  302.                         $cartElement->addAttributValue($attributValue);
  303.                     }
  304.                 }
  305.             }
  306.             $cart->addCartElement($cartElement);
  307.             $this->em->persist($cartElement);
  308.             $this->em->flush();
  309.             $url $this->generateUrl('cart_index', ['customer' => $customer->getId()]);
  310.             if ($cartElement->getCreation()) {
  311.                 if ($product->getReference() === Product::PRODUCT_REF_SF or $product->getReference() === Product::PRODUCT_REF_MF) {
  312.                     $url $this->generateUrl('creation_classic_index', ['creation' => $cartElement->getCreation()->getId()]);
  313.                 } else {
  314.                     if ($product->isOldBuilder()) {
  315.                         $url $this->generateUrl('creation_menumaker_menumaker', ['creation' => $cartElement->getCreation()->getId(), 'cartElement' => $cartElement->getId()]);
  316.                     } else {
  317.                         $url $this->generateUrl('menubuilder_index', ['creation' => $cartElement->getCreation()->getId()]);
  318.                     }
  319.                 }
  320.             }
  321.             return new JsonResponse([
  322.                 'success' => true,
  323.                 'url' => $url,
  324.             ]);
  325.         }
  326.         return new JsonResponse([
  327.             'success' => false,
  328.         ]);
  329.     }
  330. }