| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php
- namespace App\Rules;
- use App\Models\Schedule;
- use Carbon\Carbon;
- class ScheduleBusinessRules
- {
- // Status que devem ser ignorados na validação de limite por semana
- private const EXCLUDED_STATUSES = ['cancelled', 'rejected'];
- /**
- * Valida se o prestador pode ter mais um agendamento com o cliente na semana
- * Limite: 2 agendamentos por semana (domingo a sábado)
- *
- * @param int $clientId
- * @param int $providerId
- * @param string $date (Y-m-d)
- * @param int|null $excludeScheduleId
- * @return bool
- * @throws \Exception
- */
- public static function validateWeeklyScheduleLimit($clientId, $providerId, $date, $excludeScheduleId = null)
- {
- $date = Carbon::parse($date);
-
- $weekStart = $date->copy()->startOfWeek(Carbon::SUNDAY);
- $weekEnd = $date->copy()->endOfWeek(Carbon::SATURDAY);
- $weeklySchedulesCount = Schedule::where('client_id', $clientId)
- ->where('provider_id', $providerId)
- ->whereNotIn('status', self::EXCLUDED_STATUSES)
- ->whereBetween('date', [$weekStart->format('Y-m-d'), $weekEnd->format('Y-m-d')])
- ->when($excludeScheduleId, function ($query) use ($excludeScheduleId) {
- $query->where('id', '!=', $excludeScheduleId);
- })
- ->count();
- if ($weeklySchedulesCount >= 2) {
- throw new \Exception(__('validation.custom.schedule.weekly_limit_exceeded'));
- }
- return true;
- }
- }
|