src/Repository/CommandeCaisseRepository.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\CommandeCaisse;
  4. use App\Entity\TableRestaurant;
  5. use App\Entity\User;
  6. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. /**
  9.  * @method CommandeCaisse|null find($id, $lockMode = null, $lockVersion = null)
  10.  */
  11. class CommandeCaisseRepository extends ServiceEntityRepository
  12. {
  13.     public function __construct(ManagerRegistry $registry)
  14.     {
  15.         parent::__construct($registry, CommandeCaisse::class);
  16.     }
  17.     /**
  18.      * Commandes mises en attente (hors tables), plus anciennes en premier.
  19.      *
  20.      * @return CommandeCaisse[]
  21.      */
  22.     public function findEnAttente(User $owner): array
  23.     {
  24.         return $this->createQueryBuilder('c')
  25.             ->andWhere('c.user = :owner')
  26.             ->andWhere('c.statut = :statut')
  27.             ->andWhere('c.table IS NULL')
  28.             ->setParameter('owner', $owner)
  29.             ->setParameter('statut', CommandeCaisse::STATUT_EN_ATTENTE)
  30.             ->orderBy('c.createdAt', 'ASC')
  31.             ->getQuery()
  32.             ->getResult();
  33.     }
  34.     public function countEnAttente(User $owner): int
  35.     {
  36.         return (int) $this->createQueryBuilder('c')
  37.             ->select('COUNT(c.id)')
  38.             ->andWhere('c.user = :owner')
  39.             ->andWhere('c.statut = :statut')
  40.             ->andWhere('c.table IS NULL')
  41.             ->setParameter('owner', $owner)
  42.             ->setParameter('statut', CommandeCaisse::STATUT_EN_ATTENTE)
  43.             ->getQuery()
  44.             ->getSingleScalarResult();
  45.     }
  46.     /**
  47.      * Commande en cours (non encaissĂ©e) d'une table, s'il y en a une.
  48.      */
  49.     public function findOuverteParTable(TableRestaurant $table): ?CommandeCaisse
  50.     {
  51.         return $this->createQueryBuilder('c')
  52.             ->andWhere('c.table = :table')
  53.             ->andWhere('c.statut IN (:statuts)')
  54.             ->setParameter('table', $table)
  55.             ->setParameter('statuts', [CommandeCaisse::STATUT_OUVERTE, CommandeCaisse::STATUT_EN_ATTENTE])
  56.             ->orderBy('c.id', 'ASC')
  57.             ->setMaxResults(1)
  58.             ->getQuery()
  59.             ->getOneOrNullResult();
  60.     }
  61.     /**
  62.      * Commandes en cours sur des tables, indexĂ©es par id de table.
  63.      *
  64.      * @return array<int, CommandeCaisse>
  65.      */
  66.     public function findOuvertesParTable(User $owner): array
  67.     {
  68.         $commandes = $this->createQueryBuilder('c')
  69.             ->addSelect('l')
  70.             ->leftJoin('c.lignes', 'l')
  71.             ->andWhere('c.user = :owner')
  72.             ->andWhere('c.table IS NOT NULL')
  73.             ->andWhere('c.statut IN (:statuts)')
  74.             ->setParameter('owner', $owner)
  75.             ->setParameter('statuts', [CommandeCaisse::STATUT_OUVERTE, CommandeCaisse::STATUT_EN_ATTENTE])
  76.             ->getQuery()
  77.             ->getResult();
  78.         $parTable = [];
  79.         foreach ($commandes as $commande) {
  80.             $parTable[$commande->getTable()->getId()] = $commande;
  81.         }
  82.         return $parTable;
  83.     }
  84. }