src/Repository/OccupationPerHourRepository.php line 45
<?php
namespace App\Repository;
use App\Entity\GroupAncestorDetails;
use App\Entity\OccupationPerHour;
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_ancestor_id in ($localisation))";
}
if ($resourceType) {
$resourcesReq .= " and r.resource_id in (SELECT DISTINCT id as resource FROM `resource` where group_ancestor_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_ancestor_id in ($localisation))";
}
if ($resourceType) {
$resourcesReq .= " and r.resource_id in (SELECT DISTINCT id as resource FROM `resource` where group_ancestor_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')
->leftJoin('oph.reservation','r')
->leftJoin('r.resource','res')
->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 != "resources") {
$qb->addSelect('count(distinct r.id) as value');
}
if ($filter == "resourceBySite") {
$qb->groupBy('item');
$qb->addSelect('ga.groupName as item');
$qb->leftJoin("res.groupParent", "ga");
$qb->andWhere('ga.groupTypeName=:groupTypeName')
->setParameter('groupTypeName','Localisation');
}elseif ($filter == "resourceByStage") {
$qb->addSelect('ga.groupName as item');
$qb->addSelect("st.id as status");
$qb->groupBy('item');
$qb->addGroupBy('st.id');
$qb->join(Status::class,'st',Join::WITH,'r.status=st.id');
$qb->leftJoin("res.groupAncestor", "ga")
->andWhere('ga.groupLevelId=4');
$qb->andWhere('ga.groupTypeName=:groupTypeName')
->setParameter('groupTypeName','Localisation');
}elseif ($filter == "resourceByZone") {
$qb->groupBy('item');
$qb->addSelect('ga.groupName as item');
$qb->leftJoin("res.groupAncestor", "ga")
->andWhere('ga.groupLevelId=5');
$qb->andWhere('ga.groupTypeName=:groupTypeName')
->setParameter('groupTypeName','Localisation');
}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("res.groupAncestor", "ga")
->leftJoin(GroupAncestorDetails::class,'ga_building',Join::WITH,'ga_building.od=ga.groupAncestor')
->andWhere('ga_building.groupLevelId=2');
$qb->andWhere('ga_building.groupTypeName=:groupTypeName')
->setParameter('groupTypeName','Localisation');
}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("res.groupAncestor", "ga")
->leftJoin(GroupAncestorDetails::class,'ga_building',Join::WITH,'ga_building.od=ga.groupAncestor')
->andWhere('ga_building.groupLevelId=3');
$qb->andWhere('ga_building.groupTypeName=:groupTypeName')
->setParameter('groupTypeName','Localisation');
}elseif ($filter == "hasNotChildren") {
$qb->groupBy('item');
$qb->addSelect('ga.groupName as item');
$qb->leftJoin(GroupAncestorDetails::class,'ga',Join::WITH,'ga.id=res.groupParentId');
$qb->leftJoin(GroupAncestorDetails::class,'ga_sub_category',Join::WITH,'ga.id=ga.groupAncestor');
$qb->andWhere('ga.groupTypeName=:groupTypeName and ga_sub_category.id is null')
->setParameter('groupTypeName','Type');
}elseif ($filter == "resourceByType") {
$qb->groupBy('item');
$qb->addSelect('ga.groupName as item');
$qb->leftJoin("res.groupAncestor", "ga");
$qb->leftJoin("res.groupParent", "ga_parent");
$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");
}elseif ($filter == "resources") {
$qb->addSelect("st.id as status");
$qb->join(Status::class,'st',Join::WITH,'r.status=st.id');
$qb->leftJoin("res.groupAncestor", "ga");
$qb->andWhere("ga.groupTypeName = 'Localisation'");
$qb->leftJoin("res.groupParent", "gaParent");
$qb->addSelect("concat(gaParent.groupName,' - ',res.Name) as item, count(distinct r.id) as value");
$qb->groupBy('res.id');
$qb->addGroupBy('st.id');
}
if ($resource) {
$qb->andWhere('r.resource=:resource_id')
->setParameter('resource_id',$resource);
}else{
$qb
->andWhere('r.resource in (SELECT DISTINCT rc.id FROM App\Entity\Resource rc where rc.groupAncestor in (:localisations))')
->andWhere('r.resource in (SELECT DISTINCT rc1.id FROM App\Entity\Resource rc1 where rc1.groupAncestor 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 synchroUpdateOph($data)
{
$conn = $this->getEntityManager()->getConnection();
// $reservationIds = join(',',array_unique(array_column($data,'reservation_id')));
// $stmt = $conn->prepare("delete from occupation_per_hour where reservation_id in ($reservationIds)")->executeQuery();
foreach ($data as $item) {
$sql = "INSERT INTO occupation_per_hour
(hour, date, percentage, real_percentage, reservation_id)
VALUES
(:hour, :date, :percentage, :real_percentage, :reservation_id)";
$stmt = $conn->prepare($sql);
$stmt->bindValue(':hour',$item['hour']);
$stmt->bindValue(':date',$item['date']);
$stmt->bindValue(':percentage',$item['percentage']);
$stmt->bindValue(':real_percentage',$item['realPercentage']);
$stmt->bindValue(':reservation_id',$item['reservation_id']);
$stmt->executeQuery();
}
return true;
}
// /**
// * @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()
// ;
// }
}