<?php
/**
* Created by PhpStorm.
* User: Slim Sayari
* Date: 10/07/2019
* Time: 13:43
*/
namespace App\Controller\Front;
use App\Entity\Carrier;
use App\Entity\Cart;
use App\Entity\DistributionCart;
use App\Entity\DistributionFlyer;
use App\Entity\Orders;
use App\Repository\DistributionCartRepository;
use App\Services\CartService;
use App\Services\CheckoutService;
use App\Services\Notifications;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Environment;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use App\Services\OrderService;
use App\Services\DistributionFlyerService;
/**
* @Route("/distribution")
*/
class DistributionController extends AbstractController
{
private $em;
private $session;
private $trans;
private $cart_service;
private $authenticationUtils;
private $checkout_service;
private $notifications;
private $distributionFlyerService;
private $router;
public function __construct(
Notifications $notifications,
OrderService $order_service,
CheckoutService $checkout_service,
CartService $cart_service,
SessionInterface $session,
TranslatorInterface $trans,
EntityManagerInterface $em,
AuthenticationUtils $authenticationUtils,
DistributionFlyerService $distributionFlyerService,
UrlGeneratorInterface $router
) {
$this->em = $em;
$this->trans = $trans;
$this->session = $session;
$this->cart_service = $cart_service;
$this->checkout_service = $checkout_service;
$this->authenticationUtils = $authenticationUtils;
$this->order_service = $order_service;
$this->notifications = $notifications;
$this->distributionFlyerService = $distributionFlyerService;
$this->router = $router;
}
/**
* @Route("/index", name="distribution_calcule")
*/
public function index(Request $request): Response
{
$data = $this->distributionFlyerService->add($request);
if(isset($data['success'])&&$data['success']){
return new RedirectResponse($this->router->generate('distribution_add_to_carte',array('id'=>$data['id_distribution'])));
}
$result = $this->cart_service->checkB2bUser($this->getUser());
$abonnement = $result['abonnement'];
$isAbonnementProInCart = $result['isAbonnementProInCart'];
if($abonnement['is_abonnemen_pro'] || $isAbonnementProInCart){
return $this->render('front/b2b/distribution/index.html.twig', $data);
}else{
return $this->render('front/distribution/index.html.twig', $data);
}
}
/**
* @Route("/edit/{id}", name="distribution_calcule_edit")
*/
public function edit(Request $request,$id): Response
{
$data = $this->distributionFlyerService->edit($request,$id);
if(isset($data['success'])&&$data['success']){
return new RedirectResponse($this->router->generate('distribution_add_to_carte',array('id'=>$data['id_distribution'])));
}
$result = $this->cart_service->checkB2bUser($this->getUser());
$abonnement = $result['abonnement'];
$isAbonnementProInCart = $result['isAbonnementProInCart'];
if($abonnement['is_abonnemen_pro'] || $isAbonnementProInCart){
return $this->render('front/b2b/distribution/index.html.twig', $data);
}else{
return $this->render('front/distribution/index.html.twig', $data);
}
}
/**
* @Route("/delete/{id}", name="distribution_calcule_delete")
*/
public function delete(Request $request,$id): Response
{
$data = $this->distributionFlyerService->delete($request,$id);
$refererPathInfo = Request::create($request->headers->get('referer'))->getPathInfo();
$routeInfos = $this->router->match($refererPathInfo);
$route = isset($routeInfos['_route'])? $routeInfos['_route'] :'checkout';
return $this->redirectToRoute($route);
}
/**
* @Route("/add-to-carte/{id}", name="distribution_add_to_carte")
*/
public function view(Request $request,$id): Response
{
$distribution = $this->em->getRepository(DistributionCart::class)->find($id);
if($request->getMethod() == 'POST'){
$is_add = $this->distributionFlyerService->addToCart($id);
if($is_add){
return new RedirectResponse($this->router->generate('checkout'));
}else{
die('ereur!') ;
}
}
$result = $this->cart_service->checkB2bUser($this->getUser());
$abonnement = $result['abonnement'];
$isAbonnementProInCart = $result['isAbonnementProInCart'];
if($abonnement['is_abonnemen_pro'] || $isAbonnementProInCart){
return $this->render('front/b2b/distribution/view.html.twig', array('distribution'=>$distribution));
}else{
return $this->render('front/distribution/view.html.twig', array('distribution'=>$distribution));
}
}
}