<?php
/**
* Created by PhpStorm.
* User: Slim Sayari
* Date: 10/07/2019
* Time: 13:43
*/
namespace App\Controller\Front\AbonnementPro;
use App\Entity\Abonnement;
use App\Entity\AbonnementPay;
use App\Entity\AbonnementPro;
use App\Entity\AbonnementProCart;
use App\Entity\Cart;
use App\Entity\Customer;
use App\Entity\Product;
use App\Form\AdresseFacturationAbonnementType;
use App\Services\AbonnementService;
use App\Services\CartService;
use App\Services\CheckoutService;
use App\Services\FactureService;
use App\Services\Notifications;
use App\Services\OrderService;
use App\Entity\Adresse;
use PhpParser\Node\Expr\Clone_;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
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 App\Services\Liseuse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use App\Services\Compte;
/**
* @Route("/abonnement-pro")
*/
class ProcessPaiementController extends AbstractController
{
private $em;
private $session;
private $params;
private $cart_service;
private $abonnement_service;
private $facture_service;
private $notifications;
private $compte;
public function __construct(Compte $compte,Notifications $notifications,FactureService $factureService,AbonnementService $abonnement_service, OrderService $order_service ,CheckoutService $checkout_service,CartService $cart_service,SessionInterface $session,AuthenticationUtils $authenticationUtils , EntityManagerInterface $em,ParameterBagInterface $params)
{
$this->em = $em;
$this->session = $session;
$this->params = $params;
$this->cart_service = $cart_service;
$this->checkout_service = $checkout_service;
$this->authenticationUtils = $authenticationUtils;
$this->order_service = $order_service;
$this->abonnement_service = $abonnement_service ;
$this->facture_service = $factureService ;
$this->notifications = $notifications ;
$this->compte = $compte ;
}
/**
* @Route("/adresse-facture", name="abonnementPro_adresse_facture")
*/
public function index(Request $request) : Response
{
//check abonnement exist user
$isPageCheckout = $request->get('isPageCheckout');
if($this->getUser()) {
$abonnement = $this->cart_service->checkIsAbonnementPro($this->getUser());
if ($abonnement['is_abonnemen_pro'] && $abonnement['is_admin']) {
return $this->redirectToRoute('edit_abonnement_page_b2b');
}else if($abonnement['is_abonnemen_pro'] && !$abonnement['is_admin']){
$myAbonnement = $abonnement['compte_abonnement_object'];
$response = $this->render('front/abonnement-pro/deja-abonnee.html.twig',[
'is_admin' => false,
'myAbonnement' => $myAbonnement
]);
return $response;
}else{
$token_cart = $this->session->get('token_cart');
if ($token_cart) {
$cart = $this->em->getRepository(Cart::class)->findOneBy(array('token' => $token_cart));
if($cart && $cart->getAbonnementProCart()){
return $this->redirectToRoute('accueil_page_b2b');
}
}
$typeComptePaiement = $request->get('typeComptePaiement',1);
$typeAbonnement = $request->get('typeAbonnement',1);
$abonnementPro = $this->em->getRepository(AbonnementPro::class)->findOneBy(array('typeAbonnement'=>$typeAbonnement));
$res = $this->cart_service->createCompteAboProDirect($request,$this->getUser(),$abonnementPro,$typeComptePaiement,$request->get('isFree',0));
if($res){
if($isPageCheckout){
return $this->redirectToRoute('checkout');
}else{
return $this->redirectToRoute('accueil_page_b2b');
}
}
}
}
$typeAbonnement = AbonnementPro::TYPE_PRO;
$typeComptePaiement = $request->get('typeComptePaiement',1);
$isFree = $request->get('isFree',0);
$abonnementPro = false;
if ($typeAbonnement && $typeComptePaiement) {
$abonnementPro = $this->em->getRepository(AbonnementPro::class)->findOneBy(array('typeAbonnement'=>$typeAbonnement));
}
if(!$abonnementPro){
return $this->redirectToRoute('abonnementPro_index');
}
//check abonnement pro in cart
$token_cart = $this->session->get('token_cart');
if ($token_cart) {
$cart = $this->em->getRepository(Cart::class)->findOneBy(array('token' => $token_cart));
if($cart && $cart->getAbonnementProCart()){
return $this->redirectToRoute('accueil_page_b2b');
}
}
$adresseExist = null;
if($this->getUser()){
$adresseExist = $this->em->getRepository(Adresse::class)->getAdresseFacturationByUser($this->getUser());
if(!$adresseExist){
$adresseExist = $this->getUser();
}
}
$adresse = new Adresse();
$form = $this->createForm(AdresseFacturationAbonnementType::class, $adresse,array('adresseExist'=>$adresseExist));
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$newUser = false;
if(!is_object($this->getUser())){
$data['firstname'] = $adresse->getFirstname();
$data['lastname'] = $adresse->getLastname();
$data['email'] = $adresse->getEmail();
$data['password'] = $request->get('password');
$result = $this->compte->addUser($data);
if(isset($result['erreur'])){
$response = $this->render('front/abonnement-pro/checkout/adresse-facture-new.html.twig',[
'form'=>$form->createView(),
'last_username' => $this->authenticationUtils->getLastUsername(),
'error' => $this->authenticationUtils->getLastAuthenticationError(),
'adresse' => $adresse,
'erreurEmail' => $result['erreur']
]);
return $response;
}else{
$user = $result['user'];
$user->setPhone($adresse->getPhone());
$user->setCodePostal($adresse->getCodePostal());
$user->setAddress($adresse->getAdresse());
$user->setCity($adresse->getCity());
$user->setConpany($adresse->getConpany());
$user->setBatiment($adresse->getBatiment());
$this->em->persist($user);
$this->em->flush();
$this->notifications->createUserB2b($user,$request->get('password')); //send mail
$this->notifications->sendMailBienVenu($user); // mail bienvenu
$this->notifications->sendMailInscrit($user); // mail inscrit is accepte send sms
$newUser = true;
}
}
$adresse->setTypeAdresse(Adresse::ADRESSEFACTURATION);
$this->em->persist($adresse);
$this->em->flush();
$adresseLivraison = Clone $adresse;
$adresseLivraison->setTypeAdresse(Adresse::ADRESSELIVRAISON);
if($newUser){
$adresseLivraison->setCustomer($result['customer']);
}else{
$adresseLivraison->setCustomer( $this->em->getRepository(Customer::class)->findOneBy(array('user'=>$this->getUser()->getId())));
}
$this->em->persist($adresseLivraison);
$this->em->flush();
if ($request->get('typeAbonnementBtb')) {
$abonnementPro = $this->em->getRepository(AbonnementPro::class)->findOneBy(array('typeAbonnement'=>$request->get('typeAbonnementBtb')));
}
$this->cart_service->addAbonnementPro($abonnementPro,$adresse,$typeComptePaiement,$isFree); // add abonnement to cart
$uniqid = md5(uniqid());
$this->session->set('tk_add', $uniqid);
if($isPageCheckout){
return $this->redirectToRoute('checkout');
}else{
return $this->redirectToRoute('accueil_page_b2b',array('tk_add'=>$uniqid));
}
}
// $response = $this->render('front/abonnement-pro/checkout/adresse-facture.html.twig',[
$response = $this->render('front/abonnement-pro/checkout/adresse-facture-new.html.twig',[
'form'=>$form->createView(),
'last_username' => $this->authenticationUtils->getLastUsername(),
'error' => $this->authenticationUtils->getLastAuthenticationError(),
'adresse' => $adresse
]);
return $response;
}
/**
* @Route("/adresse-facture-redirect-user", name="abonnementPro_adresse_facture_redirect_user",options={"expose"=true})
*/
public function indexRedirectUser(Request $request) : Response
{
//check abonnement exist user
if($this->getUser() && $request->get('dataUser')) {
$abonnement = $this->cart_service->checkIsAbonnementPro($this->getUser());
if ($abonnement['is_abonnemen_pro'] && $abonnement['is_admin']) {
}else if($abonnement['is_abonnemen_pro'] && !$abonnement['is_admin']){
}else{
$isAbonnee = false;
$token_cart = $this->session->get('token_cart');
if ($token_cart) {
$cart = $this->em->getRepository(Cart::class)->findOneBy(array('token' => $token_cart));
if($cart && $cart->getAbonnementProCart()){
$isAbonnee =true;
}
}
if($isAbonnee == false) {
$typeAbonnement = AbonnementPro::TYPE_PRO;
$typeComptePaiement = $request->get('typeComptePaiement', 1);
$isFree = $request->get('isFree', 0);
$abonnementPro = false;
if ($typeAbonnement && $typeComptePaiement) {
$abonnementPro = $this->em->getRepository(AbonnementPro::class)->findOneBy(array('typeAbonnement' => $typeAbonnement));
}
if ($abonnementPro) {
$user = $this->getUser();
$customer = $this->em->getRepository(Customer::class)->findOneBy(['user' => $user->getId()]);
$adresse = new Adresse();
$adresse->setCustomer($customer);
$adresse->setEmail($user->getEmail());
$adresse->setFirstname($user->getFirstname());
$adresse->setLastname($user->getlastname());
$adresse->setTypeAdresse(Adresse::ADRESSEFACTURATION);
$this->em->persist($adresse);
$this->em->flush();
$adresseLivraison = clone $adresse;
$adresseLivraison->setTypeAdresse(Adresse::ADRESSELIVRAISON);
$this->em->persist($adresseLivraison);
$this->em->flush();
$this->cart_service->addAbonnementPro($abonnementPro, $adresse, $typeComptePaiement, $isFree); // add abonnement to cart
$uniqid = md5(uniqid());
$this->session->set('tk_add', $uniqid);
}
}
}
}
$response = new Response();
$response->setContent(1);
return $response;
}
/**
* @Route("/delete/{token}", name="abonnement_pro_deleted",options={"expose"=true})
*/
public function deleted(Request $request,$token){
$cart = $this->em->getRepository(Cart::class)->findOneBy(array('token'=>$token));
if($cart){
$abonnementPro = $this->em->getRepository(AbonnementProCart::class)->findOneBy(array('cart'=>$cart->getId()));
if($abonnementPro){
$this->em->remove($abonnementPro);
$this->em->flush();
$this->cart_service->updatePriceCart($cart);
}
}
return $this->redirectToRoute('checkout');
}
/**
* @Route("/add-abonnement-cart-request", name="abonnementPro_add_abonnement_cart_with_request",options={"expose"=true})
*/
public function addAbonnementCartWithRequest(Request $request) : Response
{
//check abonnement exist user
$isPageCheckout = 1;
$rep = array(
'erreur' =>'',
'success' =>false,
);
$abonnement = $this->cart_service->checkIsAbonnementPro($this->getUser());
if ($abonnement['is_abonnemen_pro'] && $abonnement['is_admin']) {
$rep['erreur'] = 'deja abonné !';
}else if($abonnement['is_abonnemen_pro'] && !$abonnement['is_admin']){
$rep['erreur'] = 'deja abonné !';
}else{
$token_cart = $this->session->get('token_cart');
if ($token_cart) {
$cart = $this->em->getRepository(Cart::class)->findOneBy(array('token' => $token_cart));
if($cart && $cart->getAbonnementProCart()){
$rep['erreur'] = 'deja abonné !';
}
}
$data = $this->checkout_service->postFormWithBtnAbonnement($request);
if(isset($data['erreur'])){
$rep['erreur'] = $data['erreur'];
}else{
$cart->setCustomer($data['customer']);
$cart->setAdresseLivraison($data['adresseLiv']);
$cart->setAdresseFacturation($data['adresseFac']);
if($cart->getVersionCarrier() == Cart::VERSION_CARRIER_NEW){
$cart->setImprimeur(null);
$cart->setDateStart(null);
$cart->setDateEnd(null);
$cart->setCarrier(null);
$cart->setTypeLivraisonNational(null);
$cart->setIdServicePointLivraison(null);
}else {
if (count($cart->getCartDetail()) > 0) {
$cart->setImprimeur($data['carrierData']['imprimeur']);
$cart->setDateStart($data['carrierData']['dateStart']);
$cart->setDateEnd($data['carrierData']['dateEnd']);
$cart->setTypeLivraisonNational($data['carrierData']['typeLivraionNation']);
$cart->setIdServicePointLivraison($data['carrierData']['idServicePointNation']);
} else {
$cart->setImprimeur(null);
$cart->setDateStart(null);
$cart->setDateEnd(null);
$cart->setCarrier(null);
}
}
$cart->setCommentaire($data['commentaire']);
$this->em->persist($cart);
$this->em->flush();
$typeComptePaiement = $request->get('typeComptePaiement',1);
$typeAbonnement = $request->get('typeAbonnement',1);
$abonnementPro = $this->em->getRepository(AbonnementPro::class)->findOneBy(array('typeAbonnement'=>$typeAbonnement));
$this->cart_service->createCompteAboProDirect($request,$this->getUser(),$abonnementPro,$typeComptePaiement,$request->get('isFree',0));
$rep['success'] = true;
}
}
$response = new Response();
$response->setContent(json_encode($rep));
$response->setStatusCode(200);
return $response;
}
}