*/ final class BovineRepository extends ServiceEntityRepository { public function __construct(ManagerRegistry $registry) { parent::__construct($registry, Bovine::class); } /** * 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), ]; } }