<?php
namespace App\Controller\Front;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Session\Session;
use App\Manager\CartManager;
use App\Manager\CustomerManager;
use App\Manager\OrderManager;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class CheckoutController extends FrontController
{
/**
* @Route("/{_locale}/checkout/cart-validation", name="checkout_cart_validation")
*/
public function cartValidation(Request $request, CartManager $cartMgr, CustomerManager $customerMgr, Session $session)
{
$cart = $cartMgr->getCart();
$noGift = $request->get('noGift',false);
if($cart->canAddGift() && !$cart->hasGift() && ($noGift===false) && !$customerMgr->isExpert()){
$gifts = $cartMgr->getGifts();
if(count($gifts)){
$popup = $this->renderView('front/cart/gift-choice.html.twig', [
'popup'=>true,
'gifts'=>$gifts
]);
$session->getFlashBag()->add('popup', $popup);
return $this->redirectToRoute('cart_details');
}
}
$customer = $customerMgr->getCustomer();
if(empty($customer))
return $this->redirectToRoute('customer_login',[
'from' => 'checkout_cart_validation'
]);
if(!$cartMgr->checkCartItems($cart)){
return $this->redirectToRoute('cart_details');
}
return $this->redirectToRoute('checkout_shipping');
}
/**
* @Route("/{_locale}/aj/checkout/cart", name="checkout_cart")
*/
public function cartBlock(CartManager $cartMgr, CustomerManager $customerMgr)
{
$em = $this->getDoctrine()->getManager();
$customer = $customerMgr->getCustomer();
if(empty($customer))
return $this->redirectToRoute('cart_details');
$output = [
'success' => true,
'content' => $this->renderView('front/checkout/cart.html.twig',[
'cart'=>$cartMgr->getCart()
])
];
return $this->renderJsonResponse($output);
}
/**
* @Route("/{_locale}/checkout/shipping", name="checkout_shipping")
*/
public function shipping(CartManager $cartMgr, CustomerManager $customerMgr, Session $session, \App\Manager\CarrierManager $carrierMgr, \Doctrine\ORM\EntityManagerInterface $em, \App\Manager\PriceManager $priceMgr)
{
$em = $this->getDoctrine()->getManager();
$customer = $customerMgr->getCustomer();
if(empty($customer))
return $this->redirectToRoute('cart_details');
$cart = $cartMgr->getCart();
$priceMgr->updateCartPrice($cart);
$shippingAddress = $cart->getShippingAddress();
$billingAddress = $cart->getBillingAddress();
if(empty($shippingAddress)){
$shippingAddress = $customer->getDefaultAddress();
}
if(empty($billingAddress)){
$billingAddress = $customer->getDefaultAddress();
}
$form = $this->createForm(\App\Form\Checkout\CartCommentType::class, $cart, [
'action' => $this->generateUrl('checkout_shipping_validation'),
'method' => 'post'
]);
$carriers = $carrierMgr->getCarriersByCountry($shippingAddress->getCountry(), $customerMgr->getPriceContext());
return $this->render('front/checkout/shipping.html.twig',[
'carriers'=>[],
'cart'=>$cart,
'shippingAddress'=>$shippingAddress,
'billingAddress'=>$billingAddress,
'sameAs'=>$shippingAddress==$billingAddress,
'form'=>$form->createView(),
'carriers'=>$carriers
]);
}
/**
* @Route("/{_locale}/checkout/carrier/select", name="checkout_carrier_select")
*/
public function carrierSelect(Request $request, CartManager $cartMgr, CustomerManager $customerMgr, Session $session, \Doctrine\ORM\EntityManagerInterface $em)
{
$em = $this->getDoctrine()->getManager();
$customer = $customerMgr->getCustomer();
if(empty($customer))
return $this->redirectToRoute('cart_details');
$output = [
'success' => false,
'message' => ''
];
if($request->isMethod('POST')){
$carrierId = $request->get('carrier',false);
if($carrierId){
$id = $request->get('id',false);
$code = $request->get('code',false);
$carrier = $em->getRepository('App:Carrier')->find($carrierId);
if($id && $code && $carrier){
$store = $em->getRepository('App:Store')->findOneBy([
'id' => $id,
'code' => $code
]);
if($store){
try{
$cart = $cartMgr->getCart();
$cart->setCarrier($carrier);
$cart->setShippingInfos($store->getCode());
$cart->save();
$output['success'] = true;
} catch (\Exception $ex) {
$output['message'] = $ex->getMessage();
}
}else{
$output['message'] = "Point de retrait non valide.";
}
}else{
$output['message'] = "Transporteur non valide.";
}
}else{
$output['message'] = "Données incorrectes...";
}
}
return $this->renderJsonResponse($output);
}
/**
* @Route("/{_locale}/checkout/shipping/collect/{id}", name="checkout_pickup_select", requirements={"id"="\d+"})
*/
public function pickupSelect(CartManager $cartMgr, CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em, $id, \Symfony\Component\HttpFoundation\Session\SessionInterface $session)
{
$customer = $customerMgr->getCustomer();
if(empty($customer))
return $this->redirectToRoute('checkout_identification');
$cart = $cartMgr->getCart();
$store = $em->getRepository('App:Store')->find($id);
if(empty($store))
throw new NotFoundHttpException ();
$cart->setPickupStore($store);
$cartMgr->save($cart);
return $this->redirectToRoute('checkout_shipping');
}
/**
* @Route("/{_locale}/checkout/shipping-validation", name="checkout_shipping_validation")
*/
public function shippingValidation(Request $request, CartManager $cartMgr, CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em, \Symfony\Component\HttpFoundation\Session\SessionInterface $session, \Symfony\Contracts\Translation\TranslatorInterface $translator)
{
$customer = $customerMgr->getCustomer();
if(empty($customer))
return $this->redirectToRoute('checkout_identification');
$cart = $cartMgr->getCart();
$carrier = $cart->getCarrier();
if(empty($carrier)){
$session->getFlashBag()->add('error',$translator->trans('Veuillez sélectionner un mode de livraison.'));
return $this->redirectToRoute('checkout_shipping');
}
$form = $this->createForm(\App\Form\Checkout\CartCommentType::class, $cart, [
'action' => $this->generateUrl('checkout_shipping_validation'),
'method' => 'post'
]);
if($request->isMethod('POST')){
$form->handleRequest($request);
if($form->isValid()){
$cart = $form->getData();
$em->persist($cart);
$em->flush();
}
}
return $this->redirectToRoute('checkout_payment');
}
/**
* @Route("/{_locale}/checkout/payment", name="checkout_payment")
*/
public function payment(CartManager $cartMgr, CustomerManager $customerMgr)
{
$em = $this->getDoctrine()->getManager();
$customer = $customerMgr->getCustomer();
if(empty($customer))
return $this->redirectToRoute('cart_details');
$cart = $cartMgr->getCart();
$shippingAddress = $cart->getShippingAddress();
$billingAddress = $cart->getBillingAddress();
if(empty($billingAddress)){
$billingAddress = $customer->getDefaultAddress();
}
$form = $this->createForm(\App\Form\Checkout\CartPaymentType::class, $cart, [
'action' => $this->generateUrl('checkout_payment_validation'),
'method' => 'post'
]);
return $this->render('front/checkout/payment.html.twig',[
'customer' => $customer,
'cart' => $cart,
'billingAddress' => $billingAddress,
'form' => $form->createView()
]);
}
/**
* @Route("/{_locale}/checkout/payment-validation", name="checkout_payment_validation")
*/
public function paymentValidation(Request $request, CartManager $cartMgr, CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em, Session $session, \Symfony\Contracts\Translation\TranslatorInterface $translator)
{
$customer = $customerMgr->getCustomer();
if(empty($customer))
return $this->redirectToRoute('checkout_identification');
$cart = $cartMgr->getCart();
$payment = $request->get('payment');
$form = $this->createForm(\App\Form\Checkout\CartPaymentType::class, $cart, [
'action' => $this->generateUrl('checkout_payment_validation'),
'method' => 'post'
]);
if($request->isMethod('POST') && ctype_alpha($payment)){
$form->handleRequest($request);
if($form->isValid()){
$cart = $form->getData();
$paymentMean = $em->getRepository(\App\Entity\PaymentMean::class)->findOneByCode($payment);
if($paymentMean){
$output['success'] = true;
$cart->setPaymentMean($paymentMean);
$em->persist($cart);
$em->flush();
return $this->redirectToRoute('checkout_review');
}else{
$session->getFlashBag()->add('error',$translator->trans('Mode de payment inconnu... ('.$payment.')'));
}
}
}
return $this->redirectToRoute('checkout_payment');
if($request->isMethod('POST')){
$form->handleRequest($request);
if($form->isValid()){
$cart = $form->getData();
}
}
// if($payment == 'TEST'){
// try{
// $order = $orderMgr->createFromCart($cart,$payment);
// if($order){
// $session->set('order_id',$order->getId());
// //die('id : '.$order->getId().' '.$session->get('order_id'));
// return $this->redirectToRoute('checkout_review');
// }
// }
// catch (\Exception $ex) {
// $session->getFlashBag()->add('error', $ex->getMessage());
// }
// }else{
// $session->getFlashBag()->add('error', 'Moyen de paiement non valide.');
// }
return $this->redirectToRoute('checkout_payment');
}
/**
* @Route("/{_locale}/checkout/review", name="checkout_review")
*/
public function review(CartManager $cartMgr, CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em, Session $session, \App\Manager\PriceManager $priceMgr, \Symfony\Contracts\Translation\TranslatorInterface $translator)
{
$em = $this->getDoctrine()->getManager();
$customer = $customerMgr->getCustomer();
if(empty($customer))
return $this->redirectToRoute('cart_details');
$cart = $cartMgr->getCart();
if(!$cartMgr->checkShippingRate($cart)){
$session->getFlashBag()->add('error',$translator->trans('Veuillez selectionner un mode de livraison disponible pour votre adresse de livraison...'."(".$cart->getCarrier()->getId().")"));
return $this->redirectToRoute('checkout_shipping');
}
$priceMgr->updateCartPrice($cart);
$paymentMean = $cart->getPaymentMean();
return $this->render('front/checkout/review.html.twig',[
'customer' => $customer,
'cart' => $cart,
'buttonLabel' => ($paymentMean && $paymentMean->getCode() == 'cb') ? $translator->trans('Procéder au paiement') : $translator->trans('Confirmer votre commande')
]);
}
/**
* @Route("/{_locale}/checkout/review-validation", name="checkout_review_validation")
*/
public function reviewValidation(Request $request, CartManager $cartMgr, \App\Manager\OrderManager $orderMgr, CustomerManager $customerMgr, Session $session, \App\Service\Mailer $mailer, \App\Manager\PriceManager $priceMgr, \Symfony\Contracts\Translation\TranslatorInterface $translator, \App\Manager\DiscountManager $discountMgr)
{
$em = $this->getDoctrine()->getManager();
$customer = $customerMgr->getCustomer();
if(empty($customer))
return $this->redirectToRoute('cart_details');
$cart = $cartMgr->getCart();
$priceMgr->updateCartPrice($cart);
if(!$cartMgr->checkDropShipping($cart)){
$session->getFlashBag()->add('error',$translator->trans('Veuillez utiliser des adresses de livraison et de facturation différentes pour utiliser la livraison chez votre client...'));
return $this->redirectToRoute('checkout_review');
}
$couponCheck = $discountMgr->checkCoupons($cart);
if($couponCheck!==true){
$session->getFlashBag()->add('error',$translator->trans($couponCheck));
return $this->redirectToRoute('checkout_review');
}
$paymentMean = $cart->getPaymentMean();
$order = $orderMgr->createFromCart($cart, $request->getLocale());
$session->set('orderId',$order->getId());
if($paymentMean->getCode() == 'cb'){
return $this->redirectToRoute('cnp_redirect');
}elseif($paymentMean->getCode() == 'paypal'){
return $this->redirectToRoute('paypal_create_payment');
}else{
$em->refresh($order);
$mailer->sendOrderConfirmation($order);
$cartMgr->empty();
}
return $this->redirectToRoute('checkout_confirmation');
}
/**
* @Route("/{_locale}/checkout/confirmation", name="checkout_confirmation")
*/
public function confirmation(CartManager $cartMgr, CustomerManager $customerMgr, \Doctrine\ORM\EntityManagerInterface $em, Session $session)
{
$em = $this->getDoctrine()->getManager();
$customer = $customerMgr->getCustomer();
if(empty($customer))
return $this->redirectToRoute('cart_details');
$cartMgr->empty();
$orderId = $session->get('orderId');
$session->remove('orderId');
return $this->render('front/checkout/confirmation.html.twig');
}
}