<?php
// src/Controller/EntrepriseController.php
namespace App\Controller;
use App\Entity\SujetFAQ;
use App\Entity\Article;
use App\Entity\AppUser;
use App\Entity\Product;
use App\Entity\Review;
use App\Entity\Category;
use App\Entity\Order;
use App\Entity\SlidePromo;
use App\Entity\ProductOrder;
use App\Entity\Entreprise;
use App\Entity\ProductMisDeCote;
use App\Entity\SubCategory;
use App\Entity\AvisUtilisateur;
use App\Form\SujetFAQType;
use App\Form\AvisUtilisateurType;
use App\Form\AvisUtilisateurEditType;
use App\Form\ProductType;
use App\Entity\ShippingMethod;
use App\Entity\AbonneNewsletter;
use App\Entity\SideBox;
use App\Entity\ProductCollection;
use App\Entity\HomeCollection;
use App\Entity\Coupon;
use App\Services\CinetPay;
use App\Entity\WishListProduct;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
/**
* @Route("/")
*/
class EntrepriseController extends AbstractController
{
private $session;
public function __construct(SessionInterface $session)
{
$this->session = $session;
}
/**
* @Route("/", name="home")
*/
public function homeAction()
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
$avisUtilisateurs = $em->getRePository(AvisUtilisateur::class)
->findBy(
array(),
array(),
10, //nb De resultats
0 //A partir de 0
);
$products = $em->getRePository(Product::class)
->findBy(
array(
),
array(
'id' => 'DESC'
),
30,
0
);
$rightBoxes = $em->getRePository(SideBox::class)
->findBy(
array(
'position' => 'Right'
)
);
$leftBoxes = $em->getRePository(SideBox::class)
->findBy(
array(
'position' => 'Left'
)
);
$collections = array(
);
$homeCollections = $em->getRePository(HomeCollection::class)
->findAll();
foreach($homeCollections as $homeCollection){
$productCollections = $em->getRePository(ProductCollection::class)
->findBy(
array('homeCollection' => $homeCollection->getId()
),
array(),
$homeCollection->getItemNumber(),
0
);
$products = array();
$photos = array();
foreach($productCollections as $productCollection){
$p = $productCollection->getProduct();
$p->setTranslatableLocale($request->getLocale());
$em->refresh($p);
array_push($products, $p);
}
$el = array(
'nom' => $homeCollection->getNom(),
'isPromotional' => $homeCollection->getIsPromotional(),
'theLink' => $homeCollection->getTheLink(),
'slug' => $homeCollection->getSlug(),
'photos' => $homeCollection->getPhotos(),
'products' => $products,
);
$collections[] = $el;
}
return $this->render('entreprise/home.html.twig', [
'avisUtilisateurs' => $avisUtilisateurs,
'products' => $products,
'rightBoxes' => $rightBoxes,
'leftBoxes' => $leftBoxes,
'collections' => $collections
]);
}
/**
* @Route("/collection/{slug}/{page}/{currentPage}", name="product_by_collection")
*/
public function productByCollectionAction(HomeCollection $homeCollection, $page='page', $currentPage = 1)
{
$em = $this->getDoctrine()->getManager();
$numberByPage = 5;
$em = $this->getDoctrine()
->getManager();
$produitCollections = $em->getRePository(ProductCollection::class)
->findProductByHomeCollection($numberByPage, $currentPage, $homeCollection->getId());
$pageAmount = ceil((count($produitCollections))/$numberByPage) ;
if(($currentPage > $pageAmount && $pageAmount >= 1) || $currentPage <=0){
throw $this->createNotFoundException('Nombre invalide');
}
$products = array();
foreach($produitCollections as $produitCollection){
array_push($products, $produitCollection->getProduct());
}
return $this->render('entreprise/product_by_collection.html.twig', [
'products' => $products,
'currentPage' => $currentPage,
'pageAmount' => $pageAmount,
'homeCollection' => $homeCollection
]);
}
/**
* @Route("/profile", name="profile")
*/
public function profileAction()
{
$em = $this->getDoctrine()
->getManager();
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$ok_submission = '';
$user = $this->getUser();
$pageTitle = 'Mon profil';
if('POST' == $request->getMethod() )
{
$email = $request->request->get('email');
}
return $this->render('entreprise/profile.html.twig', [
'user' => $user,
'pageTitle' => $pageTitle
]);
}
/**
* @Route("/orders", name="user_orders")
*/
public function userOrdersAction()
{
$em = $this->getDoctrine()
->getManager();
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$ok_submission = '';
$user = $this->getUser();
$pageTitle = 'Mes orders';
$orders = $em->getRePository(Order::class)
->findByUser($user);
return $this->render('entreprise/orders.html.twig', [
'user' => $user,
'orders' => $orders,
'pageTitle' => $pageTitle
]);
}
/**
* @Route("/product-order/{id}", name="products_by_order")
*/
public function productsByOrderAction(Order $order)
{
$em = $this->getDoctrine()
->getManager();
$productOrders = $em->getRePository(ProductOrder::class)
->findByOrder($order->getId());
$pageTitle = 'DĂ©tails order';
$user = $this->getUser();
return $this->render('entreprise/products_by_order.html.twig', [
'productOrders' => $productOrders,
'user' => $user,
'pageTitle' => $pageTitle
]);
}
/**
* @Route("/my-wishlist", name="user_wishlist")
*/
public function wishlistAction()
{
$em = $this->getDoctrine()
->getManager();
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$ok_submission = '';
$user = $this->getUser();
$pageTitle = 'Product mis de côté';
$produitMisDeCotes = $em->getRePository(WishListProduct::class)
->findByUser($user);
return $this->render('entreprise/my_wishlist.html.twig', [
'user' => $user,
'pageTitle' => $pageTitle,
'produitMisDeCotes' => $produitMisDeCotes
]);
}
/**
* @Route("/add-product-to-wishlist/{id}", name="add_product_to_wishlist")
*/
public function addProductToWishlistAction(Product $product)
{
$em = $this->getDoctrine()
->getManager();
$ok_submission = '';
$user = $this->getUser();
if(null == $user ){
$ok_submission = 'Veuillez vous connecter d\'abord';
} else{
$ok_submission = 'Product mis de côté';
$productMisDeCote = new ProductMisDeCote;
$productMisDeCote->setProduct($product);
$productMisDeCote->setUser($user);
$em->persist($productMisDeCote);
$em->flush();
}
$response = new Response;
$response->setStatusCode(Response::HTTP_OK);
$response->setContent($ok_submission);
return $response;
}
/**
* @Route("/remove-product-from-wishlist/{id}", name="remove_product_from_wishlist")
*/
public function removeProductFromWishlistAction(Product $product)
{
$em = $this->getDoctrine()
->getManager();
$ok_submission = '';
$user = $this->getUser();
$productMisDeCote = $em->getRePository(ProductMisDeCote::class)
->findOneBy(
array(
'product' => $product->getId(),
'user' => $user->getId()
)
);
if(null != $productMisDeCote){
$em->remove($productMisDeCote);
$em->flush();
}
return $this->redirectToRoute('user_wishlist');
}
/**
* @Route("/your-password", name="user_password")
*/
public function userPasswordAction()
{
$em = $this->getDoctrine()
->getManager();
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$ok_submission = '';
$user = $this->getUser();
$pageTitle = 'Mot de passe';
if('POST' == $request->getMethod() )
{
$email = $request->request->get('email');
}
return $this->render('entreprise/user_password.html.twig', [
'user' => $user,
'pageTitle' => $pageTitle
]);
}
/**
* @Route("/s-abonner-newsletter", name="s_abonner_newsletter")
*/
public function sAbonnerNewsletterAction()
{
$em = $this->getDoctrine()
->getManager();
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$ok_submission = '';
$user = $this->getUser();
if('POST' == $request->getMethod() )
{
$email = $request->request->get('email');
$unAbonne = $em->getRePository(AbonneNewsletter::class)
->findOneByEmail($email);
if(null == $unAbonne){
$unAbonne = new AbonneNewsletter;
$unAbonne->setEmail($email);
$em->persist($unAbonne);
$em->flush();
$ok_submission = 'Enregistrement effectué';
} else{
$ok_submission = 'DejĂ inscrit';
}
}
$response = new Response;
$response->setStatusCode(Response::HTTP_OK);
$response->setContent($ok_submission);
return $response;
}
/**
* @Route("/home-slides", name="home_slides")
*/
public function homeSlidesAction()
{
$em = $this->getDoctrine()
->getManager();
$slides = $em->getRePository(SlidePromo::class)
->findBy(
array(
'estActif' => true
)
);
return $this->render('entreprise/slide.html.twig', [
'slides' => $slides
]);
}
/**
* @Route("/a-propos", name="a_propos")
*/
public function aProposAction()
{
return $this->render('entreprise/a_propos.html.twig', [
]);
}
/**
* @Route("/term", name="term")
*/
public function termAction()
{
$em = $this->getDoctrine()
->getManager();
$entreprise = $em->getRePository(Entreprise::class)
->findAll();
return $this->render('entreprise/term.html.twig', [
'entreprise' => $entreprise
]);
}
/**
* @Route("/privacy", name="privacy")
*/
public function privacyAction()
{
$em = $this->getDoctrine()
->getManager();
$entreprise = $em->getRePository(Entreprise::class)
->findAll();
return $this->render('entreprise/privacy.html.twig', [
'entreprise' => $entreprise
]);
}
/**
* @Route("/about", name="about")
*/
public function aboutAction()
{
$em = $this->getDoctrine()
->getManager();
$entreprise = $em->getRePository(Entreprise::class)
->findAll();
return $this->render('entreprise/about.html.twig', [
'entreprise' => $entreprise
]);
}
/**
* @Route("/menu-principal", name="menu_principal")
*/
public function menuPrincipalAction()
{
$em = $this->getDoctrine()
->getManager();
$categories = $em->getRePository(Category::class)
->findAll();
return $this->render('entreprise/menu_principal.html.twig', [
'categories' => $categories
]);
}
/**
* @Route("/home-menu", name="home_menu")
*/
public function homeMenuAction()
{
$em = $this->getDoctrine()
->getManager();
$categories = $em->getRePository(Category::class)
->findAll();
return $this->render('entreprise/home_menu.html.twig', [
'categories' => $categories
]);
}
/**
* @Route("/all-categories", name="all_categories")
*/
public function allCategoriesAction()
{
$em = $this->getDoctrine()
->getManager();
$categories = $em->getRePository(Category::class)
->findAll();
return $this->render('entreprise/all_categories.html.twig', [
'categories' => $categories
]);
}
/**
* @Route("/menu", name="menu")
*/
public function menuAction()
{
$em = $this->getDoctrine()
->getManager();
$categories = $em->getRePository(Category::class)
->findAll();
return $this->render('entreprise/menu.html.twig', [
'categories' => $categories
]);
}
/**
* @Route("/category/{slug/{paginer}/{pageCourante}", name="product_par_categorie")
*/
public function productRecentAction($paginer='page', $pageCourante = 1)
{
$em = $this->getDoctrine()
->getManager();
$products = $em->getRePository(Products::class)
->findAll();
$productNumber = 10;
$products = $em->getRePository(Product::class)
->getProducts($productNumber, $pageCourante);
return $this->render('entreprise/menu_principal.html.twig', [
'categories' => $categories
]);
}
/**
* @Route("/category/{slug/{paginer}/{pageCourante}", name="product_par_categorie")
*/
public function productParCategorieAction($paginer='page', $pageCourante = 1)
{
$em = $this->getDoctrine()
->getManager();
$categories = $em->getRePository(Category::class)
->findAll();
$productNumber = 10;
$products = $em->getRePository(Product::class)
->getProducts($productNumber, $pageCourante);
return $this->render('entreprise/menu_principal.html.twig', [
'categories' => $categories
]);
}
/**
* @Route("/galerie", name="galerie")
*/
public function galerieAction()
{
return $this->render('entreprise/galerie.html.twig', [
]);
}
/**
* @Route("/partenaire", name="partenaire")
*/
public function partenaireAction()
{
return $this->render('entreprise/partenaire.html.twig', [
]);
}
/**
* @Route("/services", name="services")
*/
public function servicesAction()
{
return $this->render('entreprise/services.html.twig', [
]);
}
/**
* @Route("/product/{leSlug}", name="display_product")
*/
public function displayProductAction(Product $product)
{
$em = $this->getDoctrine()
->getManager();
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$product->setTranslatableLocale($request->getLocale());
$em->refresh($product);
$reviews = $em->getRePository(Review::class)
->findByProduct($product);
$ok_submission = '';
$user = $this->getUser();
if('POST' == $request->getMethod() )
{
$review = new Review;
$rating = $request->request->get('rating');
$note = $request->request->get('review');
$review->setReview($note);
$review->setRating($rating);
$review->setProduct($product);
$review->setUser($user);
$em->persist($review);
$em->flush();
$ok_submission = 'Enregistrement effectué';
}
$moyenneRating = 0;
$recommandationNumber = 0;
$i = 0;
$fiveStar = array('number' => 0, 'pourcentage' => 0);
$fourStar = array('number' => 0, 'pourcentage' => 0);
$threeStar = array('number' => 0, 'pourcentage' => 0);
$twoStar = array('number' => 0, 'pourcentage' => 0);
$oneStar = array('number' => 0, 'pourcentage' => 0);
$rating = 0;
foreach($reviews as $review){
$i++;
$rating += $review->getRating();
if($rating > 2){
$recommandationNumber++;
}
switch ($rating){
case 1;
$oneStar['number']++;
break;
case 2;
$twoStar['number']++;
break;
case 3;
$threeStar['number']++;
break;
case 4;
$fourStar['number']++;
break;
case 5;
$fiveStar['number']++;
break;
}
}
if( $i > 0 ){
$moyenneRating = $rating/$i;
}
$shippingMethods = $em->getRePository(ShippingMethod::class)
->findAll();
$reviewNumber = count($reviews);
return $this->render('entreprise/display_product.html.twig', [
'product' => $product,
'ok_submission' => $ok_submission,
'reviews' => $reviews,
'reviewNumber' => $reviewNumber,
'shippingMethods' => $shippingMethods,
'moyenneRating' => $moyenneRating,
'recommandationNumber' => $recommandationNumber,
'fiveStar' => $fiveStar,
'fourStar' => $fourStar,
'threeStar' => $threeStar,
'twoStar' => $twoStar,
'oneStar' => $oneStar,
]);
}
/**
* @Route("/c/{slug}/{page}/{currentPage}", name="product_by_category")
*/
public function productByCategoryAction(Category $category, $page='page', $currentPage = 1)
{
$em = $this->getDoctrine()->getManager();
$numberByPage = 5;
$em = $this->getDoctrine()
->getManager();
$products = $em->getRePository(Product::class)
->findProductByCategory($numberByPage, $currentPage, $category->getSlug());
$pageAmount = ceil((count($products))/$numberByPage) ;
if(($currentPage > $pageAmount && $pageAmount >= 1) || $currentPage <=0){
throw $this->createNotFoundException('Nombre invalide');
}
return $this->render('entreprise/product_by_category.html.twig', [
'products' => $products,
'currentPage' => $currentPage,
'pageAmount' => $pageAmount,
'category' => $category
]);
}
/**
* @Route("/sc/{slug}/{page}/{currentPage}", name="product_by_sub_category")
*/
public function productBySubCategoryAction(SubCategory $subCategory, $page='page', $currentPage = 1)
{
$em = $this->getDoctrine()->getManager();
$numberByPage = 5;
$em = $this->getDoctrine()
->getManager();
$products = $em->getRePository(Product::class)
->findProductBySubCategory($numberByPage, $currentPage, $subCategory->getSlug());
$pageAmount = ceil((count($products))/$numberByPage) ;
if(($currentPage > $pageAmount && $pageAmount >= 1) || $currentPage <=0){
throw $this->createNotFoundException('Nombre invalide');
}
return $this->render('entreprise/product_by_sub_category.html.twig', [
'products' => $products,
'currentPage' => $currentPage,
'pageAmount' => $pageAmount,
'subCategory' => $subCategory
]);
}
/**
* @Route("/s/", name="search")
*/
public function searchAction($q='',$page='page',$currentPage=1)
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()->getManager();
$numberByPage = 20;
$em = $this->getDoctrine()
->getManager();
$q = $request->get('q');
$category = $request->get('category');
$minPrice = $request->get('minPrice');
$maxPrice = $request->get('maxPrice');
$products = $em->getRePository(Product::class)
->search($q, $category, $minPrice, $maxPrice, $numberByPage, $currentPage);
$categories = $em->getRePository(Category::class)
->findAll();
$pageAmount = ceil((count($products))/$numberByPage) ;
if(($currentPage > $pageAmount && $pageAmount >= 1) || $currentPage <=0){
throw $this->createNotFoundException('Nombre invalide');
}
return $this->render('entreprise/search.html.twig', [
'products' => $products,
'currentPage' => $currentPage,
'pageAmount' => $pageAmount,
'q' => $q,
'categories' => $categories,
'minPrice' => $minPrice,
'maxPrice' => $maxPrice,
'category' => $category
]);
}
/**
* @Route("/nos-products", name="nos_products")
*/
public function productsAction()
{
$em = $this->getDoctrine()
->getManager();
$products = $em->getRePository(Product::class)
->findAll();
return $this->render('entreprise/products.html.twig', [
'products' => $products
]);
}
/**
* @Route("/ajouter-product", name="ajouter_product")
*/
public function ajouterProductAction()
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
$product = new Product;
$form = $this->createForm(ProductType::class, $product);
$ok = '';
if('POST' == $request->getMethod() )
{
$form->handleRequest($request);
if( $form->isValid()){
$em->persist($product);
$em->flush();
$ok = 'Enregistrement effectué';
$product = new Product;
$form = $this->createForm(ProductType::class, $product);
}
}
return $this->render('entreprise/ajouter-product.html.twig', [
'form' => $form->createView(),
'ok' =>$ok
]);
}
/**
* @Route("/add-to-cart", name="add_to_cart")
*/
public function addToCartAction()
{
$response = new JsonResponse;
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Content-Type', 'application/json');
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
$nombreProduct = (int)$this->session->get('nombreProduct');
$cart = (array)$this->session->get('cart');
$product = '';
$price = '';
if('POST' == $request->getMethod() ){
$product = $request->get('product');
$quantity = $request->get('quantity');
$product = $em->getRePository(Product::class)
->findOneById($product);
$leProduct = array();
if(array_key_exists($product->getId(), $cart)){
$cart[$product->getId()]['quantity'] += 1;
} else{
$leProduct['id'] = $product->getId();
$leProduct['nom'] = $product->getNom();
$leProduct['price'] = $product->getPrice();
$leProduct['slug'] = $product->getLeSlug();
$leProduct['quantity'] = $quantity;
$leProduct['imagePrincipale'] = $product->getImagePrincipale()->getUrl();
$cart[$product->getId()] = $leProduct;
}
$nombreProduct = count($cart);
$montantProduct = 0;
foreach($cart as $prod){
$montantProduct += $prod['price'] * $prod['quantity'];
}
$montantOrder = (float)$this->session->get('montantOrder');
$couponValue = (float)$this->session->get('couponValue');
$livraison = (float)$this->session->get('livraison');
$montantOrder = $montantProduct - $couponValue + $livraison ;
$this->session->set('cart', $cart);
$this->session->set('nombreProduct', $nombreProduct);
$this->session->set('montantProduct', $montantProduct);
$this->session->set('montantOrder', $montantOrder);
$this->session->set('livraison', $livraison);
//return $this->redirectToRoute('display_product', array('leSlug' => $product->getLeSlug() ));
$data = array(
"montantProduit" => $montantProduct
);
$response->setContent(json_encode($data));
return $response;
}
return $response;
}
/**
* @Route("/side-cart-product", name="side_cart_product")
*/
public function sideCartProductsAction()
{
return $this->render('entreprise/side_cart_product.html.twig', [
'form' => $form->createView(),
'ok' =>$ok
]);
}
/**
* @Route("/apply-coupon", name="apply_coupon")
*/
public function applayCouponAction()
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
if('POST' == $request->getMethod() ){
$coupon = $request->get('coupon');
$coupon = $em->getRePository(Coupon::class)
->findOneByCode($coupon);
if(null != $coupon){
$toDay = new \DateTime();
if($coupon->getDateDebut() <= $toDay
&& $toDay <= $coupon->getDateFin()
//&& $coupon->getUsageLimit() > (integer)$coupon->getActualUsage()
){
$codePromo = $this->session->get('codePromo');
if($codePromo != $coupon->getCode()){
$this->session->set('codePromo', $coupon->getCode());
$montantOrder = (float)$this->session->get('montantOrder');
if($coupon->getIsPercentage()){
$couponValue = ($montantOrder * $coupon->getCouponValue())/100;
} else{
$couponValue = $coupon->getCouponValue();
}
$this->session->set('couponValue', $couponValue);
$montantOrder = $montantOrder - $couponValue;
$this->session->set('montantOrder', $montantOrder);
} else{
}
} else{
}
}
}
return $this->redirectToRoute('cart');
//return new Response();
}
/**
* @Route("/delete-cart-item/{product}", name="delete_cart_item")
*/
public function deleteCartItemAction($product)
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
$nombreProduct = (int)$this->session->get('nombreProduct');
$cart = (array)$this->session->get('cart');
$montantProduct = $this->session->get('montantProduct');
$couponValue = (float)$this->session->get('couponValue');
$livraison = (float)$this->session->get('livraison');
$montantOrder = (float)$this->session->get('montantOrder');
$montantProduct = $montantProduct - ($cart[$product]['quantity'] * $cart[$product]['price']);
unset($cart[$product]);
$nombreProduct = count($cart);
$montantOrder = $montantProduct + $livraison - $couponValue;
$this->session->set('cart', $cart);
$this->session->set('nombreProduct', $nombreProduct);
$this->session->set('montantProduct', $montantProduct);
$this->session->set('montantOrder', $montantOrder);
return $this->redirectToRoute('cart');
}
/**
* @Route("/update-cart/", name="update_cart")
*/
public function updateCartAction()
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
$cart = (array)$this->session->get('cart');
$product = '';
$price = '';
if('POST' == $request->getMethod() ){
$product = $request->get('product');
$quantity = (float)$request->get('quantity');
$product = $em->getRePository(Product::class)
->findOneById($product);
$leProduct = array();
if(array_key_exists($product->getId(), $cart)){
$cart[$product->getId()]['quantity'] = $quantity;
}
$montantProduct = 0;
foreach($cart as $prod){
$montantProduct += $prod['price'] * $prod['quantity'];
}
$couponValue = (float)$this->session->get('couponValue');
$livraison = (float)$this->session->get('livraison');
$montantOrder = $montantProduct + $livraison - $couponValue;
$this->session->set('cart', $cart);
$this->session->set('montantProduct', $montantProduct);
$this->session->set('montantOrder', $montantOrder);
}
return $this->redirectToRoute('cart');
}
/**
* @Route("/top-menu-cart", name="top_menu_cart")
*/
public function topMenuCartAction()
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
$nombreProduit = (int)$this->session->get('nombreProduct');
$montantProduit = (float)$this->session->get('montantProduit');
$panier = (array)$this->session->get('cart');
return $this->render('entreprise/top_menu_cart.html.twig', [
'panier' => $panier,
'nombreProduit' => $nombreProduit,
'montantProduit' => $montantProduit
]);
}
/**
* @Route("/cart-side-section", name="cart_side_session")
*/
public function cartSideSessionAction()
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
$nombreProduit = (int)$this->session->get('nombreProduct');
$montantOrder = (float)$this->session->get('montantOrder');
$couponValue = (float)$this->session->get('couponValue');
$panier = (array)$this->session->get('cart');
return $this->render('entreprise/cart_side_session.html.twig', [
'panier' => $panier,
'nombreProduit' => $nombreProduit,
'montantOrder' => $montantOrder,
'couponValue' => $couponValue
]);
}
/**
* @Route("/cart", name="cart")
*/
public function cartAction()
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
$nombreProduct = (int)$this->session->get('nombreProduct');
$cart = (array)$this->session->get('cart');
return $this->render('entreprise/cart.html.twig', [
'cart' => $cart
]);
}
/**
* @Route("/empty-cart", name="empty_cart")
*/
public function emptycartAction()
{
$this->emptyRealCart();
return $this->redirectToRoute('cart');
}
/**
* @Route("/indiquer-adresse", name="indiquer_adresse")
*/
public function indiquerAdresseAction()
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
$nombreProduit = (int)$this->session->get('nombreProduct');
$montantOrder = (float)$this->session->get('montantOrder');
$couponValue = (float)$this->session->get('couponValue');
$livraison = (float)$this->session->get('livraison');
$panier = (array)$this->session->get('cart');
$estConnecte = $this->container->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED');
if(!$estConnecte){
return $this->redirectToRoute('fos_user_security_login');
}
return $this->render('entreprise/indiquer_adresse.html.twig', [
'nombreProduit' => $nombreProduit,
'montantOrder' => $montantOrder,
'couponValue' => $couponValue,
'livraison' => $livraison,
'panier' => $panier
]);
}
/**
* @Route("/shipping", name="shipping")
*/
public function shippingAction()
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
$shippingMethods = $em->getRePository(ShippingMethod::class)
->findAll();
$user = $this->getUser();
if('POST' == $request->getMethod()){
$firstname = $request->request->get('firstname');
$lastname = $request->request->get('lastname');
$email = $request->request->get('email');
$telephone = $request->request->get('telephone');
$adresse = $request->request->get('adresse');
$user->setAdresse($adresse);
$user->setTelephone($telephone);
$user->setEmail($email);
$user->setLastname($lastname);
$user->setFirstname($firstname);
$em->flush();
}
$nombreProduit = (int)$this->session->get('nombreProduct');
$montantOrder = (float)$this->session->get('montantOrder');
$couponValue = (float)$this->session->get('couponValue');
$livraison = (float)$this->session->get('livraison');
$panier = (array)$this->session->get('cart');
return $this->render('entreprise/shipping.html.twig', [
'shippingMethods' => $shippingMethods,
'nombreProduit' => $nombreProduit,
'montantOrder' => $montantOrder,
'couponValue' => $couponValue,
'livraison' => $livraison,
'panier' => $panier
]);
}
/**
* @Route("/choose-payment", name="choose_payment")
*/
public function choosePaymentAction()
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
$user = $this->getUser();
$shippingMethod = 0;
$montantProduct = 0;
$shippingMethod = (float)$request->request->get('shippingMethod');
$montantOrder = (float)$this->session->get('montantOrder');
$livraison = (float)$this->session->get('livraison');
if('POST' == $request->getMethod()){
$montantOrder = $montantOrder - $livraison + $shippingMethod;
$this->session->set('livraison', $shippingMethod);
$this->session->set('montantOrder', $montantOrder);
}
return $this->render('entreprise/choose_payment.html.twig', [
'montantOrder' => $montantOrder
]);
}
/**
* @Route("/payment", name="payment")
*/
public function paymentAction()
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
$user = $this->getUser();
$shippingMethod = 0;
$montantProduct = 0;
$shippingMethod = (float)$request->request->get('shippingMethod');
$montantOrder = (float)$this->session->get('montantOrder');
$livraison = (float)$this->session->get('livraison');
if('POST' == $request->getMethod()){
$montantOrder = $montantOrder - $livraison + $shippingMethod;
$this->session->set('livraison', $shippingMethod);
$this->session->set('montantOrder', $montantOrder);
}
return $this->render('entreprise/payment.html.twig', [
'montantOrder' => $montantOrder
]);
}
/**
* @Route("/review", name="review")
*/
public function reviewAction()
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
$paymentMethod = '';
if('POST' == $request->getMethod()){
$paymentMethod = $request->request->get('paymentMethod');
$this->session->set('paymentMethod', $paymentMethod);
}
$nombreProduit = (int)$this->session->get('nombreProduct');
$montantOrder = (float)$this->session->get('montantOrder');
$couponValue = (float)$this->session->get('couponValue');
$livraison = (float)$this->session->get('livraison');
$panier = (array)$this->session->get('cart');
return $this->render('entreprise/review.html.twig', [
'paymentMethod' => $paymentMethod,
'nombreProduit' => $nombreProduit,
'montantOrder' => $montantOrder,
'couponValue' => $couponValue,
'livraison' => $livraison,
'panier' => $panier
]);
}
/**
* @Route("/save-order", name="save_order")
*/
public function saveOrderAction(\Swift_Mailer $mailer)
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
$nombreProduct = (int)$this->session->get('nombreProduct');
$user = $this->getUser();
$cart = (array)$this->session->get('cart');
$montantProduct = 0;
$order = new Order;
$this->doSaveOrder($order, $mailer);
return $this->render('entreprise/save_order.html.twig', [
'order' => $order
]);
}
public function doSaveOrder($order, \Swift_Mailer $mailer){
$em = $this->getDoctrine()
->getManager();
$cart = (array)$this->session->get('cart');
$montantProduct = 0;
$user = $this->getUser();
$order->setUser($user);
$order->setTotalHT($this->session->get('montantProduct'));
$order->setMontantLivraison($this->session->get('livraison'));
$order->setTotalTTC($this->session->get('montantOrder'));
$order->setPaymentMethod($this->session->get('paymentMethod'));
$order->setCouponValue($this->session->get('couponValue'));
$order->setCodePromo($this->session->get('codePromo'));
$em->persist($order);
$em->flush();
$productOrders = array();
foreach($cart as $prod){
$montantProduct += $prod['price'] * $prod['quantity'];
$leProduct = $em->getRepository(Product::class)->findOneById($prod['id']);
$leProduct->setStockDisponible($leProduct->getStockDisponible() - $prod['quantity']);
$productOrder = new ProductOrder;
$productOrder->setProduct($leProduct);
$productOrder->setOrder($order);
$productOrder->setQuantity($prod['quantity']);
$productOrder->setPrice($prod['price']);
$productOrder->setTotalHT($montantProduct);
$productOrder->setTotalTTC($montantProduct);
$em->persist($productOrder);
array_push($productOrders, $prod);
}
$codePromo = $this->session->get('codePromo');
$coupon = $em->getRepository(Coupon::class)
->findOneByCode($codePromo);
if(null != $coupon){
$coupon->setActualUsage($coupon->getActualUsage()+1);
$this->session->set('codePromo', '');
}
$em->flush();
//SendEmail and notifications
$message = (new \Swift_Message('Votre commande'))
->setFrom('contact@virtuelmarket.com')
->setTo($user->getEmail())
->setBody(
$this->renderView(
// templates/emails/registration.html.twig
'entreprise/order_email.html.twig',
['productOrders' => $productOrders]
),
'text/html'
);
$mailer->send($message);
$this->emptyRealCart();
}
/**
* @Route("/pay-order/", name="pay_order")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function payOrderAction(Request $request)
{
$em = $this->getDoctrine()
->getManager();
$user = $this->getUser();
$transId = rand(4560, 100000);
$transDate = new \DateTime();
$montantOrder = $this->session->get('montantOrder');
$amount = $montantOrder;
if('POST' == $request->getMethod()){
var_dump($_POST);
}
$commande = new Order;
//transaction id
$id_transaction = CinetPay::generateTransId();
// Payment description
$description_du_paiement = "Devenir premium";
// Payment Date must be on date format
$date_transaction = date("Y-m-d H:i:s");
// Amount
//$montant_a_payer = 100;
$montant_a_payer = $montantOrder;
// put a value that you can use to identify the buyer in your system
$identifiant_du_payeur = $user->getId().'@@@'.$commande->getId();
//Veuillez entrer votre apiKey
$apiKey = "1182873575626ba8e6c712e9.05305545";
//Secret Key: 127127447062796bafS
//Veuillez entrer votre siteId
$site_id = "792421";
//platform , utiliser PROD si vous avez créé votre compte sur www.cinetpay.com ou TEST si vous avez créé votre compte sur www.sandbox.cinetpay.com
$plateform = "PROD";
//la version , utilisé V1 si vous voulez utiliser la version 1 de l'api
$version = "V2";
// nom du formulaire CinetPay
$formName = "goCinetPay";
// notify url
$notify_url = $this->generateUrl('cinetpay_notify', array('transaction' => $id_transaction), UrlGeneratorInterface::ABSOLUTE_URL);
// return url
$return_url = $this->generateUrl('cinetpay_return', array('transaction' => $id_transaction), UrlGeneratorInterface::ABSOLUTE_URL);
// cancel url
$cancel_url = $this->generateUrl('cinetpay_cancel', array('transaction' => $id_transaction), UrlGeneratorInterface::ABSOLUTE_URL);
// cinetpay button type, must be 1, 2, 3, 4 or 5
$btnType = 4;
// button size, can be 'small' , 'large' or 'larger'
$btnSize = 'large';
// fill command class
$commande->setCpmTransId($id_transaction);
$commande->setCpmAmount($montant_a_payer);
$commande->setUser($user);
$em->persist($commande);
$em->flush();
// create html form for your basket
$CinetPay = new CinetPay($site_id, $apiKey, $plateform, $version);
$CinetPay->setTransId($id_transaction)
->setDesignation($description_du_paiement)
->setTransDate($date_transaction)
->setAmount($montant_a_payer)
->setDebug(true)// put it on true, if you want to activate debug
->setCustom($identifiant_du_payeur)// optional
->setNotifyUrl($notify_url)// optional
->setReturnUrl($return_url)// optional
->setCancelUrl($cancel_url);// optional
//->displayPayButton($formName, $btnType, $btnSize);
return $this->render('entreprise/pay_order.html.twig', [
'user' => $user,
'transId' => $transId,
'transDate' => $transDate,
'amount' => $amount,
'CinetPay' => $CinetPay,
'transaction' => $id_transaction,
'formName' => $formName,
'btnType' => $btnType,
'btnSize' => $btnSize,
]);
return $this->redirectToRoute('home');
}
/**
* @Route("/cinetpay/notify/{transaction}", name="cinetpay_notify")
*/
public function cinetPayNotifyAction($transaction)
{
$em = $this->getDoctrine()
->getManager();
$user = $this->getUser();
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$ok_submission = '';
$error = '';
$commande = $em->getRePository(Order::class)
->findOneByCpmTransId($transaction);
$messageRequete = '';
$errorMessage = '';
if('POST' == $request->getMethod()){
$id_transaction = $_POST['cpm_trans_id'];
$apiKey = "1182873575626ba8e6c712e9.05305545";
//Secret Key: 127127447062796bafS
//Veuillez entrer votre siteId
$site_id = "792421";
$plateform = "PROD"; // Valorisé à PROD si vous êtes en production
$version = "V2"; // Valorisé à V1 si vous voulez utiliser la version 1 de l'api
$CinetPay = new CinetPay($site_id, $apiKey, $plateform, $version);
// Reprise exacte des bonnes données chez CinetPay
$CinetPay->setTransId($id_transaction)->getPayStatus();
$cpm_site_id = $CinetPay->_cpm_site_id;
$signature = $CinetPay->_signature;
$cpm_amount = $CinetPay->_cpm_amount;
$cpm_trans_id = $CinetPay->_cpm_trans_id;
$cpm_custom = $CinetPay->_cpm_custom;
$cpm_currency = $CinetPay->_cpm_currency;
$cpm_payid = $CinetPay->_cpm_payid;
$cpm_payment_date = $CinetPay->_cpm_payment_date;
$cpm_payment_time = $CinetPay->_cpm_payment_time;
$cpm_error_message = $CinetPay->_cpm_error_message;
$payment_method = $CinetPay->_payment_method;
$cpm_phone_prefixe = $CinetPay->_cpm_phone_prefixe;
$cel_phone_num = $CinetPay->_cel_phone_num;
$cpm_ipn_ack = $CinetPay->_cpm_ipn_ack;
$created_at = $CinetPay->_created_at;
$updated_at = $CinetPay->_updated_at;
$cpm_result = $CinetPay->_cpm_result;
$cpm_trans_status = $CinetPay->_cpm_trans_status;
$cpm_designation = $CinetPay->_cpm_designation;
$buyer_name = $CinetPay->_buyer_name;
$commande = $em->getRePository(Order::class)
->findOneByCpmTransId($cpm_trans_id);
if($commande->getCpmTransStatus() == '00'){
//Paiement déjà validé
} else{
$commande->setPaymentMethod($payment_method);
$commande->setCpmPayid($cpm_payid);
$commande->setBuyerName($buyer_name);
$commande->setSignature($signature);
$commande->setCelPhoneNum($cel_phone_num);
//$commande->setCpmPaymentDate($cpm_payment_date . ' ' . $cpm_payment_time);
if ($commande->getCpmAmount() == $cpm_amount) {
// C'est OK : On continue le remplissage des nouvelles données
$commande->setCpmErrorMessage($cpm_error_message);
$commande->setCpmTransStatus($cpm_result);
//$commande->setCpmTransStatus($cpm_trans_status);
if($cpm_result == '00'){
//Le paiement est bon
// Traitez et delivrez le service au client
$mailer = new \Swift_Mailer;
$this->doSaveOrder($commande, $mailer);
$errorMessage = 'Paiement effectué avec succès';
return $this->redirect($this->generateUrl('home'));
}else{
//Le paiement a échoué
}
} else {
//Fraude : montant payé ' . $cpm_amount . ' ne correspond pas au montant de la commande
$commande->setCpmTransStatus('-1');
//$commande->setTransStatus('REFUSED');
}
// On met Ă jour notre ligne
//$commande->update();
}
}
return $this->render('entreprise/cinetpay_notify.html.twig', [
'pageTitle' => $pageTitle,
'user' => $user,
'errorMessage' => $errorMessage
]);
}
/**
* @Route("/cinetpay/return/{transaction}", name="cinetpay_return")
*/
public function cinetPayReturnAction($transaction)
{
$em = $this->getDoctrine()
->getManager();
$user = $this->getUser();
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$ok_submission = '';
$error = '';
$messageRequete = '';
$errorMessage = '';
$pageTitle = 'Devenir premium';
$commande = $em->getRePository(Commande::class)
->findOneByCpmTransId($transaction);
if('POST' == $request->getMethod()){
$errorMessage = 'Paiement effectué avec succès';
$id_transaction = $_POST['cpm_trans_id'];
$apiKey = "1182873575626ba8e6c712e9.05305545";
//Secret Key: 127127447062796bafS
//Veuillez entrer votre siteId
$site_id = "792421";
$plateform = "PROD"; // Valorisé à PROD si vous êtes en production
$version = "V2"; // Valorisé à V1 si vous voulez utiliser la version 1 de l'api
$CinetPay = new CinetPay($site_id, $apiKey, $plateform, $version);
// Reprise exacte des bonnes données chez CinetPay
$CinetPay->setTransId($id_transaction)->getPayStatus();
$cpm_site_id = $CinetPay->_cpm_site_id;
$signature = $CinetPay->_signature;
$cpm_amount = $CinetPay->_cpm_amount;
$cpm_trans_id = $CinetPay->_cpm_trans_id;
$cpm_custom = $CinetPay->_cpm_custom;
$cpm_currency = $CinetPay->_cpm_currency;
$cpm_payid = $CinetPay->_cpm_payid;
$cpm_payment_date = $CinetPay->_cpm_payment_date;
$cpm_payment_time = $CinetPay->_cpm_payment_time;
$cpm_error_message = $CinetPay->_cpm_error_message;
$payment_method = $CinetPay->_payment_method;
$cpm_phone_prefixe = $CinetPay->_cpm_phone_prefixe;
$cel_phone_num = $CinetPay->_cel_phone_num;
$cpm_ipn_ack = $CinetPay->_cpm_ipn_ack;
$created_at = $CinetPay->_created_at;
$updated_at = $CinetPay->_updated_at;
$cpm_result = $CinetPay->_cpm_result;
$cpm_trans_status = $CinetPay->_cpm_trans_status;
$cpm_designation = $CinetPay->_cpm_designation;
$buyer_name = $CinetPay->_buyer_name;
$customArray = explode('@@@', $cpm_custom);
$boutique = $em->getRepository(Boutique::class)
->findOneById($customArray[1]);
return $this->redirect($this->generateUrl('gestion.show_shop', array('slug' =>$boutique->getSlug())));
}
return $this->render('gestion/cinetpay_return.html.twig', [
'pageTitle' => $pageTitle,
'user' => $user,
'errorMessage' => $errorMessage
]);
}
/**
* @Route("/cinetpay/cancel/{transaction}", name="cinetpay_cancel")
*/
public function cinetPayCancelction($transaction)
{
$em = $this->getDoctrine()
->getManager();
$user = $this->getUser();
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$ok_submission = '';
$error = '';
$commande = $em->getRePository(Commande::class)
->findOneByCpmTransId($transaction);
$messageRequete = '';
if('POST' == $request->getMethod()){
var_dump($_POST);
}
return $this->render('entreprise/cinetpay_cancel.html.twig', [
'user' => $user
]);
}
public function emptyRealCart()
{
$this->session->set('cart', array());
$this->session->set('nombreProduct', '');
$this->session->set('montantProduct', 0);
$this->session->set('montantOrder', 0);
$this->session->set('paymentMethod', '');
$this->session->set('livraison', 0);
$this->session->set('couponValue', 0);
$this->session->set('codePromo', '');
}
/**
* @Route("/contact", name="contact")
*/
public function contactAction()
{
return $this->render('entreprise/contact.html.twig', [
]);
}
/**
* @Route("/faq", name="faq")
*/
public function faqAction()
{
$em = $this->getDoctrine()
->getManager();
$faqs = $em->getRePository(SujetFAQ::class)
->findAll();
return $this->render('entreprise/faq.html.twig', [
'faqs' => $faqs
]);
}
/**
* @Route("/faq/nouveau-sujet", name="faq_nouveau_sujet")
*/
public function nouveauSujetFaqAction()
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
$sujetFAQ = new SujetFAQ;
$form = $this->createForm(SujetFAQType::class, $sujetFAQ);
$ok = '';
if('POST' == $request->getMethod() )
{
$form->handleRequest($request);
if( $form->isValid()){
$em->persist($sujetFAQ);
$em->flush();
$ok = 'Enregistrement effectué';
$sujetFAQ = new SujetFAQ;
$form = $this->createForm(SujetFAQType::class, $sujetFAQ);
}
}
return $this->render('entreprise/faq_nouveau_sujet.html.twig', [
'form' => $form->createView(),
'ok' => $ok
]);
}
/**
* @Route("/faq/avis-utilisateur", name="avis_utilisateur")
*/
public function avisUtilisateurAction()
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
$avisUtilisateurs = $em->getRePository(AvisUtilisateur::class)
->findAll();
return $this->render('entreprise/avis_utilisateur.html.twig', [
'avisUtilisateurs' => $avisUtilisateurs
]);
}
/**
* @Route("/faq/nouvel-avis-utilisateur", name="nouvel_avis_utilisateur")
*/
public function nouvelAvisUtilisateurAction()
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
$avisUtilisateur = new AvisUtilisateur;
$form = $this->createForm(AvisUtilisateurType::class, $avisUtilisateur);
$ok = '';
if('POST' == $request->getMethod() )
{
$form->handleRequest($request);
if( $form->isValid()){
$em->persist($avisUtilisateur);
$em->flush();
$ok = 'Enregistrement effectué';
$avisUtilisateur = new AvisUtilisateur;
$form = $this->createForm(AvisUtilisateurType::class, $avisUtilisateur);
}
}
return $this->render('entreprise/nouvel_avis_utilisateur.html.twig', [
'form' => $form->createView(),
'ok' => $ok
]);
}
/**
* @Route("/faq/modifier-avis-utilisateur/{id}", name="modifier_avis_utilisateur")
*/
public function modifierAvisUtilisateurAction(AvisUtilisateur $avisUtilisateur)
{
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
$em = $this->getDoctrine()
->getManager();
$form = $this->createForm(AvisUtilisateurEditType::class, $avisUtilisateur);
$ok = '';
if('POST' == $request->getMethod() )
{
$form->handleRequest($request);
if( $form->isValid()){
$em->persist($avisUtilisateur);
$em->flush();
$ok = 'Enregistrement effectué';
}
}
return $this->render('entreprise/modifier_avis_utilisateur.html.twig', [
'form' => $form->createView(),
'ok' => $ok
]);
}
/**
* @Route("/faq/supprimer-avis-utilisateur/{id}", name="supprimer_avis_utilisateur")
*/
public function supprimerAvisUtilisateurAction(AvisUtilisateur $avisUtilisateur)
{
$em = $this->getDoctrine()
->getManager();
$em->remove($avisUtilisateur);
$em->flush();
return $this->redirectToRoute('accueil');
}
}