<?php
namespace App\Repository;
use App\Entity\Contract;
use App\Entity\Enterprise;
use App\Entity\Provider;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Doctrine\Persistence\ManagerRegistry;
use function Doctrine\ORM\QueryBuilder;
/**
* @method Contract|null find($id, $lockMode = null, $lockVersion = null)
* @method Contract|null findOneBy(array $criteria, array $orderBy = null)
* @method Contract[] findAll()
* @method Contract[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ContractRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Contract::class);
}
private function getContractActif()
{
$qb = $this->createQueryBuilder('c');
return $qb->where($qb->expr()->eq('c.actif', true));
}
private function getContractArchived()
{
$qb = $this->createQueryBuilder('c');
return $qb->where('c.actif = false');
}
/**
* @throws NonUniqueResultException
*/
public function countActifContract()
{
$qb = $this->createQueryBuilder('c');
return $qb
->select('count(c.id)')
->where($qb->expr()->eq('c.actif', true))
->getQuery()
->getOneOrNullResult();
}
public function getContractActifProvider(Provider $provider, ?bool $actif = true)
{
$qb = $this->getContractActif();
return $qb->join('c.providers', 'p')
->andWhere($qb->expr()->eq('p', $provider->getId()))
->getQuery()
->getResult();
}
public function getContractArchivedByProvider(Provider $provider)
{
$qb = $this->getContractArchived();
return $qb->join('c.providers', 'p')
->andWhere($qb->expr()->eq('p', $provider->getId()))
->getQuery()
->getResult();
}
public function getCountOfAssociatifContract(string $slug)
{
$qb = $this->createQueryBuilder('c');
return $qb
->select('count(c)')
->where('c.slug LIKE :slug')
->setParameter(':slug', '%' . $slug . '%')
->getQuery()
->getSingleScalarResult();
}
/**
* @throws NonUniqueResultException
* @throws NoResultException
*/
public function countCollectByEnterprise(?Enterprise $enterprise)
{
$qb = $this->createQueryBuilder('c')
->select('count(collects)');
if ($enterprise instanceof Enterprise) {
$qb->andWhere('c.enterprise = :enterprise')
->setParameter('enterprise', $enterprise);
}
$qb->leftJoin('c.collects', 'collects');
return $qb
->getQuery()
->getSingleScalarResult();
}
/**
* @throws NonUniqueResultException
* @throws NoResultException
*/
public function countCollectByEnterpriseTypeAndTime(
?Enterprise $enterprise,
bool $total = false,
bool $valorization,
string $start,
string $end
): int {
$qb = $this->createQueryBuilder('c')
->select('count(collects)')
->leftJoin('c.valorization', 'v');
$nonValoriser = ["Enfouissement", "Incinération (sans valorisation)"];
if(!$total) {
if ($valorization) {
$qb->andWhere('v.name not in (:nomValoriser)');
} else {
$qb->andWhere('v.name in (:nomValoriser)');
}
$qb->setParameter('nomValoriser', $nonValoriser);
}
if ($enterprise instanceof Enterprise) {
$qb->andWhere('c.enterprise = :enterprise')
->setParameter('enterprise', $enterprise);
}
$qb->leftJoin('c.collects', 'collects')
->andWhere('collects.created_at BETWEEN :start AND :end ')
->setParameter('start', $start)
->setParameter('end', $end);
return $qb
->getQuery()
->getSingleScalarResult();
}
}