|
|
@@ -165,4 +165,78 @@ class ScheduleService
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+ public function getGroupedByClient()
|
|
|
+ {
|
|
|
+ $schedules = Schedule::with(['client.user', 'provider.user', 'address'])
|
|
|
+ // ->orderBy('date', 'desc')
|
|
|
+ ->orderBy('id', 'desc')
|
|
|
+ ->select(
|
|
|
+ 'schedules.*'
|
|
|
+
|
|
|
+ )
|
|
|
+ ->get();
|
|
|
+
|
|
|
+ $grouped = $schedules->groupBy('client_id')->map(function ($clientSchedules) {
|
|
|
+ $firstSchedule = $clientSchedules->first();
|
|
|
+ return [
|
|
|
+ 'client_id' => $firstSchedule->client_id,
|
|
|
+ 'client_name' => $firstSchedule->client->user->name ?? 'N/A',
|
|
|
+ 'schedules' => $clientSchedules->map(function ($schedule) {
|
|
|
+ return [
|
|
|
+ 'id' => $schedule->id,
|
|
|
+ 'date' => $schedule->date ? Carbon::parse($schedule->date)->format('d/m/Y') : null,
|
|
|
+ 'start_time' => $schedule->start_time,
|
|
|
+ 'end_time' => $schedule->end_time,
|
|
|
+ 'period_type' => $schedule->period_type,
|
|
|
+ 'status' => $schedule->status,
|
|
|
+ 'total_amount' => $schedule->total_amount,
|
|
|
+ 'code' => $schedule->code,
|
|
|
+ 'code_verified' => $schedule->code_verified,
|
|
|
+ 'provider_id' => $schedule->provider_id,
|
|
|
+ 'provider_name' => $schedule->provider->user->name ?? 'N/A',
|
|
|
+ 'address' => $schedule->address ? [
|
|
|
+ 'id' => $schedule->address->id,
|
|
|
+ 'address' => $schedule->address->address,
|
|
|
+ 'complement' => $schedule->address->complement,
|
|
|
+ 'zip_code' => $schedule->address->zip_code,
|
|
|
+ 'city' => $schedule->address->city->name ?? '',
|
|
|
+ 'state' => $schedule->address->city->state->name ?? '',
|
|
|
+ ] : null,
|
|
|
+ 'client_name' => $schedule->client->user->name ?? 'N/A',
|
|
|
+ ];
|
|
|
+ })->values()
|
|
|
+ ];
|
|
|
+ })->sortBy('id')->values();
|
|
|
+
|
|
|
+ return $grouped;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function updateStatus($id, string $status)
|
|
|
+ {
|
|
|
+ $schedule = Schedule::findOrFail($id);
|
|
|
+
|
|
|
+ $allowedTransitions = [
|
|
|
+ 'pending' => ['accepted', 'rejected'],
|
|
|
+ 'accepted' => ['paid', 'cancelled'],
|
|
|
+ 'paid' => ['cancelled', 'started'],
|
|
|
+ 'started' => ['finished'],
|
|
|
+ 'rejected' => [],
|
|
|
+ 'cancelled' => [],
|
|
|
+ 'finished' => [],
|
|
|
+ ];
|
|
|
+
|
|
|
+ $currentStatus = $schedule->status;
|
|
|
+
|
|
|
+ if (!isset($allowedTransitions[$currentStatus])) {
|
|
|
+ throw new \Exception("Status atual inválido.");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!in_array($status, $allowedTransitions[$currentStatus])) {
|
|
|
+ throw new \Exception("Transição de status não permitida: {$currentStatus} → {$status}");
|
|
|
+ }
|
|
|
+
|
|
|
+ $schedule->update(['status' => $status]);
|
|
|
+ return $schedule->fresh(['client.user', 'provider.user', 'address']);
|
|
|
+ }
|
|
|
}
|