Fix: more consistency in Resource and Charge repositories

This commit is contained in:
2023-03-28 16:59:08 +02:00
parent a64d75ffc9
commit 59147b2217
3 changed files with 83 additions and 84 deletions

View File

@@ -11,9 +11,12 @@ declare(strict_types=1);
namespace Chill\BudgetBundle\Repository;
use Chill\BudgetBundle\Entity\Charge;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person;
use DateTime;
use Doctrine\ORM\EntityRepository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* ChargeRepository.
@@ -21,24 +24,44 @@ use Doctrine\ORM\EntityRepository;
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class ChargeRepository extends EntityRepository
class ChargeRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Charge::class);
}
/**
* @return Charge[]
*/
public function findAllByEntity(Person|Household $entity): array
{
$qb = $this->createQueryBuilder('c');
$property = $entity instanceof Person ? 'person' : 'household';
$qb->where("c.{$property} = :entity")
->setParameter('entity', $entity);
return $qb->getQuery()->getResult();
}
public function findByEntityAndDate($entity, DateTime $date, $sort = null)
{
$qb = $this->createQueryBuilder('c');
$entityStr = $entity instanceof Person ? 'person' : 'household';
$qb->where("c.{$entityStr} = :{$entityStr}")
->andWhere('c.startDate < :date')
->andWhere('c.startDate < :date OR c.startDate IS NULL');
$qb->where("c.{$entityStr} = :entity")
->andWhere('c.startDate <= :date')
->andWhere('c.endDate > :date OR c.endDate IS NULL');
if (null !== $sort) {
$qb->orderBy($sort);
}
$qb->setParameters([
$entityStr => $entity,
'entity' => $entity,
'date' => $date,
]);