src/Repository/Product/MarginProductRepository.php line 28

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