src/Repository/Product/MarginCabinRepository.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Repository\Product;
  3. use App\Entity\Booking\Booking;
  4. use App\Entity\Booking\Cabin;
  5. use App\Entity\Booking\ProspectRequest;
  6. use App\Entity\Booking\Quote;
  7. use App\Entity\Product\MarginCabin;
  8. use App\Entity\Product\Product;
  9. use App\Entity\Product\ProductVariant;
  10. use App\Repository\ApplicationFilterRepository;
  11. use DateTime;
  12. use DateTimeInterface;
  13. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  14. use Doctrine\ORM\NonUniqueResultException;
  15. use Doctrine\ORM\QueryBuilder;
  16. use Doctrine\Persistence\ManagerRegistry;
  17. use Exception;
  18. /**
  19.  * @method MarginCabin|null find($id, $lockMode = null, $lockVersion = null)
  20.  * @method MarginCabin|null findOneBy(array $criteria, array $orderBy = null)
  21.  * @method MarginCabin[]    findAll()
  22.  * @method MarginCabin[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  23.  */
  24. class MarginCabinRepository extends ServiceEntityRepository
  25. {
  26.     public function __construct(ManagerRegistry $registry)
  27.     {
  28.         parent::__construct($registryMarginCabin::class);
  29.     }
  30.     /**
  31.      * @throws NonUniqueResultException
  32.      * @throws Exception
  33.      */
  34.     public function findByProductVariantAtDate(Object $objectProductVariant $productVariantDateTimeInterface $dateCabin $cabin null): ?MarginCabin
  35.     {
  36.         $qb $this->createQueryBuilder('mc')
  37.             ->join('mc.productVariant''pv')
  38.             ->andWhere('pv.id = :productVariantId')
  39.             ->setParameter('productVariantId'$productVariant->getId());
  40.         return $this->getFinalResult($qb$object$date$cabin);
  41.     }
  42.     /**
  43.      * @throws NonUniqueResultException
  44.      * @throws Exception
  45.      */
  46.     public function findByProductAtDate(Object $objectProduct $productDateTimeInterface $dateCabin $cabin null): ?MarginCabin
  47.     {
  48.         $qb $this->createQueryBuilder('mc')
  49.             ->join('mc.product''p')
  50.             ->andWhere('p.id = :productId')
  51.             ->setParameter('productId'$product->getId());
  52.         return $this->getFinalResult($qb$object$date$cabin);
  53.     }
  54.     /**
  55.      * @throws NonUniqueResultException
  56.      */
  57.     public function findDefaultByProduct(Object $objectProduct $productCabin $cabin null): ?MarginCabin
  58.     {
  59.         $qb $this->createQueryBuilder('mc')
  60.             ->join('mc.product''p')
  61.             ->andWhere('p.id = :productId')
  62.             ->andWhere('mc.isDefault = 1')
  63.             ->andWhere('mc.isArchived = 0')
  64.             ->setParameter('productId'$product->getId());
  65.         return $this->getFinalResult($qb$objectnull$cabin);
  66.     }
  67.     /**
  68.      * @throws NonUniqueResultException
  69.      * @throws Exception
  70.      */
  71.     private function getFinalResult(QueryBuilder $qbObject $objectDateTimeInterface $date nullCabin $cabin null): ?MarginCabin
  72.     {
  73.         if ((!$object instanceof Quote && !$object instanceof Booking && !$object instanceof ProspectRequest) || ($object instanceof ProspectRequest && $object->getCruiseHistory() === null)) {
  74.             throw new Exception('Invalid argument in "'.__METHOD__.'" method from '.get_class($this).' : object have to be a Quote or a Booking.');
  75.         }
  76.         $qb $qb->innerJoin('mc.applicationFilter''af');
  77.         $qb ApplicationFilterRepository::addApplicationFilterQueryBuilderForCruiseHistory($qb$object->getCruiseHistory(), 'af'$date);
  78.         if ($cabin !== null) {
  79.             $qb ApplicationFilterRepository::addApplicationFilterQueryBuilderForCabin($qb$cabin);
  80.         }
  81.         if (!$object instanceof ProspectRequest) {
  82.             $qb ApplicationFilterRepository::addApplicationFilterQueryBuilderForObject($qb$object);
  83.         }
  84.         return $qb->orderBy('mc.id''DESC')
  85.             ->setMaxResults(1)
  86.             ->getQuery()
  87.             ->getOneOrNullResult();
  88.     }
  89. }