src/Repository/ContractRepository.php line 106

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Contract;
  4. use App\Entity\Enterprise;
  5. use App\Entity\Provider;
  6. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  7. use Doctrine\ORM\NonUniqueResultException;
  8. use Doctrine\ORM\NoResultException;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use function Doctrine\ORM\QueryBuilder;
  11. /**
  12.  * @method Contract|null find($id, $lockMode = null, $lockVersion = null)
  13.  * @method Contract|null findOneBy(array $criteria, array $orderBy = null)
  14.  * @method Contract[]    findAll()
  15.  * @method Contract[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  16.  */
  17. class ContractRepository extends ServiceEntityRepository
  18. {
  19.     public function __construct(ManagerRegistry $registry)
  20.     {
  21.         parent::__construct($registryContract::class);
  22.     }
  23.     private function getContractActif()
  24.     {
  25.         $qb $this->createQueryBuilder('c');
  26.         return $qb->where($qb->expr()->eq('c.actif'true));
  27.     }
  28.     private function getContractArchived()
  29.     {
  30.         $qb $this->createQueryBuilder('c');
  31.         return $qb->where('c.actif = false');
  32.     }
  33.     /**
  34.      * @throws NonUniqueResultException
  35.      */
  36.     public function countActifContract()
  37.     {
  38.         $qb $this->createQueryBuilder('c');
  39.         return $qb
  40.             ->select('count(c.id)')
  41.             ->where($qb->expr()->eq('c.actif'true))
  42.             ->getQuery()
  43.             ->getOneOrNullResult();
  44.     }
  45.     public function getContractActifProvider(Provider $provider, ?bool $actif true)
  46.     {
  47.         $qb $this->getContractActif();
  48.         return $qb->join('c.providers''p')
  49.             ->andWhere($qb->expr()->eq('p'$provider->getId()))
  50.             ->getQuery()
  51.             ->getResult();
  52.     }
  53.     public function getContractArchivedByProvider(Provider $provider)
  54.     {
  55.         $qb $this->getContractArchived();
  56.         return $qb->join('c.providers''p')
  57.             ->andWhere($qb->expr()->eq('p'$provider->getId()))
  58.             ->getQuery()
  59.             ->getResult();
  60.     }
  61.     public function getCountOfAssociatifContract(string $slug)
  62.     {
  63.         $qb $this->createQueryBuilder('c');
  64.         return $qb
  65.             ->select('count(c)')
  66.             ->where('c.slug LIKE :slug')
  67.             ->setParameter(':slug''%' $slug '%')
  68.             ->getQuery()
  69.             ->getSingleScalarResult();
  70.     }
  71.     /**
  72.      * @throws NonUniqueResultException
  73.      * @throws NoResultException
  74.      */
  75.     public function countCollectByEnterprise(?Enterprise $enterprise)
  76.     {
  77.         $qb $this->createQueryBuilder('c')
  78.             ->select('count(collects)');
  79.         if ($enterprise instanceof Enterprise) {
  80.             $qb->andWhere('c.enterprise = :enterprise')
  81.                 ->setParameter('enterprise'$enterprise);
  82.         }
  83.         $qb->leftJoin('c.collects''collects');
  84.         return $qb
  85.             ->getQuery()
  86.             ->getSingleScalarResult();
  87.     }
  88.     /**
  89.      * @throws NonUniqueResultException
  90.      * @throws NoResultException
  91.      */
  92.     public function countCollectByEnterpriseTypeAndTime(
  93.         ?Enterprise $enterprise,
  94.         bool $total false,
  95.         bool $valorization,
  96.         string $start,
  97.         string $end
  98.     ): int {
  99.         $qb $this->createQueryBuilder('c')
  100.             ->select('count(collects)')
  101.             ->leftJoin('c.valorization''v');
  102.         $nonValoriser = ["Enfouissement""IncinĂ©ration (sans valorisation)"];
  103.         if(!$total) {
  104.             if ($valorization) {
  105.                 $qb->andWhere('v.name not in (:nomValoriser)');
  106.             } else {
  107.                 $qb->andWhere('v.name in (:nomValoriser)');
  108.             }
  109.             $qb->setParameter('nomValoriser'$nonValoriser);
  110.         }
  111.         if ($enterprise instanceof Enterprise) {
  112.             $qb->andWhere('c.enterprise = :enterprise')
  113.                 ->setParameter('enterprise'$enterprise);
  114.         }
  115.         $qb->leftJoin('c.collects''collects')
  116.             ->andWhere('collects.created_at BETWEEN :start AND :end ')
  117.             ->setParameter('start'$start)
  118.             ->setParameter('end'$end);
  119.         return $qb
  120.             ->getQuery()
  121.             ->getSingleScalarResult();
  122.     }
  123. }