src/EventSubscriber/PaymentSubscriber.php line 65

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Constant\AkoulaConstant;
  5. use App\Dto\ReservationEventInput;
  6. use App\Entity\OperationHistory;
  7. use App\Entity\ReservationBedroom;
  8. use App\Entity\ReservationEvent;
  9. use App\Entity\ReservationPayment;
  10. use App\Entity\User;
  11. use App\Repository\CustomerReservationEventFormulatRepository;
  12. use App\Repository\CustomerReservationEventRepository;
  13. use App\Repository\ReservationBedroomRepository;
  14. use App\Repository\ReservationPaymentRepository;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpKernel\Event\RequestEvent;
  19. use Symfony\Component\HttpKernel\Event\ViewEvent;
  20. use Symfony\Component\HttpKernel\KernelEvents;
  21. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  22. use Symfony\Component\Security\Core\Security;
  23. final class PaymentSubscriber implements EventSubscriberInterface
  24. {
  25.     private $reservationPaymentRepo;
  26.     private $reservationBedroomRepository;
  27.     private $customerReservationEventRepo;
  28.     private $reservationEvFomulaRepo;
  29.     private $entityManager;
  30.     private $tokenStorage;
  31.     private $security;
  32.     public function __construct(ReservationPaymentRepository $repositoryReservationBedroomRepository $reservationBedroomRepository,
  33.                                 EntityManagerInterface $entityManager,TokenStorageInterface $tokenStorage,Security $security,
  34.                                 CustomerReservationEventRepository $customerReservationEventRepo,
  35.                                 CustomerReservationEventFormulatRepository $customerReservationEventFormulatRepository
  36.     )
  37.     {
  38.         $this->reservationPaymentRepo $repository;
  39.         $this->reservationBedroomRepository $reservationBedroomRepository;
  40.         $this->entityManager $entityManager;
  41.         $this->tokenStorage $tokenStorage;
  42.         $this->security $security;
  43.         $this->customerReservationEventRepo $customerReservationEventRepo;
  44.         $this->reservationEvFomulaRepo $customerReservationEventFormulatRepository;
  45.     }
  46.     public static function getSubscribedEvents()
  47.     {
  48.         return [
  49.             KernelEvents::VIEW => [
  50.                 ['changeStatus'EventPriorities::POST_WRITE],
  51.                 ['setUser'EventPriorities::PRE_WRITE],
  52.                 ['afterCreate'EventPriorities::POST_WRITE],
  53.                 ['onUpdate'EventPriorities::POST_WRITE],
  54.             ],
  55.         ];
  56.     }
  57.     public function changeStatus(ViewEvent $event)
  58.     {
  59.         $reservationPayment $event->getControllerResult();
  60.         $method $event->getRequest()->getMethod();
  61.         if (!$reservationPayment instanceof ReservationPayment || Request::METHOD_POST !== $method) {
  62.             return;
  63.         }
  64.         if (!is_null($reservationPayment->getReservationBedroom())) {
  65.             $totalDirectPayElement $this->reservationPaymentRepo->getTotalPayByBedroom($reservationPayment->getReservationBedroom()->getId());
  66.             $totalPay 0;
  67.             if (!is_null($totalDirectPayElement)) {
  68.                 $totalPay $totalDirectPayElement['total'];
  69.             }
  70.             //TODO TO ADD PRICE OF RESTAURANT DATA
  71.             /*$totalCinetPayElement = $this->reservationBedroomRepository->getTotalPayByBedroom($reservationPayment->getReservationBedroom()->getId());
  72.             $totalCinetPay = 0;
  73.             if (!is_null($totalCinetPayElement)) {
  74.                 $totalCinetPay = $totalCinetPayElement['total'];
  75.             }*/
  76.             if ($reservationPayment->getReservationBedroom()->getAmount() <= $totalPay) {
  77.                 $reservationPayment->getReservationBedroom()->setPaymentStatus(AkoulaConstant::MODE_PAYED);
  78.             }
  79.             $this->entityManager->flush();
  80.         }elseif (!is_null($reservationPayment->getReservationEvent())){
  81.             $totalDirectPayElement $this->reservationPaymentRepo->getTotalPayByResaEvent($reservationPayment->getReservationEvent()->getId());
  82.             $totalPay 0;
  83.             if (!is_null($totalDirectPayElement)) {
  84.                 $totalPay $totalDirectPayElement['total'];
  85.             }
  86.             $totalCinetPayElement $this->customerReservationEventRepo->getTotalPayByReservationEvent($reservationPayment->getReservationEvent()->getId());
  87.             $totalCinetPay 0;
  88.             if (!is_null($totalCinetPayElement)) {
  89.                 $totalCinetPay $totalCinetPayElement['total'];
  90.             }
  91.             $totalToPayElement $this->reservationEvFomulaRepo->getTotalToPayByResaEvent($reservationPayment->getReservationEvent());
  92.             $totalToPay 0;
  93.             if(!is_null($totalToPayElement)){
  94.                 $totalToPay $totalToPayElement['total'];
  95.             }
  96.             if ($totalToPay <= ($totalPay $totalCinetPay)) {
  97.                 $reservationPayment->getReservationEvent()->setPaymentStatus(AkoulaConstant::MODE_PAYED);
  98.             }
  99.             $this->entityManager->flush();
  100.         }
  101.     }
  102.     public function setUser(ViewEvent $event){
  103.         $reservationPayment $event->getControllerResult();
  104.         $method $event->getRequest()->getMethod();
  105.         if (!$reservationPayment instanceof ReservationPayment || Request::METHOD_POST !== $method) {
  106.             return;
  107.         }
  108.         if($this->tokenStorage->getToken()->getUser() instanceof User){
  109.             $reservationPayment->setUser($this->security->getUser());
  110.         }
  111.     }
  112.     public function afterCreate(ViewEvent $event): void
  113.     {
  114.         $reservationPayment $event->getControllerResult();
  115.         $method $event->getRequest()->getMethod();
  116.         if (!$reservationPayment instanceof ReservationPayment || Request::METHOD_POST !== $method) {
  117.             return;
  118.         }
  119.         $operationHistory = new OperationHistory();
  120.         $operationHistory->setUser($this->security->getUser());
  121.         $operationHistory->setAction(AkoulaConstant::ACTION_PAIEMENT);
  122.         $operationHistory->setDetail('Versement, Mode: '.$reservationPayment->getPaymentMethod()->getName().', Montant: '.$reservationPayment->getAmount());
  123.         $operationHistory->setReservationBedroom($reservationPayment->getReservationBedroom());
  124.         $this->entityManager->persist($operationHistory);
  125.         $this->entityManager->flush();
  126.     }
  127.     public function onUpdate(ViewEvent $event): void
  128.     {
  129.         $reservationPayment $event->getControllerResult();
  130.         $method $event->getRequest()->getMethod();
  131.         if (!$reservationPayment instanceof ReservationPayment || Request::METHOD_PUT !== $method) {
  132.             return;
  133.         }
  134.         $operationHistory = new OperationHistory();
  135.         $operationHistory->setUser($this->security->getUser());
  136.         $operationHistory->setAction(AkoulaConstant::ACTION_PAIEMENT);
  137.         $operationHistory->setDetail('Modification Versement, Mode: '.$reservationPayment->getPaymentMethod()->getName().', Montant: '.$reservationPayment->getAmount());
  138.         $operationHistory->setReservationBedroom($reservationPayment->getReservationBedroom());
  139.         $this->entityManager->persist($operationHistory);
  140.         $this->entityManager->flush();
  141.     }
  142. }