Browse Source

feat: ✨ feat (pedidos e solicitacoes) adicionada listagem de solicitacoes do cliente na aba pedidos

foi adicionada a listagem de solicitacoes do cliente e as propostas recebidas dos prestadores na aba pedidos. Tambem foi feita correcao gerao das datas nos cards de agendamentos

fase:dev | origin:escopo
Gustavo Zanatta 4 days ago
parent
commit
97613f4ad6

+ 15 - 0
app/Http/Controllers/DashboardController.php

@@ -30,6 +30,21 @@ class DashboardController extends Controller
         }
         }
     }
     }
 
 
+    public function dadosPedidosCliente(): JsonResponse
+    {
+        try {
+            $dados = $this->service->dadosPedidosCliente();
+
+            return $this->successResponse(payload: $dados);
+        } catch (AuthorizationException $e) {
+            return $this->errorResponse(message: $e->getMessage(), code: 403);
+        } catch (ModelNotFoundException) {
+            return $this->errorResponse(message: __('messages.client_not_found'), code: 404);
+        } catch (\Exception $e) {
+            return $this->errorResponse(message: __('messages.error_fetching_data'), code: 500, exception: $e);
+        }
+    }
+
     public function dadosDashboardPrestador(): JsonResponse
     public function dadosDashboardPrestador(): JsonResponse
     {
     {
         try {
         try {

+ 4 - 0
app/Services/ClientCalendarService.php

@@ -64,6 +64,10 @@ class ClientCalendarService
         $upcomingSchedules = Schedule::with('address:district,address,number,source_id,source,id')
         $upcomingSchedules = Schedule::with('address:district,address,number,source_id,source,id')
             ->where('schedules.client_id', $client->id)
             ->where('schedules.client_id', $client->id)
             ->whereIn('schedules.status', ['pending', 'accepted', 'paid', 'started'])
             ->whereIn('schedules.status', ['pending', 'accepted', 'paid', 'started'])
+            ->where(function ($query) {
+                $query->where('schedules.schedule_type', '!=', 'custom')
+                    ->orWhereIn('schedules.status', ['paid', 'started']);
+            })
             ->whereDate('schedules.date', '>=', now()->toDateString())
             ->whereDate('schedules.date', '>=', now()->toDateString())
             ->leftJoin('providers', 'providers.id', '=', 'schedules.provider_id')
             ->leftJoin('providers', 'providers.id', '=', 'schedules.provider_id')
             ->leftJoin('users as provider_user', 'provider_user.id', '=', 'providers.user_id')
             ->leftJoin('users as provider_user', 'provider_user.id', '=', 'providers.user_id')

+ 1 - 1
app/Services/CustomScheduleService.php

@@ -256,7 +256,7 @@ class CustomScheduleService
         $opportunities = Schedule::with([
         $opportunities = Schedule::with([
             'client.user',
             'client.user',
             'client.profileMedia',
             'client.profileMedia',
-            'address',
+            'address:id,district,zip_code,latitude,longitude',
             'customSchedule.serviceType',
             'customSchedule.serviceType',
             'customSchedule.specialities',
             'customSchedule.specialities',
         ])
         ])

+ 167 - 5
app/Services/DashboardService.php

@@ -499,7 +499,8 @@ class DashboardService
             $schedulesProposals->whereNull('distance_km')->pluck('provider_zip_code')
             $schedulesProposals->whereNull('distance_km')->pluck('provider_zip_code')
         );
         );
 
 
-        $custom_schedules_with_no_proposals = Schedule::where('client_id', $cliente->id)
+        $custom_schedules_with_no_proposals = Schedule::with('address:district,address,number,source_id,source,id')
+            ->where('client_id', $cliente->id)
             ->where('schedule_type', 'custom')
             ->where('schedule_type', 'custom')
             ->where('status', 'pending')
             ->where('status', 'pending')
             ->whereDate('date', '>=', now()->toDateString())
             ->whereDate('date', '>=', now()->toDateString())
@@ -673,6 +674,157 @@ class DashboardService
         ];
         ];
     }
     }
 
 
