Refactor SlotCalculatorService to improve slot calculation logic
- Removed unused DAY_MAP constant. - Enhanced getAvailableSlots method to prioritize holiday checks and date overrides. - Simplified session handling by filtering and sorting active sessions. - Introduced buildSessionSlots method to handle session slot creation with support for rest breaks and patient limits. - Updated buildFlatSlots method for backward compatibility with date overrides. - Improved filterBookedSlots method for clarity and conciseness.
This commit is contained in:
@@ -2,20 +2,15 @@
|
||||
|
||||
namespace App\Appointment\Service;
|
||||
|
||||
use App\Appointment\Entity\Appointment;
|
||||
use App\Appointment\Repository\AppointmentRepository;
|
||||
use App\Appointment\Repository\DateOverrideRepository;
|
||||
use App\Appointment\Repository\HolidayRepository;
|
||||
use App\Appointment\Repository\WeeklyScheduleRepository;
|
||||
use App\Doctor\Entity\Doctor;
|
||||
|
||||
|
||||
class SlotCalculatorService
|
||||
{
|
||||
private const DAY_MAP = [
|
||||
0 => 'sunday', 1 => 'monday', 2 => 'tuesday', 3 => 'wednesday',
|
||||
4 => 'thursday', 5 => 'friday', 6 => 'saturday',
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
private readonly WeeklyScheduleRepository $scheduleRepo,
|
||||
private readonly DateOverrideRepository $overrideRepo,
|
||||
@@ -25,64 +20,133 @@ class SlotCalculatorService
|
||||
|
||||
/**
|
||||
* Returns available slots for a doctor on a given date.
|
||||
* @param string $date 'Y-m-d' format
|
||||
* @return array[] [{start: int, end: int, start_time: string, end_time: string}]
|
||||
* Day index convention: 0=Saturday(شنبه), 1=Sunday, ..., 6=Friday(جمعه)
|
||||
*
|
||||
* @return array[] [{start: int, end: int, start_time: string, end_time: string, location_id: int|null}]
|
||||
*/
|
||||
public function getAvailableSlots(Doctor $doctor, string $date): array
|
||||
{
|
||||
$dayStart = (int) strtotime($date . ' 00:00:00');
|
||||
$dayEnd = $dayStart + 86400;
|
||||
|
||||
// Check if in holiday
|
||||
$holidays = $this->holidayRepo->findActiveByDoctor($doctor, $dayStart, $dayEnd - 1);
|
||||
if (!empty($holidays)) return [];
|
||||
// 1. Blocked by holiday
|
||||
if (!empty($this->holidayRepo->findActiveByDoctor($doctor, $dayStart, $dayEnd - 1))) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Check date override first
|
||||
$overrides = $this->overrideRepo->findByDoctor($doctor);
|
||||
foreach ($overrides as $override) {
|
||||
$overDate = date('Y-m-d', $override->getDate());
|
||||
if ($overDate === $date) {
|
||||
// 2. Date override takes precedence over weekly schedule
|
||||
foreach ($this->overrideRepo->findByDoctor($doctor) as $override) {
|
||||
if (date('Y-m-d', $override->getDate()) === $date) {
|
||||
if (!$override->isActive()) return [];
|
||||
return $this->buildSlots($override->getSetting() ?? [], $dayStart);
|
||||
return $this->filterBookedSlots(
|
||||
$doctor,
|
||||
$this->buildFlatSlots($override->getSetting() ?? [], $dayStart)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to weekly schedule
|
||||
// 3. Weekly schedule
|
||||
$schedule = $this->scheduleRepo->findByDoctor($doctor);
|
||||
if ($schedule === null) return [];
|
||||
|
||||
$dow = (int) date('w', $dayStart);
|
||||
$dayName = self::DAY_MAP[$dow];
|
||||
$setting = $schedule->getSetting();
|
||||
// Convert PHP date('w') (0=Sunday) to Iranian week index (0=Saturday)
|
||||
$phpDow = (int) date('w', $dayStart);
|
||||
$dayKey = (string)(($phpDow + 1) % 7);
|
||||
|
||||
$dayConfig = $setting[$dayName] ?? null;
|
||||
if ($dayConfig === null || !($dayConfig['active'] ?? false)) return [];
|
||||
$dayConf = $schedule->getSetting()[$dayKey] ?? null;
|
||||
if ($dayConf === null) return [];
|
||||
|
||||
$rawSlots = $this->buildSlots($dayConfig['slots'] ?? [], $dayStart);
|
||||
$sessions = $dayConf['sessions'] ?? [];
|
||||
// Sort active sessions by start_time, skip overlapping ones
|
||||
$activeSessions = array_filter($sessions, fn($s) => $s['active'] ?? false);
|
||||
usort($activeSessions, fn($a, $b) =>
|
||||
$this->parseTime($a['start_time'] ?? '00:00') <=> $this->parseTime($b['start_time'] ?? '00:00')
|
||||
);
|
||||
|
||||
// Filter out already-booked slots
|
||||
return $this->filterBookedSlots($doctor, $rawSlots);
|
||||
$allSlots = [];
|
||||
$prevEnd = 0;
|
||||
foreach ($activeSessions as $session) {
|
||||
$sessionStart = $this->parseTime($session['start_time'] ?? '00:00');
|
||||
if ($sessionStart < $prevEnd) continue; // skip overlapping session
|
||||
$allSlots = array_merge($allSlots, $this->buildSessionSlots($session, $dayStart));
|
||||
$prevEnd = $this->parseTime($session['end_time'] ?? '00:00');
|
||||
}
|
||||
|
||||
usort($allSlots, fn($a, $b) => $a['start'] - $b['start']);
|
||||
|
||||
return $this->filterBookedSlots($doctor, $allSlots);
|
||||
}
|
||||
|
||||
/** @return array[] */
|
||||
private function buildSlots(array $slotConfigs, int $dayStart): array
|
||||
/**
|
||||
* Build slots from a morning/evening session config.
|
||||
* Supports: rest breaks, patient limits.
|
||||
*/
|
||||
private function buildSessionSlots(array $session, int $dayStart): array
|
||||
{
|
||||
$startSec = $this->parseTime($session['start_time'] ?? '00:00');
|
||||
$endSec = $this->parseTime($session['end_time'] ?? '00:00');
|
||||
$dur = (int)($session['duration_per_patient'] ?? 20) * 60;
|
||||
$hasRest = (bool)($session['has_rest'] ?? false);
|
||||
$restInt = (int)($session['rest_interval'] ?? 60) * 60; // convert min → sec
|
||||
$restDur = (int)($session['time_to_rest'] ?? 10) * 60; // convert min → sec
|
||||
$limit = isset($session['patient_limit']) && $session['patient_limit'] !== null
|
||||
? (int)$session['patient_limit'] : null;
|
||||
$locationId = isset($session['location_id']) ? (int)$session['location_id'] : null;
|
||||
|
||||
if ($dur <= 0 || $endSec <= $startSec) return [];
|
||||
|
||||
$slots = [];
|
||||
$currentSec = $startSec;
|
||||
$elapsedWork = 0; // seconds worked since last rest
|
||||
$patientCount = 0;
|
||||
|
||||
while ($currentSec + $dur <= $endSec) {
|
||||
if ($limit !== null && $patientCount >= $limit) break;
|
||||
|
||||
// Insert rest break if needed
|
||||
if ($hasRest && $restInt > 0 && $elapsedWork > 0 && $elapsedWork >= $restInt) {
|
||||
$currentSec += $restDur;
|
||||
$elapsedWork = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
$slots[] = [
|
||||
'start' => $dayStart + $currentSec,
|
||||
'end' => $dayStart + $currentSec + $dur,
|
||||
'start_time' => gmdate('H:i', $currentSec),
|
||||
'end_time' => gmdate('H:i', $currentSec + $dur),
|
||||
'location_id' => $locationId,
|
||||
];
|
||||
|
||||
$currentSec += $dur;
|
||||
$elapsedWork += $dur;
|
||||
$patientCount += 1;
|
||||
}
|
||||
|
||||
return $slots;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build slots from flat date-override format: [{start, end, duration}]
|
||||
* Kept for backward-compatibility with DateOverride.custom_slots.
|
||||
*/
|
||||
private function buildFlatSlots(array $slotConfigs, int $dayStart): array
|
||||
{
|
||||
$slots = [];
|
||||
foreach ($slotConfigs as $config) {
|
||||
$startSec = $this->parseTime($config['start'] ?? '00:00');
|
||||
$endSec = $this->parseTime($config['end'] ?? '00:00');
|
||||
$duration = (int) ($config['duration'] ?? 30) * 60;
|
||||
$duration = (int)($config['duration'] ?? 30) * 60;
|
||||
|
||||
if ($duration <= 0 || $endSec <= $startSec) continue;
|
||||
|
||||
for ($t = $startSec; $t + $duration <= $endSec; $t += $duration) {
|
||||
$slotStart = $dayStart + $t;
|
||||
$slotEnd = $slotStart + $duration;
|
||||
$slots[] = [
|
||||
'start' => $slotStart,
|
||||
'end' => $slotEnd,
|
||||
'start_time' => gmdate('H:i', $t),
|
||||
'end_time' => gmdate('H:i', $t + $duration),
|
||||
$slots[] = [
|
||||
'start' => $dayStart + $t,
|
||||
'end' => $dayStart + $t + $duration,
|
||||
'start_time' => gmdate('H:i', $t),
|
||||
'end_time' => gmdate('H:i', $t + $duration),
|
||||
'location_id' => null,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -91,9 +155,9 @@ class SlotCalculatorService
|
||||
|
||||
private function filterBookedSlots(Doctor $doctor, array $slots): array
|
||||
{
|
||||
return array_values(array_filter($slots, function (array $slot) use ($doctor): bool {
|
||||
return !$this->appointmentRepo->isSlotTaken($doctor, $slot['start'], $slot['end']);
|
||||
}));
|
||||
return array_values(array_filter($slots, fn(array $slot): bool =>
|
||||
!$this->appointmentRepo->isSlotTaken($doctor, $slot['start'], $slot['end'])
|
||||
));
|
||||
}
|
||||
|
||||
private function parseTime(string $time): int
|
||||
|
||||
Reference in New Issue
Block a user