<?php
/**
* Created by PhpStorm.
*/
namespace App\Controller\Front;
use App\Entity\AbonnementPay;
use App\Entity\BesoinProductPage;
use App\Entity\BesoinProductPageCategory;
use App\Entity\Cart;
use App\Entity\CartDetail;
use App\Entity\CompteAbonnementPro;
use App\Entity\CompteAbonnementProCommandesFactures;
use App\Entity\CompteAbonnementProFacture;
use App\Entity\Imprimeur;
use App\Entity\LanguageCountry;
use App\Entity\NosCrea;
use App\Entity\NotifB2b;
use App\Entity\NotifB2cCompte;
use App\Entity\OrderDetailVirement;
use App\Entity\Orders;
use App\Entity\Page\Page;
use App\Entity\Product;
use App\Entity\Stats;
use App\Entity\StuartOrder;
use App\Entity\User;
use App\Entity\UserStripe;
use App\Services\AbonnementService;
use App\Services\AvisVerifiesService;
use App\Services\CartService;
use App\Services\CheckoutService;
use App\Services\FactureService;
use App\Services\NbDesigner;
use App\Services\Notifications;
use App\Services\OrderService;
use App\Services\ReferalCandyService;
use App\Services\StripeConnect;
use App\Services\StripeConnectTest;
use Spipu\Html2Pdf\Html2Pdf;
use Stripe\Invoice;
use Stripe\Subscription;
use Symfony\Component\Finder\Finder;
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\DependencyInjection\ParameterBag\ParameterBagInterface;
use JouwWeb\SendCloud\Client;
use JouwWeb\SendCloud\Model\Address;
use JouwWeb\SendCloud\Model\Parcel;
use JouwWeb\SendCloud\Model\WebhookEvent;
use JouwWeb\SendCloud\Exception\SendCloudRequestException;
use App\Services\SendCloudApi;
use App\Services\DelivreeService;
use Stripe\Stripe;
use Stripe\Customer;
use Stripe\PaymentMethod;
use App\Services\Exaprint;
class IndexController extends AbstractController
{
private $em;
private $session;
private $apiCloudSend;
private $delivreeService;
private $cartService;
private $mailer;
private $router;
public function __construct(
SessionInterface $session,
ParameterBagInterface $params,
TranslatorInterface $trans,
EntityManagerInterface $em,
CheckoutService $checkout_service,
SendCloudApi $apiCloudSend,
DelivreeService $delivreeService,
CartService $cartService,
AbonnementService $abonnementService,
StripeConnect $stripeConnect,
FactureService $facture_service,
Notifications $notifications,
AvisVerifiesService $avisVerifiesService,
ReferalCandyService $candyService,
OrderService $order_service,
\Swift_Mailer $mailer,
Environment $twig,
UrlGeneratorInterface $router,
Exaprint $exaprint,
NbDesigner $nbDesigner
) {
$this->em = $em;
$this->trans = $trans;
$this->session = $session;
$this->params = $params;
$this->checkout_service = $checkout_service;
$this->apiCloudSend = $apiCloudSend;
$this->delivreeService = $delivreeService;
$this->cartService = $cartService;
$this->abonnementService = $abonnementService;
$this->stripeConnectService = $stripeConnect;
$this->facture_service = $facture_service;
$this->notifications = $notifications;
$this->avisVerifiesService = $avisVerifiesService;
$this->candyService = $candyService;
$this->order_service = $order_service;
$this->mailer = $mailer;
$this->twig = $twig;
$this->nbDesigner = $nbDesigner;
$this->router = $router;
$this->exaprint = $exaprint;
}
/**
* @Route("/", name="accueil",options={"expose"=true})
*/
public function index(Request $request) : Response
{
if($request->get('displayProduct') && $request->get('imprimerRelierEdit') && $request->get('imprimerRelierEdit') == 1 && $request->get('displayProduct') == 1)
return $this->redirectToRoute('edit_pdf_page');
$response = $this->render('front/accueil/index.html.twig');
return $response;
}
/**
* @Route("/vcompteur", name="add_visiteur_count",options={"expose"=true})
*/
public function compteurVisite(Request $request) : Response
{
if ($request->isXmlHttpRequest()) {
$this->checkout_service->addCompteurVisite($request);
}
$response = new Response();
$response->setContent(1);
return $response;
}
/**
* @Route("/add-cookies", name="add_cookies",options={"expose"=true})
*/
public function addCookies(Request $request) : Response
{
if ($request->isXmlHttpRequest()) {
$this->cartService->addCookies($request);
}
$response = new Response();
$response->setContent(1);
return $response;
}
/**
* @Route("/add-cookies-refuse", name="add_cookies_refuse",options={"expose"=true})
*/
public function addCookiesRefuse(Request $request) : Response
{
if ($request->isXmlHttpRequest()) {
$this->cartService->addCookiesRefuse($request);
}
$response = new Response();
$response->setContent(1);
return $response;
}
/**
* @Route("/block-livraison-accueil", name="block_livraison_accueil",options={"expose"=true})
*/
public function blockLivraison(Request $request): Response
{
$dataRequest = $request->request->all();
$this->session->set('longiAdresse',$dataRequest['longi']);
$this->session->set('latAdresse',$dataRequest['lat']);
$this->session->set('addresseAdresse',$dataRequest['addresseAdresse']);
$this->session->set('postalcodeAdresse',$dataRequest['postalcode']);
$this->session->set('cityAdresse',$dataRequest['cityAdresse']);
$this->session->set('CountryLiv',$dataRequest['CountryLiv']);
$this->session->set('CountryCodeLiv',$dataRequest['CountryCodeLiv']);
$dataLivrason = $this->checkout_service->getModeLivraisonSansStock($dataRequest);
$dataLivrason['latLongi'] = ['lat' => $dataRequest['lat'],'longi'=>$dataRequest['longi']];
return $this->render('front/accueil/block_livraison.html.twig', $dataLivrason);
}
/**
* @Route("/block-livraison-new-accueil", name="block_livraison_new_accueil",options={"expose"=true})
*/
public function blockLivraisonNewAccueil(Request $request): Response
{
$dataRequest = $request->request->all();
$this->session->set('longiAdresse',$dataRequest['longi']);
$this->session->set('latAdresse',$dataRequest['lat']);
$this->session->set('addresseAdresse',$dataRequest['addresseAdresse']);
$this->session->set('postalcodeAdresse',$dataRequest['postalcode']);
$this->session->set('cityAdresse',$dataRequest['cityAdresse']);
$this->session->set('CountryLiv',$dataRequest['CountryLiv']);
$this->session->set('CountryCodeLiv',$dataRequest['CountryCodeLiv']);
$dataLivrason = $this->checkout_service->getModeLivraisonSansStock($dataRequest);
$dataLivrason['latLongi'] = ['lat' => $dataRequest['lat'],'longi'=>$dataRequest['longi']];
return $this->render('front/accueil/block_livraison_new_accueil.html.twig', $dataLivrason);
}
/**
* @Route("/check-livraison-new-accueil", name="check_livraison_new_accueil",options={"expose"=true})
*/
public function checkLivraisonNewAccueil(Request $request): Response
{
$dataRequest = $request->request->all();
$this->session->set('longiAdresse',$dataRequest['longi']);
$this->session->set('latAdresse',$dataRequest['lat']);
$this->session->set('addresseAdresse',$dataRequest['addresseAdresse']);
$this->session->set('postalcodeAdresse',$dataRequest['postalcode']);
$this->session->set('cityAdresse',$dataRequest['cityAdresse']);
$this->session->set('CountryLiv',$dataRequest['CountryLiv']);
$this->session->set('CountryCodeLiv',$dataRequest['CountryCodeLiv']);
$dataLivrason = $this->checkout_service->getModeLivraisonSansStock($dataRequest);
$dataLivrason['latLongi'] = ['lat' => $dataRequest['lat'],'longi'=>$dataRequest['longi']];
return $this->render('front/accueil/block_product_new_accueil.html.twig', $dataLivrason);
}
/**
* @Route("/block-livraison-time-accueil", name="block_livraison_time_accueil",options={"expose"=true})
*/
public function blockLivraisontime(Request $request): Response
{
$dataRequest = $request->request->all();
$this->session->set('longiAdresse',$dataRequest['longi']);
$this->session->set('latAdresse',$dataRequest['lat']);
$this->session->set('addresseAdresse',$dataRequest['addresseAdresse']);
$this->session->set('postalcodeAdresse',$dataRequest['postalcode']);
$this->session->set('cityAdresse',$dataRequest['cityAdresse']);
$this->session->set('CountryLiv',$dataRequest['CountryLiv']);
$this->session->set('CountryCodeLiv',$dataRequest['CountryCodeLiv']);
$dataLivrason = $this->checkout_service->getModeLivraisonSansStock($dataRequest);
$time =false ;
if($dataLivrason['imprimeurZoneChalandise'] ){
$time = $this->cartService->getFirstTime($dataLivrason['imprimeurZoneChalandise']->getId());
}
$responce = new Response();
$responce->setContent($time?$time:'');
return $responce;
}
/**
* @Route("/besoin-produits", name="besoin_produits",options={"expose"=true})
*/
public function besoinProduits(Request $request): Response
{
$response = $this->render('front/accueil/besoin-products-new.html.twig');
return $response;
}
/**
* @Route("/besoin-produits-preference", name="besoin_produits_preference",options={"expose"=true})
*/
public function besoinProduitsPreference(Request $request): Response
{
$requestAll = $request->request->all();
$array = [];
if($requestAll){
$i = 0 ;
$arrayGlobal = [];
foreach ($requestAll as $item){
$products = $this->em->getRepository(BesoinProductPage::class)->getByCategoryAndCretere($item);
$tab = [];
if($products){
foreach ($products as $product){
$tab[$product->getId()] = $product->getId();
}
}
if($i == 0 ){
$arrayGlobal = $tab ;
}else{
$arrayGlobal = array_intersect($arrayGlobal , $tab);
}
$i ++ ;
}
if($arrayGlobal){
foreach ($arrayGlobal as $value){
$product = $this->em->getRepository(BesoinProductPage::class)->find($value);
$array[$product->getCategory()->getName()]['category'] = $product->getCategory();
$array[$product->getCategory()->getName()]['products'][] = $product;
}
}
}
$response = $this->render('front/accueil/besoin-product-bloc/content-search-preference.html.twig',['categorys' => $array]);
return $response;
}
/**
* @Route("/besoin-produits-default", name="besoin_produits_default",options={"expose"=true})
*/
public function besoinProduitsdefault(Request $request): Response
{
$categorys = $this->em->getRepository(BesoinProductPageCategory::class)->getAllActive();
$response = $this->render('front/accueil/besoin-product-bloc/content-new.html.twig',['categorys'=>$categorys]);
return $response;
}
/**
* @Route("/besoin-produits-2", name="besoin_produits_2",options={"expose"=true})
*/
public function besoinProduitsNew(Request $request): Response
{
$response = $this->render('front/accueil/besoin-products-2.html.twig');
return $response;
}
/**
* @Route("/besoin-produits/search", name="searchBesoinProduct",options={"expose"=true})
*/
public function searchBesoinProduct(Request $request) : Response
{
$requestData = $request->query->get('search');
$data = $this->nbDesigner->searchProductOption($request,$requestData,$this->getUser());
$response = $this->render('front/accueil/besoin-product-bloc/content-search.html.twig',['data'=>$data,'valueInput'=>$requestData]);
return $response;
}
/**
* @Route("/besoin-produits-crello-add-to-cart/{id}", name="besoin_produits_crollo_add_to_cart",options={"expose"=true})
*/
public function besoinProduitsCrelloAddToCart(Request $request,$id): Response
{
$data = $this->nbDesigner->addToCartNewNbDesignerNosCrea($request,$this->getUser(),$id);
if($data){
$redirect = $this->generateUrl('product_option', array('token'=>$data->getToken(),'product'=>$data->getProduct()->getId()));
}else{
$redirect = $this->generateUrl('besoin_produits');
}
$responce = new Response();
$responce->setContent($redirect);
return $responce;
}
/**
* @Route("/besoin-produits-old", name="besoin_produits_old",options={"expose"=true})
*/
public function besoinProduitsOld(Request $request): Response
{
$response = $this->render('front/accueil/index-products.html.twig');
return $response;
}
/**
* @Route("/webhook-stripe", name="webhook_stripe_copees")
*/
public function webhookStripe(Request $request): Response
{
$endpoint_secret = 'whsec_7Hzm2izHMWSwhcPNWCbDmWGX3FJdHujM';
$payload = @file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
$event = null;
try {
$event = \Stripe\Webhook::constructEvent(
$payload, $sig_header, $endpoint_secret
);
} catch(\UnexpectedValueException $e) {
exit();
} catch(\Stripe\Exception\SignatureVerificationException $e) {
exit();
}
if ($event->type == "payment_intent.succeeded") {
$intent = $event->data->object;
$charge = $intent->id;
$order = $this->em->getRepository(Orders::class)->findBy(array('chargeStripeId'=> $charge),array('id'=>'desc'));
if (isset($order[0]) && $order[0] && !$order[0]->getIsValid()) {
$cart = $order[0]->getCart();
$order[0]->setIsValid(true);
$status = $this->em->getRepository(Stats::class)->findOneBy(array('statusNbr' => Stats::ENCOURT));
$order[0]->setStatus($status);
$nameFacture = $this->facture_service->uploadFacture($order[0]->getId()); //creation facture
$order[0]->setPathFacture($nameFacture);
$order[0]->setIsAbonnementPro($cart->getIsAbonnementPro());
$this->em->persist($order[0]);
$this->em->flush();
$this->stripeConnectService->createRefundByPaymentIntentIfEmptyDetail($order[0], $cart); // refound 0.5€ if 0€ in order
http_response_code(200);
}
exit();
} elseif ($event->type == "payment_intent.payment_failed") {
exit();
}elseif ( $event->type == "charge.dispute.closed" or $event->type == "charge.dispute.funds_withdrawn") {
$dispute = $event->data->object;
$charge = $dispute->payment_intent;
$factureAbo = $this->em->getRepository(CompteAbonnementProFacture::class)->findBy(array('chargeStripeId'=> $charge),array('id'=>'desc'));
$factureAboCommandes = $this->em->getRepository(CompteAbonnementProCommandesFactures::class)->findBy(array('chargeStripeId'=> $charge),array('id'=>'desc'));
if(($factureAbo && $factureAbo[0])){
$facture = $factureAbo[0];
$facture->setStatusPaiement(CompteAbonnementProFacture::STATUS_IMPAYE);
$this->em->persist($facture);
$this->em->flush();
}
if(($factureAboCommandes && $factureAboCommandes[0])){
$factureCommande = $factureAboCommandes[0];
$factureCommande->setStatusPaiement(CompteAbonnementProCommandesFactures::STATUS_IMPAYE);
$this->em->persist($factureCommande);
$this->em->flush();
}
http_response_code(200);
exit();
}
}
/**
* @Route("/webhook-request-merci-facteur", name="webhook_request_merci_facteur")
*/
public function webhookRequestMerciFacteur(Request $request) : Response
{
if (isset($_POST['event'])) {
$objectEvent = json_decode($_POST['event']);
if ((is_object($objectEvent) and $objectEvent->name_event == 'printed') or (is_object($objectEvent) and $objectEvent->name_event == 'new-state') or (is_object($objectEvent) and $objectEvent->name_event == 'delivered')) {
$objectDetail = json_decode($_POST['detail']);
foreach ($objectDetail as $detail) {
$idEvent = $detail->id_envoi;
$object = $this->em->getRepository(StuartOrder::class)->findOneBy(array('idJob' => $idEvent));
$order = null;
if ($object) {
$order = $object->getOrders();
$object->setTrakingurl($detail->ref_courrier);
$object->setTrakckingPoste($detail->tracking_number);
$object->setStatus($detail->statut_description);
$this->em->persist($object);
$this->em->flush();
}
$this->notifications->mailRequestDetailPoste($detail, $order);
}
}
}
http_response_code(200);
exit();
}
/**
* @Route("/offre-B2B", name="offre_B2B")
*/
public function offreB2B(Request $request) : Response
{
return $this->redirectToRoute('offre_B2B_2');
$response = $this->render('front/accueil/offre-b2b.html.twig');
return $response;
}
/**
* @Route("/offre-B2B-2", name="offre_B2B_2")
*/
public function offreB2B2(Request $request) : Response
{
$response = $this->render('front/accueil/offre-b2b2.html.twig');
return $response;
}
/**
* @Route("/page/{url}", name="page_cms_front")
*/
public function pageCms(Request $request,$url) : Response
{
$page = $this->em->getRepository(Page::class)->findOneBy(['url'=>$url]);
if($page){
if($page->getPageType() == Page::PAGE_VILLE){
$response = $this->render('front/page-cms/page-ville.html.twig',['page'=>$page]);
}elseif ($page->getPageType() == Page::PAGE_METIER){
$response = $this->render('front/page-cms/page-metier.html.twig',['page'=>$page]);
}elseif ($page->getPageType() == Page::PAGE_PRODUIT){
$response = $this->render('front/page-cms/page-produit.html.twig',['page'=>$page]);
}else{
$response = $this->render('front/error.html.twig');
}
}else{
$response = $this->render('front/error.html.twig');
}
return $response;
}
/**
* @Route("/change-country", name="change_country_code" ,options={"expose"=true})
*/
public function ChangeCountry(Request $request) : Response
{
$referer = $request->headers->get('referer'); // get the referer, it can be empty!
if (!\is_string($referer) || !$referer) {
$refererRoute = 'accueil';
}
$refererPathInfo = Request::create($referer)->getPathInfo();
$routeInfos = $this->router->match($refererPathInfo);
$refererRoute = $routeInfos['_route'] ?? 'accueil';
unset($routeInfos['_route']);
unset($routeInfos['_controller']);
$routeInfos['_locale'] = $this->session->get('_locale');
return $this->redirectToRoute($refererRoute,$routeInfos);
}
// /**
// * @Route("/aqsdqsdqsdaa", name="changeazeaze_country_code" ,options={"expose"=true})
// * @IsGranted("ROLE_SUPER_ADMINISTRATOR")
// */
// public function aazeazeaz(Request $request) : Response
// {
// $imprimeur = $this->em->getRepository(Imprimeur::class)->find(29);
// $orderDetailVirement = $this->em->getRepository(OrderDetailVirement::class)->find(12412);
// $orderDetails = $orderDetailVirement->getOrderDetail();
//// $nameFacture = $this->facture_service->uploadFactureVirementDetails30j($orderDetails,$orderDetailVirement); //creation facture virement 30j
// foreach ($orderDetails as $orderDetail) {
// $orderDetail->setPathFactureVirement('not_facture');
// $orderDetail->setPayoutStripeId('po_1Oh6vtQ1EFGM3mS7em6SNaSO');
// $this->em->persist($orderDetail);
// }
// // set orderDetailVirement
// $orderDetailVirement->setPayoutStripeId('po_1Oh6vtQ1EFGM3mS7em6SNaSO');
// $orderDetailVirement->setPathFactureVirement('not_facture');
// $this->em->persist($orderDetailVirement);
// $this->em->flush();
//
//// $this->notifications->sendOrderDetailVirement30j($imprimeur, 'Virement imprimeur', $orderDetailVirement, $nameFacture,$orderDetails); // send order virement to imprimeur
// die();
// }
/**
* @Route("/lab-copees", name="lab_copees")
*/
public function labCopees(Request $request) : Response
{
$response = $this->render('front/accueil/labCopees.html.twig');
return $response;
}
/**
* @Route("/change-receiver-notif-b2b/{token}", name="change_mail_receiver_b2b_notif",options={"expose"=true})
*/
public function changeNotifCustomerB2c(Request $request,$token) : Response
{
if($token){
$notif = $this->em->getRepository(NotifB2b::class)->findOneBy(['token'=>$token]);
if($notif){
$customer = $notif->getCustomer();
if($customer){
$customer->setIsNotReceiver(true);
$this->em->persist($customer);
$this->em->flush();
$response = $this->render('front/pageDeleteNotification.html.twig');
return $response;
}
}
}
$response = new Response(1);
return $response;
}
}