<?php
namespace App\Repository;
use App\Entity\CommandeCaisse;
use App\Entity\TableRestaurant;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method CommandeCaisse|null find($id, $lockMode = null, $lockVersion = null)
*/
class CommandeCaisseRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, CommandeCaisse::class);
}
/**
* Commandes mises en attente (hors tables), plus anciennes en premier.
*
* @return CommandeCaisse[]
*/
public function findEnAttente(User $owner): array
{
return $this->createQueryBuilder('c')
->andWhere('c.user = :owner')
->andWhere('c.statut = :statut')
->andWhere('c.table IS NULL')
->setParameter('owner', $owner)
->setParameter('statut', CommandeCaisse::STATUT_EN_ATTENTE)
->orderBy('c.createdAt', 'ASC')
->getQuery()
->getResult();
}
public function countEnAttente(User $owner): int
{
return (int) $this->createQueryBuilder('c')
->select('COUNT(c.id)')
->andWhere('c.user = :owner')
->andWhere('c.statut = :statut')
->andWhere('c.table IS NULL')
->setParameter('owner', $owner)
->setParameter('statut', CommandeCaisse::STATUT_EN_ATTENTE)
->getQuery()
->getSingleScalarResult();
}
/**
* Commande en cours (non encaissée) d'une table, s'il y en a une.
*/
public function findOuverteParTable(TableRestaurant $table): ?CommandeCaisse
{
return $this->createQueryBuilder('c')
->andWhere('c.table = :table')
->andWhere('c.statut IN (:statuts)')
->setParameter('table', $table)
->setParameter('statuts', [CommandeCaisse::STATUT_OUVERTE, CommandeCaisse::STATUT_EN_ATTENTE])
->orderBy('c.id', 'ASC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
}
/**
* Commandes en cours sur des tables, indexées par id de table.
*
* @return array<int, CommandeCaisse>
*/
public function findOuvertesParTable(User $owner): array
{
$commandes = $this->createQueryBuilder('c')
->addSelect('l')
->leftJoin('c.lignes', 'l')
->andWhere('c.user = :owner')
->andWhere('c.table IS NOT NULL')
->andWhere('c.statut IN (:statuts)')
->setParameter('owner', $owner)
->setParameter('statuts', [CommandeCaisse::STATUT_OUVERTE, CommandeCaisse::STATUT_EN_ATTENTE])
->getQuery()
->getResult();
$parTable = [];
foreach ($commandes as $commande) {
$parTable[$commande->getTable()->getId()] = $commande;
}
return $parTable;
}
}