← Back
Editing: HistoriqueAlerteRepository.php
<?php namespace App\Repository; use App\Entity\Adresse; use App\Entity\Agency; use App\Entity\AgencyPrestataire; use App\Entity\HistoriqueAlerte; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\ORM\QueryBuilder; use Doctrine\Persistence\ManagerRegistry; use Symfony\Component\Security\Core\User\UserInterface; /** * @method HistoriqueAlerte|null find($id, $lockMode = null, $lockVersion = null) * @method HistoriqueAlerte|null findOneBy(array $criteria, array $orderBy = null) * @method HistoriqueAlerte[] findAll() * @method HistoriqueAlerte[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) */ class HistoriqueAlerteRepository extends ServiceEntityRepository { use OptionsTrait { addOptions as protected addOptionsTrait; } public function __construct(ManagerRegistry $registry) { parent::__construct($registry, HistoriqueAlerte::class); } /** * @return HistoriqueAlerte[] */ public function findByUser( UserInterface $user, ?Adresse $adresse = null, array $options = [] ): array { $qb = $this->createQueryBuilder('ha') ->leftJoin('ha.controle', 'c') ->leftJoin('c.adresse', 'ad'); if ($adresse) { $qb->andWhere('c.adresse = :adresse') ->setParameter('adresse', $adresse); } if (in_array('ROLE_MANAGER', $user->getRoles())) { $qb->andWhere( $qb->expr()->orX( 'ad.manager = :manager', 'c.agent = :manager' ) ) ->setParameter('manager', $user); } if (in_array('ROLE_AGENT', $user->getRoles())) { $qb ->andWhere('c.agent = :agent') ->setParameter('agent', $user); } $this->addOptions( qb: $qb, forceOptions: $options ); return $qb->getQuery()->getResult(); } /** * @return HistoriqueAlerte[] */ public function getByManagerByPrestataire( UserInterface $manager, AgencyPrestataire $prestataire ): array { $qb = $this->createQueryBuilder('ha') ->leftJoin('ha.controle', 'c') ->leftJoin('c.agent', 'ag') ->leftJoin('ha.prestataire', 'cp') ->leftJoin('cp.prestataire', 'p') ->where('ha.prestataire = :prestataire'); $qb->andWhere($qb->expr()->orX('ag.manager = :manager', 'c.agent = :manager')) ->setParameter('prestataire', $prestataire) ->setParameter('manager', $manager); $this->addOptions($qb); return $qb ->getQuery() ->getResult(); } /** * @return HistoriqueAlerte[] */ public function getByAgencyByPrestataire(Agency $agency, AgencyPrestataire $prestataire): array { $dateStart = new \DateTime('midnight first day of this month 11 month ago'); $dateEnd = new \DateTime('midnight first day of next month'); return $this->createQueryBuilder('ha') ->select('ha') ->where('ha.prestataire = :prestataire') ->andWhere('ha.agency = :agency') ->andWhere('ha.date >= :dateStart') ->andWhere('ha.date < :dateEnd') ->setParameter('prestataire', $prestataire) ->setParameter('agency', $agency) ->setParameter('dateStart', $dateStart) ->setParameter('dateEnd', $dateEnd) ->getQuery() ->getResult(); } public function getQueryByAgencies(array $agenciesId): QueryBuilder { $qb = $this->createQueryBuilder('ha') ->where('ha.agency IN ('.implode(',', $agenciesId).')'); $qb->leftJoin('ha.controle', 'c'); $qb->leftJoin('c.adresse', 'a'); return $this->addOptions($qb); } /** * @return array<array-key, array{cnt: int, month: string}> */ public function getByAgenciesByMonth(array $agenciesId, bool $previous = false, array $options = []): array { $qb = $this->createQueryBuilder('ha') ->select('COUNT(ha) as cnt') ->addSelect('DATE_FORMAT(c.dateRealisation, \'%Y-%m\') as month') ->leftJoin('ha.controle', 'c') ->leftJoin('c.adresse', 'a') ->where('a.agency IN (:agencies)') ->setParameter('agencies', $agenciesId) ->groupBy('month'); $qb = $this->addOptions($qb, $previous, $options); return $qb->getQuery()->getResult(); } private function addOptions(QueryBuilder $qb, bool $previous = false, array $forceOptions = []): QueryBuilder { return $this->addOptionsTrait($qb, $previous, $forceOptions); } }
Save File
Cancel