<?php
namespace App\Repository\Accounting;
use App\Entity\Accounting\AttributedProductVariant;
use App\Entity\Booking\Booking;
use App\Entity\Booking\BookingParticipant;
use App\Entity\Booking\Cabin;
use App\Entity\Booking\Quote;
use App\Entity\Booking\QuoteParticipant;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Exception;
/**
* @method AttributedProductVariant|null find($id, $lockMode = null, $lockVersion = null)
* @method AttributedProductVariant|null findOneBy(array $criteria, array $orderBy = null)
* @method AttributedProductVariant[] findAll()
* @method AttributedProductVariant[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class AttributedProductVariantRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, AttributedProductVariant::class);
}
/**
* @throws Exception
*/
public function findByAttributed(object $attributed, array $specificities = [], array $productCategoryCodes = []): array
{
$qb = $this->createQueryBuilder('apv')
->innerJoin('apv.productVariant', 'pv')
->innerJoin('pv.product', 'p')
->innerJoin('p.productCategory', 'pc')
->innerJoin('pc.parent', 'pcp');
if ($attributed instanceof Booking) {
$qb->innerJoin('apv.invoiceLine', 'l')
->andWhere($qb->expr()->eq('apv.booking', ':attributed'));
}
elseif ($attributed instanceof BookingParticipant) {
$qb->innerJoin('apv.invoiceLine', 'l')
->andWhere($qb->expr()->eq('apv.bookingParticipant', ':attributed'));
}
elseif ($attributed instanceof Quote) {
$qb->innerJoin('apv.quoteLine', 'l')
->andWhere($qb->expr()->eq('apv.quote', ':attributed'));
}
elseif ($attributed instanceof QuoteParticipant) {
$qb->innerJoin('apv.quoteLine', 'l')
->andWhere($qb->expr()->eq('apv.quoteParticipant', ':attributed'));
}
elseif ($attributed instanceof Cabin) {
$qb->innerJoin('apv.'.($attributed->getBooking() !== null ? 'invoiceLine' : 'quoteLine'), 'l')
->andWhere($qb->expr()->eq('apv.cabin', ':attributed'));
}
else {
throw new Exception('Invalid argument "attributed" in "'.__METHOD__.'" method from '.get_class($this).'.');
}
$parameters = ['attributed' => $attributed];
if (count($specificities) > 0) {
$qb->andWhere($qb->expr()->in('l.specificity', ':specificities'));
$parameters['specificities'] = $specificities;
}
if (count($productCategoryCodes) > 0) {
$qb->andWhere($qb->expr()->in('pcp.code', ':productCategoryCodes'));
$parameters['productCategoryCodes'] = $productCategoryCodes;
}
return $qb->setParameters($parameters)
->getQuery()
->getResult();
}
}