src/Controller/Front/PanierController.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\Commande;
  4. use App\Entity\DetailCommande;
  5. use App\Entity\User;
  6. use App\Repository\BasketRepository;
  7. use App\Repository\CommandeRepository;
  8. use App\Repository\DetailCommandeRepository;
  9. use App\Repository\ProduitRepository;
  10. use App\Services\BasketService;
  11. use App\Services\CurrentPathService;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Security\Core\Security;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. /**
  18.  * @Route("/panier")
  19.  */
  20. class PanierController extends AbstractController
  21. {
  22.     public function dateArray(TranslatorInterface $translator)
  23.     {
  24.         $dateTime = new \DateTime();
  25.         $dateArray = [
  26.             $translator->trans($dateTime->format('l')),
  27.             $dateTime->format('d'),
  28.             $translator->trans($dateTime->format('F')),
  29.             $dateTime->format('H:i')
  30.         ];
  31.         return $dateArray;
  32.     }
  33.     // #[Route('/', name: 'app_front_panier')]
  34.     // public function index(): Response
  35.     // {
  36.     //     return $this->render('front/panier/index.html.twig', [
  37.     //         'controller_name' => 'PanierController',
  38.     //     ]);
  39.     // }
  40.     /**
  41.      * @Route("/", name="app_front_panier", methods={"GET"})
  42.      */
  43.     public function index(Security $securityProduitRepository $produitRepositoryCurrentPathService $currentPathServiceTranslatorInterface $translatorBasketRepository $basketRepository): Response
  44.     {
  45.         $user $security->getUser();
  46.         if($user instanceof User){
  47.             $basketService = new BasketService($basketRepository$produitRepository);
  48.             $produitsGot $basketService->getAllBaskets($usertrue);
  49.             $produits = [];
  50.             $total 0;
  51.             foreach ($produitsGot as $key => $value) {
  52.                 $total += $value->getProduit()->getPrixDeVente();
  53.                 $logo $value->getProduit()->getLogo();
  54.                 $produits[] = [
  55.                     'id' => $value->getProduit()->getId(),
  56.                     'logo' => (!is_null($logo) && !empty($logo)) ? "/uploads/images/".$logo null,
  57.                     'nom' => $value->getProduit()->getNom(),
  58.                     'rom' => $value->getProduit()->getRom(),
  59.                     'ram' => $value->getProduit()->getRam(),
  60.                     'prix' => $value->getProduit()->getPrixDeVente(),
  61.                 ];
  62.             }
  63.             $groupedArray = [];
  64.             foreach ($produits as $item) {
  65.                 $id $item['id'];
  66.                 if (!isset($groupedArray[$id])) {
  67.                     $groupedArray[$id] = $item;
  68.                     $groupedArray[$id]['quantity'] = 1;
  69.                 } else {
  70.                     $groupedArray[$id]['quantity']++;
  71.                 }
  72.             }
  73.             // Convert associative array to indexed array
  74.             $produits array_values($groupedArray);
  75.             // return $this->render('back/produit/catalogue/cart.html.twig', [
  76.             return $this->render('front/panier/index.html.twig', [
  77.                 'isCart' => true,
  78.                 'produits' => $produits,
  79.                 'basket' => [
  80.                     'nbPanier' => count($basketService->getAllBaskets($user)),
  81.                     'total' => number_format($total0','' ')
  82.                 ],
  83.                 'dateArray' => $this->dateArray($translator),
  84.                 'classes' => $currentPathService->classes(),
  85.             ]);
  86.         }
  87.         return $this->redirectToRoute('app_login', [], Response::HTTP_SEE_OTHER);
  88.     }
  89.     /**
  90.      * @Route("/valider", name="app_front_panier_validate", methods={"POST"})
  91.      */
  92.     public function validate(Security $securityBasketRepository $basketRepositoryCommandeRepository $commandeRepositoryDetailCommandeRepository $detailCommandeRepositoryProduitRepository $produitRepository): Response
  93.     {
  94.         $user $security->getUser();
  95.         if($user instanceof User){
  96.             $userBaskets $basketRepository->findBy(['user' => $user]);
  97.             $commande = new Commande();
  98.             $commande->setDate(new \DateTime());
  99.             $commande->setUser($user);
  100.             $commandeRepository->add($commandetrue);
  101.             $basketQteParProduit = [];
  102.             foreach ($userBaskets as $basket) {
  103.                 $produitId $basket->getProduit()->getId();
  104.                 if (isset($basketQteParProduit[$produitId])) {
  105.                     $basketQteParProduit[
  106.                         $produitId]['quantity'] += 1;
  107.                 } else {
  108.                     $basketQteParProduit[$produitId] = [
  109.                         'produit' => $produitId,
  110.                         'quantity' => 1,
  111.                     ];
  112.                 }
  113.                 $basketRepository->remove($baskettrue);
  114.             }
  115.             foreach($basketQteParProduit as $produitData) {
  116.                 $detailCommande = new DetailCommande();
  117.                 $produit $produitRepository->find($produitData['produit']);
  118.                 $detailCommande->setProduit($produit);
  119.                 $detailCommande->setQuantite($produitData['quantity']);
  120.                 $detailCommande->setCommande($commande);
  121.                 $detailCommandeRepository->add($detailCommandetrue);
  122.             }
  123.             return $this->json(['success' => "Panier validé avec succès!"]);
  124.         }
  125.         return $this->redirectToRoute('app_login', [], Response::HTTP_SEE_OTHER);
  126.     }
  127.     
  128. }