<?php
namespace App\Controller;
use App\Entity\Order;
use App\Services\Cart;
use App\Services\Mail;
use App\Entity\Forecast;
use App\Entity\HistoriquePaiement;
use App\Repository\CartRepository;
use App\Repository\OrderRepository;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\EtatPaiementRepository;
use App\Repository\TypepaiementRepository;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
/**
* @IsGranted("ROLE_USER")
*/
class OrderSuccessController extends AbstractController
{
private $em;
public function __construct(EntityManagerInterface $em){
$this->em = $em;
}
/**
* @Route("/commande/success/", name="order_validate")
* @Route("/commande/success/{reference}", name="order_validate_no_pay")
*/
public function index(
EtatPaiementRepository $repo,
CartRepository $cartRepo,
TypepaiementRepository $typepaiementRepo,
OrderRepository $orderRepo,
$reference = null
): Response
{
$carts = $cartRepo->findByUser($this->getUser());
if ($this->getUser()->getGrossiste()->getIsPaid() == 1 && isset($_GET['payment_intent_client_secret'])) {
$stripeSessionId = $_GET['payment_intent_client_secret'];
$order = $orderRepo->findOneByStripeSessionId($stripeSessionId);
$typepaiement = $typepaiementRepo->findOneById(1);
$etatPaiement = $repo->findOneById(1);
$hitorique_paiement = new HistoriquePaiement();
$order->setEtatpaiement($etatPaiement)
->setTypepaiement($typepaiement)
->setMontantPaye($order->getTotal())
->setDateLastPaiement(new \DateTime());
//insertion dans la table historique_paiement
$hitorique_paiement->setUser($this->getUser())
->setMyOrder($order)
->setMotantPaye($order->getTotal())
->setDatePaiement(new \DateTime())
->setTypepaiement($typepaiement)
->setIsValid(true);
$this->em->persist($hitorique_paiement);
foreach ($carts as $cart) {
$this->em->remove($cart);
}
$this->em->flush();
return $this->render('order_success/index.html.twig', [
'order' => $order,
]);
}else{
foreach ($carts as $cart) {
$this->em->remove($cart);
}
$hitorique_paiement = new HistoriquePaiement();
$order = $orderRepo->findOneByReference($reference);
$hitorique_paiement->setUser($this->getUser())
->setMyOrder($order)
->setMotantPaye(0)
->setTypepaiement($order->getTypepaiement())
->setIsValid(false);
$this->em->persist($hitorique_paiement);
$this->em->flush();
return $this->render('order_success/index.html.twig', [
'order' => $order,
]);
}
}
/**
* @Route("/commande/success-prevision/", name="prevision_validate")
* @Route("/commande/success-prevision/{reference}", name="forcast_validate_no_pay")
*/
public function forcastSuccess(
EtatPaiementRepository $repo,
$reference = null,
CartRepository $cartRepo,
TypepaiementRepository $typepaiementRepo
): Response
{
$carts = $cartRepo->findByUser($this->getUser());
$etatPaiement = $repo->findOneById(1);
$typepaiement = $typepaiementRepo->findOneById(1);
$hitorique_paiement = new HistoriquePaiement();
if ($this->getUser()->getGrossiste()->getIsPaid() == 1 && isset($_GET['payment_intent_client_secret'])) {
$stripeSessionId = $_GET['payment_intent_client_secret'];
$forcast = $this->em->getRepository(Forecast::class)->findOneByStripeSessionId($stripeSessionId);
$forcast->setEtatpaiement($etatPaiement)
->setTypepaiement($typepaiement)
->setStripeSessionId($stripeSessionId);
//etatpaiement_id;
//insertion dans la table historique_paiement
$hitorique_paiement->setUser($this->getUser())
->setForecast($forcast)
->setMotantPaye($forcast->getTotal())
->setDatePaiement(new \DateTime())
->setTypepaiement($typepaiement)
->setIsValid(true);
$this->em->persist($hitorique_paiement);
}else{
$forcast = $this->em->getRepository(Forecast::class)->findOneByReference($reference);
$hitorique_paiement = new HistoriquePaiement();
$order = $this->em->getRepository(Forecast::class)->findOneByReference($reference);
$hitorique_paiement->setUser($this->getUser())
->setForecast($forcast)
->setMotantPaye(0)
->setTypepaiement($forcast->getTypepaiement())
->setIsValid(false);
$this->em->persist($hitorique_paiement);
}
foreach ($carts as $cart) {
$this->em->remove($cart);
}
$this->em->flush();
return $this->render('order_success/prevoir.html.twig', [
'order' => $forcast
]);
}
}