src/Controller/CartController.php line 47

Open in your IDE?
  1. <?php 
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Twig\Environment;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Sylius\Component\Product\Repository\ProductVariantRepositoryInterface;
  10. use Sylius\Component\Channel\Context\ChannelContextInterface;
  11. use App\Services\PPMCErpSynchroService;
  12. use Symfony\Component\Process\Process;
  13. use Symfony\Component\Process\Exception\ProcessTimedOutException;
  14. use Carbon\Carbon;
  15. use Symfony\Component\Messenger\MessageBusInterface;
  16. use App\Message\SyncCustomer;
  17. // use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. class CartController /*extends AbstractController*/
  19. {
  20.     protected $twigEnvironment;
  21.     protected $container;
  22.     protected $productVariantRepository;
  23.     protected $ppmcErpSynchroService;
  24.     protected $bus;
  25.     public function __construct(
  26.         Environment $twigEnvironment,
  27.         ContainerInterface $container,
  28.         ProductVariantRepositoryInterface $productVariantRepository,
  29.         PPMCErpSynchroService $ppmcErpSynchroService,
  30.         MessageBusInterface $bus
  31.     ){
  32.         $this->twigEnvironment $twigEnvironment;
  33.         $this->container $container;
  34.         $this->productVariantRepository $productVariantRepository;
  35.         $this->ppmcErpSynchroService $ppmcErpSynchroService;
  36.         $this->bus $bus;
  37.         
  38.     }
  39.     public function popupAction(Request $request): Response
  40.     {        
  41.         // $this->payLog($request, "");
  42.         $cart =  $this->container->get('sylius.context.cart')->getCart();  
  43.         $amount 0;
  44.         $total 0;
  45.         $diff 0;
  46.         $progress 0;
  47.         $cdts $this->container->get('sylius.repository.promotion')->findOneByCode('FREE_DELIVERY')->getRules();
  48.         foreach ($cdts as $cdt){
  49.             if ($cdt->getType() == 'item_total') {
  50.                 $amount $cdt->getConfiguration()['ppmc']['amount'];
  51.                 $total $cart->getItemsTotal();   
  52.                 $diff $amount-$total;   
  53.                 $progress $total $amount 100;        
  54.             }
  55.         }
  56.         // Produits dans "Je craque aussi pour"
  57.         // Des accessoires cheveux 
  58.         // de nouvelles collections uniquement
  59.         // 7 produits max aleatoire
  60.         // et 1 chouchou aléatoire peu importe la collection
  61.         $moreVariants $this->container->get('sylius.repository.product_variant')
  62.         ->findVariantsForCartPageIWantThisToo(
  63.             $this->container->get('sylius.context.channel')->getChannel(),
  64.             $this->container->get('sylius.context.locale')->getLocaleCode()
  65.         );
  66.         //On vinet associé une adresse si la personne est connectée
  67.         // if(null != $cart->getCustomer() && null != $cart->getCustomer()->getId() && $cart->getBillingAddress() == null) {
  68.         //     // dump($cart->getCustomer());
  69.         //     $addresses = $cart->getCustomer()->getAddresses();
  70.         //     $this->container->get('doctrine')->getManager()->initializeObject($addresses);
  71.         //     dump($addresses);
  72.         // //     $cart->setBillingAddress($cart->getCustomer()->getAddresses()[0]);
  73.         // //     $cart->setShippingAddress($cart->getCustomer()->getAddresses()[0]);
  74.         // //     $cartManager = $this->container->get('sylius.manager.order');
  75.         // //     $cartManager->persist($cart);
  76.         // //     $cartManager->flush();
  77.         // }
  78.         // $this->container->get('doctrine')->getManager()->initializeObject($cart);
  79.         // dump($cart);
  80.        
  81.         
  82.         return new Response(
  83.             $this->twigEnvironment->render('@SyliusShop/Webapic/panier.html.twig', [
  84.                 'cart' => $cart,
  85.                 'amount' => $amount,
  86.                 'total' => $total,
  87.                 'diff' => $diff,
  88.                 'progress' => $progress,
  89.                 'moreVariants' => $moreVariants
  90.             ])
  91.         );
  92.     }
  93.     public function addToCartAjax(ChannelContextInterface $channelCtxRequest $request)
  94.     {
  95.         // POST : 
  96.         // data[<id product variant>] = <quantity>
  97.         // data[<id product variant>] = <quantity>
  98.         // etc.
  99.         $data = (array) $request->request->all();
  100.         if (empty($data) == true) {
  101.             return new JsonResponse([
  102.                 'success' => true
  103.             ]);
  104.         }
  105.         // Selectionne le canal pour calculer le prix
  106.         $channel $channelCtx->getChannel();
  107.         //gestion champ carte kdo
  108.         $kdoFields = [];
  109.         if(isset($data['kdo_mail'])) {
  110.             $kdoFields['kdo_mail'] = $data['kdo_mail'];
  111.             $kdoFields['kdo_lastname'] = $data['kdo_lastname'];
  112.             $kdoFields['kdo_firstname'] = $data['kdo_firstname'];
  113.             $kdoFields['kdo_message'] = $data['kdo_message'];
  114.             unset($data['kdo_mail']);
  115.             unset($data['kdo_lastname']);
  116.             unset($data['kdo_firstname']);
  117.             unset($data['kdo_message']);
  118.         }
  119.         // Ajout au panier
  120.         $cart $this->container->get('sylius.context.cart')->getCart();
  121.         foreach ($data as $variantId => $quantity) {
  122.             $quantity round(floatval($quantity), 1);
  123.             $variant $this->productVariantRepository->findOneById($variantId);
  124.             if (is_null($variant) == false && $quantity 0) {
  125.                 if($variant->countItemStock() <= || $variant->getIsBlocked()){
  126.                 // if($variant->getOnHand() - $variant->getOnHold() <= 5){
  127.                     return new JsonResponse([
  128.                         'stock' => 0
  129.                     ]);
  130.                 }
  131.                 
  132.                 $orderItem $this->container->get('sylius.repository.order_item')->findOneBy([
  133.                     'variant' => $variant,
  134.                     'order' => $cart
  135.                 ]);
  136.     
  137.                 if (!empty($orderItem) && $variant->getProduct()->getConditioning() != 'per_meter_each_0_5_meter' && $variant->getProduct()->getConditioning() != 'card_kdo_per_euros_each_10_euros') {
  138.                    $quantity  $orderItem->getQuantity() + $quantity;
  139.                    if($quantity 10){                       
  140.                         $quantity 10;
  141.                     }
  142.                     $this->container->get('sylius.order_item_quantity_modifier')->modify($orderItem, (int) $quantity);
  143.                     
  144.                 }else{                    
  145.                     // if(!empty($orderItem) &&  $variant->getProduct()->getConditioning() == 'per_meter_each_0_5_meter'){
  146.                     //     // $this->container->get('sylius.order_modifier')->removeFromOrder($cart, $orderItem);                        
  147.                     //     // $this->container->get('sylius.order_item_quantity_modifier')->modify($orderItem, 0);
  148.                     // }
  149.                     if($variant->getProduct()->getConditioning() == 'per_meter_each_0_5_meter') {
  150.                         if($quantity 10){
  151.                             $quantity 10;
  152.                         }   
  153.                     }
  154.                    
  155.                     $this->addOrderItemToCart($channel$cart$variant$quantitynull$kdoFields);
  156.                 }
  157.                
  158.             }
  159.         }
  160.     
  161.         $cartManager $this->container->get('sylius.manager.order');
  162.         $cartManager->persist($cart);
  163.         $cartManager->flush();
  164.         $isFine true;
  165.         // $isFine =$this->stockVerification($cart, $request);
  166.         // Valorisation du panier côté Generix
  167.         $this->applyReducedPricesToCart($channel$cart);
  168.         $cart $this->container->get('sylius.context.cart')->getCart();
  169.         // dump($cart);
  170.         
  171.         $amount 0;
  172.         $total 0;
  173.         $diff 0;
  174.         $progress 0;
  175.         $cdts $this->container->get('sylius.repository.promotion')->findOneByCode('FREE_DELIVERY')->getRules();
  176.         foreach($cdts as $cdt){
  177.            
  178.             if( $cdt->getType() == 'item_total'){
  179.                 $amount $cdt->getConfiguration()['ppmc']['amount'];
  180.                 $total $cart->getItemsTotal();   
  181.                 $diff $amount-$total;   
  182.                 $progress $total $amount 100;        
  183.             }
  184.         }       
  185.         $fromPagepanier $request->query->get('pagepanier''');
  186.         if(!empty($fromPagepanier)){
  187.             return new JsonResponse([
  188.                 'produits' => $this->twigEnvironment->render('@SyliusShop/Webapic/panier/pagepanier-produits.html.twig', [
  189.                     'cart' => $cart,
  190.                 ]),
  191.                 'total' => $this->twigEnvironment->render('@SyliusShop/Cart/Summary/_totals.html.twig', [
  192.                      'cart' => $cart,
  193.                 ]),
  194.                 'livraison' => $this->twigEnvironment->render('@SyliusShop/Webapic/panier/panier-livraison.html.twig', [
  195.                     'cart' => $cart,
  196.                     'amount' => $amount,
  197.                     'total' => $total,
  198.                     'diff' => $diff,
  199.                     'progress' => $progress,
  200.                 ]),
  201.                 'widgettop' => $this->twigEnvironment->render('@SyliusShop/Cart/_widget.html.twig', [
  202.                     'cart' => $cart,
  203.                 ]),
  204.                 // 'isFine' => true
  205.                 'isFine' => $isFine
  206.             ]);
  207.         }
  208.         return new JsonResponse([
  209.             'produits' => $this->twigEnvironment->render('@SyliusShop/Webapic/panier/panier-produits.html.twig', [
  210.                 'cart' => $cart,
  211.             ]),
  212.             'total' => $this->twigEnvironment->render('@SyliusShop/Webapic/panier/panier-total.html.twig', [
  213.                  'cart' => $cart,
  214.             ]),
  215.             'top' => $this->twigEnvironment->render('@SyliusShop/Webapic/panier/panier-top.html.twig', [
  216.                 'cart' => $cart,
  217.             ]),
  218.             'livraison' => $this->twigEnvironment->render('@SyliusShop/Webapic/panier/panier-livraison.html.twig', [
  219.                 'cart' => $cart,
  220.                 'amount' => $amount,
  221.                 'total' => $total,
  222.                 'diff' => $diff,
  223.                 'progress' => $progress,
  224.             ]),
  225.             'widgettop' => $this->twigEnvironment->render('@SyliusShop/Cart/_widget.html.twig', [
  226.                 'cart' => $cart,
  227.             ]),
  228.             // 'isFine' => true
  229.             'isFine' => $isFine
  230.         ]);
  231.     }
  232.     public function updateCartAction(ChannelContextInterface $channelCtxRequest $request$id)
  233.     {
  234.         
  235.         $cart $this->container->get('sylius.context.cart')->getCart();
  236.         $repository $this->container->get('sylius.repository.order_item');
  237.         $orderItem $repository->findOneByIdAndCartId($id$cart->getId());
  238.         // dump($orderItem);
  239.         if (!empty($orderItem)) {
  240.             $quantity min(99max(0intval($request->request->get('quantity'1))));
  241.             if($quantity 10){
  242.                 $quantity 10;
  243.             }
  244.             if($quantity == 0){
  245.                 $this->container->get('sylius.order_modifier')->removeFromOrder($cart$orderItem);
  246.             }else{
  247.                
  248.                 $this->container->get('sylius.order_item_quantity_modifier')->modify($orderItem$quantity);
  249.             }
  250.             $this->container->get('sylius.order_item_quantity_modifier')->modify($orderItem$quantity);
  251.            
  252.         }else{
  253.             $orderItem $this->container->get('sylius.factory.order_item')->createNew();
  254.             $orderItem->setVariant($this->productVariantRepository->find($id));
  255.             $this->container->get('sylius.order_item_quantity_modifier')->modify($orderItem1);
  256.             $this->container->get('sylius.order_modifier')->addToOrder($cart$orderItem);
  257.             $cart->addItem($orderItem);
  258.             
  259.         }
  260.         $cartManager $this->container->get('sylius.manager.order');
  261.         $cartManager->persist($cart);
  262.         $cartManager->flush();
  263.         // echo 'coucou';
  264.         // dump($orderItem);
  265.         // exit();
  266.         // Selectionne le canal pour calculer le prix
  267.         $channel $channelCtx->getChannel();
  268.         $isFine true;
  269.         // $isFine =$this->stockVerification($cart, $request);
  270.         // Valorisation du panier côté Generix
  271.         $this->applyReducedPricesToCart($channel$cart);
  272.         $cart $this->container->get('sylius.repository.order')->findOneBy([
  273.             'id' => $cart->getId()
  274.         ]);
  275.         // $this->container->get('sylius.context.cart')->getCart();
  276.         // $this->container->get('sylius.context.cart')->setCart($cart);
  277.         // $cartManager = $this->container->get('sylius.manager.order');
  278.         // $cartManager->persist($cart);
  279.         // $cartManager->flush();
  280.         // $cart = $this->container->get('sylius.context.cart')->getCart();
  281.         $amount 0;
  282.         $total 0;
  283.         $diff 0;
  284.         $progress 0;
  285.         $cdts $this->container->get('sylius.repository.promotion')->findOneByCode('FREE_DELIVERY')->getRules();
  286.         foreach($cdts as $cdt){
  287.            
  288.             if( $cdt->getType() == 'item_total'){
  289.                 $amount $cdt->getConfiguration()['ppmc']['amount'];
  290.                 $total $cart->getItemsTotal();   
  291.                 $diff $amount-$total;   
  292.                 $progress $total $amount 100;        
  293.             }
  294.         }       
  295.         if($request->request->get('pagepanier')){      
  296.             $configuration $this->requestConfigurationFactory->create($this->metadata$request);
  297.             if (!$configuration->isHtmlRequest()) {
  298.                 return $this->viewHandler->handle($configurationView::create($cart));
  299.             }
  300.             $form $this->resourceFormFactory->create($configuration$cart);
  301.             
  302.             return new JsonResponse([
  303.                 'pagepaniertotal' => $this->twigEnvironment->render('@SyliusShop/Webapic/panier/pagepanier-right.html.twig', [
  304.                     'cart' => $cart,
  305.                     'form' => $form->createView(),
  306.                     'amount' => $amount,
  307.                     'total' => $total,
  308.                     'diff' => $diff,
  309.                     'progress' => $progress,
  310.                 ]),
  311.             ]);
  312.         }else{
  313.             return new JsonResponse([
  314.                 'produits' => $this->twigEnvironment->render('@SyliusShop/Webapic/panier/panier-produits.html.twig', [
  315.                     'cart' => $cart,
  316.                 ]),
  317.                 'total' => $this->twigEnvironment->render('@SyliusShop/Webapic/panier/panier-total.html.twig', [
  318.                     'cart' => $cart,
  319.                 ]),
  320.                 'top' => $this->twigEnvironment->render('@SyliusShop/Webapic/panier/panier-top.html.twig', [
  321.                     'cart' => $cart,
  322.                 ]),
  323.                 'livraison' => $this->twigEnvironment->render('@SyliusShop/Webapic/panier/panier-livraison.html.twig', [
  324.                     'cart' => $cart,
  325.                     'amount' => $amount,
  326.                     'total' => $total,
  327.                     'diff' => $diff,
  328.                     'progress' => $progress,
  329.                 ]),
  330.                 'widgettop' => $this->twigEnvironment->render('@SyliusShop/Cart/_widget.html.twig', [
  331.                     'cart' => $cart,
  332.                 ]),
  333.                 // 'isFine' => true
  334.                 'isFine' => $isFine
  335.             ]);            
  336.         }       
  337.     }
  338.     public function meaAction(Request $request): Response
  339.     {        
  340.         return new Response(
  341.             $this->twigEnvironment->render('@SyliusShop/Webapic/panier/mea.html.twig', [
  342.                 // 'cart' => $cart
  343.             ])
  344.         );
  345.     }
  346.     private function addOrderItemToCart($channel$cart$variant$quantity, ?int $customPrice null, ?array $kdoFields): void
  347.     {
  348.         if($variant->getIsBlocked()) {
  349.             return;
  350.         }
  351.         if ($variant->getProduct()->getConditioning() == 'per_meter_each_0_5_meter') {
  352.             // Ajout d'un tissu au mètre (0,5m etc)
  353.             // On ajoute une ligne à chaque fois avec une quantité de "1"
  354.             $basePrice is_null($customPrice) ? $variant->getChannelPricingForChannel($channel)->getPrice() : $customPrice;
  355.             $price = (int) round($basePrice $quantity0);
  356.             $orderItem $this->container->get('sylius.factory.order_item')->createNew();
  357.             $orderItem->setVariant($variant);
  358.             $orderItem->setVariantName($variant->getName() . ' - ' $quantity 'm');
  359.             $orderItem->setOriginalUnitPrice($price);
  360.             $orderItem->setUnitPrice($price);
  361.             $orderItem->setImmutable(true);
  362.             $orderItem->setConditioning($variant->getProduct()->getConditioning());
  363.             $orderItem->setConditioningValue($quantity);
  364.             $this->container->get('sylius.order_item_quantity_modifier')->modify($orderItem1);
  365.             $this->container->get('sylius.order_modifier')->addToOrder($cart$orderItem);
  366.         } else if ($variant->getProduct()->getConditioning() == 'card_kdo_per_euros_each_10_euros' && !empty($kdoFields)) {
  367.             // Ajout d'une carte cadeau
  368.             // On ajoute une ligne à chaque fois avec une quantité de "1"
  369.             
  370.             $basePrice is_null($customPrice) ? $variant->getChannelPricingForChannel($channel)->getPrice() : $customPrice;
  371.             $price = (int) round($basePrice $quantity0);
  372.             // dump($cart);
  373.             // dump($variant);
  374.             // dump($quantity);
  375.             // dump($price);
  376.             // exit();
  377.             $orderItem $this->container->get('sylius.factory.order_item')->createNew();
  378.             $orderItem->setVariant($variant);
  379.             $orderItem->setVariantName($variant->getName() . ' - ' $quantity '€');
  380.             $orderItem->setOriginalUnitPrice((int) round($quantity 1000));
  381.             $orderItem->setUnitPrice((int) round($quantity 1000));
  382.             $orderItem->setImmutable(true);
  383.             $orderItem->setConditioning($variant->getProduct()->getConditioning());
  384.             $orderItem->setConditioningValue($quantity);
  385.             $orderItem->setKdoEmail($kdoFields['kdo_mail']);
  386.             $orderItem->setKdoLastname($kdoFields['kdo_lastname']);
  387.             $orderItem->setKdoFirstname($kdoFields['kdo_firstname']);
  388.             $orderItem->setKdoMessage($kdoFields['kdo_message']);
  389.             $this->container->get('sylius.order_item_quantity_modifier')->modify($orderItem1);
  390.             $this->container->get('sylius.order_modifier')->addToOrder($cart$orderItem);
  391.         } else {
  392.             $quantity intval($quantity);
  393.             $orderItem $this->container->get('sylius.repository.order_item')->findOneBy([
  394.                 'variant' => $variant,
  395.                 'order' => $cart
  396.             ]);
  397.             // dump($orderItem);
  398.             if (empty($orderItem) == true) {
  399.                 $orderItem $this->container->get('sylius.factory.order_item')->createNew();
  400.                 $orderItem->setVariant($variant);
  401.                 if (is_null($customPrice) == false) {
  402.                     $orderItem->setOriginalUnitPrice($customPrice);
  403.                     $orderItem->setUnitPrice($customPrice);
  404.                     $orderItem->setImmutable(true);
  405.                 }
  406.                 if(($orderItem->getQuantity() + $quantity) > 10){
  407.                     $this->container->get('sylius.order_item_quantity_modifier')->modify($orderItem10);
  408.                 }else{
  409.                     
  410.                     $this->container->get('sylius.order_item_quantity_modifier')->modify($orderItem$orderItem->getQuantity() + $quantity);
  411.                 }
  412.                
  413.                 $this->container->get('sylius.order_modifier')->addToOrder($cart$orderItem);
  414.             } else {
  415.                 if (is_null($customPrice) == false) {
  416.                     $orderItem->setOriginalUnitPrice($customPrice);
  417.                     $orderItem->setUnitPrice($customPrice);
  418.                     $orderItem->setImmutable(true);
  419.                 }
  420.                
  421.                 $this->container->get('sylius.order_item_quantity_modifier')->modify($orderItem$orderItem->getQuantity() + $quantity);
  422.             }
  423.         }
  424.     }
  425.     public function applyReducedPricesToCart($channel$cart): void
  426.     {
  427.         
  428.         if (isset($_GET['frommail']) ) {
  429.             return;
  430.         }
  431.         // echo $webPath = $this->container->get('kernel')->getProjectDir();
  432.         $process = new Process(['/usr/bin/php'$this->container->get('kernel')->getProjectDir().'/bin/console',  'ppmc:valorize-cart''--cartId='.$cart->getId()]);
  433.         $process->setTimeout(4);
  434.         try {
  435.         
  436.             $process->start();
  437.             $process->wait();
  438.             // echo $process->getOutput();
  439.             $em $this->container->get('doctrine.orm.entity_manager');
  440.             $em->refresh($cart);
  441.             $cartItems $cart->getItems();
  442.             foreach ($cartItems as $cartRow) {
  443.                 $em->refresh($cartRow);
  444.             }
  445.         } catch (ProcessTimedOutException $exception) {
  446.             // echo 'bofbof';
  447.         }
  448.     }
  449.     public function convertFideliteAction(
  450.         Request $request): JsonResponse
  451.     {
  452.         $customer $this->container->get('sylius.context.customer')->getCustomer();
  453.         $this->bus->dispatch(new SyncCustomer((int)$customer->getId()));
  454.         $fidelity $request->query->get('fidelity'0);        
  455.     
  456.         $cart $this->container->get('sylius.context.cart')->getCart();       
  457.         $cart->setHasLoyalty($fidelity);       
  458.         // $this->applyReducedPricesToCart($channel, $cart);
  459.         $cartManager $this->container->get('sylius.manager.order');
  460.         $cartManager->persist($cart);
  461.         $cartManager->flush();
  462.         return new JsonResponse([
  463.             'success' => true
  464.         ]);
  465.     }
  466.     public static function stockVerification($cart$request): bool
  467.     {
  468.         
  469.       
  470.         $isFine true;
  471.         $cartItems $cart->getItems();
  472.         foreach ($cartItems as $cartRow) {
  473.             // si panier > 30mn et stock insuffisant
  474.             if($cart->getCompteur() == 0){ 
  475.                                     
  476.                 if(!$cartRow->canAddItem()){
  477.                     $isFine false;
  478.                     $quantity $cartRow->nbItemStock();
  479.                     if($quantity == 0){
  480.                         $this->container->get('sylius.order_modifier')->removeFromOrder($cart$cartRow);
  481.                         $request->getSession()->getFlashBag()->add(
  482.                             'error',
  483.                             "Le produit ".$cartRow->getVariant()->getName()." n'est plus disponible "
  484.                         );
  485.                     }else{
  486.                         $this->container->get('sylius.order_item_quantity_modifier')->modify($cartRow$quantity);
  487.                         $request->getSession()->getFlashBag()->add(
  488.                             'info',
  489.                             "Le produit ".$cartRow->getVariant()->getName()." n'est plus disponible dans la quantité demandée "
  490.                         );
  491.                     }
  492.                     $this->container->get('sylius.order_processing.order_processor')->process($cart);
  493.                     $cartManager $this->container->get('sylius.manager.order');
  494.                     $cartManager->persist($cart);
  495.                     $cartManager->flush();
  496.                 }
  497.             }            
  498.         }
  499.         return $isFine;
  500.     }
  501.     public function addMessageToCart(Request $request) {
  502.         $cart $this->container->get('sylius.context.cart')->getCart();
  503.         $data = (array) $request->request->all();        
  504.         if(!empty($data['message'])) {
  505.             $cart->setGiftMessage($data['message']);
  506.             $cartManager $this->container->get('sylius.manager.order');
  507.             $cartManager->persist($cart);
  508.             $cartManager->flush();
  509.         }
  510.         return new JsonResponse([
  511.             'success' => true
  512.         ]);
  513.     }
  514.     
  515.     
  516. }