<?php
namespace App\Repository;
use App\Entity\Actions;
use App\Entity\Clients;
use App\Entity\Contacts;
use App\Entity\Status;
use App\Entity\Tickets;
use App\Service\Securizer;
use DateTime;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use DateTimeInterface;
use Doctrine\ORM\QueryBuilder;
/**
* @method Tickets|null find($id, $lockMode = null, $lockVersion = null)
* @method Tickets|null findOneBy(array $criteria, array $orderBy = null)
* @method Tickets[] findAll()
* @method Tickets[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class TicketsRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Tickets::class);
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function add(Tickets $entity, bool $flush = true): void
{
$this->_em->persist($entity);
if ($flush) {
$this->_em->flush();
}
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function remove(Tickets $entity, bool $flush = true): void
{
$this->_em->remove($entity);
if ($flush) {
$this->_em->flush();
}
}
//récupère la liste de tous les ticket accessible a l'utilisateur
public function findTicketAll($user, Securizer $securizer, ContactsRepository $repoContact): array
{
//recupere l'id du ou des contact qu'il est possible d'afficher
$contacts = $repoContact->findContactTous($user, $securizer);
//gestion des données retournées en fonction du role de la personne connecté
if ($securizer->isGranted($user, "ROLE_TECH")) {
//les roles qui sont au minimum tech peuvent voir tous les tickets
$tickets = $this->findAll();
} else if ($securizer->isGranted($user, "ROLE_CLIENT")) {
//les roles client peuvent voir la liste des tickets dons les beneficiares sont lié au même client qu'eux
$tickets = $this->findBy(['beneficiaire' => $contacts]);
} else {
$tickets = $this->findBy(['beneficiaire' => $user->getId()]);
}
return $tickets;
}
public function findTicketStatus($user, $status, Securizer $securizer, ContactsRepository $repoContact): array
{
$tickets = $this->findTicketAll($user, $securizer, $repoContact);
$res = [];
foreach ($tickets as $ticket) {
if (in_array($ticket->getIdStatus()->getStatus(), $status))
$res[] = $ticket;
}
return $res;
}
protected function genericRequestOrder(QueryBuilder $queryBuilder)
{
$response = $queryBuilder->select('t')
->orderBy('t.dateCreation')
->getQuery()
->getResult();
return $response;
}
public function getTicketsByDate(string $start, string $fin, $user, Securizer $securizer, ContactsRepository $repoContact)
{
if ($securizer->isGranted($user, "ROLE_TECH")) {
$queryBuilder = ($this->createQueryBuilder('t')
->where('t.dateCreation BETWEEN :startDate AND :endDate')
->setParameter('startDate', $start)
->setParameter('endDate', $fin));
$tickets = $this->genericRequestOrder($queryBuilder);
} else if ($securizer->isGranted($user, "ROLE_CLIENT")) {
//recupere l'id du ou des contact qu'il est possible d'afficher
$contacts = $repoContact->findContactTous($user, $securizer);
//les roles client peuvent voir la liste des tickets dons les beneficiares sont lié au même client qu'eux
$tickets = $this->findBy(['beneficiaire' => $contacts]);
} else {
$tickets = $this->findBy(['beneficiaire' => $user->getId()]);
}
return $tickets;
}
public function findTimeTicketTelemaintenance()
{
return $this->createQueryBuilder('t')
->select('SUM(ac.duree)')
->addSelect('cl.id')
->addSelect('cl.hotlineCompteur')
->addSelect("cl.nom")
->innerJoin(Actions::class, 'ac', Join::WITH, 'ac.ticket = t.id')
->innerJoin(Contacts::class, 'co', Join::WITH, 'co.id = t.beneficiaire')
->innerJoin(Clients::class, 'cl', Join::WITH, 'cl.id = co.idClient')
->where('ac.ticket IS NOT NULL')
->andWhere('ac.intervention IS NULL')
->andWhere('t.idStatus NOT IN (3, 4)')
->andWhere('cl.archiver != true')
->groupBy('cl.id')
->orderBy('cl.id')
->getQuery()
->getResult();
}
public function findTimeNotCloseTicketTelemaintenance()
{
return $this->createQueryBuilder('t')
->select('SUM(ac.duree)')
->addSelect('cl.id')
->addSelect('cl.hotlineCompteur')
->innerJoin(Actions::class, 'ac', Join::WITH, 'ac.ticket = t.id')
->innerJoin(Contacts::class, 'co', Join::WITH, 'co.id = t.beneficiaire')
->innerJoin(Clients::class, 'cl', Join::WITH, 'cl.id = co.idClient')
->where('ac.ticket IS NOT NULL')
->andWhere('t.idStatus IN (3, 4)')
->andWhere('cl.archiver != true')
->groupBy('cl.id')
->orderBy('cl.id')
->getQuery()
->getResult();
}
public function getClientTopTickets(int $idClient, DateTime $dateDebut, DateTime $dateFin, int $limit = 10): array
{
return $this->createQueryBuilder('t')
->select('t.id, t.dateCreation, t.titre, co.nom, co.prenom, s.status, SUM(ac.duree) as duree')
->innerJoin(Contacts::class, 'co', Join::WITH, 'co.id = t.beneficiaire')
->innerJoin(Clients::class, 'cl', Join::WITH, 'cl.id = co.idClient')
->innerJoin(Actions::class, 'ac', Join::WITH, 'ac.ticket = t.id')
->innerJoin(Status::class, 's', Join::WITH, 's.id = t.idStatus')
->where('cl.id = :idClient')
->andWhere('ac.intervention IS NULL')
->andWhere('t.dateCreation BETWEEN :dateDebut AND :dateFin')
->setParameter('idClient', $idClient)
->setParameter('dateDebut', $dateDebut->format('Y-m-d'))
->setParameter('dateFin', $dateFin->format('Y-m-d'))
->groupBy('t.id, t.dateCreation, t.titre, co.nom, co.prenom, s.status')
->orderBy('duree', 'DESC')
->setMaxResults($limit)
->getQuery()
->getResult();
}
public function getClientTopDemandes(int $idClient, DateTime $dateDebut, DateTime $dateFin, int $limit = 10): array
{
return $this->createQueryBuilder('t')
->select('COUNT(DISTINCT t.id) as nbTickets, co.nom, co.prenom, SUM(ac.duree) as duree')
->innerJoin(Contacts::class, 'co', Join::WITH, 'co.id = t.beneficiaire')
->innerJoin(Clients::class, 'cl', Join::WITH, 'cl.id = co.idClient')
->innerJoin(Actions::class, 'ac', Join::WITH, 'ac.ticket = t.id')
->where('cl.id = :idClient')
->andWhere('ac.intervention IS NULL')
->andWhere('t.dateCreation BETWEEN :dateDebut AND :dateFin')
->setParameter('idClient', $idClient)
->setParameter('dateDebut', $dateDebut->format('Y-m-d'))
->setParameter('dateFin', $dateFin->format('Y-m-d'))
->groupBy('co.nom, co.prenom')
->orderBy('nbTickets', 'DESC')
->setMaxResults($limit)
->getQuery()
->getResult();
}
public function getClientTopConsommateurs(int $idClient, DateTime $dateDebut, DateTime $dateFin, int $limit = 10): array
{
return $this->createQueryBuilder('t')
->select('COUNT(DISTINCT t.id) as nbTickets, co.nom, co.prenom, SUM(ac.duree) as duree')
->innerJoin(Contacts::class, 'co', Join::WITH, 'co.id = t.beneficiaire')
->innerJoin(Clients::class, 'cl', Join::WITH, 'cl.id = co.idClient')
->innerJoin(Actions::class, 'ac', Join::WITH, 'ac.ticket = t.id')
->where('cl.id = :idClient')
->andWhere('ac.intervention IS NULL')
->andWhere('t.dateCreation BETWEEN :dateDebut AND :dateFin')
->setParameter('idClient', $idClient)
->setParameter('dateDebut', $dateDebut->format('Y-m-d'))
->setParameter('dateFin', $dateFin->format('Y-m-d'))
->groupBy('co.nom, co.prenom')
->orderBy('duree', 'DESC')
->setMaxResults($limit)
->getQuery()
->getResult();
}
public function getTicketsByClientAndDate(int $idClient, DateTimeInterface $start, DateTimeInterface $fin): array
{
return $this->createQueryBuilder('t')
->select('t.id, SUM(ac.duree) as duree')
->innerJoin(Contacts::class, 'co', Join::WITH, 'co.id = t.beneficiaire')
->innerJoin(Actions::class, 'ac', Join::WITH, 'ac.ticket = t.id')
->where('co.idClient = :idClient')
->andWhere('ac.intervention IS NULL')
->andWhere('t.dateCreation BETWEEN :startDate AND :endDate')
->setParameter('idClient', $idClient)
->setParameter('startDate', $start)
->setParameter('endDate', $fin)
->groupBy('t.id')
->getQuery()
->getResult();
}
}