← Back
Editing: ElevatorBreakdownRepository.php
<?php namespace App\Repository; use App\Entity\Adresse; use App\Entity\ElevatorBreakdown; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\NonUniqueResultException; use Doctrine\ORM\QueryBuilder; use Doctrine\Persistence\ManagerRegistry; use Symfony\Component\Security\Core\User\UserInterface; /** * @method ElevatorBreakdown|null find($id, $lockMode = null, $lockVersion = null) * @method ElevatorBreakdown|null findOneBy(array $criteria, array $orderBy = null) * @method ElevatorBreakdown[] findAll() * @method ElevatorBreakdown[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) */ class ElevatorBreakdownRepository extends ServiceEntityRepository { use OptionsTrait; public function __construct(ManagerRegistry $registry) { parent::__construct($registry, ElevatorBreakdown::class); } /** * @throws NonUniqueResultException */ public function getLastStatus(Adresse $adresse): ?ElevatorBreakdown { return $this->createQueryBuilder('e') ->leftJoin('e.adresse', 'a') ->where('a.id = :adresseId') ->andWhere('e.endDate IS NULL') ->setParameter('adresseId', $adresse->getId()) ->orderBy('e.startDate', 'desc') ->setMaxResults(1) ->getQuery() ->getOneOrNullResult(); } public function findAllByAgencies(array $agenciesId): QueryBuilder { $qb = $this->createQueryBuilder('eb') ->leftJoin('eb.adresse', 'a') ->where('a.agency IN ('.implode(',', $agenciesId).')') ->groupBy('eb') ->orderBy('eb.startDate', Criteria::DESC); $options = $this->prepareOptions($qb); $this->addOptions( qb: $qb, forceOptions: $options ); return $qb; } /** * @param array<int> $agenciesId */ public function getByAgenciesByMonth(array $agenciesId, bool $previous = false): array { $qb = $this->createQueryBuilder('eb') ->select('COUNT(eb) as cnt') ->addSelect('DATE_FORMAT(eb.startDate, \'%Y-%m\') as month') ->leftJoin('eb.adresse', 'a') ->where('a.agency IN ('.implode(',', $agenciesId).')') ->groupBy('month'); $options = $this->prepareOptions($qb); $this->addOptions($qb, $previous, $options); return $qb->getQuery()->getResult(); } public function getBreakDownByAgenciesByMonth( array $agenciesId, ?UserInterface $manager = null, array $options = [] ): array { $qb = $this->createQueryBuilder('eb') ->select('eb.startDate, a.numero, a.voie, a.codePostal, a.ville, a.numeroAscenseur') ->leftJoin('eb.adresse', 'a'); if (!empty($agenciesId)) { $qb->where('a.agency IN ('.implode(',', $agenciesId).')'); } if ($manager) { $qb ->andWhere('a.manager = :manager') ->setParameter('manager', $manager); } $qb ->andWhere('eb.endDate IS NULL') ->orderBy('eb.startDate', 'ASC'); if (isset($options['agent'])) { $qb->leftJoin('a.userAddresses', 'ua') ->andWhere('ua.user = :agent') ->setParameter('agent', $options['agent']); unset($options['agent']); } return $qb->getQuery()->getResult(); } private function prepareOptions(QueryBuilder $qb): array { $options = []; $sessionFilters = $this->sessionFiltersContainer->getCurrent(); if ($sessionFilters->getDateRange()?->getDateStart()) { $qb->andWhere('eb.startDate >= :startDate') ->setParameter('startDate', $sessionFilters->getDateRange()->getDateStart()); } if ($sessionFilters->getDateRange()?->getDateEnd()) { $qb->andWhere('eb.endDate <= :endDate') ->orWhere('eb.endDate is null') ->setParameter('endDate', $sessionFilters->getDateRange()->getDateEnd()); } $sessionFilters->setDateRange(null); $options['solution'] = null; $options['dateRange'] = null; return $options; } }
Save File
Cancel