where('day_of_week', $day)->first(); $this->schedule[$day] = [ 'is_active' => $workingHour?->is_active ?? false, 'start_time' => $workingHour ? Carbon::parse($workingHour->start_time)->format('H:i') : '09:00', 'end_time' => $workingHour ? Carbon::parse($workingHour->end_time)->format('H:i') : '17:00', ]; } } public function save(): void { foreach ($this->schedule as $day => $config) { if ($config['is_active'] && $config['end_time'] <= $config['start_time']) { $this->addError("schedule.{$day}.end_time", __('validation.end_time_after_start')); return; } } $oldValues = WorkingHour::all()->keyBy('day_of_week')->toArray(); foreach ($this->schedule as $day => $config) { WorkingHour::query()->updateOrCreate( ['day_of_week' => $day], [ 'is_active' => $config['is_active'], 'start_time' => $config['start_time'], 'end_time' => $config['end_time'], ] ); } AdminLog::create([ 'admin_id' => auth()->id(), 'action' => 'update', 'target_type' => 'working_hours', 'old_values' => $oldValues, 'new_values' => $this->schedule, 'ip_address' => request()->ip(), 'created_at' => now(), ]); session()->flash('success', __('messages.working_hours_saved')); } public function getSlotCount(int $day): int { if (! $this->schedule[$day]['is_active']) { return 0; } $start = Carbon::parse($this->schedule[$day]['start_time']); $end = Carbon::parse($this->schedule[$day]['end_time']); if ($end->lte($start)) { return 0; } $duration = 60; $count = 0; while ($start->copy()->addMinutes($duration)->lte($end)) { $count++; $start->addMinutes($duration); } return $count; } public function formatTime(string $time): string { return Carbon::parse($time)->format('g:i A'); } }; ?>
{{ __('admin.working_hours') }} {{ __('admin.working_hours_description') }}
@if (session('success'))
{{ session('success') }}
@endif
@foreach (range(0, 6) as $day)
{{ \App\Models\WorkingHour::getDayName($day) }}
@if ($schedule[$day]['is_active'])
{{ __('admin.to') }}
({{ $this->formatTime($schedule[$day]['start_time']) }} - {{ $this->formatTime($schedule[$day]['end_time']) }}) @php($slots = $this->getSlotCount($day)) @if ($slots > 0) {{ __('admin.slots_available', ['count' => $slots]) }} @else {{ __('admin.no_slots') }} @endif
@error("schedule.{$day}.end_time")
{{ $message }}
@enderror @else {{ __('admin.closed') }} @endif
@endforeach
{{ __('admin.save_working_hours') }}