+    public function dadosPedidosCliente(): array
+    {
+        $user = Auth::user();
+
+        if ($user->type !== UserTypeEnum::CLIENT) {
+            throw new AuthorizationException(__('messages.only_clients_allowed'));
+        }
+
+        $cliente = Client::where('user_id', $user->id)->firstOrFail();
+
+        $clientPrimaryAddress = Address::where('source', 'client')
+            ->where('source_id', $cliente->id)
+            ->orderByDesc('is_primary')
+            ->orderByDesc('id')
+            ->first();
+
+        $proposalsDistanceSelect = DistanceService::sqlExpression(
+            $clientPrimaryAddress?->latitude !== null ? (float) $clientPrimaryAddress->latitude : null,
+            $clientPrimaryAddress?->longitude !== null ? (float) $clientPrimaryAddress->longitude : null,
+        );
+
+        $schedulesProposals = ScheduleProposal::query()
+            ->leftJoin(
+                'schedules',
+                'schedule_proposals.schedule_id',
+                '=',
+                'schedules.id'
+            )
+            ->leftJoin(
+                'providers',
+                'schedule_proposals.provider_id',
+                '=',
+                'providers.id'
+            )
+            ->whereExists(Provider::hasActivePrimaryBankAccount())
+            ->leftJoin('users', 'providers.user_id', '=', 'users.id')
+            ->leftJoin(
+                DB::raw("
+                    (
+                        SELECT DISTINCT ON (source_id)
+                            *
+                        FROM addresses
+                        WHERE source = 'provider'
+                        AND deleted_at IS NULL
+                        ORDER BY source_id, is_primary DESC
+                    ) AS provider_address
+                "),
+                'provider_address.source_id',
+                '=',
+                'providers.id'
+            )
+            ->leftJoin(
+                'addresses as schedule_address',
+                'schedule_address.id',
+                '=',
+                'schedules.address_id'
+            )
+            ->where('schedules.client_id', $cliente->id)
+            ->where('schedules.schedule_type', 'custom')
+            ->where('schedules.status', 'pending')
+            ->whereNull('schedules.deleted_at')
+            ->whereDate('schedules.date', '>=', now()->toDateString())
+            ->orderBy('schedule_proposals.created_at', 'desc')
+            ->select([
+                'schedule_proposals.id',
+
+                DB::raw("
+                    DATE_PART('year', AGE(providers.birth_date)) AS idade
+                "),
+
+                'providers.id as provider_id',
+                'providers.gender',
+                'schedules.id as schedule_id',
+                'schedules.date',
+                'schedules.start_time',
+                'schedules.end_time',
+                'schedules.period_type',
+                'schedules.total_amount',
+                'providers.daily_price_8h',
+                'providers.average_rating',
+                'providers.total_services',
+
+                'users.name as provider_name',
+
+                'provider_address.latitude as provider_latitude',
+                'provider_address.longitude as provider_longitude',
+                'provider_address.zip_code as provider_zip_code',
+
+                'schedule_address.address as address',
+                'schedule_address.number as address_number',
+                'schedule_address.district as address_district',
+                'schedule_address.address_type as address_type',
+
+                $proposalsDistanceSelect,
+            ])
+            ->get();
+
+        $this->zipCodeCoordinatesService->preload(
+            $schedulesProposals->whereNull('distance_km')->pluck('provider_zip_code')
+        );
+
+        $customSchedulesNoProposals = Schedule::with('address:district,address,number,source_id,source,id')
+            ->where('client_id', $cliente->id)
+            ->where('schedule_type', 'custom')
+            ->where('status', 'pending')
+            ->whereDate('date', '>=', now()->toDateString())
+            ->doesntHave('proposals')
+            ->get();
+
+        $schedulesProposals->each(function ($item) use ($clientPrimaryAddress) {
+            $item->gender_label = GenderEnum::labelFor($item->gender);
+
+            $item->address = [
+                'address'      => $item->address,
+                'number'       => $item->address_number,
+                'district'     => $item->address_district,
+                'address_type' => $item->address_type,
+            ];
+
+            unset($item->address_number, $item->address_district);
+
+            if ($item->distance_km === null) {
+                $item->distance_km = $this->zipCodeCoordinatesService->calculateDistance(
+                    $clientPrimaryAddress?->latitude !== null ? (float) $clientPrimaryAddress->latitude : null,
+                    $clientPrimaryAddress?->longitude !== null ? (float) $clientPrimaryAddress->longitude : null,
+                    $clientPrimaryAddress?->zip_code,
+                    $item->provider_latitude !== null ? (float) $item->provider_latitude : null,
+                    $item->provider_longitude !== null ? (float) $item->provider_longitude : null,
+                    $item->provider_zip_code,
+                );
+            }
+
+            unset(
+                $item->provider_latitude,
+                $item->provider_longitude,
+                $item->provider_zip_code,
+            );
+        });
+
+        $providerPhotoUrls = $this->providerPhotoUrls($schedulesProposals->pluck('provider_id'));
+
+        $schedulesProposals->each(function ($item) use ($providerPhotoUrls) {
+            $item->provider_photo = $providerPhotoUrls->get($item->provider_id);
+        });
+
+        return [
+            'schedulesProposals'         => $schedulesProposals,
+            'customSchedulesNoProposals' => $customSchedulesNoProposals,
+        ];
+    }
+
     public function dadosDashboardPrestador(): array
     public function dadosDashboardPrestador(): array
     {
     {
         $user = Auth::user();
         $user = Auth::user();
@@ -863,9 +1015,11 @@ class DashboardService
             ->get();
             ->get();
 
 
 
 
-        $pendingConfirmation = Schedule::with(
-            'address:district,address,number,source_id,source,id,zip_code,latitude,longitude'
-        )
+        $pendingConfirmation = Schedule::with([
+            'address' => fn ($query) => $query->withTrashed()->select([
+                'district', 'address', 'number', 'source_id', 'source', 'id', 'zip_code', 'latitude', 'longitude',
+            ]),
+        ])
             ->where('schedules.provider_id', $provider->id)
             ->where('schedules.provider_id', $provider->id)
             ->where('schedules.status', 'accepted')
             ->where('schedules.status', 'accepted')
             ->whereDate('schedules.date', '>=', now()->toDateString())
             ->whereDate('schedules.date', '>=', now()->toDateString())
@@ -909,7 +1063,11 @@ class DashboardService
             );
             );
         });
         });
 
 
