<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Constant\AkoulaConstant;
use App\Dto\ReservationEventInput;
use App\Entity\OperationHistory;
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 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 PaymentSubscriber 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],
['setUser', EventPriorities::PRE_WRITE],
['afterCreate', EventPriorities::POST_WRITE],
['onUpdate', EventPriorities::POST_WRITE],
],
];
}
public function changeStatus(ViewEvent $event)
{
$reservationPayment = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$reservationPayment instanceof ReservationPayment || Request::METHOD_POST !== $method) {
return;
}
if (!is_null($reservationPayment->getReservationBedroom())) {
$totalDirectPayElement = $this->reservationPaymentRepo->getTotalPayByBedroom($reservationPayment->getReservationBedroom()->getId());
$totalPay = 0;
if (!is_null($totalDirectPayElement)) {
$totalPay = $totalDirectPayElement['total'];
}
//TODO TO ADD PRICE OF RESTAURANT DATA
/*$totalCinetPayElement = $this->reservationBedroomRepository->getTotalPayByBedroom($reservationPayment->getReservationBedroom()->getId());
$totalCinetPay = 0;
if (!is_null($totalCinetPayElement)) {
$totalCinetPay = $totalCinetPayElement['total'];
}*/
if ($reservationPayment->getReservationBedroom()->getAmount() <= $totalPay) {
$reservationPayment->getReservationBedroom()->setPaymentStatus(AkoulaConstant::MODE_PAYED);
}
$this->entityManager->flush();
}elseif (!is_null($reservationPayment->getReservationEvent())){
$totalDirectPayElement = $this->reservationPaymentRepo->getTotalPayByResaEvent($reservationPayment->getReservationEvent()->getId());
$totalPay = 0;
if (!is_null($totalDirectPayElement)) {
$totalPay = $totalDirectPayElement['total'];
}
$totalCinetPayElement = $this->customerReservationEventRepo->getTotalPayByReservationEvent($reservationPayment->getReservationEvent()->getId());
$totalCinetPay = 0;
if (!is_null($totalCinetPayElement)) {
$totalCinetPay = $totalCinetPayElement['total'];
}
$totalToPayElement = $this->reservationEvFomulaRepo->getTotalToPayByResaEvent($reservationPayment->getReservationEvent());
$totalToPay = 0;
if(!is_null($totalToPayElement)){
$totalToPay = $totalToPayElement['total'];
}
if ($totalToPay <= ($totalPay + $totalCinetPay)) {
$reservationPayment->getReservationEvent()->setPaymentStatus(AkoulaConstant::MODE_PAYED);
}
$this->entityManager->flush();
}
}
public function setUser(ViewEvent $event){
$reservationPayment = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$reservationPayment instanceof ReservationPayment || Request::METHOD_POST !== $method) {
return;
}
if($this->tokenStorage->getToken()->getUser() instanceof User){
$reservationPayment->setUser($this->security->getUser());
}
}
public function afterCreate(ViewEvent $event): void
{
$reservationPayment = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$reservationPayment instanceof ReservationPayment || Request::METHOD_POST !== $method) {
return;
}
$operationHistory = new OperationHistory();
$operationHistory->setUser($this->security->getUser());
$operationHistory->setAction(AkoulaConstant::ACTION_PAIEMENT);
$operationHistory->setDetail('Versement, Mode: '.$reservationPayment->getPaymentMethod()->getName().', Montant: '.$reservationPayment->getAmount());
$operationHistory->setReservationBedroom($reservationPayment->getReservationBedroom());
$this->entityManager->persist($operationHistory);
$this->entityManager->flush();
}
public function onUpdate(ViewEvent $event): void
{
$reservationPayment = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$reservationPayment instanceof ReservationPayment || Request::METHOD_PUT !== $method) {
return;
}
$operationHistory = new OperationHistory();
$operationHistory->setUser($this->security->getUser());
$operationHistory->setAction(AkoulaConstant::ACTION_PAIEMENT);
$operationHistory->setDetail('Modification Versement, Mode: '.$reservationPayment->getPaymentMethod()->getName().', Montant: '.$reservationPayment->getAmount());
$operationHistory->setReservationBedroom($reservationPayment->getReservationBedroom());
$this->entityManager->persist($operationHistory);
$this->entityManager->flush();
}
}