<?php
namespace App\Controller;
use App\Entity\Commandes;
use App\Entity\LignesCommande;
use App\Entity\Quartier;
use App\Entity\Reservation;
use App\Entity\ReservationItems;
use App\Form\FraisLivraisonType;
use App\Form\RegistrationFormType;
use App\Form\ReservationType;
use App\Form\UserRegistrationFormType;
use App\Repository\CommandesRepository;
use App\Repository\LignesCommandeRepository;
use App\Repository\ProduitRepository;
use App\Repository\ProduitsRepository;
use App\Repository\ReservationItemRepository;
use App\Repository\ReservationRepository;
use App\Service\PanierService;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;
use App\Entity\User;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class CommmandeController extends AbstractController
{
/**
* @Route("/commmande", name="commmande")
*/
public function index()
{
return $this->render('commmande/index.html.twig', [
'controller_name' => 'CommmandeController',
]);
}
/**
* @Route("/commande/panier", name="commmande_panier_affichage")
*/
public function panierAffichage(SessionInterface $session, ProduitRepository $produitRepository, Request $request)
{
$panier = $session->get('panier', []);
$panierdata = [];
$max = count($produitRepository->findAll());
$rand_id = [];
for ($i = 1; $i <= 5; $i++) {
$rand_id[] = random_int(1, $max);
}
$produit_randon = $produitRepository->findBy(['id' => $rand_id]);
/*$livraison = new Quartier();
$form = $this->createForm(FraisLivraisonType::class, $livraison);
$form->handleRequest($request);*/
foreach ($panier as $id => $quantity) {
$panierdata[] = [
'produit' => $produitRepository->find($id),
'quantite' => $quantity
];
}
$total = 0;
foreach ($panierdata as $item) {
$subtotal = (int)$item['produit']->getPrix() * $item['quantite'];
$total += $subtotal;
}
// dd($panierdata);
return $this->render('commmande/panier/panier.html.twig', [
'elements' => $panierdata,
'total' => $total,
// 'form' => $form->createView(),
'produits' => $produit_randon
]);
}
/**
* @Route("/panier/{id}", name="ajout_panier")
*/
public function ajout_panier($id, PanierService $panier)
{
$panier->ajout($id);
// return $this->render('commmande/index.html.twig', [
// 'controller_name' => 'CommmandeController',
// ]);
return $this->redirectToRoute('commmande_panier_affichage');
}
/**
* @Route("/validation/commande", name="commande_chechkout")
*/
public function checkout(Request $request, SessionInterface $session, ProduitRepository $produitRepository, UserPasswordEncoderInterface $passwordEncoder, MailerInterface $mailer, CommandesRepository $commandesRepository)
{
// $panier->ajout($id);
$form = $this->createForm(UserRegistrationFormType::class)
->add('Methodepayement', ChoiceType::class, [
'choices' => [
'Paiement à livraison' => "livraison",
'MoMo/Flooz/Carte Visa' => "mobile",
],
'mapped' => false,
'expanded' => true,
'multiple' => false,
'attr' => [
'class' => 'custome-radio'
],
'label_attr' =>[
'class'=>'form-check-label'
],
'label'=>'Methode de paiement'
]
);
$panier = $session->get('panier', []);
$panierdata = [];
foreach ($panier as $id => $quantity) {
$panierdata[] = [
'produit' => $produitRepository->find($id),
'quantite' => $quantity
];
}
$total = 0;
foreach ($panierdata as $item) {
$subtotal = (int)$item['produit']->getPrix() * $item['quantite'];
$total += $subtotal;
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// dd($form['Methodepayement']->getData());
$entityManager = $this->getDoctrine()->getManager();
$commande = new Commandes();
// enregistrement des produits de la commande
foreach ($panier as $id => $quantity) {
$lignescommande = new LignesCommande();
$lignescommande->setCommande($commande);
$lignescommande->setProduit($produitRepository->find($id));
$lignescommande->setPrix($produitRepository->find($id)->getPrix());
$lignescommande->setQuantite($quantity);
$entityManager->persist($lignescommande);
}
//fin
$user = $form->getData();
$user->setPassword($passwordEncoder->encodePassword(
$user,
$form['plainPassword']->getData()
));
// $user->addRole("ROLE_CLIENT");
$user->setRoles((array)"ROLE_CLIENT");;
// $user-> setEnabled(true);
$entityManager->persist($user);
$commande->setUser($user);
$commande->setEtat('Encours');
$commande->setClient(1);
$entityManager->persist($commande);
$entityManager->flush();
$session->set('commande_id', $commande->getId());
if ($form['Methodepayement']->getData() == "mobile") {
$session->set("montant", $total);
$session->set("methodpaiement", "kkipay");
return $this->redirectToRoute('commande_paiement_kkiapay');
}
// $lacommande = $commandesRepository->find($commande->getId());
// $email = (new TemplatedEmail())
// ->from('info@ucctmdogbo.bj')
// ->to(new Address('commandes@ucctmdogbo.bj'))
// ->priority(Email::PRIORITY_HIGH);
// if ($methodpaiement == "kkipay"){
// $email->subject('[Commande déja payée en ligne]Nouvelle commande') ;
// }
// else {
// $email->subject('Nouvelle commande');
// }
// $email->subject('[Commande]Nouvelle commande ')
// // // path of the Twig template to render
// ->htmlTemplate('mails/signup.html.twig')
// // // pass variables (name => value) to the template
// ->context([
// 'commande' => $commande,
// ]);
// $mailer->send($email);
return $this->redirectToRoute('commande_paiement');
}
return $this->render('commmande/panier/checkout.html.twig', [
'elements' => $panierdata,
'total' => $total,
'form' => $form->createView(),
]);
}
/**
* @Route("/paiement/kkiapay", name="commande_paiement_kkiapay")
*/
public function kkiapay(SessionInterface $session)
{
return $this->render('commmande/panier/kkiapay.html.twig', [
'total' => $session->get("montant"),
]);
}
/**
* @Route("/paiement/details-facture", name="commande_paiement")
*/
public function facture(SessionInterface $session, LignesCommandeRepository $lignesCommandeRepository, CommandesRepository $commandesRepository, MailerInterface $mailer)
{
$idcommande = $session->get('commande_id', null);
$methodpaiement = $session->get('methodpaiement', null);
$produits = $lignesCommandeRepository->findBy(['commande' => $idcommande]);
$lacommande = $commandesRepository->find($idcommande);
//dd($panierdata);
$total = 0;
foreach ($produits as $item) {
$subtotal = (int)$item->getPrix() * $item->getQuantite();
$total += $subtotal;
}
$email = (new TemplatedEmail())
->from('info@ucctmdogbo.bj')
->to(new Address('commandes@ucctmdogbo.bj'))
//->to(new Address('gildas31@gmail.com'))
->priority(Email::PRIORITY_HIGH);
if ($methodpaiement == "kkipay") {
$email->subject('[Commande déja payée en ligne]Nouvelle commande');
} else {
$email->subject('Nouvelle commande');
}
$email
// path of the Twig template to render
->htmlTemplate('mails/signup.html.twig')
// pass variables (name => value) to the template
->context([
'methodpaiement' => $methodpaiement,
'commande' => $lacommande,
]);
$mailer->send($email);
return $this->render('commmande/panier/facture.html.twig', [
'commande' => $produits,
'total' => $total,
'lacommande' => $lacommande
]);
}
/**
* @Route("/panier/supprimer/{id}", name="supprimer_du_panier")
*/
public function supprimer($id, SessionInterface $session)
{
$panier = $session->get('panier', []);
if (!empty ($panier[$id])) {
unset($panier[$id]);
}
$session->set('panier', $panier);
return $this->redirectToRoute('commmande_panier_affichage');
}
/**
* @Route("/panier-ajax/{id}", name="ajout_panier_ajax")
*/
public function ajout_panier_ajax($id, PanierService $panier, ProduitRepository $produitRepository, SessionInterface $session)
{
$panier->ajout($id);
// return $this->render('commmande/index.html.twig', [
// 'controller_name' => 'CommmandeController',
// ]);
// return $this->redirectToRoute('commmande_panier_affichage');
$panier = $session->get('panier', []);
$panierdata = [];
foreach ($panier as $ids => $quantity) {
$panierdata[] = [
'produit' => $produitRepository->find($ids),
'quantite' => $quantity
];
}
$total = 0;
foreach ($panierdata as $item) {
$subtotal = (int)$item['produit']->getPrix() * $item['quantite'];
$total += $subtotal;
}
$produit = $produitRepository->find($id);
$session->set('panierdata', $panierdata);
return $this->json([
'id' => $produit->getId(),
'nom' => $produit->getNom(),
'prix' => $produit->getPrix(),
'image' => $produit->getImagePrincipale()
], 200);
}
/**
* @Route("/reservation", name="reservation")
*/
public function reservation(Request $request, SessionInterface $session)
{
$reservation = new Reservation();
$items = new ReservationItems();
$reservation->addItem($items);
$form = $this->createForm(ReservationType::class, $reservation);
// dd()
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
foreach ($form->getData()->getItems() as $item) {
$item->setReservations($reservation);
$entityManager->persist($item);
}
$entityManager->persist($reservation);
$entityManager->flush();
$this->addFlash('notice', 'Votre reservation enregistrée avec sucess ');
$session->set('reservation_id', $reservation->getId());
return $this->redirectToRoute('end_reservation');
}
//
return $this->render('commmande/reservations/nouvelle_reservation.html.twig', array(
'form' => $form->createView(),
));
}
/**
* @Route("/reservation-success", name="end_reservation")
*/
public function reservationFin(SessionInterface $session, ReservationRepository $reservationRepository, ReservationItemRepository $itemRepository)
{
$idresevation = $session->get('reservation_id', null);
$produits = $itemRepository->findBy(['reservations' => $idresevation]);
$lareservation = $reservationRepository->find($idresevation);
//dd($panierdata);
$total = 0;
foreach ($produits as $item) {
$subtotal = (int)$item->getPrix() * $item->getQuantite();
$total += $subtotal;
}
return $this->render('commmande/reservations/facture-reservation.html.twig', [
'commande' => $produits,
'total' => $total,
'lacommande' => $lareservation
]);
}
/**
* @Route("recherche/produits/", name="frontend_recherche_produit")
**/
public function rechercheProduit(Request $request, PaginatorInterface $paginator)
{
$entityManager = $this->getDoctrine()->getManager();
$produits = $paginator->paginate(
$entityManager->getRepository('App:Produit')->SearchProduit($request->query->get('searchProduit')), // Requête contenant les données à paginer (ici nos articles)
$request->query->getInt('page', 1), // Numéro de la page en cours, passé dans l'URL, 1 si aucune page
15 // Nombre de résultats par page
);
// $produitRepository->SearchProduit($request->query->get('searchProduit'));
// dd($produits);
$topproduit = $entityManager->getRepository('App:Produit')->findBy(['id' => [3, 5, 6, 9, 4]]);
return $this->render('front-end/produits/boutique.html.twig', [
'produits' => $produits,
'lacategorie' => 'Resultat de la recherche pour : ' . $request->query->get('searchProduit'),
'topproduit' => $topproduit
]);
}
/**
* @Route("/paniers/ajout/{id}", name="cart_add")
*/
public function addToCart($id, SessionInterface $session)
{
$panier = $session->get('panier', []);
if (!empty($panier[$id])) {
$panier[$id]++;
} else {
$panier[$id] = 1;
}
$session->set('panier', $panier);
return $this->redirectToRoute('commmande_panier_affichage');
}
/**
* @Route("/paniers/retrait/{id}", name="cart_remove")
*/
public function removeToCart($id, SessionInterface $session)
{
$panier = $session->get('panier', []);
if (($panier[$id]) > 1) {
$panier[$id]--;
} else {
unset($panier[$id]);
}
$session->set('panier', $panier);
return $this->redirectToRoute('commmande_panier_affichage');
}
/**
* @Route("/cart/delete/{id}", name="cart_delete")
*/
public function deleteCart($id, SessionInterface $session)
{
$panier = $session->get('panier', []);
if (!empty($panier[$id])) {
unset($panier[$id]);
}
$session->set('panier', $panier);
return $this->redirectToRoute('commmande_panier_affichage');
}
}