<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Constant\AkoulaConstant;
use App\Dto\ReservationEventInput;
use App\Entity\Reservation;
use App\Entity\ReservationBedroom;
use App\Entity\ReservationEvent;
use App\Entity\ReservationPayment;
use App\Entity\User;
use App\Repository\CustomerReservationEventFormulatRepository;
use App\Repository\CustomerReservationEventRepository;
use App\Repository\ReservationBedroomRepository;
use App\Repository\ReservationPaymentRepository;
use App\Service\NotificationService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Security;
final class CinetpaySubscriber implements EventSubscriberInterface
{
private $reservationPaymentRepo;
private $reservationBedroomRepository;
private $customerReservationEventRepo;
private $reservationEvFomulaRepo;
private $entityManager;
private $tokenStorage;
private $security;
public function __construct(ReservationPaymentRepository $repository, ReservationBedroomRepository $reservationBedroomRepository,
EntityManagerInterface $entityManager,TokenStorageInterface $tokenStorage,Security $security,
CustomerReservationEventRepository $customerReservationEventRepo,
CustomerReservationEventFormulatRepository $customerReservationEventFormulatRepository
)
{
$this->reservationPaymentRepo = $repository;
$this->reservationBedroomRepository = $reservationBedroomRepository;
$this->entityManager = $entityManager;
$this->tokenStorage = $tokenStorage;
$this->security = $security;
$this->customerReservationEventRepo = $customerReservationEventRepo;
$this->reservationEvFomulaRepo = $customerReservationEventFormulatRepository;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => [['changeStatus', EventPriorities::POST_WRITE]],
];
}
public function changeStatus(ViewEvent $event)
{
$reservation = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$reservation instanceof Reservation || Request::METHOD_POST !== $method) {
return;
}
$findResaBedrooms = $this->reservationBedroomRepository->findBy(['reservation'=>$reservation]);
if (!is_null($findResaBedrooms)) {
foreach ($findResaBedrooms as $bedroom){
$totalDirectPayElement = $this->reservationPaymentRepo->getTotalPayByBedroom($bedroom->getId());
}
$totalPay = 0;
if (!is_null($totalDirectPayElement)) {
$totalPay = $totalDirectPayElement['total'];
}
$totalCinetPayElement = $this->reservationBedroomRepository->getTotalPayByBedroom($bedroom->getId());
$totalCinetPay = 0;
if (!is_null($totalCinetPayElement)) {
$totalCinetPay = $totalCinetPayElement['total'];
}
if ($bedroom->getAmount() <= ($totalPay + $totalCinetPay)) {
$bedroom->setPaymentStatus(AkoulaConstant::MODE_PAYED);
}
$this->entityManager->flush();
}else{
$findCustomerReaseEvents= $this->customerReservationEventRepo->findBy(['reservation'=>$reservation]);
if(!is_null($findCustomerReaseEvents)){
foreach ($findCustomerReaseEvents as $customerReaseEvent){
$totalDirectPayElement = $this->reservationPaymentRepo->getTotalPayByResaEvent($customerReaseEvent->getReservationEvent()->getId());
$totalPay = 0;
if (!is_null($totalDirectPayElement)) {
$totalPay = $totalDirectPayElement['total'];
}
$totalCinetPayElement = $this->customerReservationEventRepo->getTotalPayByReservationEvent($customerReaseEvent->getReservationEvent()->getId());
$totalCinetPay = 0;
if (!is_null($totalCinetPayElement)) {
$totalCinetPay = $totalCinetPayElement['total'];
}
$totalToPayElement = $this->reservationEvFomulaRepo->getTotalToPayByResaEvent($customerReaseEvent->getReservationEvent());
$totalToPay = 0;
if(!is_null($totalToPayElement)){
$totalToPay = $totalToPayElement['total'];
}
if ($totalToPay <= ($totalPay + $totalCinetPay)) {
$customerReaseEvent->getReservationEvent()->setPaymentStatus(AkoulaConstant::MODE_PAYED);
}
$this->entityManager->flush();
}
}
}
}
}