|
|
@@ -10,6 +10,7 @@ use App\Models\ProviderBlockedDay;
|
|
|
use App\Models\ProviderWorkingDay;
|
|
|
use App\Rules\ScheduleBusinessRules;
|
|
|
use Carbon\Carbon;
|
|
|
+use Illuminate\Support\Facades\Auth;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
@@ -291,4 +292,32 @@ class ScheduleService
|
|
|
throw new \Exception("Não foi possível atualizar o status do agendamento.");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public function cancelWithReason(int $id, string $cancelText)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ DB::beginTransaction();
|
|
|
+ $schedule = Schedule::findOrFail($id);
|
|
|
+
|
|
|
+ $allowedStatuses = ['accepted', 'paid'];
|
|
|
+ if (!in_array($schedule->status, $allowedStatuses)) {
|
|
|
+ throw new \Exception("Cancelamento não permitido para o status atual: {$schedule->status}");
|
|
|
+ }
|
|
|
+
|
|
|
+ $cancelled_by = Auth::user()->type;
|
|
|
+
|
|
|
+ $schedule->update([
|
|
|
+ 'status' => 'cancelled',
|
|
|
+ 'cancel_text' => $cancelText,
|
|
|
+ 'cancelled_by' => $cancelled_by,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ return $schedule->fresh(['client.user', 'provider.user', 'address']);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ DB::rollBack();
|
|
|
+ Log::error("Erro ao cancelar agendamento: " . $e->getMessage());
|
|
|
+ throw new \Exception("Não foi possível cancelar o agendamento.");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|