src/Repository/TicketsRepository.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Actions;
  4. use App\Entity\Clients;
  5. use App\Entity\Contacts;
  6. use App\Entity\Status;
  7. use App\Entity\Tickets;
  8. use App\Service\Securizer;
  9. use DateTime;
  10. use Doctrine\ORM\ORMException;
  11. use Doctrine\ORM\OptimisticLockException;
  12. use Doctrine\ORM\Query\Expr\Join;
  13. use Doctrine\Persistence\ManagerRegistry;
  14. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  15. use DateTimeInterface;
  16. use Doctrine\ORM\QueryBuilder;
  17. /**
  18.  * @method Tickets|null find($id, $lockMode = null, $lockVersion = null)
  19.  * @method Tickets|null findOneBy(array $criteria, array $orderBy = null)
  20.  * @method Tickets[]    findAll()
  21.  * @method Tickets[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  22.  */
  23. class TicketsRepository extends ServiceEntityRepository
  24. {
  25.     public function __construct(ManagerRegistry $registry)
  26.     {
  27.         parent::__construct($registryTickets::class);
  28.     }
  29.     /**
  30.      * @throws ORMException
  31.      * @throws OptimisticLockException
  32.      */
  33.     public function add(Tickets $entitybool $flush true): void
  34.     {
  35.         $this->_em->persist($entity);
  36.         if ($flush) {
  37.             $this->_em->flush();
  38.         }
  39.     }
  40.     /**
  41.      * @throws ORMException
  42.      * @throws OptimisticLockException
  43.      */
  44.     public function remove(Tickets $entitybool $flush true): void
  45.     {
  46.         $this->_em->remove($entity);
  47.         if ($flush) {
  48.             $this->_em->flush();
  49.         }
  50.     }
  51.     //récupère la liste de tous les ticket accessible a l'utilisateur
  52.     public function findTicketAll($userSecurizer $securizerContactsRepository $repoContact): array
  53.     {
  54.         //recupere l'id du ou des contact qu'il est possible d'afficher
  55.         $contacts $repoContact->findContactTous($user$securizer);
  56.         //gestion des données retournées en fonction du role de la personne connecté
  57.         if ($securizer->isGranted($user"ROLE_TECH")) {
  58.             //les roles qui sont au minimum tech peuvent voir tous les tickets
  59.             $tickets $this->findAll();
  60.         } else if ($securizer->isGranted($user"ROLE_CLIENT")) {
  61.             //les roles client peuvent voir la liste des tickets dons les beneficiares sont lié au même client qu'eux
  62.             $tickets $this->findBy(['beneficiaire' => $contacts]);
  63.         } else {
  64.             $tickets $this->findBy(['beneficiaire' => $user->getId()]);
  65.         }
  66.         return $tickets;
  67.     }
  68.     public function findTicketStatus($user$statusSecurizer $securizerContactsRepository $repoContact): array
  69.     {
  70.         $tickets $this->findTicketAll($user$securizer$repoContact);
  71.         $res = [];
  72.         foreach ($tickets as $ticket) {
  73.             if (in_array($ticket->getIdStatus()->getStatus(), $status))
  74.                 $res[] = $ticket;
  75.         }
  76.         return $res;
  77.     }
  78.     protected function genericRequestOrder(QueryBuilder $queryBuilder)
  79.     {
  80.         $response $queryBuilder->select('t')
  81.             ->orderBy('t.dateCreation')
  82.             ->getQuery()
  83.             ->getResult();
  84.         return $response;
  85.     }
  86.     public function getTicketsByDate(string $startstring $fin$userSecurizer $securizerContactsRepository $repoContact)
  87.     {
  88.         if ($securizer->isGranted($user"ROLE_TECH")) {
  89.             $queryBuilder = ($this->createQueryBuilder('t')
  90.                 ->where('t.dateCreation BETWEEN :startDate AND :endDate')
  91.                 ->setParameter('startDate'$start)
  92.                 ->setParameter('endDate'$fin));
  93.             $tickets $this->genericRequestOrder($queryBuilder);
  94.         } else if ($securizer->isGranted($user"ROLE_CLIENT")) {
  95.             //recupere l'id du ou des contact qu'il est possible d'afficher
  96.             $contacts $repoContact->findContactTous($user$securizer);
  97.             //les roles client peuvent voir la liste des tickets dons les beneficiares sont lié au même client qu'eux
  98.             $tickets $this->findBy(['beneficiaire' => $contacts]);
  99.         } else {
  100.             $tickets $this->findBy(['beneficiaire' => $user->getId()]);
  101.         }
  102.         return $tickets;
  103.     }
  104.     public function findTimeTicketTelemaintenance()
  105.     {
  106.         return $this->createQueryBuilder('t')
  107.             ->select('SUM(ac.duree)')
  108.             ->addSelect('cl.id')
  109.             ->addSelect('cl.hotlineCompteur')
  110.             ->addSelect("cl.nom")
  111.             ->innerJoin(Actions::class, 'ac'Join::WITH'ac.ticket = t.id')
  112.             ->innerJoin(Contacts::class, 'co'Join::WITH'co.id = t.beneficiaire')
  113.             ->innerJoin(Clients::class, 'cl'Join::WITH'cl.id = co.idClient')
  114.             ->where('ac.ticket IS NOT NULL')
  115.             ->andWhere('ac.intervention IS NULL')
  116.             ->andWhere('t.idStatus NOT IN (3, 4)')
  117.             ->andWhere('cl.archiver != true')
  118.             ->groupBy('cl.id')
  119.             ->orderBy('cl.id')
  120.             ->getQuery()
  121.             ->getResult();
  122.     }
  123.     public function findTimeNotCloseTicketTelemaintenance()
  124.     {
  125.         return $this->createQueryBuilder('t')
  126.             ->select('SUM(ac.duree)')
  127.             ->addSelect('cl.id')
  128.             ->addSelect('cl.hotlineCompteur')
  129.             ->innerJoin(Actions::class, 'ac'Join::WITH'ac.ticket = t.id')
  130.             ->innerJoin(Contacts::class, 'co'Join::WITH'co.id = t.beneficiaire')
  131.             ->innerJoin(Clients::class, 'cl'Join::WITH'cl.id = co.idClient')
  132.             ->where('ac.ticket IS NOT NULL')
  133.             ->andWhere('t.idStatus IN (3, 4)')
  134.             ->andWhere('cl.archiver != true')
  135.             ->groupBy('cl.id')
  136.             ->orderBy('cl.id')
  137.             ->getQuery()
  138.             ->getResult();
  139.     }
  140.     public function getClientTopTickets(int $idClientDateTime $dateDebutDateTime $dateFinint $limit 10): array
  141.     {
  142.         return $this->createQueryBuilder('t')
  143.             ->select('t.id, t.dateCreation, t.titre, co.nom, co.prenom, s.status, SUM(ac.duree) as duree')
  144.             ->innerJoin(Contacts::class, 'co'Join::WITH'co.id = t.beneficiaire')
  145.             ->innerJoin(Clients::class, 'cl'Join::WITH'cl.id = co.idClient')
  146.             ->innerJoin(Actions::class, 'ac'Join::WITH'ac.ticket = t.id')
  147.             ->innerJoin(Status::class, 's'Join::WITH's.id = t.idStatus')
  148.             ->where('cl.id = :idClient')
  149.             ->andWhere('ac.intervention IS NULL')
  150.             ->andWhere('t.dateCreation BETWEEN :dateDebut AND :dateFin')
  151.             ->setParameter('idClient'$idClient)
  152.             ->setParameter('dateDebut'$dateDebut->format('Y-m-d'))
  153.             ->setParameter('dateFin'$dateFin->format('Y-m-d'))
  154.             ->groupBy('t.id, t.dateCreation, t.titre, co.nom, co.prenom, s.status')
  155.             ->orderBy('duree''DESC')
  156.             ->setMaxResults($limit)
  157.             ->getQuery()
  158.             ->getResult();
  159.     }
  160.     public function getClientTopDemandes(int $idClientDateTime $dateDebutDateTime $dateFinint $limit 10): array
  161.     {
  162.         return $this->createQueryBuilder('t')
  163.             ->select('COUNT(DISTINCT t.id) as nbTickets, co.nom, co.prenom, SUM(ac.duree) as duree')
  164.             ->innerJoin(Contacts::class, 'co'Join::WITH'co.id = t.beneficiaire')
  165.             ->innerJoin(Clients::class, 'cl'Join::WITH'cl.id = co.idClient')
  166.             ->innerJoin(Actions::class, 'ac'Join::WITH'ac.ticket = t.id')
  167.             ->where('cl.id = :idClient')
  168.             ->andWhere('ac.intervention IS NULL')
  169.             ->andWhere('t.dateCreation BETWEEN :dateDebut AND :dateFin')
  170.             ->setParameter('idClient'$idClient)
  171.             ->setParameter('dateDebut'$dateDebut->format('Y-m-d'))
  172.             ->setParameter('dateFin'$dateFin->format('Y-m-d'))
  173.             ->groupBy('co.nom, co.prenom')
  174.             ->orderBy('nbTickets''DESC')
  175.             ->setMaxResults($limit)
  176.             ->getQuery()
  177.             ->getResult();
  178.     }
  179.     public function getClientTopConsommateurs(int $idClientDateTime $dateDebutDateTime $dateFinint $limit 10): array
  180.     {
  181.         return $this->createQueryBuilder('t')
  182.             ->select('COUNT(DISTINCT t.id) as nbTickets, co.nom, co.prenom, SUM(ac.duree) as duree')
  183.             ->innerJoin(Contacts::class, 'co'Join::WITH'co.id = t.beneficiaire')
  184.             ->innerJoin(Clients::class, 'cl'Join::WITH'cl.id = co.idClient')
  185.             ->innerJoin(Actions::class, 'ac'Join::WITH'ac.ticket = t.id')
  186.             ->where('cl.id = :idClient')
  187.             ->andWhere('ac.intervention IS NULL')
  188.             ->andWhere('t.dateCreation BETWEEN :dateDebut AND :dateFin')
  189.             ->setParameter('idClient'$idClient)
  190.             ->setParameter('dateDebut'$dateDebut->format('Y-m-d'))
  191.             ->setParameter('dateFin'$dateFin->format('Y-m-d'))
  192.             ->groupBy('co.nom, co.prenom')
  193.             ->orderBy('duree''DESC')
  194.             ->setMaxResults($limit)
  195.             ->getQuery()
  196.             ->getResult();
  197.     }
  198.     public function getTicketsByClientAndDate(int $idClientDateTimeInterface $startDateTimeInterface $fin): array
  199.     {
  200.         return $this->createQueryBuilder('t')
  201.             ->select('t.id, SUM(ac.duree) as duree')
  202.             ->innerJoin(Contacts::class, 'co'Join::WITH'co.id = t.beneficiaire')
  203.             ->innerJoin(Actions::class, 'ac'Join::WITH'ac.ticket = t.id')
  204.             ->where('co.idClient = :idClient')
  205.             ->andWhere('ac.intervention IS NULL')
  206.             ->andWhere('t.dateCreation BETWEEN :startDate AND :endDate')
  207.             ->setParameter('idClient'$idClient)
  208.             ->setParameter('startDate'$start)
  209.             ->setParameter('endDate'$fin)
  210.             ->groupBy('t.id')
  211.             ->getQuery()
  212.             ->getResult();
  213.     }
  214. }