<?php
namespace App\Service;
use App\Entity\AffiliatedEventApplication;
use App\Entity\Event;
use App\Entity\MarketplaceReservation;
use App\Entity\User;
use App\Repository\EventRepository;
use App\Repository\MarketplaceReservationRepository;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Security\Core\Security;
class AdminEventInfosService
{
/**
* @var Security
*/
private $security;
/**
* @var EventRepository
*/
private $eventRepository;
/**
* @var MarketplaceReservationRepository
*/
private $marketplaceReservationRepository;
/**
* @var ParameterBagInterface
*/
private $parameterBagInterface;
/**
* @var EntityManagerInterface
*/
private $em;
public function __construct(Security $security, EntityManagerInterface $em, ParameterBagInterface $parameterBagInterface)
{
$this->security = $security;
$this->em = $em;
$this->parameterBagInterface = $parameterBagInterface;
$this->eventRepository = $this->em->getRepository(Event::class);
$this->marketplaceReservationRepository = $this->em->getRepository(MarketplaceReservation::class);
$this->affiliatedEventApplicationRepo = $this->em->getRepository(AffiliatedEventApplication::class);
}
/**
* Get the count for the current events for admin
* @return int
*/
public function getCurrentEventsCount(): int
{
$result = 0;
$user = $this->security->getUser();
if ($user instanceof User && $this->security->isGranted(User::ROLE_ADMIN)){
try {
$result = intval($this->eventRepository->getAdminCurrentEventsQueryBuilder()
->select('COUNT(event)')->getQuery()->getSingleScalarResult());
} catch (NoResultException|NonUniqueResultException $e) {
$result = 0;
}
}
return $result;
}
/**
* Get the count for events that will be cancelled in 96 hours
* @return int
*/
public function getEventsWarningCount(): int
{
$result = 0;
$user = $this->security->getUser();
if ($user instanceof User && $this->security->isGranted(User::ROLE_ADMIN)){
$startLimit = (new DateTime())->modify('+'. $this->parameterBagInterface->get('alert_expert_event') .'hours')->setTime(0,0,1);
$endLimit = (new DateTime())->modify('+'. $this->parameterBagInterface->get('alert_expert_event') .'hours')->setTime(23,59,59);
try {
$result = intval( $this->eventRepository->queryFindByDatesIntervalle(
[
'startLimit' => $startLimit,
'endLimit' => $endLimit,
'regCount' => true
])
->select('COUNT(DISTINCT e)')->getQuery()->getSingleScalarResult());
} catch (NoResultException|NonUniqueResultException $e) {
$result = 0;
}
}
return $result;
}
/**
* Get the count for events that will be cancelled in 96 hours
* @return int
*/
public function getEventsAlertCount(): int
{
$result = 0;
$user = $this->security->getUser();
if ($user instanceof User && $this->security->isGranted(User::ROLE_ADMIN)){
$startLimit = (new DateTime())->modify('+'. $this->parameterBagInterface->get('before_send_warning_event_mail')['limit_hours'] .'hours')->setTime(0,0,1);
$endLimit = (new DateTime())->modify('+'. $this->parameterBagInterface->get('before_send_warning_event_mail')['limit_hours'] .'hours')->setTime(23,59,59);
try {
$result = intval( $this->eventRepository->queryFindByDatesIntervalle(
[
'startLimit' => $startLimit,
'endLimit' => $endLimit,
'regCount' => true
])
->select('COUNT(e)')->getQuery()->getSingleScalarResult());
} catch (NoResultException|NonUniqueResultException $e) {
$result = 0;
}
}
return $result;
}
/**
* Get the count for pending marketplaceReservations
* @return int
*/
public function getPendingMarketplaceReservations(): int
{
$result = 0;
$user = $this->security->getUser();
if ($user instanceof User && $this->security->isGranted(User::ROLE_ADMIN)){
try {
$result = $result = intval($this->marketplaceReservationRepository->createQueryBuilder('marketplaceRes')
->andWhere('marketplaceRes.paymentStatus = :success')
->andWhere('marketplaceRes.status = :pending')
->setParameter('pending', MarketplaceReservation::STATUS_PENDING)
->setParameter('success', MarketplaceReservation::PAYMENT_STATUS_SUCCESS)
->select('COUNT(marketplaceRes)')->getQuery()->getSingleScalarResult());;
} catch (NoResultException|NonUniqueResultException $e) {
$result = 0;
}
}
return $result;
}
/**
* Get the count for special marketplace requests
* @return int
*/
public function getSpecialMarketplaceRequests(): int
{
$result = 0;
$user = $this->security->getUser();
if ($user instanceof User && $this->security->isGranted(User::ROLE_ADMIN)){
try {
$result = $result = intval($this->marketplaceReservationRepository->createQueryBuilder('marketplaceRes')
->andWhere('marketplaceRes.paymentStatus = :free')
->andWhere('marketplaceRes.status = :pending')
->setParameter('pending', MarketplaceReservation::STATUS_PENDING)
->setParameter('free', MarketplaceReservation::PAYMENT_STATUS_FREE)
->select('COUNT(marketplaceRes)')->getQuery()->getSingleScalarResult());;
} catch (NoResultException|NonUniqueResultException $e) {
$result = 0;
}
}
return $result;
}
/**
* Get the count for accepted marketplaceReservations
* @return int
*/
public function getAcceptedMarketplaceReservations(): int
{
$result = 0;
$user = $this->security->getUser();
if ($user instanceof User && $this->security->isGranted(User::ROLE_ADMIN)){
try {
$result = $result = intval($this->marketplaceReservationRepository->createQueryBuilder('marketplaceRes')
->andWhere('marketplaceRes.paymentStatus IN (:success, :refund)')
->andWhere('marketplaceRes.status IN (:accepted, :canceled)')
->setParameter('accepted', MarketplaceReservation::STATUS_ACCEPTED)
->setParameter('canceled', MarketplaceReservation::STATUS_CANCELED)
->setParameter('success', MarketplaceReservation::PAYMENT_STATUS_SUCCESS)
->setParameter('refund', MarketplaceReservation::PAYMENT_STATUS_REFUND)
->select('COUNT(marketplaceRes)')->getQuery()->getSingleScalarResult());;
} catch (NoResultException|NonUniqueResultException $e) {
$result = 0;
}
}
return $result;
}
// /**
// * Get the count for the available events for a specialist
// * @return int
// */
// public function getAvailableEventsCount(): int
// {
// $result = 0;
// $user = $this->security->getUser();
// if ($user instanceof User && $this->security->isGranted(User::ROLE_COMPANY)){
// $company = $this->em->getRepository(Company::class)->find($user->getCompany()->getId());
// try {
// $result = intval($this->eventRepository->getCompanySpecialistEventsQueryBuilder($company)
// ->select('COUNT(event)')->getQuery()->getSingleScalarResult());
// } catch (NoResultException|NonUniqueResultException $e) {
// $result = 0;
// }
// }
// return $result;
// }
// /**
// * Get the count for the available affiliated event applications for a specialist
// * @return int
// */
// public function getAffiliatedEventsApplicationCount(): int
// {
// $result = 0;
// $user = $this->security->getUser();
// if ($user instanceof User && $this->security->isGranted(User::ROLE_COMPANY)){
// $company = $this->em->getRepository(Company::class)->find($user->getCompany()->getId());
// try {
// $result = intval($this->affiliatedEventApplicationRepo->getAffiliatedEventsForCompanyQueryBuilder($company)
// ->select('COUNT(event)')->getQuery()->getSingleScalarResult());
// } catch (NoResultException|NonUniqueResultException $e) {
// $result = 0;
// }
// }
// return $result;
// }
}