src/Sylius/Bundle/CoreBundle/Doctrine/ORM/TurnRepository.php line 126

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\CoreBundle\Doctrine\ORM;
  12. use Doctrine\ORM\QueryBuilder;
  13. use Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository;
  14. class TurnRepository extends EntityRepository
  15. {
  16.     public function getEvents(\DateTimeInterface $start\DateTimeInterface $endstring $url, array $filters nullstring $urlHc null): array
  17.     {
  18.         $query $this->createQueryBuilder('o')
  19.             ->select("
  20.                 CONCAT(
  21.                     CASE WHEN c.id IS NULL THEN '' ELSE CONCAT('<a class=\"ui label circular teal float-right hidden\" href=\"".$urlHc."?id=',c.id, '\"><i class=\"icon address book outline\"></i></a>') END,
  22.                     CASE WHEN pay.state = 'STATE_NEW' THEN '<a class=\"ui red circular label\">-</a>' WHEN pay.state = 'STATE_COMPLETED' THEN '<a class=\"ui green circular label\">P</a>' WHEN pay.state = 'STATE_OVERPAYMENT' THEN '<a class=\"ui purple circular label\">S</a>' WHEN pay.state = 'STATE_INCOMPLETE' THEN '<a class=\"ui yellow circular label\">S</a>' WHEN pay.state = 'STATE_BONUS' THEN '<a class=\"ui pink circular label\">B</a>' WHEN pay.state IS NULL THEN '' ELSE '<a class=\"ui red circular label\">-</a>'
  23.                         END,
  24.                     CASE WHEN  pay.state IS NOT NULL THEN '' WHEN o.payStatus = 'PAY_COMPLETED' THEN '<a class=\"ui green circular label\">P</a>' WHEN o.payStatus = 'PAY_INCOMPLETE' THEN '<a class=\"ui yellow circular label\">S</a>' ELSE '<a class=\"ui gray circular label\">-</a>' END,
  25.                     CASE WHEN o.turnStatus = 'STATUS_NEW' THEN '<a class=\"ui yellow circular label\">-</a>' WHEN o.turnStatus = 'STATUS_CANCELLED' THEN '<a class=\"ui red circular label\">X</a>' WHEN o.turnStatus = 'STATUS_RESCHEDULE' THEN '<a class=\"ui blue circular label\">R</a>' ELSE '<a class=\"ui green circular label\">C</a>' END,
  26.                     CASE WHEN o.finalStatus = 'FINAL_NOT_ATTENDANCE' THEN '<a class=\"ui red circular label\">-</a>' WHEN o.finalStatus = 'FINAL_ATTENDANCE' THEN '<a class=\"ui green circular label\">A</a>' ELSE '' END,
  27.                     '</br>',    
  28.                     CASE WHEN c.id IS NULL THEN CONCAT(CASE WHEN o.lastname IS NULL THEN '' ELSE o.lastname END, ' ', CASE WHEN o.name IS NULL THEN '' ELSE o.name END) ELSE CONCAT(c.lastName, ' ', c.firstName) END,
  29.                     '<br>T',
  30.                     o.id,
  31.                     '<div class=\"cal-ext-cont\">',
  32.                     CASE WHEN p.name IS NULL THEN '' ELSE CONCAT('Profesional: ', p.lastname, ' ', p.name) END,
  33.                     CASE WHEN pr.name IS NULL THEN '' ELSE CONCAT('<br/>Sala: ', pr.name) END,
  34.                     CASE WHEN GROUP_CONCAT(taxont.name, ' ') IS NULL THEN ' ' ELSE CONCAT('<br>Tipos: ', GROUP_CONCAT(taxont.name, ' ')) END,
  35.                     CASE WHEN o.description IS NULL THEN '' ELSE CONCAT('</br> Descripción: ', o.description) END,
  36.                     '</div>'
  37.                  ) AS title, 
  38.                 DATE_FORMAT(o.start, '%Y-%m-%dT%H:%i:%s') AS start, 
  39.                 DATE_FORMAT(o.end, '%Y-%m-%dT%H:%i:%s') AS end,
  40.                 p.color as color,
  41.                 CASE WHEN pr.color IS NULL THEN p.color WHEN pr.color = '#FFFFFF' THEN p.color ELSE pr.color END as borderColor,
  42.                 o.id as id,
  43.                 CONCAT('".$url."', o.id, '/edit') AS url
  44.                 ")
  45.                 // CASE WHEN o.payStatus = 'PAY_NEW' THEN '#d15b61' ELSE '#21ba45' END as color,
  46.                 // CASE WHEN o.turnStatus = 'STATUS_NEW' THEN '#d15b61' WHEN o.turnStatus = 'STATUS_CANCELLED' THEN '#000000' ELSE '#21ba45' END as borderColor,
  47.             ->leftJoin('o.customer''c')
  48.             ->leftJoin('o.professional''p')
  49.             ->leftJoin('o.room''pr')
  50.             ->leftJoin('o.payCustomer''pc')
  51.             ->leftJoin('o.turnTaxons''tt')
  52.             ->leftJoin('tt.taxon''taxon')
  53.             ->leftJoin('taxon.translations''taxont')
  54.             ->leftJoin('pc.pay''pay')
  55.             ->andWhere('o.start >= :start')
  56.             ->andWhere('o.start <= :end')
  57.             ->andWhere('o.enabled = :enabled')
  58.             ->setParameter('start'$start)
  59.             ->setParameter('end'$end)
  60.             ->setParameter('enabled'true)
  61.             ->addGroupBy('o.id')
  62.             ;
  63.             if($filters && $filters['professional']){
  64.                 $query->andWhere('p.id = :professional')
  65.                     ->setParameter(':professional'$filters['professional']);
  66.             }
  67.             if($filters && $filters['room']){
  68.                 $query->andWhere('pr.id = :room')
  69.                     ->setParameter(':room'$filters['room']);
  70.             }
  71.             if($filters && $filters['payStatus']){
  72.                 if($filters['payStatus'] == 'NULL'){
  73.                     $query->andWhere('pay IS NULL');
  74.                 }else{
  75.                     $query->andWhere('pay.state = :payStatus')
  76.                         ->setParameter(':payStatus'$filters['payStatus']);                    
  77.                 }
  78.             }
  79.             if($filters && $filters['turnStatus']){
  80.                 $query->andWhere('o.turnStatus = :turnStatus')
  81.                     ->setParameter(':turnStatus'$filters['turnStatus']);
  82.             }else{
  83.                 // filtra por defecto los cancelados y los a reprogramar    
  84.                 $query->andWhere('o.turnStatus != :notCanceled')
  85.                 ->andWhere('o.turnStatus != :notReschedule')
  86.                 ->setParameter('notCanceled''STATUS_CANCELLED')            
  87.                 ->setParameter('notReschedule''STATUS_RESCHEDULE');
  88.             }
  89.             if($filters && $filters['finalStatus']){
  90.                 $query->andWhere('o.finalStatus = :finalStatus')
  91.                     ->setParameter(':finalStatus'$filters['finalStatus']);
  92.             }
  93.             if($filters && $filters['id']){
  94.                 $query->andWhere('o.id LIKE :id')
  95.                     ->setParameter(':id''%' .$filters['id']. '%');
  96.             }
  97.             if($filters && $filters['taxon']){
  98.                 $query->andWhere('taxont.name LIKE :taxon')
  99.                     ->setParameter(':taxon''%' .$filters['taxon']. '%');
  100.             }
  101.             if($filters && $filters['customer']){
  102.                 $searchArray explode(' '$filters['customer']);
  103.                 if($searchArray && count($searchArray)){
  104.                     foreach ($searchArray as $key => $subSearch) {
  105.                         $query $query->andWhere("CONCAT('T',o.id) LIKE :search".$key." OR (CONCAT(c.lastName, ' ', c.firstName) LIKE :search".$key." OR CONCAT(CASE WHEN o.lastname IS NULL THEN '' ELSE o.lastname END, ' ', o.name) LIKE :search"$key .")")
  106.                         ->setParameter(':search'.$key'%' $subSearch '%')
  107.                         ;
  108.                     }
  109.                 }
  110.             }
  111.             $query $query->addOrderBy("o.start""DESC")->getQuery()->getResult();
  112.         return (array) $query;
  113.     }    
  114.     public function updateEventEditable(int $id\DateTime $start\DateTime $end)
  115.     {
  116.         $query $this->createQueryBuilder('o')
  117.             ->update()
  118.             ->set('o.start'':start')
  119.             ->setParameter(':start'$start)
  120.             ->set('o.end'':end')
  121.             ->setParameter(':end'$end)
  122.             ->where('o.id = :id')
  123.             ->setParameter('id'$id)
  124.             ->getQuery()
  125.             ->execute()
  126.             ;
  127.     }
  128.     public function createListQueryBuilder($sorting null): QueryBuilder
  129.     {
  130.         $qb $this->createQueryBuilder('o')
  131.             ->addSelect('o.id as id, CASE WHEN o.customer IS NULL THEN CONCAT(CASE WHEN o.lastname IS NULL THEN \'\' ELSE CONCAT(o.lastname, \' \') END, o.name) ELSE CONCAT(c.lastName, \' \', c.firstName) END as name, r.name as room, CONCAT(p.lastname, \' \', p.name) as professional, o.start as start, o.duration as duration, o.turnStatus as turnStatus, o.finalStatus as finalStatus, 
  132.                 CASE WHEN pc.id IS NULL THEN o.payStatus ELSE pay.state END as payState, 
  133.                 c.id as customerId, pc.id as payCustomerId, o.reschedule, o.enabled as enabled')
  134.             ->leftJoin('o.customer''c')
  135.             ->leftJoin('o.room''r')
  136.             ->leftJoin('o.professional''p')
  137.             ->leftJoin('o.payCustomer''pc')
  138.             ->leftJoin('o.turnTaxons''tt')
  139.             ->leftJoin('tt.taxon''taxon')
  140.             ->leftJoin('taxon.translations''taxont')
  141.             ->leftJoin('pc.pay''pay')
  142.         ;
  143.         if($sorting && is_array($sorting) && array_key_exists('name'$sorting)){
  144.             $qb->orderBy('name'$sorting['name']);
  145.         }
  146.         return  $qb;
  147.     }
  148.     public function getWithMainTaxon(): array
  149.     {
  150.         $qb $this->createQueryBuilder('o')
  151.             ->andWhere('o.mainTaxon IS NOT NULL')
  152.             ->addOrderBy('o.createdAt''DESC')
  153.         ;
  154.         return  $qb->getQuery()->getResult();
  155.     }
  156.     public function findLatest(int $count): array
  157.     {
  158.         return $this->createQueryBuilder('o')
  159.             ->addOrderBy('o.createdAt''DESC')
  160.             ->setMaxResults($count)
  161.             ->getQuery()
  162.             ->getResult()
  163.         ;
  164.     }
  165.     public function findNexts(int $count): array
  166.     {
  167.         return $this->createQueryBuilder('o')
  168.             ->addOrderBy('o.createdAt''ASC')
  169.             ->andWhere('o.start >= :start')
  170.             ->setParameter('start', new \DateTime())
  171.             ->setMaxResults($count)
  172.             ->getQuery()
  173.             ->getResult()
  174.         ;
  175.     }
  176. }