src/Repository/Management/SubscriptionRepository.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Repository\Management;
  3. use App\Entity\Management\Subscription\Formule;
  4. use App\Entity\Management\Subscription\Offer;
  5. use App\Entity\Management\Subscription\Subscription;
  6. use App\Entity\Users\Child;
  7. use App\Entity\Users\Guardiant;
  8. use App\Entity\Users\StudentsParents;
  9. use Bo\StudentBundle\Entity\OfferStudent;
  10. use Bo\StudentBundle\Entity\Student;
  11. use App\Entity\Users\Parents;
  12. use Bo\UserBundle\Entity\User;
  13. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  14. use Doctrine\ORM\EntityRepository;
  15. use Doctrine\ORM\NonUniqueResultException;
  16. use Doctrine\ORM\Query\ResultSetMapping;
  17. use Doctrine\Persistence\ManagerRegistry;
  18. /**
  19.  * StudentRepository
  20.  */
  21. class SubscriptionRepository extends ServiceEntityRepository
  22. {
  23.     public function __construct(ManagerRegistry $registry)
  24.     {
  25.         parent::__construct($registrySubscription::class);
  26.     }
  27.     public function getLastSubscriptionsByParents(Parents $user)
  28.     {
  29.         $q $this->_em
  30.             ->createQueryBuilder()
  31.             ->select('sp')
  32.             ->from(StudentsParents::class, 'sp')
  33.             ->join('sp.student','child')
  34.             ->where('sp.parents=:pid')
  35.             ->orderBy('child.firstname','asc')
  36.             ->setParameter('pid'$user->getId());
  37.         $list $q->getQuery()->getResult();
  38.         $sub = [];
  39.         foreach ($list as $sp){
  40.             $sub [] = $this->createQueryBuilder('s')
  41.                 ->where('s.student =:sid and s.guardiant =:gid')
  42.                 ->setParameters(['sid'=>$sp->getStudent()->getId(),'gid'=>$user->getId()])
  43.                 ->orderBy('s.id','desc')
  44.                 ->setMaxResults(1)
  45.                 ->getQuery()->getOneOrNullResult();
  46.         }
  47.         return $sub;
  48.     }
  49.     public function getStudents()
  50.     {
  51.         $date = new \DateTime("now");
  52.         $q $this->_em->createQueryBuilder()
  53.             ->select('os')
  54.             ->from('BoStudentBundle:OfferStudent''os')
  55.             ->andWhere("os.unSubscripted >= '" $date->format('Y-m-d') . "' or os.unSubscripted is null");
  56.         return $q->getQuery()->getResult();
  57.     }
  58.     public function getAllCurrentSubscription(Parents $parents){
  59.         $now = new \DateTime();
  60.         return $this->createQueryBuilder('os')
  61.             ->select('count(os)')
  62.             ->andWhere('((os.subscripted <=:now and :now<= os.unSubscripted ) or os.unSubscripted is null ) and os.guardiant =:id')
  63.             ->setParameters([
  64.                 'now'=>$now->format('Y-m-d H:i:s'),
  65.                 'id'=>$parents->getId()])
  66.             ->getQuery()->getSingleScalarResult();
  67.     }
  68.     public function getListStudentToDoBilan()
  69.     {
  70.         $q $this->_em->createQueryBuilder()
  71.             ->select('os')
  72.             ->from('BoStudentBundle:OfferStudent''os')
  73.             ->join('os.student''s')
  74.             ->Where('s.isBilan = 1');
  75.         return $q->getQuery()->getResult();
  76.     }
  77.     public function registrationExamAlert()
  78.     {
  79.         $rsm = new ResultSetMapping();
  80.         $rsm->addEntityResult('Bo\StudentBundle\Entity\ReviewProgram''u');
  81.         $rsm->addFieldResult('u''id''id');
  82.         $rsm->addJoinedEntityResult('Bo\StudentBundle\Entity\Student''s''u''student', array('id' => 'student_id'));
  83.         $rsm->addFieldResult('s''student_id''id');
  84.         $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);
  85.         return $query->getResult();
  86.     }
  87.     public function registrationExamAlertNoPLaylist()
  88.     {
  89.         $rsm = new ResultSetMapping();
  90.         $rsm->addEntityResult('Bo\StudentBundle\Entity\ReviewProgram''u');
  91.         $rsm->addFieldResult('u''id''id');
  92.         $rsm->addJoinedEntityResult('Bo\StudentBundle\Entity\Student''s''u''student', array('id' => 'student_id'));
  93.         $rsm->addFieldResult('s''student_id''id');
  94.         $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);
  95.         return $query->getResult();
  96.     }
  97.     public function getPreviousSubscriptionByStudentId($studentId)
  98.     {
  99.         $stmt $this
  100.             ->_em
  101.             ->getConnection()
  102.             ->prepare("
  103.                         SELECT *
  104.                         FROM lbp_prod.lnk_Student_Offer AS so
  105.                         WHERE so.student_id = :studentId
  106.                         ORDER BY so.id DESC LIMIT 1,1;
  107.                         ");
  108.         $stmt->execute(['studentId' => $studentId]);
  109.         $previousOs $stmt->fetchAll(\PDO::FETCH_COLUMN);
  110.         if (empty($previousOs)) {
  111.             return [];
  112.         }
  113.         $q $this->createQueryBuilder('os');
  114.         return $q
  115.             ->where('os.id = :previousOs')
  116.             ->setParameter('previousOs'$previousOs)
  117.             ->getQuery()
  118.             ->getOneOrNullResult();
  119.     }
  120.     public function getLastSubscriptionByParentChild(Parents $parentChild $student)
  121.     {
  122.         $q $this->_em->createQueryBuilder('os')
  123.             ->select('os')
  124.             ->from(Subscription::class, 'os')
  125.             ->where("os.student=:studentId and os.guardiant=:guardiantId and ((os.unSubscripted is not null and :now<=os.unSubscripted) or os.unSubscripted is null )")
  126.             ->setParameters([
  127.                 'guardiantId' => $parent->getId(),
  128.                 'studentId' => $student->getId(),
  129.                 'now'=>(new \DateTime('now'))->format('Y-m-d H:i:s')
  130.             ])
  131.             ->setMaxResults(1)
  132.             ->orderBy('os.id''desc');
  133.         return $q->getQuery()->getOneOrNullResult();
  134.     }
  135.     public function getLastSubscriptionByChild(Child $student)
  136.     {
  137.         $q $this->_em->createQueryBuilder('os')
  138.             ->select('os')
  139.             ->from(Subscription::class, 'os')
  140.             ->where("os.student=:studentId and ((os.unSubscripted is not null and :now<=os.unSubscripted) or os.unSubscripted is null )")
  141.             ->setParameters([
  142.                 'now'=>(new \DateTime('now'))->format('Y-m-d H:i:s'),
  143.                 'studentId' => $student->getId()
  144.             ])
  145.             ->setMaxResults(1)
  146.             ->orderBy('os.id''desc');
  147.         return $q->getQuery()->getOneOrNullResult();
  148.     }
  149.     public function getLastSubscriptionByStudent(Child $user)
  150.     {
  151.         $q $this->createQueryBuilder('os')
  152.             ->where("os.student=:studentId")
  153.             ->setParameters(['studentId' => $user->getId()])
  154.             ->setMaxResults(1)
  155.             ->orderBy('os.id''desc');
  156.         return $q->getQuery()->getOneOrNullResult();
  157.     }
  158.     /*
  159.     * Find students unsubcribed with role
  160.     */
  161.     public function findSubscriptionsInactifByParents(Parents $parents)
  162.     {
  163.         $q $this->createQueryBuilder('os')
  164.             ->leftjoin('os.guardiant''g')
  165.             ->where('( os.unSubscripted is null or (os.unSubscripted is not null and os.unSubscripted >= CURRENT_TIMESTAMP()))')
  166.             ->andWhere('os.inactif = 1 and g.id=:id')
  167.             ->setParameters(['id'=>$parents->getId()])
  168.         ;
  169.         return $q->getQuery()->getResult();
  170.     }
  171.     public function listInactifNoUnsubcripted()
  172.     {
  173.         return $this->createQueryBuilder('os')
  174.             ->where("os.inactif=1 and os.unSubscripted is null and os.stripeId is not null")
  175.            ->getQuery()->getResult();
  176.     }
  177.     public function listSubOffer(Parents $guardiant)
  178.     {
  179.         return $this->createQueryBuilder('os')
  180.             ->select('count(os.id) as nb')
  181.             ->join('os.offer''o')
  182.             ->join(Formule::class, 'f''WITH''f.id = o.id')
  183.             ->where('os.guardiant=:guardiant')
  184.             ->setParameters([ 'guardiant'=>$guardiant->getId()])
  185.             ->getQuery()->getSingleScalarResult();
  186.     }
  187.     public function invoice()
  188.     {
  189.         $q $this->_em
  190.             ->createQueryBuilder()
  191.             ->select('os')
  192.             ->from('BoStudentBundle:OfferStudent''os')
  193.             ->join('os.student''st')
  194.             ->where('os.unSubscripted is null or CURRENT_TIMESTAMP() < os.unSubscripted')
  195.             ->andWhere('os.offer INSTANCEOF FORMULE');
  196.         return $q->getQuery()->getResult();
  197.     }
  198.     /**
  199.      * return OfferStudent Objects where today = 15 days prior to credit card month expiration
  200.      *
  201.      * @return array
  202.      * @throws \Doctrine\DBAL\DBALException
  203.      */
  204.     public function getSoonExpiredCard()
  205.     {
  206.         $stmt $this
  207.             ->_em
  208.             ->getConnection()
  209.             ->prepare('
  210.                 SELECT so.id, date_add(so.card_expiration_date,interval -DAY(so.card_expiration_date)+1 DAY) AS first_day
  211.                 FROM lnk_Student_Offer AS so
  212.                 WHERE so.card_expiration_date IS NOT NULL
  213.                 GROUP BY so.guardiant_id
  214.                 HAVING CURRENT_DATE() = DATE_SUB(first_day, INTERVAL 15 DAY);
  215.             ');
  216.         $stmt->execute();
  217.         $studentOffers $stmt->fetchAll(\PDO::FETCH_ASSOC);
  218.         $ids = [];
  219.         foreach ($studentOffers as $row) {
  220.             $ids[] = $row['id'];
  221.         }
  222.         if (empty($ids)) {
  223.             return [];
  224.         }
  225.         $q $this->createQueryBuilder('os');
  226.         return $q
  227.             ->where($q->expr()->in('os.id'$ids))
  228.             ->getQuery()
  229.             ->getResult();
  230.     }
  231.     /**
  232.      * return OfferStudent Objects where today = 16th day of credit card current month expiration
  233.      *
  234.      * @return array
  235.      * @throws \Doctrine\DBAL\DBALException
  236.      */
  237.     public function getExpiredCardFirstReminder()
  238.     {
  239.         $stmt $this
  240.             ->_em
  241.             ->getConnection()
  242.             ->prepare("
  243.               SELECT so.id, CONCAT(YEAR(so.card_expiration_date), '-', MONTH(so.card_expiration_date), '-16') AS given_date
  244.               FROM lnk_Student_Offer AS so
  245.               WHERE so.card_expiration_date IS NOT NULL
  246.               GROUP BY so.guardiant_id
  247.               HAVING CURRENT_DATE() = given_date;
  248.               ");
  249.         $stmt->execute();
  250.         $studentOffers $stmt->fetchAll(\PDO::FETCH_ASSOC);
  251.         $ids = [];
  252.         foreach ($studentOffers as $row) {
  253.             $ids[] = $row['id'];
  254.         }
  255.         if (empty($ids)) {
  256.             return [];
  257.         }
  258.         $q $this->createQueryBuilder('os');
  259.         return $q
  260.             ->where($q->expr()->in('os.id'$ids))
  261.             ->getQuery()
  262.             ->getResult();
  263.     }
  264.     /**
  265.      * return OfferStudent Objects where today = 25th day of credit card current month expiration
  266.      *
  267.      * @return array
  268.      * @throws \Doctrine\DBAL\DBALException
  269.      */
  270.     public function getExpiredCardLastReminder()
  271.     {
  272.         $stmt $this
  273.             ->_em
  274.             ->getConnection()
  275.             ->prepare("
  276.               SELECT so.id, CONCAT(YEAR(so.card_expiration_date), '-', MONTH(so.card_expiration_date), '-25') AS given_date
  277.               FROM lnk_Student_Offer AS so
  278.               WHERE so.card_expiration_date IS NOT NULL
  279.               GROUP BY so.guardiant_id
  280.               HAVING CURRENT_DATE() = given_date;
  281.               ");
  282.         $stmt->execute();
  283.         $studentOffers $stmt->fetchAll(\PDO::FETCH_ASSOC);
  284.         $ids = [];
  285.         foreach ($studentOffers as $row) {
  286.             $ids[] = $row['id'];
  287.         }
  288.         if (empty($ids)) {
  289.             return [];
  290.         }
  291.         $q $this->createQueryBuilder('os');
  292.         return $q
  293.             ->where($q->expr()->in('os.id'$ids))
  294.             ->getQuery()
  295.             ->getResult();
  296.     }
  297.     /**
  298.      * return OfferStudent Objects where today = credit card expiration date
  299.      *
  300.      * @return array
  301.      * @throws \Doctrine\DBAL\DBALException
  302.      */
  303.     public function getExpiredCardAlert()
  304.     {
  305.         return $this
  306.             ->createQueryBuilder('os')
  307.             ->where('os.cardExpirationDate = CURRENT_DATE()')
  308.             ->groupBy('os.guardiant')
  309.             ->getQuery()
  310.             ->getResult();
  311.     }
  312.     public function getBankCollectionByUser($userStudent $student null)
  313.     {
  314.         $q $this
  315.             ->createQueryBuilder('os');
  316.         if ($user instanceof Parents) {
  317.             $q $q->where('os.guardiant =:user')->setParameter('user'$user->getId());
  318.         } elseif ($user instanceof Student) {
  319.             $q $q->where('os.student =:user')->setParameter('user'$user->getId());
  320.         }
  321.         if (!is_null($student)) {
  322.             $q $q->andwhere('os.student =:student')->setParameter('student'$student->getId());
  323.         }
  324.         $q $q->andWhere('os.isBankCollectionProcess = 1')->orderBy('os.id''desc');
  325.         return $q->getQuery()->getResult();
  326.     }
  327.     public function isSuspendedSubscription($user)
  328.     {
  329.         $q $this->_em
  330.             ->createQueryBuilder()
  331.             ->select('os')
  332.             ->from('BoStudentBundle:OfferStudent''os')
  333.             ->where('os.unSubscripted is null')
  334.             ->andWhere('os.isSuspended = 1')
  335.             ->andWhere('os.guardiant =:user')
  336.             ->setParameter('user'$user->getId());
  337.         return $q->getQuery()->getResult();
  338.     }
  339.     public function getSuspendedSubscription()
  340.     {
  341.         $q $this->_em
  342.             ->createQueryBuilder()
  343.             ->select('os')
  344.             ->from('BoStudentBundle:OfferStudent''os')
  345.             ->where('os.unSubscripted is null')
  346.             ->andWhere('os.isSuspended = 1')
  347.             ->andWhere('os.dateSuspended is not null');
  348.         return $q->getQuery()->getResult();
  349.     }
  350.     public function getCurrentSubscriptionByChild(Child $child){
  351.         $now = new \DateTime();
  352.         $parameters = ['now' => $now->format('Y-m-d H:i:s'),'student'=>$child->getId()];
  353.         $q $this->createQueryBuilder('s')
  354.             ->select('s')
  355.             ->join('s.offer''offers')
  356.             ->where('
  357.                 (os.unSubscripted is null AND os.subscripted <= :now ) OR 
  358.                 (os.unSubscripted is not null AND os.subscripted <= :now AND :now <= os.unSubscripted) OR
  359.                 (:now < os.trialDate)')
  360.             ->andWhere('offers INSTANCE OF BoTrainningBundle:Formule')
  361.             ->andWhere('s.student =:student')
  362.             ->setParameters($parameters);
  363.         return $q->getQuery()->getResult();
  364.     }
  365.     public function findOfferFreeByChild(?Child $child){
  366.         if(is_null($child))
  367.             return null;
  368.         $parameters = ['student'=>$child->getId(),'offer'=>Offer::OFFER_FREE];
  369.         $q $this->createQueryBuilder('s')
  370.             ->select('s')
  371.             ->join('s.offer''offers')
  372.             ->Where('s.student =:student and offers.slug=:offer')
  373.             ->orderBy('s.id','desc')
  374.             ->setMaxResults(1)
  375.             ->setParameters($parameters);
  376.         return $q->getQuery()->getOneOrNullResult();
  377.     }
  378.     public function getCurrentSubscriptionByUser($user$student null$guardiantIsNull true$returnAll false)
  379.     {
  380.         $now = new \DateTime('now');
  381.         $q $this->_em
  382.             ->createQueryBuilder('os')
  383.             ->select('os')
  384.             ->from('BoStudentBundle:OfferStudent''os')
  385.             ->join('os.offer''offers')
  386.             ->where('
  387.                 (os.unSubscripted is null AND os.subscripted <= :now ) OR 
  388.                 (os.unSubscripted is not null AND os.subscripted <= :now AND :now <= os.unSubscripted) OR
  389.                 (:now < os.trialDate)')
  390.             ->andWhere('offers INSTANCE OF BoTrainningBundle:Formule');
  391.         $parameters = ['now' => $now->format('Y-m-d H:i:s')];
  392.         if ($user instanceof Parents) {
  393.             $q->andWhere('os.guardiant =:guardiant');
  394.             $parameters['guardiant'] = $user->getId();
  395.             if($student !== null) {
  396.                 $q->andWhere('os.student=:student');
  397.                 $parameters['student'] = $student->getId();
  398.                 if($returnAll === true) {
  399.                     $q->groupBy('os.student');
  400.                 }
  401.             } else {
  402.                 // NOTE : to avoid exception of getOneOrNullResult() for multiple results
  403.                 $q->setParameters($parameters);
  404.                 if($returnAll === true) {
  405.                     $q->groupBy('os.student');
  406.                 }
  407.                 $studentOffers $q->getQuery()->getResult();
  408.                 if(empty($studentOffers) === true) {
  409.                     return null;
  410.                 } else {
  411.                     if($returnAll === true) {
  412.                         return $studentOffers;
  413.                     }
  414.                     return current($studentOffers);
  415.                 }
  416.             }
  417.         } else {
  418.             if($guardiantIsNull === true) {
  419.                 $q->andWhere('os.guardiant is null');
  420.             }
  421.             $q->andWhere('os.student=:student');
  422.             $parameters['student'] = $user->getId();
  423.             if($returnAll === true) {
  424.                 $q->groupBy('os.student');
  425.             }
  426.         }
  427.         $q->setParameters($parameters);
  428.         $result $q->getQuery()->getResult();
  429.         // NOTE : it seems that getOneOrNullResult() generate an error
  430.         if(empty($result)) {
  431.             return null;
  432.         } else {
  433.             if($returnAll === true) {
  434.                 return $result;
  435.             }
  436.             return current($result);
  437.         }
  438.     }
  439.     public function getCurrentSubscriptionsByParent(Parents $parents)
  440.     {
  441.         $now = new \DateTime('now');
  442.         $q $this->_em
  443.             ->createQueryBuilder('os')
  444.             ->select('os')
  445.             ->from(Subscription::class, 'os')
  446.             ->join('os.student','s')
  447.             ->where('os.unSubscripted is null or :now <= os.unSubscripted')
  448.             ->andWhere('os.guardiant =:guardiant')
  449.             ->orderBy('s.firstname','asc')
  450.             ->setParameters([
  451.                 'guardiant' => $parents->getId(),
  452.                 'now'       => $now->format('Y-m-d H:i:s')
  453.             ]);
  454.         return $q->getQuery()->getResult();
  455.     }
  456.     public function getChangeOfferSubscriptionsByParentId(Parents $parents)
  457.     {
  458.         $now = new \DateTime('now');
  459.         $q $this->_em
  460.             ->createQueryBuilder('os')
  461.             ->select('os')
  462.             ->from('BoStudentBundle:OfferStudent''os')
  463.             ->join('os.offer''offers')
  464.             ->where('offers INSTANCE OF BoTrainningBundle:Formule')
  465.             ->andWhere('os.guardiant =:guardiant')
  466.             ->andWhere('offers.stripeId != :newlbp')
  467.             ->setParameters([
  468.                 'guardiant' => $parents->getId(),
  469.                 'newlbp'=> 'new-lbp',
  470.                 //'now' => $now->format('Y-m-d H:i:s')
  471.             ]);
  472.         return $q->getQuery()->getResult();
  473.     }
  474.     public function getCurrentSubscriptionsByUserId(Child $student)
  475.     {
  476.         $now = new \DateTime('now');
  477.         $q $this->_em
  478.             ->createQueryBuilder('os')
  479.             ->select('os')
  480.             ->from(Subscription::class, 'os')
  481.             ->join('os.offer''offers')
  482.             ->where('os.unSubscripted is null or :now <= os.unSubscripted')
  483.             ->andWhere('os.student =:guardiant')
  484.             ->setParameters(['guardiant' => $student->getId(), 'now' => $now->format('Y-m-d H:i:s')])
  485.             ->setMaxResults(1);
  486.         try {
  487.             return $q->getQuery()->getOneOrNullResult();
  488.         } catch (NonUniqueResultException $e) {
  489.             return null;
  490.         }
  491.     }
  492.     public function findAllCurrent()
  493.     {
  494.         $q $this->_em
  495.             ->createQueryBuilder('os')
  496.             ->select('os')
  497.             ->from('BoStudentBundle:OfferStudent''os')
  498.             ->where('os.unSubscripted is null')
  499.             ->andWhere('os.stripeId is not null')
  500.             ->andWhere('os.offer = 40');
  501.         return $q->getQuery()->getResult();
  502.     }
  503.     public function updateCardExpiration()
  504.     {
  505.         $query $this->_em
  506.             ->createQueryBuilder('os')
  507.             ->select('os')
  508.             ->from('BoStudentBundle:OfferStudent''os')
  509.             ->join('os.guardiant''guardiant')
  510.             ->where('os.unSubscripted is null')
  511.             ->andWhere('os.stripeId is not null')
  512.             ->andWhere('os.offer = 40')
  513.             ->andWhere('os.guardiant = guardiant.id')
  514.             ->orderBy('os.id''desc')
  515.         ;
  516.         $stripeIds = [];
  517.         $query $query->getQuery()->getResult();
  518.         foreach ($query as $q ){
  519.                 $stripeIds[] = [
  520.                     'id' => $q->getGuardiant()->getId(),
  521.                     'stripeId' =>  $q->getGuardiant()->getStripeId(),
  522.                     'dateCardExpiration' => $q->getCardExpirationDate()
  523.                 ];
  524.         }
  525.         return $stripeIds;
  526.     }
  527.     public function sendWarningCardExpiration($dateCardExpiration)
  528.     {
  529.         $query $this->_em
  530.             ->createQueryBuilder('os')
  531.             ->select('os')
  532.             ->from('BoStudentBundle:OfferStudent''os')
  533.             ->join('os.guardiant''guardiant')
  534.             ->where('os.unSubscripted is null')
  535.             ->andWhere('os.cardExpirationDate = :dateCardExpiration')
  536.             ->andWhere('os.stripeId is not null')
  537.             ->andWhere('os.offer = 40')
  538.             ->andWhere('os.guardiant = guardiant.id')
  539.             ->setParameter('dateCardExpiration'$dateCardExpiration)
  540.             ->orderBy('os.id''desc')
  541.         ;
  542.         return $query->getQuery()->getResult();
  543.     }
  544.     public function SubscritptionAndInvoicesByGuardiant($guardiantId)
  545.     {
  546.         $q $this->_em
  547.             ->createQueryBuilder('os')
  548.             ->select('os''inv')
  549.             ->from('BoStudentBundle:OfferStudent''os')
  550.             ->join('os.invoices''inv')
  551.             ->where('os.guardiant =:guardiant')
  552.             ->andWhere('inv INSTANCE OF BoSubscriptionBundle:Invoice')
  553.             ->setParameter('guardiant'$guardiantId);
  554.         return $q->getQuery()->getResult();
  555.     }
  556.     public function allSubscription(User $user)
  557.     {
  558.         $q $this->_em
  559.             ->createQueryBuilder('max(os.id)')
  560.             ->select('max(os.id)')
  561.             ->from('BoStudentBundle:OfferStudent''os')
  562.             ->join('os.offer''offers')
  563.             ->where('os.guardiant =:guardiant')
  564.             ->andWhere('offers INSTANCE OF BoTrainningBundle:Formule')
  565.             ->groupBy('os.guardiant,os.student')
  566.             ->setParameter('guardiant'$user->getId());
  567.         $q $q->getQuery()->getArrayResult();
  568.         foreach ($q as $it) {
  569.             $ids[] = $it[1];
  570.         }
  571.         if (empty($ids)) {
  572.             return [];
  573.         }
  574.         $q2 $this->_em
  575.             ->createQueryBuilder('os');
  576.         return $this->_em
  577.             ->createQueryBuilder('os')
  578.             ->select('os')
  579.             ->from('BoStudentBundle:OfferStudent''os')
  580.             ->where($q2->expr()->in('os.id'$ids))
  581.             ->orderBy('os.unSubscripted''asc')
  582.             ->getQuery()->getResult();
  583.     }
  584.     public function justOnceTrial(User $userOfferStudent $offerStudent null)
  585.     {
  586.         if (is_null($user) || is_null($offerStudent))
  587.             return null;
  588.         $q $this->_em
  589.             ->createQueryBuilder('os')
  590.             ->select('os')
  591.             ->from('BoStudentBundle:OfferStudent''os')
  592.             ->join('os.offer''offers')
  593.             ->where('os.guardiant =:guardiant')
  594.             ->andWhere('offers INSTANCE OF BoTrainningBundle:Formule')
  595.             ->andWhere('os.id <> :current')
  596.             ->andWhere('offers.id=:offer_id')
  597.             ->setParameters(['guardiant' => $user->getId(), 'current' => $offerStudent->getId(), 'offer_id' => Offer::ID_OFFER_NEW_OFFER]);
  598.         $q $q->getQuery()->getResult();
  599.         return count($q) == null true;
  600.     }
  601.     public function checkIfCurrentTrialSubscription($parentId)
  602.     {
  603.         if (is_null($parentId))
  604.             return null;
  605.         $q $this->_em
  606.             ->createQueryBuilder('os')
  607.             ->select('os')
  608.             ->from('BoStudentBundle:OfferStudent''os')
  609.             ->join('os.offer''offers')
  610.             ->where('os.guardiant =:guardiant')
  611.             ->andWhere('offers INSTANCE OF BoTrainningBundle:Formule')
  612.             ->andWhere('os.unSubscripted is null')
  613.             ->andWhere('os.trialDate is not null')
  614.             ->andWhere('offers.id=:offer_id')
  615.             ->andwhere(' CURRENT_DATE() < os.subscripted')
  616.             ->orderBy('os.id''desc')
  617.             ->setParameters([
  618.                 'guardiant' => $parentId,
  619.                 'offer_id' => Offer::ID_OFFER_NEW_OFFER
  620.             ]);
  621.         $q $q->getQuery()->getResult();
  622.         return $q $q[0] : false;
  623.     }
  624.     public function getSponsoredPaid(User $user)
  625.     {
  626.         $q $this->_em
  627.             ->createQueryBuilder('os')
  628.             ->select('os')
  629.             ->from('BoStudentBundle:OfferStudent''os')
  630.             ->where('os.sponsor =:guardiant')
  631.             ->andWhere('os.unSubscripted is null')
  632.             ->andWhere('os.trialDate is null or os.trialDate < CURRENT_DATE()')
  633.             ->setParameters(['guardiant' => $user->getId()]);
  634.         return $q->getQuery()->getResult();
  635.     }
  636.     public function sendEmailAlertSubscription()
  637.     {
  638.         $stmt $this
  639.             ->_em
  640.             ->getConnection()
  641.             ->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
  642. (DATEDIFF(subscripted, now())=-1 or DATEDIFF(subscripted, now())=-3 or DATEDIFF(l.created, now())=-2 or DATEDIFF(l.created, now())=-7)');
  643.         $stmt->execute();
  644.         return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  645.     }
  646.     public function sendEmailAlertLastTrial()
  647.     {
  648.         $dateNow = new \DateTime();
  649.         $dateNow->modify('+ 1 day');
  650.         $q $this->_em->createQueryBuilder()
  651.             ->select('os')
  652.             ->from('BoStudentBundle:OfferStudent''os')
  653.             ->where("os.trialDate BETWEEN :date1 AND :date2 ")
  654.             ->andWhere("os.unSubscripted is null")
  655.             ->setParameter('date1'$dateNow->format('Y-m-d 00:00:00'))
  656.             ->setParameter('date2'$dateNow->format('Y-m-d 23:59:59'))
  657.         ;
  658.         return $q->getQuery()->getResult();
  659.     }
  660.     public function buttonSubscription(User $user$mode)
  661.     {
  662.         $q $this->_em
  663.             ->createQueryBuilder('os')
  664.             ->select('os')
  665.             ->from('BoStudentBundle:OfferStudent''os')
  666.             ->join('os.offer''offers')
  667.             ->where('offers INSTANCE OF BoTrainningBundle:Formule');
  668.         if ($user INSTANCEOF Parents) {
  669.             $q $q->andWhere('os.guardiant=:guardiant')->setParameter('guardiant'$user->getId());
  670.         } else {
  671.             $q $q->andWhere('os.student=:student')->setParameter('student'$user->getId());
  672.         }
  673.         if ($mode) {
  674.             switch ($mode) {
  675.                 case 1:
  676.                     $q $q->andWhere('offers.slug=:offer_slug')->setParameter('offer_slug''abonnement-assistance');
  677.                     break;
  678.                 case 2:
  679.                     $q $q->andWhere('os.unSubscripted is null or os.unSubscripted > CURRENT_TIMESTAMP()');
  680.                     break;
  681.             }
  682.         }
  683.         $q $q->orderBy('os.id''desc');
  684.         return $q->getQuery()->getResult();
  685.     }
  686.     public function hadOnePackExam(Parents $parents)
  687.     {
  688.         $q $this->_em->createQueryBuilder('os')
  689.             ->select('os')
  690.             ->from('BoStudentBundle:OfferStudent''os')
  691.             ->join('os.offer''offer')
  692.             ->where('offer.slug =:slug ')
  693.             ->andwhere("os.guardiant=:guardiantId")
  694.             ->andwhere('os.unSubscripted is null or CURRENT_DATE() <= os.unSubscripted')
  695.             ->setParameters([
  696.                 'guardiantId' => $parents->getId(),
  697.                 'slug' => 'packexam-2018'
  698.             ])
  699.             ->orderBy('os.id''desc');
  700.         return $q->getQuery()->getResult();
  701.     }
  702.     /**
  703.      * Reactive offer student
  704.      *
  705.      * @param OfferStudent $offerStudent
  706.      * @return int
  707.      */
  708.     public function reactivate(OfferStudent $offerStudent)
  709.     {
  710.         $queryBuilder $this->_em->createQueryBuilder('os');
  711.         $queryBuilder->update('BoStudentBundle:OfferStudent','os')
  712.             ->set('os.unSubscripted'':unSubscripted')
  713.             ->setParameter('unSubscripted'null)
  714.             ->where('os.id = :offerStudentId')
  715.             ->andWhere('os.unSubscripted IS NOT NULL')
  716.             ->setParameter('offerStudentId'$offerStudent->getId());
  717.         return $queryBuilder
  718.             ->getQuery()
  719.             ->execute();
  720.     }
  721.     public function getSubscriberFormule($formule)
  722.     {
  723.         $q $this->_em->createQueryBuilder()
  724.             ->select('os')
  725.             ->from('BoStudentBundle:OfferStudent''os')
  726.             ->join('os.offer''offer')
  727.             ->where('offer.slug = :slug')
  728.             ->setParameter('slug'$formule )
  729.             ;
  730.         return $q->getQuery()->getResult();
  731.     }
  732.     /**
  733.      * Return offerStudent statement by course/serie
  734.      *
  735.      * @param $courseId
  736.      * @param $serieId
  737.      * @param \DateTime $startDate
  738.      * @param array $studentIds
  739.      * @return \Doctrine\DBAL\Driver\Statement
  740.      * @throws \Doctrine\DBAL\DBALException
  741.      */
  742.     public function getStatementAllByCourseAndSerie($courseId$serieId\DateTime $startDate,\DateTime $endDate$studentIds = [])
  743.     {
  744.         if(empty($studentIds) === false) {
  745.             $queryComplement "AND os.student_id IN (".implode(", "$studentIds).")";
  746.         } else {
  747.             $queryComplement "";
  748.         }
  749.         $stmt $this
  750.             ->_em
  751.             ->getConnection()
  752.             ->prepare(
  753.                 "
  754.                         SELECT os.*, s.firstname AS student_firstname, g.firstname AS parent_firstname, g.email AS parent_email, s.serie AS student_serie
  755.                         FROM ".$this->_em->getClassMetadata('BoStudentBundle:OfferStudent')->getTableName()." AS os
  756.                         INNER JOIN ".$this->_em->getClassMetadata('BoStudentBundle:Student')->getTableName()." AS s ON s.id = os.student_id
  757.                         INNER JOIN ".$this->_em->getClassMetadata('BoUserBundle:Parents')->getTableName()." AS g ON g.id = os.guardiant_id
  758.                         WHERE  os.stripe_id is not null
  759.                         AND (os.un_subscripted is null or '".$endDate->format('Y-m-d')."' < os.un_subscripted)
  760.                         AND os.offer_id = 40
  761.                         AND os.created  < '".$endDate->format('Y-m-d')."'
  762.                         ".$queryComplement."
  763.                         ;"
  764.             );
  765.         $stmt->execute();
  766.         return $stmt;
  767.     }
  768.     /**
  769.      * Reactive offer student
  770.      *
  771.      * @param OfferStudent $offerStudent
  772.      */
  773.     public function getExistOfferStudent$parentID,  $studentID)
  774.     {
  775.         $q $this->_em->createQueryBuilder('os')
  776.             ->select('os')
  777.             ->from('BoStudentBundle:OfferStudent''os')
  778.             ->join('os.guardiant''parent')
  779.             ->join('os.student''student')
  780.             ->where('os.unSubscripted is null')
  781.             ->andWhere('os.guardiant = :parentID')
  782.             ->andWhere('os.student = :studentID')
  783.             ->setParameter('parentID'$parentID )
  784.             ->setParameter('studentID'$studentID)
  785.             ->orderBy('os.id''DESC')
  786.         ;
  787.         try {
  788.            return $q->getQuery()->getSingleResult();
  789.         } catch (\Exception $e) {
  790.            return false;
  791.         }
  792.     }
  793.     /**
  794.      * Active Offer Student
  795.      *
  796.      * @param OfferStudent $offerStudent
  797.      */
  798.     public function getAllSubscriptionActive()
  799.     {
  800.         $q $this->_em
  801.             ->createQueryBuilder('os')
  802.             ->select('os')
  803.             ->from('BoStudentBundle:OfferStudent''os')
  804.             ->join('os.guardiant''guardiant')
  805.             ->join('os.discount''discount')
  806.             ->where('os.unSubscripted is null')
  807.             ->andWhere('discount.stripeId != :condition1 ')
  808.             ->andWhere('discount.stripeId != :condition2 ')
  809.             ->andWhere('CURRENT_DATE() > discount.endValid  ')
  810.             ->setParameter('condition1''lbp-rf-2')
  811.             ->setParameter('condition2''lbp-rf-3')
  812.             ->getQuery()->getResult();
  813.         return $q;
  814.     }
  815.     /**
  816.      * Reactive offer student
  817.      *
  818.      * @param OfferStudent $offerStudent
  819.      */
  820.     public function getSuspendUser()
  821.     {
  822.         $date = new \DateTime("now");
  823.         $q $this->_em->createQueryBuilder('os')
  824.             ->select('os')
  825.             ->from('BoStudentBundle:OfferStudent''os')
  826.             ->where('os.unSubscripted is null')
  827.             ->andWhere('os.isSuspended = 1')
  828.             ->andWhere('os.nextInvoiceSuspended <= :datePast')
  829.             ->setParameter('datePast'$date->modify('2019-10-01') )
  830.             ->orderBy('os.id''DESC')
  831.         ;
  832.         return $q->getQuery()->getResult();
  833.     }
  834.     /**
  835.      * Reactive offer student
  836.      *
  837.      * @param OfferStudent $offerStudent
  838.      */
  839.     public function getOfferWithEngagementSoonEnd($jday)
  840.     {
  841.         $date = new \DateTime("now");
  842.         $dayDiff $date->modify("+$jday days");
  843.         $q $this->_em->createQueryBuilder('os')
  844.             ->select('os')
  845.             ->from('BoStudentBundle:OfferStudent''os')
  846.             ->join('os.offer''offer')
  847.             ->where('os.unSubscripted is not null')
  848.             ->andWhere("offer.slug IN ( 'offre-3-mois', 'offre-6-mois-1', 'offre-6-mois', 'offre-12-mois', 'offre-9-mois' )")
  849.             ->andWhere('os.isStopTrial = 0')
  850.             ->andWhere('os.renew = 0')
  851.             ->andWhere('os.unSubscripted  BETWEEN :day1  AND :day2')
  852.             ->setParameter('day1'$dayDiff->format("Y-m-d 00:00:00") )
  853.             ->setParameter('day2'$dayDiff->format("Y-m-d 23:59:59") )
  854.             ->orderBy('os.id''DESC')
  855.         ;
  856.         return $q->getQuery()->getResult();
  857.     }
  858. }