<?php
namespace App\Repository\Management;
use App\Entity\Management\Subscription\Formule;
use App\Entity\Management\Subscription\Offer;
use App\Entity\Management\Subscription\Subscription;
use App\Entity\Users\Child;
use App\Entity\Users\Guardiant;
use App\Entity\Users\StudentsParents;
use Bo\StudentBundle\Entity\OfferStudent;
use Bo\StudentBundle\Entity\Student;
use App\Entity\Users\Parents;
use Bo\UserBundle\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\Persistence\ManagerRegistry;
/**
* StudentRepository
*/
class SubscriptionRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Subscription::class);
}
public function getLastSubscriptionsByParents(Parents $user)
{
$q = $this->_em
->createQueryBuilder()
->select('sp')
->from(StudentsParents::class, 'sp')
->join('sp.student','child')
->where('sp.parents=:pid')
->orderBy('child.firstname','asc')
->setParameter('pid', $user->getId());
$list = $q->getQuery()->getResult();
$sub = [];
foreach ($list as $sp){
$sub [] = $this->createQueryBuilder('s')
->where('s.student =:sid and s.guardiant =:gid')
->setParameters(['sid'=>$sp->getStudent()->getId(),'gid'=>$user->getId()])
->orderBy('s.id','desc')
->setMaxResults(1)
->getQuery()->getOneOrNullResult();
}
return $sub;
}
public function getStudents()
{
$date = new \DateTime("now");
$q = $this->_em->createQueryBuilder()
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->andWhere("os.unSubscripted >= '" . $date->format('Y-m-d') . "' or os.unSubscripted is null");
return $q->getQuery()->getResult();
}
public function getAllCurrentSubscription(Parents $parents){
$now = new \DateTime();
return $this->createQueryBuilder('os')
->select('count(os)')
->andWhere('((os.subscripted <=:now and :now<= os.unSubscripted ) or os.unSubscripted is null ) and os.guardiant =:id')
->setParameters([
'now'=>$now->format('Y-m-d H:i:s'),
'id'=>$parents->getId()])
->getQuery()->getSingleScalarResult();
}
public function getListStudentToDoBilan()
{
$q = $this->_em->createQueryBuilder()
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->join('os.student', 's')
->Where('s.isBilan = 1');
return $q->getQuery()->getResult();
}
public function registrationExamAlert()
{
$rsm = new ResultSetMapping();
$rsm->addEntityResult('Bo\StudentBundle\Entity\ReviewProgram', 'u');
$rsm->addFieldResult('u', 'id', 'id');
$rsm->addJoinedEntityResult('Bo\StudentBundle\Entity\Student', 's', 'u', 'student', array('id' => 'student_id'));
$rsm->addFieldResult('s', 'student_id', 'id');
$query = $this->_em->createNativeQuery('select s.id,s.student_id from (select r.day,r.student_id,r.`started`,r.id from aba_user a left join `ReviewProgram` r on r.`student_id`=a.id where a.`guardiant_id` is not null and r.`started` is not null and r.`started_playlist` is null and DATEDIFF(now(),r.started)=2 order by r.`started` desc) s group by s.student_id having min(s.`started`)', $rsm);
return $query->getResult();
}
public function registrationExamAlertNoPLaylist()
{
$rsm = new ResultSetMapping();
$rsm->addEntityResult('Bo\StudentBundle\Entity\ReviewProgram', 'u');
$rsm->addFieldResult('u', 'id', 'id');
$rsm->addJoinedEntityResult('Bo\StudentBundle\Entity\Student', 's', 'u', 'student', array('id' => 'student_id'));
$rsm->addFieldResult('s', 'student_id', 'id');
$query = $this->_em->createNativeQuery('select s.id , s.student_id from (select r.day,r.student_id,r.`created`,r.id from aba_user a left join `ReviewProgram` r on r.`student_id`=a.id where a.`guardiant_id` is not null and DATEDIFF(now(),r.created)=5 order by r.`created` desc) s group by s.student_id having min(s.`created`)', $rsm);
return $query->getResult();
}
public function getPreviousSubscriptionByStudentId($studentId)
{
$stmt = $this
->_em
->getConnection()
->prepare("
SELECT *
FROM lbp_prod.lnk_Student_Offer AS so
WHERE so.student_id = :studentId
ORDER BY so.id DESC LIMIT 1,1;
");
$stmt->execute(['studentId' => $studentId]);
$previousOs = $stmt->fetchAll(\PDO::FETCH_COLUMN);
if (empty($previousOs)) {
return [];
}
$q = $this->createQueryBuilder('os');
return $q
->where('os.id = :previousOs')
->setParameter('previousOs', $previousOs)
->getQuery()
->getOneOrNullResult();
}
public function getLastSubscriptionByParentChild(Parents $parent, Child $student)
{
$q = $this->_em->createQueryBuilder('os')
->select('os')
->from(Subscription::class, 'os')
->where("os.student=:studentId and os.guardiant=:guardiantId and ((os.unSubscripted is not null and :now<=os.unSubscripted) or os.unSubscripted is null )")
->setParameters([
'guardiantId' => $parent->getId(),
'studentId' => $student->getId(),
'now'=>(new \DateTime('now'))->format('Y-m-d H:i:s')
])
->setMaxResults(1)
->orderBy('os.id', 'desc');
return $q->getQuery()->getOneOrNullResult();
}
public function getLastSubscriptionByChild(Child $student)
{
$q = $this->_em->createQueryBuilder('os')
->select('os')
->from(Subscription::class, 'os')
->where("os.student=:studentId and ((os.unSubscripted is not null and :now<=os.unSubscripted) or os.unSubscripted is null )")
->setParameters([
'now'=>(new \DateTime('now'))->format('Y-m-d H:i:s'),
'studentId' => $student->getId()
])
->setMaxResults(1)
->orderBy('os.id', 'desc');
return $q->getQuery()->getOneOrNullResult();
}
public function getLastSubscriptionByStudent(Child $user)
{
$q = $this->createQueryBuilder('os')
->where("os.student=:studentId")
->setParameters(['studentId' => $user->getId()])
->setMaxResults(1)
->orderBy('os.id', 'desc');
return $q->getQuery()->getOneOrNullResult();
}
/*
* Find students unsubcribed with role
*/
public function findSubscriptionsInactifByParents(Parents $parents)
{
$q = $this->createQueryBuilder('os')
->leftjoin('os.guardiant', 'g')
->where('( os.unSubscripted is null or (os.unSubscripted is not null and os.unSubscripted >= CURRENT_TIMESTAMP()))')
->andWhere('os.inactif = 1 and g.id=:id')
->setParameters(['id'=>$parents->getId()])
;
return $q->getQuery()->getResult();
}
public function listInactifNoUnsubcripted()
{
return $this->createQueryBuilder('os')
->where("os.inactif=1 and os.unSubscripted is null and os.stripeId is not null")
->getQuery()->getResult();
}
public function listSubOffer(Parents $guardiant)
{
return $this->createQueryBuilder('os')
->select('count(os.id) as nb')
->join('os.offer', 'o')
->join(Formule::class, 'f', 'WITH', 'f.id = o.id')
->where('os.guardiant=:guardiant')
->setParameters([ 'guardiant'=>$guardiant->getId()])
->getQuery()->getSingleScalarResult();
}
public function invoice()
{
$q = $this->_em
->createQueryBuilder()
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->join('os.student', 'st')
->where('os.unSubscripted is null or CURRENT_TIMESTAMP() < os.unSubscripted')
->andWhere('os.offer INSTANCEOF FORMULE');
return $q->getQuery()->getResult();
}
/**
* return OfferStudent Objects where today = 15 days prior to credit card month expiration
*
* @return array
* @throws \Doctrine\DBAL\DBALException
*/
public function getSoonExpiredCard()
{
$stmt = $this
->_em
->getConnection()
->prepare('
SELECT so.id, date_add(so.card_expiration_date,interval -DAY(so.card_expiration_date)+1 DAY) AS first_day
FROM lnk_Student_Offer AS so
WHERE so.card_expiration_date IS NOT NULL
GROUP BY so.guardiant_id
HAVING CURRENT_DATE() = DATE_SUB(first_day, INTERVAL 15 DAY);
');
$stmt->execute();
$studentOffers = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$ids = [];
foreach ($studentOffers as $row) {
$ids[] = $row['id'];
}
if (empty($ids)) {
return [];
}
$q = $this->createQueryBuilder('os');
return $q
->where($q->expr()->in('os.id', $ids))
->getQuery()
->getResult();
}
/**
* return OfferStudent Objects where today = 16th day of credit card current month expiration
*
* @return array
* @throws \Doctrine\DBAL\DBALException
*/
public function getExpiredCardFirstReminder()
{
$stmt = $this
->_em
->getConnection()
->prepare("
SELECT so.id, CONCAT(YEAR(so.card_expiration_date), '-', MONTH(so.card_expiration_date), '-16') AS given_date
FROM lnk_Student_Offer AS so
WHERE so.card_expiration_date IS NOT NULL
GROUP BY so.guardiant_id
HAVING CURRENT_DATE() = given_date;
");
$stmt->execute();
$studentOffers = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$ids = [];
foreach ($studentOffers as $row) {
$ids[] = $row['id'];
}
if (empty($ids)) {
return [];
}
$q = $this->createQueryBuilder('os');
return $q
->where($q->expr()->in('os.id', $ids))
->getQuery()
->getResult();
}
/**
* return OfferStudent Objects where today = 25th day of credit card current month expiration
*
* @return array
* @throws \Doctrine\DBAL\DBALException
*/
public function getExpiredCardLastReminder()
{
$stmt = $this
->_em
->getConnection()
->prepare("
SELECT so.id, CONCAT(YEAR(so.card_expiration_date), '-', MONTH(so.card_expiration_date), '-25') AS given_date
FROM lnk_Student_Offer AS so
WHERE so.card_expiration_date IS NOT NULL
GROUP BY so.guardiant_id
HAVING CURRENT_DATE() = given_date;
");
$stmt->execute();
$studentOffers = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$ids = [];
foreach ($studentOffers as $row) {
$ids[] = $row['id'];
}
if (empty($ids)) {
return [];
}
$q = $this->createQueryBuilder('os');
return $q
->where($q->expr()->in('os.id', $ids))
->getQuery()
->getResult();
}
/**
* return OfferStudent Objects where today = credit card expiration date
*
* @return array
* @throws \Doctrine\DBAL\DBALException
*/
public function getExpiredCardAlert()
{
return $this
->createQueryBuilder('os')
->where('os.cardExpirationDate = CURRENT_DATE()')
->groupBy('os.guardiant')
->getQuery()
->getResult();
}
public function getBankCollectionByUser($user, Student $student = null)
{
$q = $this
->createQueryBuilder('os');
if ($user instanceof Parents) {
$q = $q->where('os.guardiant =:user')->setParameter('user', $user->getId());
} elseif ($user instanceof Student) {
$q = $q->where('os.student =:user')->setParameter('user', $user->getId());
}
if (!is_null($student)) {
$q = $q->andwhere('os.student =:student')->setParameter('student', $student->getId());
}
$q = $q->andWhere('os.isBankCollectionProcess = 1')->orderBy('os.id', 'desc');
return $q->getQuery()->getResult();
}
public function isSuspendedSubscription($user)
{
$q = $this->_em
->createQueryBuilder()
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->where('os.unSubscripted is null')
->andWhere('os.isSuspended = 1')
->andWhere('os.guardiant =:user')
->setParameter('user', $user->getId());
return $q->getQuery()->getResult();
}
public function getSuspendedSubscription()
{
$q = $this->_em
->createQueryBuilder()
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->where('os.unSubscripted is null')
->andWhere('os.isSuspended = 1')
->andWhere('os.dateSuspended is not null');
return $q->getQuery()->getResult();
}
public function getCurrentSubscriptionByChild(Child $child){
$now = new \DateTime();
$parameters = ['now' => $now->format('Y-m-d H:i:s'),'student'=>$child->getId()];
$q = $this->createQueryBuilder('s')
->select('s')
->join('s.offer', 'offers')
->where('
(os.unSubscripted is null AND os.subscripted <= :now ) OR
(os.unSubscripted is not null AND os.subscripted <= :now AND :now <= os.unSubscripted) OR
(:now < os.trialDate)')
->andWhere('offers INSTANCE OF BoTrainningBundle:Formule')
->andWhere('s.student =:student')
->setParameters($parameters);
return $q->getQuery()->getResult();
}
public function findOfferFreeByChild(?Child $child){
if(is_null($child))
return null;
$parameters = ['student'=>$child->getId(),'offer'=>Offer::OFFER_FREE];
$q = $this->createQueryBuilder('s')
->select('s')
->join('s.offer', 'offers')
->Where('s.student =:student and offers.slug=:offer')
->orderBy('s.id','desc')
->setMaxResults(1)
->setParameters($parameters);
return $q->getQuery()->getOneOrNullResult();
}
public function getCurrentSubscriptionByUser($user, $student = null, $guardiantIsNull = true, $returnAll = false)
{
$now = new \DateTime('now');
$q = $this->_em
->createQueryBuilder('os')
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->join('os.offer', 'offers')
->where('
(os.unSubscripted is null AND os.subscripted <= :now ) OR
(os.unSubscripted is not null AND os.subscripted <= :now AND :now <= os.unSubscripted) OR
(:now < os.trialDate)')
->andWhere('offers INSTANCE OF BoTrainningBundle:Formule');
$parameters = ['now' => $now->format('Y-m-d H:i:s')];
if ($user instanceof Parents) {
$q->andWhere('os.guardiant =:guardiant');
$parameters['guardiant'] = $user->getId();
if($student !== null) {
$q->andWhere('os.student=:student');
$parameters['student'] = $student->getId();
if($returnAll === true) {
$q->groupBy('os.student');
}
} else {
// NOTE : to avoid exception of getOneOrNullResult() for multiple results
$q->setParameters($parameters);
if($returnAll === true) {
$q->groupBy('os.student');
}
$studentOffers = $q->getQuery()->getResult();
if(empty($studentOffers) === true) {
return null;
} else {
if($returnAll === true) {
return $studentOffers;
}
return current($studentOffers);
}
}
} else {
if($guardiantIsNull === true) {
$q->andWhere('os.guardiant is null');
}
$q->andWhere('os.student=:student');
$parameters['student'] = $user->getId();
if($returnAll === true) {
$q->groupBy('os.student');
}
}
$q->setParameters($parameters);
$result = $q->getQuery()->getResult();
// NOTE : it seems that getOneOrNullResult() generate an error
if(empty($result)) {
return null;
} else {
if($returnAll === true) {
return $result;
}
return current($result);
}
}
public function getCurrentSubscriptionsByParent(Parents $parents)
{
$now = new \DateTime('now');
$q = $this->_em
->createQueryBuilder('os')
->select('os')
->from(Subscription::class, 'os')
->join('os.student','s')
->where('os.unSubscripted is null or :now <= os.unSubscripted')
->andWhere('os.guardiant =:guardiant')
->orderBy('s.firstname','asc')
->setParameters([
'guardiant' => $parents->getId(),
'now' => $now->format('Y-m-d H:i:s')
]);
return $q->getQuery()->getResult();
}
public function getChangeOfferSubscriptionsByParentId(Parents $parents)
{
$now = new \DateTime('now');
$q = $this->_em
->createQueryBuilder('os')
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->join('os.offer', 'offers')
->where('offers INSTANCE OF BoTrainningBundle:Formule')
->andWhere('os.guardiant =:guardiant')
->andWhere('offers.stripeId != :newlbp')
->setParameters([
'guardiant' => $parents->getId(),
'newlbp'=> 'new-lbp',
//'now' => $now->format('Y-m-d H:i:s')
]);
return $q->getQuery()->getResult();
}
public function getCurrentSubscriptionsByUserId(Child $student)
{
$now = new \DateTime('now');
$q = $this->_em
->createQueryBuilder('os')
->select('os')
->from(Subscription::class, 'os')
->join('os.offer', 'offers')
->where('os.unSubscripted is null or :now <= os.unSubscripted')
->andWhere('os.student =:guardiant')
->setParameters(['guardiant' => $student->getId(), 'now' => $now->format('Y-m-d H:i:s')])
->setMaxResults(1);
try {
return $q->getQuery()->getOneOrNullResult();
} catch (NonUniqueResultException $e) {
return null;
}
}
public function findAllCurrent()
{
$q = $this->_em
->createQueryBuilder('os')
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->where('os.unSubscripted is null')
->andWhere('os.stripeId is not null')
->andWhere('os.offer = 40');
return $q->getQuery()->getResult();
}
public function updateCardExpiration()
{
$query = $this->_em
->createQueryBuilder('os')
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->join('os.guardiant', 'guardiant')
->where('os.unSubscripted is null')
->andWhere('os.stripeId is not null')
->andWhere('os.offer = 40')
->andWhere('os.guardiant = guardiant.id')
->orderBy('os.id', 'desc')
;
$stripeIds = [];
$query = $query->getQuery()->getResult();
foreach ($query as $q ){
$stripeIds[] = [
'id' => $q->getGuardiant()->getId(),
'stripeId' => $q->getGuardiant()->getStripeId(),
'dateCardExpiration' => $q->getCardExpirationDate()
];
}
return $stripeIds;
}
public function sendWarningCardExpiration($dateCardExpiration)
{
$query = $this->_em
->createQueryBuilder('os')
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->join('os.guardiant', 'guardiant')
->where('os.unSubscripted is null')
->andWhere('os.cardExpirationDate = :dateCardExpiration')
->andWhere('os.stripeId is not null')
->andWhere('os.offer = 40')
->andWhere('os.guardiant = guardiant.id')
->setParameter('dateCardExpiration', $dateCardExpiration)
->orderBy('os.id', 'desc')
;
return $query->getQuery()->getResult();
}
public function SubscritptionAndInvoicesByGuardiant($guardiantId)
{
$q = $this->_em
->createQueryBuilder('os')
->select('os', 'inv')
->from('BoStudentBundle:OfferStudent', 'os')
->join('os.invoices', 'inv')
->where('os.guardiant =:guardiant')
->andWhere('inv INSTANCE OF BoSubscriptionBundle:Invoice')
->setParameter('guardiant', $guardiantId);
return $q->getQuery()->getResult();
}
public function allSubscription(User $user)
{
$q = $this->_em
->createQueryBuilder('max(os.id)')
->select('max(os.id)')
->from('BoStudentBundle:OfferStudent', 'os')
->join('os.offer', 'offers')
->where('os.guardiant =:guardiant')
->andWhere('offers INSTANCE OF BoTrainningBundle:Formule')
->groupBy('os.guardiant,os.student')
->setParameter('guardiant', $user->getId());
$q = $q->getQuery()->getArrayResult();
foreach ($q as $it) {
$ids[] = $it[1];
}
if (empty($ids)) {
return [];
}
$q2 = $this->_em
->createQueryBuilder('os');
return $this->_em
->createQueryBuilder('os')
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->where($q2->expr()->in('os.id', $ids))
->orderBy('os.unSubscripted', 'asc')
->getQuery()->getResult();
}
public function justOnceTrial(User $user, OfferStudent $offerStudent = null)
{
if (is_null($user) || is_null($offerStudent))
return null;
$q = $this->_em
->createQueryBuilder('os')
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->join('os.offer', 'offers')
->where('os.guardiant =:guardiant')
->andWhere('offers INSTANCE OF BoTrainningBundle:Formule')
->andWhere('os.id <> :current')
->andWhere('offers.id=:offer_id')
->setParameters(['guardiant' => $user->getId(), 'current' => $offerStudent->getId(), 'offer_id' => Offer::ID_OFFER_NEW_OFFER]);
$q = $q->getQuery()->getResult();
return count($q) == 0 ? null : true;
}
public function checkIfCurrentTrialSubscription($parentId)
{
if (is_null($parentId))
return null;
$q = $this->_em
->createQueryBuilder('os')
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->join('os.offer', 'offers')
->where('os.guardiant =:guardiant')
->andWhere('offers INSTANCE OF BoTrainningBundle:Formule')
->andWhere('os.unSubscripted is null')
->andWhere('os.trialDate is not null')
->andWhere('offers.id=:offer_id')
->andwhere(' CURRENT_DATE() < os.subscripted')
->orderBy('os.id', 'desc')
->setParameters([
'guardiant' => $parentId,
'offer_id' => Offer::ID_OFFER_NEW_OFFER
]);
$q = $q->getQuery()->getResult();
return $q ? $q[0] : false;
}
public function getSponsoredPaid(User $user)
{
$q = $this->_em
->createQueryBuilder('os')
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->where('os.sponsor =:guardiant')
->andWhere('os.unSubscripted is null')
->andWhere('os.trialDate is null or os.trialDate < CURRENT_DATE()')
->setParameters(['guardiant' => $user->getId()]);
return $q->getQuery()->getResult();
}
public function sendEmailAlertSubscription()
{
$stmt = $this
->_em
->getConnection()
->prepare('select DATEDIFF(subscripted, now())=-1 as "s+1",DATEDIFF(subscripted, now())=-3 as "s+3",DATEDIFF(l.created, now())=-2 as "pe+2",DATEDIFF(l.created, now())=-7 as "pe+7",l.id from lnk_Student_Offer l where offer_id = 40 and `un_subscripted`is null and ( DATEDIFF(subscripted, l.created)=7 or DATEDIFF(subscripted, l.created) = 8 ) and
(DATEDIFF(subscripted, now())=-1 or DATEDIFF(subscripted, now())=-3 or DATEDIFF(l.created, now())=-2 or DATEDIFF(l.created, now())=-7)');
$stmt->execute();
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
public function sendEmailAlertLastTrial()
{
$dateNow = new \DateTime();
$dateNow->modify('+ 1 day');
$q = $this->_em->createQueryBuilder()
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->where("os.trialDate BETWEEN :date1 AND :date2 ")
->andWhere("os.unSubscripted is null")
->setParameter('date1', $dateNow->format('Y-m-d 00:00:00'))
->setParameter('date2', $dateNow->format('Y-m-d 23:59:59'))
;
return $q->getQuery()->getResult();
}
public function buttonSubscription(User $user, $mode)
{
$q = $this->_em
->createQueryBuilder('os')
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->join('os.offer', 'offers')
->where('offers INSTANCE OF BoTrainningBundle:Formule');
if ($user INSTANCEOF Parents) {
$q = $q->andWhere('os.guardiant=:guardiant')->setParameter('guardiant', $user->getId());
} else {
$q = $q->andWhere('os.student=:student')->setParameter('student', $user->getId());
}
if ($mode) {
switch ($mode) {
case 1:
$q = $q->andWhere('offers.slug=:offer_slug')->setParameter('offer_slug', 'abonnement-assistance');
break;
case 2:
$q = $q->andWhere('os.unSubscripted is null or os.unSubscripted > CURRENT_TIMESTAMP()');
break;
}
}
$q = $q->orderBy('os.id', 'desc');
return $q->getQuery()->getResult();
}
public function hadOnePackExam(Parents $parents)
{
$q = $this->_em->createQueryBuilder('os')
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->join('os.offer', 'offer')
->where('offer.slug =:slug ')
->andwhere("os.guardiant=:guardiantId")
->andwhere('os.unSubscripted is null or CURRENT_DATE() <= os.unSubscripted')
->setParameters([
'guardiantId' => $parents->getId(),
'slug' => 'packexam-2018'
])
->orderBy('os.id', 'desc');
return $q->getQuery()->getResult();
}
/**
* Reactive offer student
*
* @param OfferStudent $offerStudent
* @return int
*/
public function reactivate(OfferStudent $offerStudent)
{
$queryBuilder = $this->_em->createQueryBuilder('os');
$queryBuilder->update('BoStudentBundle:OfferStudent','os')
->set('os.unSubscripted', ':unSubscripted')
->setParameter('unSubscripted', null)
->where('os.id = :offerStudentId')
->andWhere('os.unSubscripted IS NOT NULL')
->setParameter('offerStudentId', $offerStudent->getId());
return $queryBuilder
->getQuery()
->execute();
}
public function getSubscriberFormule($formule)
{
$q = $this->_em->createQueryBuilder()
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->join('os.offer', 'offer')
->where('offer.slug = :slug')
->setParameter('slug', $formule )
;
return $q->getQuery()->getResult();
}
/**
* Return offerStudent statement by course/serie
*
* @param $courseId
* @param $serieId
* @param \DateTime $startDate
* @param array $studentIds
* @return \Doctrine\DBAL\Driver\Statement
* @throws \Doctrine\DBAL\DBALException
*/
public function getStatementAllByCourseAndSerie($courseId, $serieId, \DateTime $startDate,\DateTime $endDate, $studentIds = [])
{
if(empty($studentIds) === false) {
$queryComplement = "AND os.student_id IN (".implode(", ", $studentIds).")";
} else {
$queryComplement = "";
}
$stmt = $this
->_em
->getConnection()
->prepare(
"
SELECT os.*, s.firstname AS student_firstname, g.firstname AS parent_firstname, g.email AS parent_email, s.serie AS student_serie
FROM ".$this->_em->getClassMetadata('BoStudentBundle:OfferStudent')->getTableName()." AS os
INNER JOIN ".$this->_em->getClassMetadata('BoStudentBundle:Student')->getTableName()." AS s ON s.id = os.student_id
INNER JOIN ".$this->_em->getClassMetadata('BoUserBundle:Parents')->getTableName()." AS g ON g.id = os.guardiant_id
WHERE os.stripe_id is not null
AND (os.un_subscripted is null or '".$endDate->format('Y-m-d')."' < os.un_subscripted)
AND os.offer_id = 40
AND os.created < '".$endDate->format('Y-m-d')."'
".$queryComplement."
;"
);
$stmt->execute();
return $stmt;
}
/**
* Reactive offer student
*
* @param OfferStudent $offerStudent
*/
public function getExistOfferStudent( $parentID, $studentID)
{
$q = $this->_em->createQueryBuilder('os')
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->join('os.guardiant', 'parent')
->join('os.student', 'student')
->where('os.unSubscripted is null')
->andWhere('os.guardiant = :parentID')
->andWhere('os.student = :studentID')
->setParameter('parentID', $parentID )
->setParameter('studentID', $studentID)
->orderBy('os.id', 'DESC')
;
try {
return $q->getQuery()->getSingleResult();
} catch (\Exception $e) {
return false;
}
}
/**
* Active Offer Student
*
* @param OfferStudent $offerStudent
*/
public function getAllSubscriptionActive()
{
$q = $this->_em
->createQueryBuilder('os')
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->join('os.guardiant', 'guardiant')
->join('os.discount', 'discount')
->where('os.unSubscripted is null')
->andWhere('discount.stripeId != :condition1 ')
->andWhere('discount.stripeId != :condition2 ')
->andWhere('CURRENT_DATE() > discount.endValid ')
->setParameter('condition1', 'lbp-rf-2')
->setParameter('condition2', 'lbp-rf-3')
->getQuery()->getResult();
return $q;
}
/**
* Reactive offer student
*
* @param OfferStudent $offerStudent
*/
public function getSuspendUser()
{
$date = new \DateTime("now");
$q = $this->_em->createQueryBuilder('os')
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->where('os.unSubscripted is null')
->andWhere('os.isSuspended = 1')
->andWhere('os.nextInvoiceSuspended <= :datePast')
->setParameter('datePast', $date->modify('2019-10-01') )
->orderBy('os.id', 'DESC')
;
return $q->getQuery()->getResult();
}
/**
* Reactive offer student
*
* @param OfferStudent $offerStudent
*/
public function getOfferWithEngagementSoonEnd($jday)
{
$date = new \DateTime("now");
$dayDiff = $date->modify("+$jday days");
$q = $this->_em->createQueryBuilder('os')
->select('os')
->from('BoStudentBundle:OfferStudent', 'os')
->join('os.offer', 'offer')
->where('os.unSubscripted is not null')
->andWhere("offer.slug IN ( 'offre-3-mois', 'offre-6-mois-1', 'offre-6-mois', 'offre-12-mois', 'offre-9-mois' )")
->andWhere('os.isStopTrial = 0')
->andWhere('os.renew = 0')
->andWhere('os.unSubscripted BETWEEN :day1 AND :day2')
->setParameter('day1', $dayDiff->format("Y-m-d 00:00:00") )
->setParameter('day2', $dayDiff->format("Y-m-d 23:59:59") )
->orderBy('os.id', 'DESC')
;
return $q->getQuery()->getResult();
}
}