projectDir = $projectDir; $this->entityManager = $entityManager; $this->courtRepository = $courtRepository; $this->courtSessionRepository = $courtSessionRepository; $this->postcodeLatLngRepository = $postcodeLatLngRepository; } public function buildOverview(): array { $year = date('Y'); $month = date('m'); $today = date('Y-m-d'); $timeStart = '00:00:00'; $timeEnd = '23:59:59'; $from = $today . ' ' . $timeStart; $to = $today . ' ' . $timeEnd; //TODAY $courtSessionsToday = count($this->courtSessionRepository->findAllByHearingDate($from, $to)); $motoringRelatedToday = count($this->courtSessionRepository->findAllByHearingDate($from, $to, true)); $motoringRelatedTodayPCT = 0; if ($motoringRelatedToday > 0 && $courtSessionsToday > 0) { $motoringRelatedTodayPCT = ceil(($motoringRelatedToday / $courtSessionsToday) * 100); } //MONTH TO DATE $monthStart = $year . '-' . $month . '-01'; $monthEnd = $year . '-' . $month . '-31'; switch ($month) { case '02': if ($year % 4 == 0) { $monthEnd = $year . '-' . $month . '-29'; } else { $monthEnd = $year . '-' . $month . '-28'; } break; case '04': case '06': case '09': case '11': $monthEnd = $year . '-' . $month . '-30'; break; } $from = $monthStart . ' ' . $timeStart; $to = $monthEnd . ' ' . $timeEnd; $courtSessionsMonth = count($this->courtSessionRepository->findAllByHearingDate($from, $to)); $motoringRelatedMonth = count($this->courtSessionRepository->findAllByHearingDate($from, $to, true)); $motoringRelatedMonthPCT = 0; if ($motoringRelatedMonth > 0 && $courtSessionsMonth > 0) { $motoringRelatedMonthPCT = ceil(($motoringRelatedMonth / $courtSessionsMonth) * 100); } //LAST 30 DAYS $thirtyDayStart = date('Y-m-d', time() - (30 * 86400)); $thirtyDayEnd = $today; $from = $thirtyDayStart . ' ' . $timeStart; $to = $thirtyDayEnd . ' ' . $timeEnd; $courtSessionsThirtyDays = count($this->courtSessionRepository->findAllByHearingDate($from, $to)); $motoringRelatedThirtyDays = count($this->courtSessionRepository->findAllByHearingDate($from, $to, true)); $motoringRelatedThirtyDaysPCT = 0; if ($motoringRelatedThirtyDays > 0 && $courtSessionsThirtyDays > 0) { $motoringRelatedThirtyDaysPCT = ceil(($motoringRelatedThirtyDays / $courtSessionsThirtyDays) * 100); } //YEAR TO DATE $yearStart = $year . '-01-01'; $yearEnd = $year . '-12-31'; $from = $yearStart . ' ' . $timeStart; $to = $yearEnd . ' ' . $timeEnd; $courtSessionsYear = count($this->courtSessionRepository->findAllByHearingDate($from, $to)); $motoringRelatedYear = count($this->courtSessionRepository->findAllByHearingDate($from, $to, true)); $motoringRelatedYearPCT = 0; if ($motoringRelatedYear > 0 && $courtSessionsYear > 0) { $motoringRelatedYearPCT = ceil(($motoringRelatedYear / $courtSessionsYear) * 100); } //ALL TIME $allTimeStart = '2000-01-01'; $allTimeEnd = $year . '-12-31'; $from = $allTimeStart . ' ' . $timeStart; $to = $allTimeEnd . ' ' . $timeEnd; $courtSessionsAllTime = count($this->courtSessionRepository->findAllByHearingDate($from, $to)); $motoringRelatedAllTime = count($this->courtSessionRepository->findAllByHearingDate($from, $to, true)); $motoringRelatedAllTimePCT = 0; if ($motoringRelatedAllTime > 0 && $courtSessionsAllTime > 0) { $motoringRelatedAllTimePCT = ceil(($motoringRelatedAllTime / $courtSessionsAllTime) * 100); } //EARLIEST/LATEST RECORD DATES $firstRecord = $this->courtSessionRepository->find(1); $lastRecord = $this->courtSessionRepository->findLast(); $earliestRecordedSessionDate = 'Unknown'; $latestRecordedSessionDate = 'Unknown'; if ($firstRecord != null) { $earliestRecordedSessionDate = $firstRecord->getHearing_Date()->format('d/m/Y'); } if ($lastRecord != null) { $latestRecordedSessionDate = $lastRecord->getHearing_Date()->format('d/m/Y'); } //LOCATION OVERVIEW $locations = []; $sumOfCourtSessions = 0; $sumOfMotoringRelated = 0; foreach ($this->courtRepository->findAll() as $court) { $___noOfCourtSessions = count($this->courtSessionRepository->findAllByCourt($court)); $___noOfMotoringRelated = count($this->courtSessionRepository->findAllByCourt($court, true)); $___pctMotoringRelated = 0; if ($___noOfCourtSessions > 0 && $___noOfMotoringRelated > 0) { $___pctMotoringRelated = ceil(($___noOfMotoringRelated / $___noOfCourtSessions) * 100); } $lat = $court->getCourt_Latitude(); $lng = $court->getCourt_Longitude(); if ($lat < 0.1 && $lng < 0.1) { $postcodeLatLng = $this->postcodeLatLngRepository->findOneByPostcode($court->getCourt_Postcode()); if ($postcodeLatLng != null) { $lat = $postcodeLatLng->getLatitude(); $lng = $postcodeLatLng->getLongitude(); $court->setCourt_Latitude($lat); $court->setCourt_Longitude($lng); $this->entityManager->persist($court); $this->entityManager->flush(); } } $sumOfCourtSessions += $___noOfCourtSessions; $sumOfMotoringRelated += $___noOfMotoringRelated; $locations[] = [ 'courtName' => $court->getCourt_Name_Cleaned(), 'courtPostcode' => $court->getCourt_Postcode(), 'courtLatitude' => $lat, 'courtLongitude' => $lng, 'noOfCourtSessions' => $___noOfCourtSessions, 'noOfMotoringRelated' => $___noOfMotoringRelated, 'motoringRelatedPCT' => $___pctMotoringRelated ]; } $sumMotoringRelatedPCT = 0; if ($sumOfCourtSessions > 0 && $sumOfMotoringRelated > 0) { $sumMotoringRelatedPCT = ceil(($sumOfMotoringRelated / $sumOfCourtSessions) * 100); } //usort($locations, fn($a, $b) => $a['noOfMotoringRelated'] <=> $b['noOfMotoringRelated']); usort($locations, function($a, $b) { $retval = $a['noOfMotoringRelated'] <=> $b['noOfMotoringRelated']; if ($retval == 0) { $retval = $a['courtName'] <=> $b['courtName']; } return $retval; }); $locations = array_reverse($locations); //MAPS $map = []; $headOfficePostcode = 'L5 9PR'; $headOfficeLatLng = $this->postcodeLatLngRepository->findOneByPostcode($headOfficePostcode); $maps['headOfficeLatLng'] = $headOfficeLatLng; return [ 'courtSessionsToday' => $courtSessionsToday, 'motoringRelatedToday' => $motoringRelatedToday, 'motoringRelatedTodayPCT' => $motoringRelatedTodayPCT, 'courtSessionsMonth' => $courtSessionsMonth, 'motoringRelatedMonth' => $motoringRelatedMonth, 'motoringRelatedMonthPCT' => $motoringRelatedMonthPCT, 'courtSessionsThirtyDays' => $courtSessionsThirtyDays, 'motoringRelatedThirtyDays' => $motoringRelatedThirtyDays, 'motoringRelatedThirtyDaysPCT' => $motoringRelatedThirtyDaysPCT, 'courtSessionsYear' => $courtSessionsYear, 'motoringRelatedYear' => $motoringRelatedYear, 'motoringRelatedYearPCT' => $motoringRelatedYearPCT, 'courtSessionsAllTime' => $courtSessionsAllTime, 'motoringRelatedAllTime' => $motoringRelatedAllTime, 'motoringRelatedAllTimePCT' => $motoringRelatedAllTimePCT, 'earliestRecordedSessionDate' => $earliestRecordedSessionDate, 'latestRecordedSessionDate' => $latestRecordedSessionDate, 'locations' => $locations, 'sumOfCourtSessions' => $sumOfCourtSessions, 'sumOfMotoringRelated' => $sumOfMotoringRelated, 'sumMotoringRelatedPCT' => $sumMotoringRelatedPCT, 'maps' => $maps ]; } }