src/Repository/OrganizationRepository.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Animal;
  4. use App\Entity\Event;
  5. use App\Entity\EventData;
  6. use App\Entity\Organization;
  7. use App\Entity\Producer;
  8. use App\Entity\Treatment;
  9. use App\Entity\User;
  10. use App\Entity\UserType;
  11. use App\Repository\AnimalRepository;
  12. use App\Repository\Common\FilterBuilder;
  13. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use \Doctrine\ORM\QueryBuilder;
  16. use Doctrine\Persistence\ManagerRegistry;
  17. use Psr\Container\ContainerInterface;
  18. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  19. use Symfony\Component\Security\Core\Security;
  20. use Symfony\Contracts\Translation\TranslatorInterface;
  21. /**
  22.  * @method Organization|null find($id, $lockMode = null, $lockVersion = null)
  23.  * @method Organization|null findOneBy(array $criteria, array $orderBy = null)
  24.  * @method Organization[]    findAll()
  25.  * @method Organization[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  26.  */
  27. class OrganizationRepository extends ServiceEntityRepository
  28. {
  29.     use FilterBuilder;
  30.     private $security;
  31.     private $tokenStorage;
  32.     private EventDataRepository|\Doctrine\ORM\EntityRepository $eventDataRepository;
  33.     private EntityManagerInterface $em;
  34.     private \App\Repository\AnimalRepository|\Doctrine\ORM\EntityRepository $animalRepository;
  35.     private UserRepository|\Doctrine\ORM\EntityRepository $userRepository;
  36.     private TreatmentRepository|\Doctrine\ORM\EntityRepository $treatmentRepository;
  37.     public function __construct(
  38.         ManagerRegistry $registry,
  39.         EntityManagerInterface $em,
  40.         Security $security,
  41.         TokenStorageInterface $tokenStorage
  42.     ) {
  43.         parent::__construct($registryOrganization::class);
  44.         $this->em $em;
  45.         $this->eventDataRepository $this->em->getRepository(EventData::class);
  46.         $this->animalRepository $this->em->getRepository(Animal::class);
  47.         $this->userRepository $this->em->getRepository(User::class);
  48.         $this->treatmentRepository $this->em->getRepository(Treatment::class);
  49.         $this->security $security;
  50.         $this->tokenStorage $tokenStorage;
  51.     }
  52.     public function addAnimal$qb$aId null )
  53.     {/*{{{*/
  54.         $qb->leftJoin('App\Entity\Animal''a'"with""o.id = a.organization");
  55.         $qb->andWhere('(a.deleted = 0) OR a.id IS NULL');
  56.         $aId and $qb->andWhere("a.id = :aId")->setParameter'aId'$aId );
  57.         return $qb;
  58.     }/*}}}*/
  59.     public function addOrganization$qbint $org null 
  60.     {/*{{{*/
  61.         $org and $qb->andWhere('o.id = :org')->setParameter('org'$org);
  62.         return $qb;
  63.     }/*}}}*/
  64.     public function countBlackList$qb$to_date null 
  65.     {/*{{{*/
  66.         $to_date $to_date ?? new \DateTime();
  67.         return $qb->addSelect('SUM( if ( a.lackPeriod > :date, 1, 0) ) AS blackList')->setParameter':date'$to_date );
  68.     }/*}}}*/
  69.     public function addActiveAnimals$qb 
  70.     {/*{{{*/
  71.         return $qb->andWhere('(a.status = :statusActive) OR a.id is NULL ')->setParameter('statusActive'Animal::STATUS_ACTIVE);
  72.     }/*}}}*/
  73.     public function addCurrentLot$qbLot $lot null 
  74.     {/*{{{*/
  75.         $qb->leftJoin('a.currentLot''l'"with""a.currentLot = l.id")
  76.                 ->andWhere('(l.deleted = 0 and l.status = 1) or l.id is null');
  77.         $lot and $qb->andWhere("l.id = :lot")->setParameter'lot'$lot );
  78.         return $qb;
  79.     }/*}}}*/
  80.     public function countAnimal$qb 
  81.     {/*{{{*/
  82.         return $qb->addSelect('(
  83.         SELECT COUNT(DISTINCT aC.id) 
  84.             FROM App\Entity\Animal aC
  85.             -- ahora queda animal -> lot -> farm -> organization 
  86.             -- que pertenezca al lote
  87.             INNER JOIN App\Entity\Lot lC WITH aC.currentLot = lC AND (aC.currentLot = lC.id) 
  88.             -- que pertenezca al establecimiento
  89.             INNER JOIN App\Entity\Farm fC WITH lC.farm = fC.id 
  90.             -- que pertenezca a la organizacion
  91.             INNER JOIN App\Entity\Organization oC WITH fC.organization = oC.id 
  92.             WHERE aC.deleted = 0 
  93.                 AND aC.status = :statusActive 
  94.                 AND (a.createdAt >= a.deletedAt OR a.updatedAt >= a.deletedAt OR a.deletedAt is NULL)
  95.                 AND oC.id = o.id 
  96.                 AND fC.deleted = false 
  97.                 AND lC.deleted = false 
  98.                 AND lC.status = true 
  99.             ) as countAnimal')
  100.             ->setParameter('statusActive'Animal::STATUS_ACTIVE);
  101.     }/*}}}*/
  102.     public function countSubSelect$qb$organizationId null 
  103.     {/*{{{*/
  104.     $this->countAnimal$qb );
  105.         $qb->addSelect('(
  106.             SELECT COUNT(t.id)
  107.             FROM App\Entity\Treatment t
  108.             JOIN App\Entity\Farm ft WITH ( t.farm = ft.id AND ft.deleted = 0 )
  109.             WHERE t.deleted = 0 AND ( ft.organization = o.id or o.id is null)
  110.             ) as countTreatment'
  111.         );
  112.         $qb->addSelect('(
  113.             SELECT COUNT(distinct f.id)
  114.             FROM App\Entity\Farm f
  115.             INNER JOIN App\Entity\Lot lf WITH ( lf.farm = f.id AND lf.deleted = 0 )
  116.             WHERE f.organization = o.id and f.deleted = 0 AND lf.status = 1
  117.             and f.createdAt is not NULL
  118.             ) as countFarm'
  119.         );
  120.         $qb->addSelect('(
  121.             SELECT COUNT(ed.id)
  122.             FROM App\Entity\EventData ed 
  123.             JOIN App\Entity\Lot led WITH ( ed.lot = led.id AND led.deleted = 0 )
  124.             JOIN App\Entity\Farm fed WITH ( led.farm = fed.id AND fed.deleted = 0 )
  125.             WHERE fed.organization = o.id and ed.deleted = 0 
  126.                AND fed.deleted = 0
  127.                AND led.deleted = 0
  128.                AND led.status = 1
  129.             ) as countEventData'
  130.         );
  131.         $qb->addSelect('(
  132.             SELECT COUNT(cl.id)
  133.             FROM App\Entity\Lot cl
  134.             JOIN App\Entity\Farm fc WITH ( cl.farm = fc.id AND fc.deleted = 0 )
  135.             WHERE fc.organization = o.id and cl.deleted = 0 
  136.                AND fc.deleted = 0
  137.                AND cl.deleted = 0
  138.                AND cl.status = 1
  139.             ) as countLot'
  140.         );
  141.         $qb->addSelect('(
  142.             SELECT COUNT(us.id)
  143.             FROM App\Entity\User us 
  144.             WHERE us.organization = o.id and us.enabled = 1 
  145.             ) as countUser'
  146.         );
  147.         return $qb;
  148.     }/*}}}*/ 
  149.     public function groupByOrganization$qb 
  150.     {/*{{{*/
  151.         return $qb->addGroupBy('o.id');
  152.     }/*}}}*/
  153.     /* main Querys */
  154.     public function getMainSelects$qb$organizationId ): QueryBuilder
  155.     {/*{{{*/
  156.         /* selects */
  157.         $qb->select("o.id as id");
  158.         $qb->addSelect("o.name as name");
  159.         $qb->addSelect("o.type as type");
  160.         $qb->addSelect("o.description as description");
  161.         $qb->addSelect("o.logo as logo");
  162.         //$qb->addSelect("o.users as users");
  163.         $qb->addSelect("o.created as created");
  164.         $qb->addSelect("o.cuit as cuit");
  165.         $qb->addSelect("o.email as email");
  166.         $qb->addSelect("o.phone as phone");
  167.         $qb->addSelect("o.businessName as businessName");
  168.         //$qb->addSelect("o.address as address");
  169.         $qb->addSelect("o.listType as listType");
  170.         $qb $this->countBlackList$qb );
  171.         $qb $this->countSubSelect$qb$organizationId );
  172.         
  173.         /* CUD timestamps */
  174.         $qb->addSelect('o.createdAt as createdAt');
  175.         $qb->addSelect('o.updatedAt as updatedAt');
  176.         $qb->addSelect('o.deletedAt as deletedAt');
  177.         return $qb;
  178.     }/*}}}*/
  179.     public function getMainJoins$qb$organizationId ): QueryBuilder
  180.     {/*{{{*/
  181.         $animalRepository $this->em->getRepository(Animal::class);
  182.         // dd( $animalRepository->getSubJoins( $animalRepository->createQueryBuilder('a'), $organizationId )->select('a.id')->getQuery()->getSQL() );
  183.         /* joins */
  184.         $qb $this->addOrganization$qb$organizationId );
  185.         $qb $this->addAnimal$qb );
  186.         if ( $organizationId 
  187.             $qb $this->groupByOrganization$qb );
  188.         /*
  189.         $qb->leftJoin( 
  190.             sprintf('(%s)', $animalRepository->getSubJoins( 
  191.                 $animalRepository->createQueryBuilder('a'), $organizationId )
  192.                 ->select('a.id')->getQuery()->getSQL()),
  193.             'aCount', 
  194.             'o.id = aCount.oId'    );
  195.          */
  196.         return $qb;
  197.     }/*}}}*/
  198.     public function getMainQuery$qb$organizationId ): QueryBuilder
  199.     {/*{{{*/
  200.         $qb $this->getMainSelects$qb$organizationId );
  201.         $qb $this->getMainJoins$qb$organizationId );
  202.         return $qb;
  203.     }/*}}}*/
  204.     public function getListQuery$organizationId ): QueryBuilder
  205.     {/*{{{*/
  206.         $qb $this->createQueryBuilder('o');
  207.         $qb $this->getMainQuery$qb$organizationId );
  208.         $qb $this->groupByOrganization$qb );
  209.         return $qb;
  210.     }/*}}}*/
  211.     public function getStatsQuery$organizationId ): QueryBuilder
  212.     {/*{{{*/
  213.         $qb $this->createQueryBuilder('o');
  214.         $qb $this->getMainQuery$qb$organizationId );
  215.         $qb->addSelect'count(o.id) as countOrganization');
  216.         /* es una vista con funciones sobre consultas JSON */
  217.         /* DEBUG: falta completar */
  218.         return $qb;
  219.     }/*}}}*/
  220.     public function entryHelp$organizationId null 
  221.     {/*{{{*/
  222.         $qb $this->createQueryBuilder('o');
  223.         $qb $this->addOrganization$qb$organizationId );
  224.         if ( $organizationId 
  225.             $qb $this->groupByOrganization$qb );
  226.             return $qb;
  227.     }/*}}}*/
  228.     /* de Logica Ligera Inc */
  229.     public function findOrganizationsByUser(User $user
  230.     {/*{{{*/
  231.         $qb $this->createQueryBuilder('o');
  232.         if ($this->security->isGranted("ROLE_MANAGER") && !$this->security->isGranted("ROLE_ADMIN")) {
  233.             $qb->join('App\\Entity\\User''u')
  234.                     ->where('u.organization = o.id')
  235.                     ->andWhere('u.organization = :organization')
  236.                     ->setParameter(':organization'$user->getOrganization());
  237.         } else if ($this->security->isGranted("ROLE_USER") && !$this->security->isGranted("ROLE_ADMIN")) {
  238.             $qb->join('App\\Entity\\User''u')
  239.                     ->where('u.organization = o.id')
  240.                     ->andWhere('u.id = :user')
  241.                     ->setParameter('user'$user);
  242.         }
  243.         $qb->orderBy('o.name''ASC');
  244.         return $qb->getQuery()->getResult();
  245.     }/*}}}*/
  246.     public function findActiveOrganizations($limit$offset): array
  247.     {/*{{{*/
  248.         return $this->createQueryBuilder('o')
  249.             ->join('App\Entity\EventData''ed'"with""ed.organization = o.id")
  250.             ->groupBy('o.id')
  251.             ->setMaxResults($limit)
  252.             ->setFirstResult($offset)
  253.             ->getQuery()
  254.             ->getResult();
  255.     }/*}}}*/
  256.     public function getTotalActiveOrganizations() : int
  257.     {/*{{{*/
  258.         return $this->createQueryBuilder('o')
  259.             ->select('COUNT(DISTINCT o.id)')
  260.             ->join('App\Entity\EventData''ed'"with""ed.organization = o.id")
  261.             ->getQuery()
  262.             ->getSingleScalarResult();
  263.     }/*}}}*/
  264.     /* Migrado de OrganizationService */
  265.     public function exist(Organization $organization)
  266.     {/*{{{*/
  267.         $name $organization->getName();
  268.         $orgName $this->findOneBy(array('name' => $name'description' => $organization->getDescription()));
  269.         if ($orgName != null) {
  270.             return $orgName;
  271.         }
  272.         return null;
  273.     }/*}}}*/
  274.     public function checkUserType(UserType $userType$user)
  275.     {/*{{{*/
  276.         if ($userType->getType() === 'PRODUCTOR') {
  277.             $producer = new Producer();
  278.             $producer->setUser($user);
  279.             $producer->setName($user->getUserName());
  280.             $producer->setBusinessName($user->getUserName());
  281.             $producer->setToUpdate(true);
  282.             $this->em->persist($producer);
  283.             $this->em->flush();
  284.         }
  285.     }/*}}}*/
  286.     public function create(Organization $organization)
  287.     {/*{{{*/
  288.         if (!$this->exist($organization)) {
  289.             $this->em->persist($organization);
  290.             $this->em->flush();
  291.         } else {
  292.             $organization $this->exist($organization);
  293.         }
  294.         return $organization;
  295.     }/*}}}*/
  296.     public function new ($username
  297.     {/*{{{*/
  298.         $organization = new Organization();
  299.         $organization->setName($username);
  300.         $organization->setDescription($username);
  301.         $organization->setType(Organization::TYPE_COMPANY);
  302.         return $organization;
  303.     }/*}}}*/
  304.     public function update(Organization $organization)
  305.     {/*{{{*/
  306.         $this->em->flush();
  307.         return $organization;
  308.     }/*}}}*/
  309. }