<?php
namespace App\Repository;
use App\Entity\CarriersSubscriptions;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<CarriersSubscriptions>
*
* @method CarriersSubscriptions|null find($id, $lockMode = null, $lockVersion = null)
* @method CarriersSubscriptions|null findOneBy(array $criteria, array $orderBy = null)
* @method CarriersSubscriptions[] findAll()
* @method CarriersSubscriptions[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class CarriersSubscriptionsRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, CarriersSubscriptions::class);
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function add(CarriersSubscriptions $entity, bool $flush = false): void
{
$this->_em->persist($entity);
if ($flush) {
$this->_em->flush();
}
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function remove(CarriersSubscriptions $entity, bool $flush = false): void
{
$this->_em->remove($entity);
if ($flush) {
$this->_em->flush();
}
}
// /**
// * @return CarriersSubscriptions[] Returns an array of CarriersSubscriptions objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('c')
// ->andWhere('c.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('c.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?CarriersSubscriptions
// {
// return $this->createQueryBuilder('c')
// ->andWhere('c.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
public function findCarrierSubscriptionsByCarrier($carrier_id){
return $this->createQueryBuilder('c')
->select('c.id, c.unsubscribed_at, c.nbDesiredVehicles, c.subscribed_at, c.start_at, c.trial_end_date, p.trial_duration_days')
->leftJoin('c.carriers', 'ca')
->leftJoin('c.package', 'p')
->where('ca.id = :carrier_id')
->setParameter('carrier_id', $carrier_id)
->getQuery()
->getResult();
}
public function findCarrierSubscriptionsEndsBefore2Months(){
return $this->createQueryBuilder('c')
->select('ca.id AS carrier_id, c.id, c.unsubscribed_at,c.end_at')
->leftJoin('c.carriers', 'ca')
->getQuery()
->getResult();
}
public function getExpirationAccountDate($carrier_id){
return $this->createQueryBuilder('cs')
->select('cs.subscribed_at, p.nbr_month_of_validity')
->leftJoin('cs.package', 'p')
->leftJoin('cs.carriers', 'ca')
->where('ca.id = :carrier_id')
->setParameter('carrier_id', $carrier_id)
->getQuery()
->getResult();
}
public function getLastSubscription($carrier_id,$status){
return $this->createQueryBuilder('cs')
->select('cs.id, cs.subscribed_at,cs.start_at,cs.end_at,cs.nbDesiredVehicles,pa.trial_duration_days')
->leftJoin('cs.carriers', 'ca')
->leftJoin('cs.payment', 'p')
->leftJoin('cs.package', 'pa')
->where('ca.id = :carrier_id')
->setParameter('carrier_id', $carrier_id)
->andWhere('p.status = :status')
->setParameter('status', $status)
->orderBy('cs.id', 'DESC')
->setMaxResults(1)
->getQuery()
->getResult();
}
}