src/Controller/CommmandeController.php line 469

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Commandes;
  4. use App\Entity\LignesCommande;
  5. use App\Entity\Quartier;
  6. use App\Entity\Reservation;
  7. use App\Entity\ReservationItems;
  8. use App\Form\FraisLivraisonType;
  9. use App\Form\RegistrationFormType;
  10. use App\Form\ReservationType;
  11. use App\Form\UserRegistrationFormType;
  12. use App\Repository\CommandesRepository;
  13. use App\Repository\LignesCommandeRepository;
  14. use App\Repository\ProduitRepository;
  15. use App\Repository\ProduitsRepository;
  16. use App\Repository\ReservationItemRepository;
  17. use App\Repository\ReservationRepository;
  18. use App\Service\PanierService;
  19. use Knp\Component\Pager\PaginatorInterface;
  20. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
  25. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  26. use Symfony\Component\Mailer\MailerInterface;
  27. use Symfony\Component\Mime\Address;
  28. use Symfony\Component\Mime\Email;
  29. use Symfony\Component\Routing\Annotation\Route;
  30. use App\Entity\User;
  31. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  32. class CommmandeController extends AbstractController
  33. {
  34.     /**
  35.      * @Route("/commmande", name="commmande")
  36.      */
  37.     public function index()
  38.     {
  39.         return $this->render('commmande/index.html.twig', [
  40.             'controller_name' => 'CommmandeController',
  41.         ]);
  42.     }
  43.     /**
  44.      * @Route("/commande/panier", name="commmande_panier_affichage")
  45.      */
  46.     public function panierAffichage(SessionInterface $sessionProduitRepository $produitRepositoryRequest $request)
  47.     {
  48.         $panier $session->get('panier', []);
  49.         $panierdata = [];
  50.         $max count($produitRepository->findAll());
  51.         $rand_id = [];
  52.         for ($i 1$i <= 5$i++) {
  53.             $rand_id[] = random_int(1$max);
  54.         }
  55.         $produit_randon $produitRepository->findBy(['id' => $rand_id]);
  56.         /*$livraison = new Quartier();
  57.         $form = $this->createForm(FraisLivraisonType::class, $livraison);
  58.         $form->handleRequest($request);*/
  59.         foreach ($panier as $id => $quantity) {
  60.             $panierdata[] = [
  61.                 'produit' => $produitRepository->find($id),
  62.                 'quantite' => $quantity
  63.             ];
  64.         }
  65.         $total 0;
  66.         foreach ($panierdata as $item) {
  67.             $subtotal = (int)$item['produit']->getPrix() * $item['quantite'];
  68.             $total += $subtotal;
  69.         }
  70.         // dd($panierdata);
  71.         return $this->render('commmande/panier/panier.html.twig', [
  72.             'elements' => $panierdata,
  73.             'total' => $total,
  74.             // 'form' => $form->createView(),
  75.             'produits' => $produit_randon
  76.         ]);
  77.     }
  78.     /**
  79.      * @Route("/panier/{id}", name="ajout_panier")
  80.      */
  81.     public function ajout_panier($idPanierService $panier)
  82.     {
  83.         $panier->ajout($id);
  84. //        return $this->render('commmande/index.html.twig', [
  85. //            'controller_name' => 'CommmandeController',
  86. //        ]);
  87.         return $this->redirectToRoute('commmande_panier_affichage');
  88.     }
  89.     /**
  90.      * @Route("/validation/commande", name="commande_chechkout")
  91.      */
  92.     public function checkout(Request $requestSessionInterface $sessionProduitRepository $produitRepositoryUserPasswordEncoderInterface $passwordEncoderMailerInterface $mailerCommandesRepository $commandesRepository)
  93.     {
  94.         // $panier->ajout($id);
  95.         $form $this->createForm(UserRegistrationFormType::class)
  96.             ->add('Methodepayement'ChoiceType::class, [
  97.                     'choices' => [
  98.                         'Paiement à livraison' => "livraison",
  99.                         'MoMo/Flooz/Carte Visa' => "mobile",
  100.                     ],
  101.                     'mapped' => false,
  102.                     'expanded' => true,
  103.                     'multiple' => false,
  104.                     'attr' => [
  105.                         'class' => 'custome-radio'
  106.                     ],
  107.                     'label_attr' =>[
  108.                         'class'=>'form-check-label'
  109.                     ],
  110.                    'label'=>'Methode de paiement'
  111.                 ]
  112.             );
  113.         $panier $session->get('panier', []);
  114.         $panierdata = [];
  115.         foreach ($panier as $id => $quantity) {
  116.             $panierdata[] = [
  117.                 'produit' => $produitRepository->find($id),
  118.                 'quantite' => $quantity
  119.             ];
  120.         }
  121.         $total 0;
  122.         foreach ($panierdata as $item) {
  123.             $subtotal = (int)$item['produit']->getPrix() * $item['quantite'];
  124.             $total += $subtotal;
  125.         }
  126.         $form->handleRequest($request);
  127.         if ($form->isSubmitted() && $form->isValid()) {
  128.             //  dd($form['Methodepayement']->getData());
  129.             $entityManager $this->getDoctrine()->getManager();
  130.             $commande = new Commandes();
  131.             // enregistrement des produits de la commande
  132.             foreach ($panier as $id => $quantity) {
  133.                 $lignescommande = new LignesCommande();
  134.                 $lignescommande->setCommande($commande);
  135.                 $lignescommande->setProduit($produitRepository->find($id));
  136.                 $lignescommande->setPrix($produitRepository->find($id)->getPrix());
  137.                 $lignescommande->setQuantite($quantity);
  138.                 $entityManager->persist($lignescommande);
  139.             }
  140.             //fin
  141.             $user $form->getData();
  142.             $user->setPassword($passwordEncoder->encodePassword(
  143.                 $user,
  144.                 $form['plainPassword']->getData()
  145.             ));
  146. //            $user->addRole("ROLE_CLIENT");
  147.             $user->setRoles((array)"ROLE_CLIENT");;
  148. //            $user-> setEnabled(true);
  149.             $entityManager->persist($user);
  150.             $commande->setUser($user);
  151.             $commande->setEtat('Encours');
  152.             $commande->setClient(1);
  153.             $entityManager->persist($commande);
  154.             $entityManager->flush();
  155.             $session->set('commande_id'$commande->getId());
  156.             if ($form['Methodepayement']->getData() == "mobile") {
  157.                 $session->set("montant"$total);
  158.                 $session->set("methodpaiement""kkipay");
  159.                 return $this->redirectToRoute('commande_paiement_kkiapay');
  160.             }
  161. //             $lacommande = $commandesRepository->find($commande->getId());
  162. //               $email = (new TemplatedEmail())
  163. //                   ->from('info@ucctmdogbo.bj')
  164. //                   ->to(new Address('commandes@ucctmdogbo.bj'))
  165. //                   ->priority(Email::PRIORITY_HIGH);
  166. //                     if ($methodpaiement == "kkipay"){
  167. //              $email->subject('[Commande déja payée en ligne]Nouvelle commande')   ;
  168. //             }
  169. //             else {
  170. //                 $email->subject('Nouvelle commande');
  171. //             }
  172. //                     $email->subject('[Commande]Nouvelle commande ')
  173. //   //                // path of the Twig template to render
  174. //                   ->htmlTemplate('mails/signup.html.twig')
  175. //   //                // pass variables (name => value) to the template
  176. //                   ->context([
  177. //                       'commande' => $commande,
  178. //                   ]);
  179. //               $mailer->send($email);
  180.             return $this->redirectToRoute('commande_paiement');
  181.         }
  182.         return $this->render('commmande/panier/checkout.html.twig', [
  183.             'elements' => $panierdata,
  184.             'total' => $total,
  185.             'form' => $form->createView(),
  186.         ]);
  187.     }
  188.     /**
  189.      * @Route("/paiement/kkiapay", name="commande_paiement_kkiapay")
  190.      */
  191.     public function kkiapay(SessionInterface $session)
  192.     {
  193.         return $this->render('commmande/panier/kkiapay.html.twig', [
  194.             'total' => $session->get("montant"),
  195.         ]);
  196.     }
  197.     /**
  198.      * @Route("/paiement/details-facture", name="commande_paiement")
  199.      */
  200.     public function facture(SessionInterface $sessionLignesCommandeRepository $lignesCommandeRepositoryCommandesRepository $commandesRepositoryMailerInterface $mailer)
  201.     {
  202.         $idcommande $session->get('commande_id'null);
  203.         $methodpaiement $session->get('methodpaiement'null);
  204.         $produits $lignesCommandeRepository->findBy(['commande' => $idcommande]);
  205.         $lacommande $commandesRepository->find($idcommande);
  206.         //dd($panierdata);
  207.         $total 0;
  208.         foreach ($produits as $item) {
  209.             $subtotal = (int)$item->getPrix() * $item->getQuantite();
  210.             $total += $subtotal;
  211.         }
  212.         $email = (new TemplatedEmail())
  213.             ->from('info@ucctmdogbo.bj')
  214.             ->to(new Address('commandes@ucctmdogbo.bj'))
  215.             //->to(new Address('gildas31@gmail.com'))
  216.             ->priority(Email::PRIORITY_HIGH);
  217.         if ($methodpaiement == "kkipay") {
  218.             $email->subject('[Commande déja payée en ligne]Nouvelle commande');
  219.         } else {
  220.             $email->subject('Nouvelle commande');
  221.         }
  222.         $email
  223.             // path of the Twig template to render
  224.             ->htmlTemplate('mails/signup.html.twig')
  225.             // pass variables (name => value) to the template
  226.             ->context([
  227.                 'methodpaiement' => $methodpaiement,
  228.                 'commande' => $lacommande,
  229.             ]);
  230.         $mailer->send($email);
  231.         return $this->render('commmande/panier/facture.html.twig', [
  232.             'commande' => $produits,
  233.             'total' => $total,
  234.             'lacommande' => $lacommande
  235.         ]);
  236.     }
  237.     /**
  238.      * @Route("/panier/supprimer/{id}", name="supprimer_du_panier")
  239.      */
  240.     public function supprimer($idSessionInterface $session)
  241.     {
  242.         $panier $session->get('panier', []);
  243.         if (!empty ($panier[$id])) {
  244.             unset($panier[$id]);
  245.         }
  246.         $session->set('panier'$panier);
  247.         return $this->redirectToRoute('commmande_panier_affichage');
  248.     }
  249.     /**
  250.      * @Route("/panier-ajax/{id}", name="ajout_panier_ajax")
  251.      */
  252.     public function ajout_panier_ajax($idPanierService $panierProduitRepository $produitRepositorySessionInterface $session)
  253.     {
  254.         $panier->ajout($id);
  255. //        return $this->render('commmande/index.html.twig', [
  256. //            'controller_name' => 'CommmandeController',
  257. //        ]);
  258.         //   return $this->redirectToRoute('commmande_panier_affichage');
  259.         $panier $session->get('panier', []);
  260.         $panierdata = [];
  261.         foreach ($panier as $ids => $quantity) {
  262.             $panierdata[] = [
  263.                 'produit' => $produitRepository->find($ids),
  264.                 'quantite' => $quantity
  265.             ];
  266.         }
  267.         $total 0;
  268.         foreach ($panierdata as $item) {
  269.             $subtotal = (int)$item['produit']->getPrix() * $item['quantite'];
  270.             $total += $subtotal;
  271.         }
  272.         $produit $produitRepository->find($id);
  273.         $session->set('panierdata'$panierdata);
  274.         return $this->json([
  275.             'id' => $produit->getId(),
  276.             'nom' => $produit->getNom(),
  277.             'prix' => $produit->getPrix(),
  278.             'image' => $produit->getImagePrincipale()
  279.         ], 200);
  280.     }
  281.     /**
  282.      * @Route("/reservation", name="reservation")
  283.      */
  284.     public function reservation(Request $requestSessionInterface $session)
  285.     {
  286.         $reservation = new Reservation();
  287.         $items = new ReservationItems();
  288.         $reservation->addItem($items);
  289.         $form $this->createForm(ReservationType::class, $reservation);
  290.         // dd()
  291.         $form->handleRequest($request);
  292.         if ($form->isSubmitted() && $form->isValid()) {
  293.             $entityManager $this->getDoctrine()->getManager();
  294.             foreach ($form->getData()->getItems() as $item) {
  295.                 $item->setReservations($reservation);
  296.                 $entityManager->persist($item);
  297.             }
  298.             $entityManager->persist($reservation);
  299.             $entityManager->flush();
  300.             $this->addFlash('notice''Votre reservation enregistrée avec sucess ');
  301.             $session->set('reservation_id'$reservation->getId());
  302.             return $this->redirectToRoute('end_reservation');
  303.         }
  304. //
  305.         return $this->render('commmande/reservations/nouvelle_reservation.html.twig', array(
  306.             'form' => $form->createView(),
  307.         ));
  308.     }
  309.     /**
  310.      * @Route("/reservation-success", name="end_reservation")
  311.      */
  312.     public function reservationFin(SessionInterface $sessionReservationRepository $reservationRepositoryReservationItemRepository $itemRepository)
  313.     {
  314.         $idresevation $session->get('reservation_id'null);
  315.         $produits $itemRepository->findBy(['reservations' => $idresevation]);
  316.         $lareservation $reservationRepository->find($idresevation);
  317.         //dd($panierdata);
  318.         $total 0;
  319.         foreach ($produits as $item) {
  320.             $subtotal = (int)$item->getPrix() * $item->getQuantite();
  321.             $total += $subtotal;
  322.         }
  323.         return $this->render('commmande/reservations/facture-reservation.html.twig', [
  324.             'commande' => $produits,
  325.             'total' => $total,
  326.             'lacommande' => $lareservation
  327.         ]);
  328.     }
  329.     /**
  330.      * @Route("recherche/produits/", name="frontend_recherche_produit")
  331.      **/
  332.     public function rechercheProduit(Request $requestPaginatorInterface $paginator)
  333.     {
  334.         $entityManager $this->getDoctrine()->getManager();
  335.         $produits $paginator->paginate(
  336.             $entityManager->getRepository('App:Produit')->SearchProduit($request->query->get('searchProduit')), // Requête contenant les données à paginer (ici nos articles)
  337.             $request->query->getInt('page'1), // Numéro de la page en cours, passé dans l'URL, 1 si aucune page
  338.             15 // Nombre de résultats par page
  339.         );
  340. //            $produitRepository->SearchProduit($request->query->get('searchProduit'));
  341. //        dd($produits);
  342.         $topproduit $entityManager->getRepository('App:Produit')->findBy(['id' => [35694]]);
  343.         return $this->render('front-end/produits/boutique.html.twig', [
  344.             'produits' => $produits,
  345.             'lacategorie' => 'Resultat de la recherche pour : ' $request->query->get('searchProduit'),
  346.             'topproduit' => $topproduit
  347.         ]);
  348.     }
  349.     /**
  350.      * @Route("/paniers/ajout/{id}", name="cart_add")
  351.      */
  352.     public function addToCart($idSessionInterface $session)
  353.     {
  354.         $panier $session->get('panier', []);
  355.         if (!empty($panier[$id])) {
  356.             $panier[$id]++;
  357.         } else {
  358.             $panier[$id] = 1;
  359.         }
  360.         $session->set('panier'$panier);
  361.         return $this->redirectToRoute('commmande_panier_affichage');
  362.     }
  363.     /**
  364.      * @Route("/paniers/retrait/{id}", name="cart_remove")
  365.      */
  366.     public function removeToCart($idSessionInterface $session)
  367.     {
  368.         $panier $session->get('panier', []);
  369.         if (($panier[$id]) > 1) {
  370.             $panier[$id]--;
  371.         } else {
  372.             unset($panier[$id]);
  373.         }
  374.         $session->set('panier'$panier);
  375.         return $this->redirectToRoute('commmande_panier_affichage');
  376.     }
  377.     /**
  378.      * @Route("/cart/delete/{id}", name="cart_delete")
  379.      */
  380.     public function deleteCart($idSessionInterface $session)
  381.     {
  382.         $panier $session->get('panier', []);
  383.         if (!empty($panier[$id])) {
  384.             unset($panier[$id]);
  385.         }
  386.         $session->set('panier'$panier);
  387.         return $this->redirectToRoute('commmande_panier_affichage');
  388.     }
  389. }