*/ final class BovineRepository extends ServiceEntityRepository { public const AGE_RANGE_OVER_24 = 'over24'; public const AGE_RANGE_BETWEEN_22_AND_24 = 'between22And24'; public const AGE_RANGE_BETWEEN_20_AND_22 = 'between20And22'; public function __construct(ManagerRegistry $registry) { parent::__construct($registry, Bovine::class); } /** * Liste des bovins actifs pour l'export inventaire. * * @param null|list $ageRanges Si null/vide → tous. Sinon filtre OR sur les tranches d'âge demandées. * * @return list */ public function findActiveForInventoryExport(?array $ageRanges = null): array { $qb = $this->createQueryBuilder('b') ->where('b.exitedAt IS NULL') ->orderBy('b.birthDate', 'ASC') ; if (null !== $ageRanges && [] !== $ageRanges) { $orX = $qb->expr()->orX(); foreach ($ageRanges as $idx => $range) { switch ($range) { case self::AGE_RANGE_OVER_24: $orX->add('b.ageMonths >= 24'); break; case self::AGE_RANGE_BETWEEN_22_AND_24: $orX->add($qb->expr()->andX('b.ageMonths >= 22', 'b.ageMonths < 24')); break; case self::AGE_RANGE_BETWEEN_20_AND_22: $orX->add($qb->expr()->andX('b.ageMonths >= 20', 'b.ageMonths < 22')); break; } } if ($orX->count() > 0) { $qb->andWhere($orX); } } return $qb->getQuery()->getResult(); } /** * Compteurs des bovins actifs par tranche d'âge. * * @return array{total: int, over24: int, between22And24: int, between20And22: int} */ public function getInventoryStats(?int $buildingCaseId = null): array { $qb = $this->createQueryBuilder('b') ->select( 'COUNT(b.id) AS total', 'SUM(CASE WHEN b.ageMonths >= 24 THEN 1 ELSE 0 END) AS over24', 'SUM(CASE WHEN b.ageMonths >= 22 AND b.ageMonths < 24 THEN 1 ELSE 0 END) AS between22And24', 'SUM(CASE WHEN b.ageMonths >= 20 AND b.ageMonths < 22 THEN 1 ELSE 0 END) AS between20And22', ) ->where('b.exitedAt IS NULL') ; if (null !== $buildingCaseId) { $qb->andWhere('b.buildingCase = :caseId') ->setParameter('caseId', $buildingCaseId) ; } $row = $qb->getQuery()->getSingleResult(); return [ 'total' => (int) ($row['total'] ?? 0), 'over24' => (int) ($row['over24'] ?? 0), 'between22And24' => (int) ($row['between22And24'] ?? 0), 'between20And22' => (int) ($row['between20And22'] ?? 0), ]; } }