src/Repository/EnterpriseRepository.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Contact;
  4. use App\Entity\Cotisation;
  5. use App\Entity\CotisationStatut;
  6. use App\Entity\Departement;
  7. use App\Entity\EntityStatus;
  8. use App\Entity\Entreprise;
  9. use App\Entity\Etablissement;
  10. use App\Entity\Event;
  11. use App\Entity\FirmType;
  12. use App\Entity\Region;
  13. use App\Entity\Syndicat;
  14. use App\Entity\User;
  15. use App\Model\Search\AdvancedSearch;
  16. use DateTime;
  17. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  18. use Doctrine\ORM\Query;
  19. use Doctrine\ORM\Query\Expr\Join;
  20. use Doctrine\Persistence\ManagerRegistry;
  21. /**
  22.  * @extends ServiceEntityRepository<Event>
  23.  *
  24.  * @method Entreprise|null find($id, $lockMode = null, $lockVersion = null)
  25.  * @method Entreprise|null findOneBy(array $criteria, array $orderBy = null)
  26.  * @method Entreprise[]    findAll()
  27.  * @method Entreprise[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  28.  */
  29. class EnterpriseRepository extends BaseRepository
  30. {
  31.     public function __construct(
  32.         ManagerRegistry $registry,
  33.         private RegionRepository $regionRepository
  34.     )
  35.     {
  36.         parent::__construct($registryEntreprise::class);
  37.     }
  38.     public function save(Entreprise $entitybool $flush false): void
  39.     {
  40.         $this->getEntityManager()->persist($entity);
  41.         if ($flush) {
  42.             $this->getEntityManager()->flush();
  43.         }
  44.     }
  45.     public function remove(Entreprise $entitybool $flush false): void
  46.     {
  47.         $this->getEntityManager()->remove($entity);
  48.         if ($flush) {
  49.             $this->getEntityManager()->flush();
  50.         }
  51.     }
  52.     public function countMemberPerRegion(int $yearint $regionId)
  53.     {
  54.         /*$cotisationLimit = new DateTime($year.'-07-01');
  55.         $now = new DateTime();
  56.         if ($now <= $cotisationLimit) {
  57.             $year = $year - 1;
  58.         }*/
  59.         return $this
  60.             ->createQueryBuilder('e')
  61.             ->select('COUNT(e.id)')
  62.             ->innerJoin(Cotisation::class, 'c'Join::WITH'c.entreprise = e.id')
  63.             ->innerJoin(CotisationStatut::class, 'cs'Join::WITH'cs.id = c.cotisationStatut')
  64.             ->innerJoin(Departement::class, 'd'Join::WITH'd.id = e.departement')
  65.             ->innerJoin(Region::class, 'r'Join::WITH'r.id = d.region AND r.id = :regionId')
  66.             ->setParameter('regionId'$regionId)
  67.             ->andWhere('c.year = :year')
  68.             ->setParameter('year'$year)
  69.             ->andWhere('cs.id = 1')
  70.             ->addGroupBy('r.id')
  71.             ->getQuery()
  72.             ->getOneOrNullResult()
  73.         ;
  74.     }
  75.     public function countPerRegion()
  76.     {
  77.         $regions $this->regionRepository->findAll();
  78.         $entreprisesPerRegion = [];
  79.         $totalRegions \count($regions) + 1;
  80.         for ($regionId 1$regionId $totalRegions$regionId++) {
  81.             $entreprisesPerRegion[$regions[$regionId 1]->getId()] = $this
  82.                 ->createQueryBuilder('e')
  83.                 ->leftJoin('e.departement''d')
  84.                 ->leftJoin('d.regions''r')
  85.                 ->innerJoin(Cotisation::class, 'c'Join::WITH'c.entreprise = e.id')
  86.                 ->innerJoin(CotisationStatut::class, 'cs'Join::WITH'cs.id = c.cotisationStatut')
  87.                 ->select('count(e.id) as value, r.nom')
  88.                 ->andWhere('r.id = :regionId')
  89.                 ->setParameter('regionId'$regionId)
  90.                 ->andWhere('cs.id = 1')
  91.                 ->getQuery()
  92.                 ->getScalarResult();
  93.         }
  94.         return $entreprisesPerRegion;
  95.     }
  96.     public function countFirmByType()
  97.     {
  98.         return $this
  99.             ->createQueryBuilder('e')
  100.             ->innerJoin(FirmType::class, 'ft'Join::WITH'ft.id = e.firmType')
  101.             ->select('COUNT(e.id) AS countFirm, ft.name')
  102.             ->andWhere('e.status != :status')
  103.             ->setParameter('status'EntityStatus::STATUS_ARCHIVED)
  104.             ->groupBy('e.firmType')
  105.             ->getQuery()
  106.             ->getScalarResult()
  107.         ;
  108.     }
  109.     public function retrieveAllData(
  110.         ?AdvancedSearch $advancedSearch,
  111.         ?bool $forEmailSending false,
  112.         ?bool $forSmsSending false,
  113.         ?bool $forDisplayArchived false,
  114.         array $params = [],
  115.         array $selection = [],
  116.         ?User $user null
  117.     ): Query
  118.     {
  119.         $searchCriteriasCollection is_null($advancedSearch) ? [] : $advancedSearch->getSearchCriteria();
  120.         $joinContact false;
  121.         $joinEtablissement false;
  122.         $joinSyndicat false;
  123.         $joinCotisation false;
  124.         $query $this->createQueryBuilder('e');
  125.         $whereParts = [];
  126.         foreach ($searchCriteriasCollection as $key => $criteria) {
  127.             $entity $criteria->getEntity();
  128.             $field $criteria->getField();
  129.             $term $criteria->getTerm();
  130.             $currentAlias null;
  131.             switch ($entity) {
  132.                 case 'entreprise':
  133.                     $currentAlias 'e';
  134.                     break;
  135.                 case 'contact':
  136.                     if (false === $joinContact) {
  137.                         $query->innerJoin(Contact::class, 'c'Join::WITH'c.firm = e.id');
  138.                         $joinContact true;
  139.                     }
  140.                     $currentAlias 'c';
  141.                     break;
  142.                 case 'etablissements':
  143.                     if (false === $joinEtablissement) {
  144.                         $query->innerJoin(Etablissement::class, 'et'Join::WITH'et.entreprise = e.id');
  145.                         $joinEtablissement true;
  146.                     }
  147.                     $currentAlias 'et';
  148.                     break;
  149.                 case 'syndicat':
  150.                     if (false === $joinSyndicat) {
  151.                         $query->innerJoin(Syndicat::class, 's'Join::WITH's.id = e.syndicat');
  152.                         $joinSyndicat true;
  153.                     }
  154.                     $currentAlias 's';
  155.                     break;
  156.                 case 'cotisation':
  157.                     if (false === $joinCotisation) {
  158.                         $query->innerJoin(Cotisation::class, 'co'Join::WITH'co.entreprise = e.id');
  159.                         $joinCotisation true;
  160.                     }
  161.                     $currentAlias 'co';
  162.                     break;
  163.             }
  164.             $whereParts $this->buildWhereParts(
  165.                 $params,
  166.                 $entity,
  167.                 $field,
  168.                 $term,
  169.                 $key,
  170.                 $currentAlias,
  171.                 array_key_exists($currentAlias.'.'.$field$whereParts),
  172.                 $whereParts
  173.             );
  174.         }
  175.         $query $this->buildWhereQuery($query$whereParts);
  176.         if (false === $forDisplayArchived) {
  177.             $query
  178.                 ->andWhere('e.status != :status')
  179.                 ->setParameter('status'EntityStatus::STATUS_ARCHIVED)
  180.             ;
  181.         }
  182.         if (in_array('ROLE_REGIONAL_AGENT'$user->getRoles())) {
  183.             $query
  184.                 ->innerJoin(Departement::class, 'd'Join::WITH'd.id = e.departement')
  185.                 ->innerJoin(Region::class, 'r'Join::WITH'r.id = d.region')
  186.                 ->andWhere('r.id = :user_region')
  187.                 ->setParameter('user_region'$user->getRegion())
  188.             ;
  189.         }
  190.         if (in_array('ROLE_UNION_PRESIDENT'$user->getRoles())) {
  191.             $query
  192.                 ->andWhere('e.syndicat = :user_syndicat')
  193.                 ->setParameter('user_syndicat'$user->getSyndicat())
  194.             ;
  195.         }
  196.         if (in_array('ROLE_BOARD_DIRECTOR'$user->getRoles())) {
  197.             $query
  198.                 ->andWhere('e.commission = :user_commission')
  199.                 ->setParameter('user_commission'$user->getCommission())
  200.             ;
  201.         }
  202.         return $query->getQuery();
  203.     }
  204.     public function findMemberOnPhoneBookQuery(?string $search null): Query
  205.     {
  206.         $query $this
  207.             ->createQueryBuilder('e')
  208.             ->leftJoin(Contact::class, 'c'Join::WITH'c.firm = e.id AND c.isResponsible = 1')
  209.             ->andWhere('e.status = :status')
  210.             ->setParameter('status'Entreprise::STATUS_ENABLED)
  211.             ->andWhere('e.figurerAnnuaireAdherents = 1')
  212.         ;
  213.         if (!is_null($search)) {
  214.             $query->andWhere(
  215.                 $query->expr()->orX(
  216.                     $query->expr()->like('e.raisonSocial'':search_1'),
  217.                     $query->expr()->like('e.tel1'':search_2'),
  218.                     $query->expr()->like('e.email'':search_3'),
  219.                     $query->expr()->like('e.adresse'':search_4'),
  220.                     $query->expr()->like('e.adresse2'':search_5'),
  221.                     $query->expr()->like('e.codePostal'':search_6'),
  222.                     $query->expr()->like('e.ville'':search_7'),
  223.                     $query->expr()->like('c.nom'':search_8'),
  224.                     $query->expr()->like('c.prenom'':search_9'),
  225.                     $query->expr()->like('c.telPortable'':search_10'),
  226.                     $query->expr()->like('c.emailContact'':search_11')
  227.                 )
  228.             )
  229.             ->setParameter('search_1''%'.$search.'%')
  230.             ->setParameter('search_2''%'.$search.'%')
  231.             ->setParameter('search_3''%'.$search.'%')
  232.             ->setParameter('search_4''%'.$search.'%')
  233.             ->setParameter('search_5''%'.$search.'%')
  234.             ->setParameter('search_6''%'.$search.'%')
  235.             ->setParameter('search_7''%'.$search.'%')
  236.             ->setParameter('search_8''%'.$search.'%')
  237.             ->setParameter('search_9''%'.$search.'%')
  238.             ->setParameter('search_10''%'.$search.'%')
  239.             ->setParameter('search_11''%'.$search.'%')
  240.             ;
  241.         }
  242.         return $query->getQuery();
  243.     }
  244.     public function getEnterprisesByCodeAdherents($codeAdherentsArray)
  245.     {
  246.         return $this->createQueryBuilder('a')
  247.             ->andWhere('a.ancienIdentifiant IN (:entreprises)')
  248.             ->setParameter('entreprises'$codeAdherentsArray)
  249.             ->getQuery()->getResult();
  250.     }
  251.     public function findMemberPerRegion(Region $regionint|string $year) {
  252.         return $this
  253.             ->createQueryBuilder('e')
  254.             ->innerJoin(Cotisation::class, 'c'Join::WITH'c.entreprise = e.id')
  255.             ->innerJoin(Departement::class, 'd'Join::WITH'd.id = e.departement')
  256.             ->innerJoin(Region::class, 'r'Join::WITH'r.id = d.region')
  257.             ->andWhere('e.isMember = 1')
  258.             ->andWhere('r.id = :region')
  259.             ->setParameter('region'$region)
  260.             ->andWhere('c.year = :year')
  261.             ->setParameter('year'$year)
  262.             ->getQuery()
  263.             ->getResult()
  264.         ;
  265.     }
  266. }