src/Repository/RDVRepository.php line 389

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Client;
  4. use App\Entity\Company;
  5. use App\Entity\Contract;
  6. use App\Entity\Intervenant;
  7. use App\Entity\Product;
  8. use App\Entity\RDV;
  9. use App\Entity\RDVProduct;
  10. use App\Entity\Salon;
  11. use App\Entity\Site;
  12. use App\Entity\User;
  13. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  14. use Doctrine\Persistence\ManagerRegistry;
  15. use Symfony\Bridge\Doctrine\RegistryInterface;
  16. /**
  17.  * @method RDV|null find($id, $lockMode = null, $lockVersion = null)
  18.  * @method RDV|null findOneBy(array $criteria, array $orderBy = null)
  19.  * @method RDV[]    findAll()
  20.  * @method RDV[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  21.  */
  22. class RDVRepository extends ServiceEntityRepository
  23. {
  24.     public function __construct(ManagerRegistry $registry)
  25.     {
  26.         parent::__construct($registryRDV::class);
  27.     }
  28.     /**
  29.      * @param Site $site
  30.      * @return mixed
  31.      */
  32.     public function findForFusion()
  33.     {
  34.         return $this->createQueryBuilder('r')
  35.             //&& $rdv->getStatus() === RDV::STATUS_PLANIFIER && $rdv2->getStatus() === RDV::STATUS_PLANIFIER
  36.             ->where('r.status IN (:status)')
  37.             ->setParameter('status', [RDV::STATUS_PLANIFIERRDV::STATUS_REPLANIFIER])
  38.             ->getQuery()->getResult();
  39.     }
  40.     /**
  41.      * @param Site $site
  42.      * @return mixed
  43.      */
  44.     public function findForPlanning()
  45.     {
  46.         $date = (new \DateTime('now'))->modify('+3 day')->format('Y-m-d 23:59:59');
  47.         $dateMinus = (new \DateTime('now'))->modify('-3 day')->format('Y-m-d 00:00:00');
  48.         return $this->createQueryBuilder('r')
  49.             //&& $rdv->getStatus() === RDV::STATUS_PLANIFIER && $rdv2->getStatus() === RDV::STATUS_PLANIFIER
  50.             ->where('r.start <= :date')
  51.             ->andWhere('r.start >= :dateMinus')
  52.             ->setParameter('date'$date)
  53.             ->setParameter('dateMinus'$dateMinus)
  54.             ->getQuery()->getResult();
  55.     }
  56.     public function findForExport()
  57.     {
  58. // Define the date 6 months ago from the current date
  59.         $dateMinus = (new \DateTime('now'))->modify('-6 months')->format('Y-m-d 00:00:00');
  60. // Define the date 6 months in the future from the current date
  61.         $datePlus = (new \DateTime('now'))->modify('+6 months')->format('Y-m-d 23:59:59');
  62.         return $this->createQueryBuilder('r')
  63.             ->where('r.start >= :dateMinus')
  64.             ->andWhere('r.start <= :datePlus')
  65.             ->orderBy('r.id','desc')
  66.             ->setParameter('dateMinus'$dateMinus)
  67.             ->setParameter('datePlus'$datePlus)
  68.             ->getQuery()
  69.             ->getResult();
  70.     }
  71.     /**
  72.      * @param Site $site
  73.      * @return mixed
  74.      */
  75.     public function findForTomorrow()
  76.     {
  77.         $tomorrow = (new \DateTime())->modify('+1 day')->format('Y-m-d 00:00:00');
  78.         $tomorrowEnd = (new \DateTime())->modify('+1 day')->format('Y-m-d 23:59:59');
  79.         $status = [RDV::STATUS_PLANIFIERRDV::STATUS_REPLANIFIER];
  80.         return $this->createQueryBuilder('r')
  81.             ->where('r.start >= :date')
  82.             ->andWhere('r.start <= :dateEnd')
  83.             ->andWhere('r.status IN (:status)')
  84.             ->setParameter('date'$tomorrow)
  85.             ->setParameter('dateEnd'$tomorrowEnd)
  86.             ->setParameter('status'$status)
  87.             ->getQuery()->getResult();
  88.     }
  89.     /**
  90.      * @param Site $site
  91.      * @return mixed
  92.      */
  93.     public function findBySite($site)
  94.     {
  95.         return $this->createQueryBuilder('r')
  96.             ->leftJoin('r.salon''s')
  97.             ->innerJoin('s.site''site')
  98.             ->where('s.site = :site')
  99.             ->setParameter('site'$site)
  100.             ->getQuery()->getResult();
  101.     }
  102.     /**
  103.      * @param Site $site
  104.      * @return mixed
  105.      */
  106.     public function findFromToday()
  107.     {
  108.         return $this->createQueryBuilder('r')
  109.             ->where('r.start > :date')
  110.             ->setParameter('date', new  \DateTime())
  111.             ->orderBy('r.start')
  112.             ->setMaxResults(100)
  113.             ->getQuery()->getResult();
  114.     }
  115.     public function findByCompany($company)
  116.     {
  117.         return $this->createQueryBuilder('r')
  118.             ->innerJoin('r.intervenant''i')
  119.             ->innerJoin('i.workContract''work')
  120.             ->where('work.company = :company')
  121.             ->setParameter('company'$company)
  122.             ->getQuery()->getResult();
  123.     }
  124.     public function replanifierIntervenant(Intervenant  $intervenant$date)
  125.     {
  126.         return $this->createQueryBuilder('r')
  127.             ->innerJoin('r.intervenant''i')
  128.             ->where('r.intervenant = :intervenant')
  129.             ->andWhere('DATE_DIFF(r.start, :date) = 0')
  130.             ->andWhere('r.status = :status OR r.status = :statusRepla')
  131.             ->setParameter('date'$date)
  132.             ->setParameter('status'RDV::STATUS_PLANIFIER)
  133.             ->setParameter('statusRepla'RDV::STATUS_REPLANIFIER)
  134.             ->setParameter('intervenant'$intervenant)
  135.             ->getQuery()->getResult();
  136.     }
  137.     public function replanifierDate(Intervenant  $intervenantSalon  $salon$date)
  138.     {
  139.         return $this->createQueryBuilder('r')
  140.             ->innerJoin('r.intervenant''i')
  141.             ->where('r.intervenant = :intervenant')
  142.             ->andWhere('DATE_DIFF(r.start, :date) = 0')
  143.             ->andWhere('r.salon = :salon')
  144.             ->andWhere('r.status = :status OR r.status = :statusRepla')
  145.             ->setParameter('date'$date)
  146.             ->setParameter('status'RDV::STATUS_PLANIFIER)
  147.             ->setParameter('salon'$salon)
  148.             ->setParameter('statusRepla'RDV::STATUS_REPLANIFIER)
  149.             ->setParameter('intervenant'$intervenant)
  150.             ->getQuery()->getResult();
  151.     }
  152.     public function findOneByClientAndToken($client)
  153.     {
  154.         return $this->createQueryBuilder('r')
  155.             ->where('r.client = :client')
  156.             ->andWhere('r.orderId IS NOT NULL')
  157.             ->andWhere('r.status = :status')
  158.             ->andWhere('r.isPlatform = 1')
  159.             ->orderBy('r.id','DESC')
  160.             ->setParameter('client'$client->getId())
  161.             ->setParameter('status'RDV::STATUS_FACTURER)
  162.             ->setMaxResults(1// Set maximum number of results to 1
  163.             ->getQuery()
  164.             ->getOneOrNullResult();
  165.     }
  166.     public function findByFilter(
  167.         $intervenant null,
  168.         $site null,
  169.         $client null,
  170.         $start null,
  171.         $end null,
  172.         $status null,
  173.         $clientId null,
  174.         $user null,
  175.         $salon null
  176.     ) {
  177.         $qr $this->createQueryBuilder('r')
  178.             ->leftJoin('r.intervenant''i')
  179.             ->leftJoin('r.client''c')
  180.             ->leftJoin('c.site''s');
  181.         $filter false;
  182.         if ($intervenant) {
  183.             $qr->andWhere('i.id = :intervenant');
  184.             $qr->setParameter('intervenant'$intervenant);
  185.             $filter true;
  186.         }
  187.         if ($site) {
  188.             $qr->andWhere('s.id = :site');
  189.             $qr->setParameter('site'$site);
  190.             $filter true;
  191.         }
  192.         if ($salon) {
  193.             $qr->andWhere('r.salon = :salon');
  194.             $qr->setParameter('salon'$salon->getId());
  195.             $filter true;
  196.         }
  197.         if ($client) {
  198.             $searchTermArray explode(' '$client);
  199.             foreach ($searchTermArray as $searchItem){
  200.                 $qr->andWhere('c.firstname LIKE :firstname OR c.lastname LIKE :lastname');
  201.                 $qr->setParameter('firstname''%' $searchItem'%');
  202.                 $qr->setParameter('lastname''%' $searchItem '%');
  203.             }
  204. //            $qr->andWhere('c.firstname LIKE :client OR c.lastname LIKE :client OR CONCAT(c.firstname,\' \',c.lastname) LIKE :client');
  205. //            $qr->setParameter('client', '%' . $client . '%');
  206.             $filter true;
  207.         }
  208.         if ($clientId) {
  209.             $qr->andWhere('c.id = :clientId');
  210.             $qr->setParameter('clientId'$clientId);
  211.             $filter true;
  212.         }
  213.         if ($status) {
  214.             //$statusString = implode(',', $status);
  215.             $qr->andWhere('r.status IN (:status)');
  216.             $qr->setParameter('status'$status);
  217.             $filter true;
  218.         }
  219.         $date = new  \DateTime();
  220.         if ($start) {
  221.             $date \DateTime::createFromFormat('Y-m-d'$start);
  222.             if ($date instanceof \DateTime) {
  223.                 $date->setTime(000);
  224.             } else {
  225.                 $date = new  \DateTime();
  226.             }
  227.         }
  228.         $qr->andWhere('r.start >= :date')
  229.             ->setParameter('date'$date->format('Y-m-d H:i:s'))
  230.             ->orderBy('r.start');
  231.         if ($end) {
  232.             $date \DateTime::createFromFormat('Y-m-d'$end);
  233.             $date->setTime(235959);
  234.             $qr->andWhere('r.start <= :end');
  235.             $qr->setParameter('end'$date->format('Y-m-d H:i:s'));
  236.         }
  237.         if ($user && $user->hasRole(User::ROLE_COIFFEUR)) {
  238.             $qr->andWhere('i.user = :user');
  239.             $qr->setParameter('user'$user);
  240.             $filter true;
  241.         }
  242.         if (!$filter) {
  243.             $qr->setMaxResults(100);
  244.         }
  245.         return $qr->getQuery()->getResult();
  246.     }
  247.     public function findByFilterCoiffeuse(
  248.         $intervenant null,
  249.         $site null,
  250.         $client null,
  251.         $start null,
  252.         $end null,
  253.         $status null,
  254.         $clientId null,
  255.         $user null,
  256.         $salon null
  257.     ) {
  258.         $qr $this->createQueryBuilder('r')
  259.             ->select('r.id, c.roomNumber AS roomNumber, r.start, r.priorite, r.status, c.lastname AS lastname, c.firstname AS firstname, c.id as client_id, rec.nbSemaine')
  260.             ->leftJoin('r.intervenant''i')
  261.             ->leftJoin('r.client''c')
  262.             ->leftJoin('c.site''s')
  263.             ->leftJoin('r.recurrenceRdvs''rec');
  264.         $filter false;
  265.         if ($intervenant) {
  266.             $qr->andWhere('i.id = :intervenant');
  267.             $qr->setParameter('intervenant'$intervenant);
  268.             $filter true;
  269.         }
  270.         if ($salon) {
  271.             $qr->andWhere('r.salon = :salon');
  272.             $qr->setParameter('salon'$salon->getId());
  273.             $filter true;
  274.         }
  275.         if ($site) {
  276.             $qr->andWhere('s.id = :site');
  277.             $qr->setParameter('site'$site);
  278.             $filter true;
  279.         }
  280.         if ($client) {
  281.             $qr->andWhere('c.firstname LIKE :client OR c.lastname LIKE :client OR CONCAT(c.firstname,\' \',c.lastname) LIKE :client');
  282.             $qr->setParameter('client''%' $client '%');
  283.             $filter true;
  284.         }
  285.         if ($clientId) {
  286.             $qr->andWhere('c.id = :clientId');
  287.             $qr->setParameter('clientId'$clientId);
  288.             $filter true;
  289.         }
  290.         if ($status) {
  291.             $qr->andWhere('r.status IN (:status)');
  292.             $qr->setParameter('status'$status);
  293.         }
  294.         $date = new  \DateTime();
  295.         if ($start) {
  296.             $date \DateTime::createFromFormat('Y-m-d'$start);
  297.             if ($date instanceof \DateTime) {
  298.                 $date->setTime(000);
  299.             } else {
  300.                 $date = new  \DateTime();
  301.             }
  302.         }
  303.         $qr->andWhere('r.start >= :date')
  304.             ->setParameter('date'$date->format('Y-m-d H:i:s'))
  305.             ->orderBy('r.start');
  306.         if ($end) {
  307.             $date \DateTime::createFromFormat('Y-m-d'$end);
  308.             $date->setTime(235959);
  309.             $qr->andWhere('r.start <= :end');
  310.             $qr->setParameter('end'$date->format('Y-m-d H:i:s'));
  311.         }
  312.         if ($user && $user->hasRole(User::ROLE_COIFFEUR)) {
  313.             $qr->andWhere('i.user = :user');
  314.             $qr->setParameter('user'$user);
  315.         }
  316.         $qr->andWhere('s.id != 146');
  317.         if (!$filter) {
  318.             $qr->setMaxResults(100);
  319.         }
  320.         return $qr->getQuery()->getResult();
  321.     }
  322.     /**
  323.      * @param null $start
  324.      * @param null $end
  325.      * @return mixed
  326.      */
  327.     public function findForBilling($start null$end null$status$company$site)
  328.     {
  329.         $qr $this->createQueryBuilder('r')
  330.             ->select('r.id''r.start''r.end''c.id as client_id''s.id as site_id')
  331.             ->leftJoin('r.intervenant''i')
  332.             ->leftJoin('r.client''c')
  333.             ->leftJoin('c.billingInfos''bi')
  334.             ->leftJoin('c.site''s');
  335.         if ($start) {
  336.             $date \DateTime::createFromFormat('Y-m-d'$start);
  337.             if ($date instanceof \DateTime) {
  338.                 $date->setTime(000);
  339.             }
  340.             $qr->andWhere('r.start >= :start');
  341.             $qr->setParameter('start'$date);
  342.         }
  343.         if ($end) {
  344.             $date2 \DateTime::createFromFormat('Y-m-d'$end);
  345.             if ($date2 instanceof \DateTime) {
  346.                 $date2->setTime(235959);
  347.             }
  348.             $qr->andWhere('r.end <= :end');
  349.             $qr->setParameter('end'$date2);
  350.         }
  351.         if ($company) {
  352.             $qr->andWhere('bi.company = :company'); // company of client
  353.             $qr->setParameter('company'$company->getId());
  354.         }
  355.         if ($site) {
  356.             $qr->andWhere('s.id = :site');
  357.             $qr->setParameter('site'$site->getId());
  358.         }
  359.         $qr->andWhere('r.status = :status');
  360.         $qr->setParameter('status'$status);
  361.         //$qr->groupBy('r.client');
  362.         return $qr->getQuery()->getArrayResult();
  363.     }
  364.     public function findForReplanification($rdvs)
  365.     {
  366.         $qr $this->createQueryBuilder('r')
  367.             ->where('r.id IN (:rdvs)');
  368.         $qr->setParameter('rdvs'$rdvs)
  369.             //->where('r.status = :status')
  370.             //            ->andWhere('r.start >= :start')
  371.             //            ->andWhere('r.start <= :end')
  372.             //            ->setParameter('start', $start)
  373.             //            ->setParameter('end', $end)
  374.             //            ->setParameter('status', RDV::STATUS_PLANIFIER)
  375.         ;
  376.         //$qr->groupBy('r.client');
  377.         return $qr->getQuery()->getResult();
  378.     }
  379.     public function     findForReplanificationByContract($clientContrats$date$salon)
  380.     {
  381.         $qr $this->createQueryBuilder('r')
  382.             ->where('r.contract IN (:contract)')
  383.             ->andWhere('r.status = :status')
  384.             ->andWhere('r.salon = :salon')
  385.             ->andWhere('r.start > :date')
  386.             ->orderBy('r.start')
  387.             ->setParameter('contract'$clientContrats)
  388.             ->setParameter('salon'$salon->getId())
  389.             ->setParameter('date'$date->format('Y-m-d'))
  390.             ->setParameter('status'RDV::STATUS_PLANIFIER);
  391.         //$qr->groupBy('r.client');
  392.         return $qr->getQuery()->getResult();
  393.     }
  394.     public function     findForReplanificationNewRecurence(Client  $client$date$salon)
  395.     {
  396.         $qr $this->createQueryBuilder('r')
  397.             ->where('r.newRecurrence = true')
  398.             ->andWhere('r.status = :status')
  399.             ->andWhere('r.client = :client')
  400.             ->andWhere('r.salon = :salon')
  401.             ->andWhere('r.start > :date')
  402.             ->orderBy('r.start')
  403.             ->setParameter('client'$client->getId())
  404.             ->setParameter('salon'$salon->getId())
  405.             ->setParameter('date'$date->format('Y-m-d'))
  406.             ->setParameter('status'RDV::STATUS_PLANIFIER);
  407.         //$qr->groupBy('r.client');
  408.         return $qr->getQuery()->getResult();
  409.     }
  410.     public function findForIntervenantReplanification(Intervenant $intervenant\DateTime $date$site)
  411.     {
  412.         $date->setTime(000);
  413.         $qr $this->createQueryBuilder('r')
  414.             ->innerJoin('r.salon''sa')
  415.             ->innerJoin('sa.site''s')
  416.             ->where('r.intervenant = :intervenant')
  417.             ->andWhere('r.status IN (:status)')
  418.             ->andWhere('r.start >= :date');
  419.         if ($site) {
  420.             $qr->andWhere('s.id = :site')->setParameter('site'$site->getId());
  421.         }
  422.         $qr->setParameter('intervenant'$intervenant->getId())
  423.             ->setParameter('date'$date->format('Y-m-d'))
  424.             ->setParameter('status', [RDV::STATUS_PLANIFIERRDV::STATUS_REPLANIFIER]);
  425.         //$qr->groupBy('r.client');
  426.         return $qr->getQuery()->getResult();
  427.     }
  428.     public function findForRdvByStatus()
  429.     {
  430.         return $this->createQueryBuilder('r')
  431.             ->andWhere('r.paymentLink IS NOT NULL')
  432.             ->andWhere('r.status = :status')
  433.             ->setParameter('status'RDV::STATUS_NON_PAYE)
  434.             ->getQuery()
  435.             ->getResult();
  436.     }
  437.     public function findForRdvWithSalonAndStatus($salon)
  438.     {
  439.         return $this->createQueryBuilder('r')
  440.             ->where('r.salon = :salon')
  441.             ->andWhere('r.status != :status')
  442.             ->setParameter('status'RDV::STATUS_ANNULER)
  443.             ->setParameter('salon'$salon)
  444.             ->getQuery()
  445.             ->getResult();
  446.     }
  447.     public function findRdvWithLinkId($linkId)
  448.     {
  449.         return $this->createQueryBuilder('r')
  450.             ->where('r.paymentLink LIKE :linkId')
  451.             ->setParameter('linkId''%/' $linkId)
  452.             ->getQuery()
  453.             ->getOneOrNullResult();
  454.     }
  455.     public function getRDVsByClient(Client $client)
  456.     {
  457.         $subQuery $this->createQueryBuilder('r')
  458.         ->select('r.id');
  459.         $subQuery->leftJoin('r.client''c');
  460.         $subQuery->andWhere('c.id = :clientId')
  461.         ->setParameter('clientId'$client->getId());
  462.         return $subQuery->getQuery()->getArrayResult();
  463.     }
  464.     public function getLastRDVsByClient(Client $client)
  465.     {
  466.         $planifie RDV::STATUS_PLANIFIER;
  467.         $facturer RDV::STATUS_FACTURER;
  468.         // Create the QueryBuilder instance to construct the query
  469.         $qb $this->createQueryBuilder('r')
  470.             ->select('r'// select full RDV entities, adjust as needed
  471.             ->leftJoin('r.client''c')
  472.             ->andWhere('c.id = :clientId')
  473.             ->andWhere("r.status = :facturer OR r.status = :plan")
  474.             ->setParameter('facturer'$facturer)
  475.             ->setParameter('plan'$planifie)
  476.             ->setParameter('clientId'$client->getId())
  477.             ->orderBy('r.id''DESC'// assuming 'date' is the field to sort by
  478.             ->setMaxResults(1); // limit the result to get only the most recent one
  479.         // Execute the query and return the result
  480.         return $qb->getQuery()->getOneOrNullResult(); // Use getOneOrNullResult() to get a single result or null if none found
  481.     }
  482.     public function deleteRDVsByClient(Client $client)
  483.     {
  484.         $subQuery $this->createQueryBuilder('r')
  485.         ->select('r.id');
  486.         $subQuery->leftJoin('r.client''c');
  487.         $subQuery->andWhere('c.id = :clientId')
  488.         ->setParameter('clientId'$client->getId());
  489.         // Create the DELETE statement with the subquery in the WHERE clause
  490.         $qb $this->createQueryBuilder('rdv');
  491.         $qb->delete()
  492.             ->where($qb->expr()->in('rdv.id'$subQuery->getDQL()))
  493.             ->setParameter('clientId'$client->getId());
  494.         // Execute the DELETE statement
  495.         $qb->getQuery()->execute();
  496.     }
  497.     public function findByIdIntervenantAndDate$intervenantId=null$start=null,$end=null)
  498.     {
  499.         $q $this->createQueryBuilder('r')
  500.             ->innerJoin('r.intervenant''i')
  501.             ->where('i.id = :intervenant')
  502.             ->setParameter('intervenant'$intervenantId);
  503.             $date \DateTime::createFromFormat('Y-m-d'$start);
  504.             $date->setTime(000);
  505.             $q->andWhere('r.start >= :start')
  506.             ->setParameter('start'$date->format('Y-m-d H:i:s'));
  507.         if ($end) {
  508.             $date \DateTime::createFromFormat('Y-m-d'$end);
  509.             $date->setTime(235959);
  510.             $q->andWhere('r.start <= :end');
  511.             $q->setParameter('end'$date->format('Y-m-d H:i:s'));
  512.         }
  513.         return $q->getQuery()->getResult();
  514.     }
  515.     public function findFromTodayByStatusPlan(Client $client) {
  516.         return $this->createQueryBuilder('r')
  517.             ->where('r.start > :date')
  518.             ->andWhere('r.client = :client')
  519.             ->andWhere('r.status = :plan OR r.status = :replan')
  520.             ->setParameter('date', new  \DateTime())
  521.             ->setParameter('client'$client->getId())
  522.             ->setParameter('plan'RDV::STATUS_PLANIFIER)
  523.             ->setParameter('replan'RDV::STATUS_REPLANIFIER)
  524.             ->getQuery()->getResult();
  525.     }
  526. }