-        $nextSchedules = Schedule::with('address:district,address,number,source_id,source,id,zip_code,latitude,longitude')
+        $nextSchedules = Schedule::with([
+            'address' => fn ($query) => $query->withTrashed()->select([
+                'district', 'address', 'number', 'source_id', 'source', 'id', 'zip_code', 'latitude', 'longitude',
+            ]),
+        ])
             ->where('schedules.provider_id', $provider->id)
             ->where('schedules.provider_id', $provider->id)
             ->where('schedules.status', 'paid')
             ->where('schedules.status', 'paid')
             ->whereDate('schedules.date', '>=', now()->toDateString())
             ->whereDate('schedules.date', '>=', now()->toDateString())
@@ -1029,6 +1187,8 @@ class DashboardService
                 'providers.birth_date as provider_birth_date',
                 'providers.birth_date as provider_birth_date',
                 'providers.gender',
                 'providers.gender',
                 'custom_schedules.offers_meal',
                 'custom_schedules.offers_meal',
+                'custom_schedules.min_price',
+                'custom_schedules.max_price',
             )
             )
             ->firstOrFail();
             ->firstOrFail();
 
 
@@ -1043,6 +1203,8 @@ class DashboardService
             'gender_label'        => GenderEnum::labelFor($schedule->gender),
             'gender_label'        => GenderEnum::labelFor($schedule->gender),
             'offers_meal'         => $schedule->offers_meal,
             'offers_meal'         => $schedule->offers_meal,
             'specialities'        => $schedule->specialities,
             'specialities'        => $schedule->specialities,
+            'min_price'           => $schedule->min_price,
+            'max_price'           => $schedule->max_price,
 
 
             'provider_photo' => $providerPhoto,
             'provider_photo' => $providerPhoto,
         ];
         ];

+ 2 - 0
routes/authRoutes/dashboard.php

@@ -7,6 +7,8 @@ Route::get('/dados-dashboard-cliente', [DashboardController::class, 'dadosDashbo
 
 
 Route::get('/dados-dashboard-cliente/schedule/{id}/detalhes', [DashboardController::class, 'scheduleClienteDetails'])->middleware('permission:dashboard,view');
 Route::get('/dados-dashboard-cliente/schedule/{id}/detalhes', [DashboardController::class, 'scheduleClienteDetails'])->middleware('permission:dashboard,view');
 
 
+Route::get('/dados-pedidos-cliente', [DashboardController::class, 'dadosPedidosCliente'])->middleware('permission:dashboard,view');
+
 Route::get('/dados-dashboard-prestador', [DashboardController::class, 'dadosDashboardPrestador'])
 Route::get('/dados-dashboard-prestador', [DashboardController::class, 'dadosDashboardPrestador'])
     ->middleware('permission:dashboard,view')
     ->middleware('permission:dashboard,view')
     ->withoutMiddleware('provider.accepted');
     ->withoutMiddleware('provider.accepted');