src/Repository/Accounting/AttributedProductVariantRepository.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Repository\Accounting;
  3. use App\Entity\Accounting\AttributedProductVariant;
  4. use App\Entity\Booking\Booking;
  5. use App\Entity\Booking\BookingParticipant;
  6. use App\Entity\Booking\Cabin;
  7. use App\Entity\Booking\Quote;
  8. use App\Entity\Booking\QuoteParticipant;
  9. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Exception;
  12. /**
  13.  * @method AttributedProductVariant|null find($id, $lockMode = null, $lockVersion = null)
  14.  * @method AttributedProductVariant|null findOneBy(array $criteria, array $orderBy = null)
  15.  * @method AttributedProductVariant[]    findAll()
  16.  * @method AttributedProductVariant[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  17.  */
  18. class AttributedProductVariantRepository extends ServiceEntityRepository
  19. {
  20.     public function __construct(ManagerRegistry $registry)
  21.     {
  22.         parent::__construct($registryAttributedProductVariant::class);
  23.     }
  24.     /**
  25.      * @throws Exception
  26.      */
  27.     public function findByAttributed(object $attributed, array $specificities = [], array $productCategoryCodes = []): array
  28.     {
  29.         $qb $this->createQueryBuilder('apv')
  30.             ->innerJoin('apv.productVariant''pv')
  31.             ->innerJoin('pv.product''p')
  32.             ->innerJoin('p.productCategory''pc')
  33.             ->innerJoin('pc.parent''pcp');
  34.         if ($attributed instanceof Booking) {
  35.             $qb->innerJoin('apv.invoiceLine''l')
  36.                 ->andWhere($qb->expr()->eq('apv.booking'':attributed'));
  37.         }
  38.         elseif ($attributed instanceof BookingParticipant) {
  39.             $qb->innerJoin('apv.invoiceLine''l')
  40.                 ->andWhere($qb->expr()->eq('apv.bookingParticipant'':attributed'));
  41.         }
  42.         elseif ($attributed instanceof Quote) {
  43.             $qb->innerJoin('apv.quoteLine''l')
  44.                 ->andWhere($qb->expr()->eq('apv.quote'':attributed'));
  45.         }
  46.         elseif ($attributed instanceof QuoteParticipant) {
  47.             $qb->innerJoin('apv.quoteLine''l')
  48.                 ->andWhere($qb->expr()->eq('apv.quoteParticipant'':attributed'));
  49.         }
  50.         elseif ($attributed instanceof Cabin) {
  51.             $qb->innerJoin('apv.'.($attributed->getBooking() !== null 'invoiceLine' 'quoteLine'), 'l')
  52.                 ->andWhere($qb->expr()->eq('apv.cabin'':attributed'));
  53.         }
  54.         else {
  55.             throw new Exception('Invalid argument "attributed" in "'.__METHOD__.'" method from '.get_class($this).'.');
  56.         }
  57.         $parameters = ['attributed' => $attributed];
  58.         if (count($specificities) > 0) {
  59.             $qb->andWhere($qb->expr()->in('l.specificity'':specificities'));
  60.             $parameters['specificities'] = $specificities;
  61.         }
  62.         if (count($productCategoryCodes) > 0) {
  63.             $qb->andWhere($qb->expr()->in('pcp.code'':productCategoryCodes'));
  64.             $parameters['productCategoryCodes'] = $productCategoryCodes;
  65.         }
  66.         return $qb->setParameters($parameters)
  67.             ->getQuery()
  68.             ->getResult();
  69.     }
  70. }