src/Repository/OccupationPerHourRepository.php line 46
<?php
namespace App\Repository;
use App\Entity\GroupAncestorDetails;
use App\Entity\OccupationPerHour;
use App\Entity\Origin;
use App\Entity\Resource;
use App\Entity\Status;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<OccupationPerHour>
*
* @method OccupationPerHour|null find($id, $lockMode = null, $lockVersion = null)
* @method OccupationPerHour|null findOneBy(array $criteria, array $orderBy = null)
* @method OccupationPerHour[] findAll()
* @method OccupationPerHour[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class OccupationPerHourRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, OccupationPerHour::class);
}
public function save(OccupationPerHour $entity, bool $flush = false): void
{
$this->getEntityManager()->persist($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function remove(OccupationPerHour $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
public function findCreneau($resource = null,$resourceType = null,$localisation = null, $startDate = null,$endDate = null,$user = null,$status): array
{
$conn = $this->getEntityManager()->getConnection();
$reqDate = "";
if ($startDate && $endDate){
$reqDate = " where date between '$startDate' and '$endDate 23:59:59'";
}elseif ($startDate){
$reqDate = " where date >= $startDate";
}elseif ($endDate){
$reqDate = " where date <= '$endDate 23:59:59'";
}
$reqUser = $user ? " and r.user_id = $user" : "";
$resourcesReq = "";
if ($resource) {
$resourcesReq = " and r.resource_id = $resource";
}else{
if ($localisation) {
$resourcesReq = " and r.resource_id in (SELECT DISTINCT id as resource FROM `resource` where group_id in ($localisation))";
}
if ($resourceType) {
$resourcesReq .= " and r.resource_id in (SELECT DISTINCT id as resource FROM `resource` where group_id in ($resourceType))";
}
}
$sql = "SELECT hour,date, sum(percentage) as 'value', sum(real_percentage) as 'value_reel'
FROM `occupation_per_hour` o
left join reservation r on o.reservation_id = r.id $reqDate $reqUser $resourcesReq and r.status_id in ($status) group by hour,date";
$stmt = $conn->prepare($sql);
$resultSet = $stmt->executeQuery();
// returns an array of arrays (i.e. a raw data set)
return $resultSet->fetchAllAssociative();
}
public function findReservationByDay($resource = null,$resourceType = null,$localisation = null, $startDate = null,$endDate = null,$user = null,$status): array
{
$conn = $this->getEntityManager()->getConnection();
$reqDate = "";
if ($startDate && $endDate){
$reqDate = " where date between '$startDate' and '$endDate 23:59:59'";
}elseif ($startDate){
$reqDate = " where date >= $startDate";
}elseif ($endDate){
$reqDate = " where date <= '$endDate 23:59:59'";
}
$reqUser = $user ? " and r.user_id = $user" : "";
$resourcesReq = "";
if ($resource) {
$resourcesReq = " and r.resource_id = $resource";
}else{
if ($localisation) {
$resourcesReq = " and r.resource_id in (SELECT DISTINCT id as resource FROM `resource` where group_id in ($localisation))";
}
if ($resourceType) {
$resourcesReq .= " and r.resource_id in (SELECT DISTINCT id as resource FROM `resource` where group_id in ($resourceType))";
}
}
$sql = "SELECT o.date,count(DISTINCT reservation_id) as value FROM `occupation_per_hour` o
left join reservation r on o.reservation_id = r.id $reqDate $reqUser $resourcesReq and r.status_id in ($status) group by date";
$stmt = $conn->prepare($sql);
$resultSet = $stmt->executeQuery();
// returns an array of arrays (i.e. a raw data set)
return $resultSet->fetchAllAssociative();
}
public function findByFilter($localisation, $user, $startDate, $endDate,$status, $lang,$resource = null,$resourceType = null, $filter = null, $limit = null){
$status = explode(',',$status);
$qb=$this->createQueryBuilder('oph')
->select('count(distinct r.id) as value')
->leftJoin('oph.reservation','r')
->leftJoin(Resource::class,'res',Join::WITH,'res.id=r.resourceId')
->andWhere('r.status in (:status)')
->andWhere('oph.date between :startDate and :endDate')
->setParameter('startDate',$startDate)
->setParameter('endDate',$endDate)
->setParameter('status',$status)
->orderBy('value','DESC');
if ($filter == "resourceBySite") {
$qb->groupBy('item');
$qb->addSelect('ga.groupName as item');
$qb->leftJoin(GroupAncestorDetails::class,'ga',Join::WITH,'ga.ancestorGroupId=res.groupParentId');
$qb->andWhere('ga.groupTypeName=:groupTypeName')
->setParameter('groupTypeName','Place');
}elseif ($filter == "resourceByStage") {
$qb->groupBy('item');
$qb->addSelect('ga.groupName as item');
$qb->leftJoin(GroupAncestorDetails::class,'ga',Join::WITH,'ga.ancestorGroupId=res.groupId')
->andWhere('ga.groupLevelId=4');
$qb->andWhere('ga.groupTypeName=:groupTypeName')
->setParameter('groupTypeName','Place');
}elseif ($filter == "resourceByZone") {
$qb->groupBy('item');
$qb->addSelect('ga.groupName as item');
$qb->leftJoin(GroupAncestorDetails::class,'ga',Join::WITH,'ga.ancestorGroupId=res.groupId')
->andWhere('ga.groupLevelId=5');
$qb->andWhere('ga.groupTypeName=:groupTypeName')
->setParameter('groupTypeName','Place');
}elseif ($filter == "resourceByBuilding") {
$qb->groupBy('item');
$qb->addSelect('ga.groupName as item');
$qb->select('ga_building.groupName as item', 'count(distinct r.id) as value')
->leftJoin(GroupAncestorDetails::class,'ga',Join::WITH,'ga.ancestorGroupId=res.groupId')
->leftJoin(GroupAncestorDetails::class,'ga_building',Join::WITH,'ga_building.ancestorGroupId=ga.groupId')
->andWhere('ga_building.groupLevelId=2');
$qb->andWhere('ga_building.groupTypeName=:groupTypeName')
->setParameter('groupTypeName','Place');
}elseif ($filter == "resourceByWing") {
$qb->groupBy('item');
$qb->addSelect('ga.groupName as item');
$qb->select('ga_building.groupName as item', 'count(distinct r.id) as value')
->leftJoin(GroupAncestorDetails::class,'ga',Join::WITH,'ga.ancestorGroupId=res.groupId')
->leftJoin(GroupAncestorDetails::class,'ga_building',Join::WITH,'ga_building.ancestorGroupId=ga.groupId')
->andWhere('ga_building.groupLevelId=3');
$qb->andWhere('ga_building.groupTypeName=:groupTypeName')
->setParameter('groupTypeName','Place');
}elseif ($filter == "hasNotChildren") {
$qb->groupBy('item');
$qb->addSelect('ga.groupName as item');
$qb->leftJoin(GroupAncestorDetails::class,'ga',Join::WITH,'ga.ancestorGroupId=res.groupParentId');
$qb->leftJoin(GroupAncestorDetails::class,'ga_sub_category',Join::WITH,'ga.ancestorGroupId=ga.groupId');
$qb->andWhere('ga.groupTypeName=:groupTypeName and ga_sub_category.ancestorGroupId is null')
->setParameter('groupTypeName','Type');
}elseif ($filter == "resourceByType") {
$qb->groupBy('item');
$qb->addSelect('ga.groupName as item');
$qb->leftJoin(GroupAncestorDetails::class,'ga',Join::WITH,'ga.ancestorGroupId=res.groupId');
$qb->leftJoin(GroupAncestorDetails::class,'ga_parent',Join::WITH,'ga_parent.ancestorGroupId=res.groupParentId');
$qb->andWhere('ga.groupTypeName=:groupTypeName')
->andWhere('ga_parent.groupTypeName=:groupTypeName')
->setParameter('groupTypeName','Type');
$qb->addSelect('ga_parent.groupName as site');
}elseif ($filter == "status") {
$qb->groupBy('item');
$qb->addSelect("case
when r.status = -1 then 'Refused'
when r.status = 0 then 'Cancelled'
when r.status = 1 then 'Pending'
else 'Confirmed' end as item");
}elseif ($filter == "origin") {
$qb->groupBy('item');
$qb->addSelect("case
when r.origin = 1 then 'App'
else '' end as item");
}elseif ($filter == "immediat_use") {
$qb->groupBy('item');
$qb->addSelect("case when r.immediatUse = 1 then 'Immediate use' else 'Deferred' end as item");
}
if ($resource) {
$qb->andWhere('r.resourceId=:resource_id')
->setParameter('resource_id',$resource);
}else{
$qb
->andWhere('r.resourceId in (SELECT DISTINCT rc.id FROM App\Entity\Resource rc where rc.groupId in (:localisations))')
->andWhere('r.resourceId in (SELECT DISTINCT rc1.id FROM App\Entity\Resource rc1 where rc1.groupId in (:resourceTypes))')
->setParameter('localisations',explode(',',$localisation))
->setParameter('resourceTypes',explode(',',$resourceType));
}
if($user){
$qb->andWhere('r.userId=:user_id')->setParameter('user_id',$user);
}
if ($limit) {
$qb->setMaxResults($limit);
}
return $qb->getQuery()->getResult();
}
public function totalReservations($resource = null,$resourceType = null,$localisation, $user, $startDate, $endDate,$status, $filter = null, $limit = null) {
$qb=$this->createQueryBuilder('oph')
->select('count(distinct r.id) as value')
->leftJoin('oph.reservation','r')
->andWhere('r.status in (:status)')
->andWhere('oph.date between :startDate and :endDate')
->andWhere('r.resourceId in (SELECT DISTINCT rc.id FROM App\Entity\Resource rc where rc.groupId in (:localisations))')
->andWhere('r.resourceId in (SELECT DISTINCT rc1.id FROM App\Entity\Resource rc1 where rc1.groupId in (:resourceTypes))')
->setParameter('startDate',$startDate)
->setParameter('endDate',$endDate)
->setParameter('status',explode(',', $status))
->setParameter('localisations',explode(',',$localisation))
->setParameter('resourceTypes',explode(',',$resourceType))
->orderBy('value','DESC');
return $qb->getQuery()->getResult();
}
// /**
// * @return OccupationPerHour[] Returns an array of OccupationPerHour objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('o')
// ->andWhere('o.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('o.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?OccupationPerHour
// {
// return $this->createQueryBuilder('o')
// ->andWhere('o.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}