Forráskód Böngészése

Merge branch 'feature/diariaapp-kay-agendamentos-sob-medida-apps' of Softpar/sfp_api_laravel_diarista into development

zntt 1 hete
szülő
commit
5567375387

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

@@ -120,4 +120,19 @@ class ScheduleController extends Controller
       return $this->errorResponse($e->getMessage(), 422);
     }
   }
+  public function clientProviderBlocks(Request $request): JsonResponse
+  {
+    $validated = $request->validate([
+      'client_id' => 'required|integer|exists:clients,id',
+      'provider_id' => 'required|integer|exists:providers,id',
+    ]);
+
+    $blocks = $this->scheduleService->getClientProviderBlocks(
+      (int) $validated['client_id'],
+      (int) $validated['provider_id'],
+    );
+
+    return $this->successResponse(payload: $blocks);
+  }
 }
+

+ 14 - 1
app/Rules/ScheduleBusinessRules.php

@@ -121,25 +121,38 @@ class ScheduleBusinessRules
       }
 
       $provider = Provider::find($provider_id);
+      $min_price_proportional = 0 ;
+      $max_price_proportional = 0; 
+
       $provider_price_period = 0;
       switch ($period_type):
         case '2': //2 horas
           $provider_price_period = $provider->daily_price_2h;
+          $min_price_proportional = $min_price * 0.30;
+          $max_price_proportional = $max_price * 0.30;
           break;
         case '4': //4 horas
           $provider_price_period = $provider->daily_price_4h;
+          $min_price_proportional = $min_price * 0.55;
+          $max_price_proportional = $max_price * 0.55;
           break;
         case '6': //6 horas
           $provider_price_period = $provider->daily_price_6h;
+          $min_price_proportional = $min_price * 0.85;
+          $max_price_proportional = $max_price * 0.85;
+
           break;
         case '8': //8 horas
           $provider_price_period = $provider->daily_price_8h;
+          $min_price_proportional = $min_price;
+          $max_price_proportional = $max_price;
+
           break;
         default:
           throw new \Exception(__('validation.custom.schedule.invalid_period_type'));
         endswitch;
 
-      if ($provider_price_period < $min_price || $provider_price_period > $max_price) {
+      if ($provider_price_period < $min_price_proportional || $provider_price_period > $max_price_proportional) {
         throw new \Exception(__('validation.custom.schedule.price_not_in_range'));
       }
 

+ 44 - 2
app/Services/ScheduleService.php

@@ -16,6 +16,8 @@ use Illuminate\Support\Facades\Log;
 
 class ScheduleService
 {
+  private const EXCLUDED_STATUSES = ['cancelled', 'rejected'];
+
   public function getAll()
   {
     return Schedule::with(['client.user', 'provider.user', 'address'])
@@ -271,10 +273,10 @@ class ScheduleService
           $date_cleaned = Carbon::parse($schedule->date)->format('Y-m-d');
           $date_time_dispatch = Carbon::parse($date_cleaned . ' ' . $schedule->start_time);
 
-          StartScheduleJob::dispatch($schedule->id)->delay($date_time_dispatch);
+          // StartScheduleJob::dispatch($schedule->id)->delay($date_time_dispatch);
 
           // dispatch de teste em local
-          // StartScheduleJob::dispatch($schedule->id)->delay(now()->addSeconds(15));
+          StartScheduleJob::dispatch($schedule->id)->delay(now()->addSeconds(15));
           break;
         case 'cancelled':
           break;
@@ -319,5 +321,45 @@ class ScheduleService
       Log::error("Erro ao cancelar agendamento: " . $e->getMessage());
       throw new \Exception("Não foi possível cancelar o agendamento.");
     }
+  }
+          public function getClientProviderBlocks(int $clientId, int $providerId): array
+  {
+    $today = Carbon::today()->format('Y-m-d');
+
+    $schedules = Schedule::where('client_id', $clientId)
+      ->where('provider_id', $providerId)
+      ->whereNotIn('status', self::EXCLUDED_STATUSES)
+      ->whereDate('date', '>=', $today)
+      ->orderBy('date')
+      ->orderBy('start_time')
+      ->get(['id', 'date', 'start_time', 'end_time', 'status']);
+
+    $existingSchedules = $schedules->map(function ($schedule) {
+      return [
+        'id' => $schedule->id,
+        'date' => Carbon::parse($schedule->date)->format('Y-m-d'),
+        'start_time' => $schedule->start_time,
+        'end_time' => $schedule->end_time,
+        'status' => $schedule->status,
+      ];
+    })->values();
+
+    $fullyBlockedWeeks = $schedules
+      ->groupBy(function ($schedule) {
+        return Carbon::parse($schedule->date)
+          ->startOfWeek(Carbon::SUNDAY)
+          ->format('Y-m-d');
+      })
+      ->filter(function ($weekSchedules) {
+        return $weekSchedules->count() >= 2;
+      })
+      ->keys()
+      ->values();
+
+    return [
+      'existing_schedules' => $existingSchedules,
+      'fully_blocked_weeks' => $fullyBlockedWeeks,
+    ];
   }
 }
+

+ 29 - 0
database/migrations/2026_04_20_100127_alter_clients_set_average_rating_default_0.php

@@ -0,0 +1,29 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::table('clients', function (Blueprint $table) {
+         $table->decimal('average_rating', 2, 1)->nullable()->default(0.0)->change();
+
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::table('clients', function (Blueprint $table) {
+                  $table->decimal('average_rating', 2, 1)->nullable()->change();
+        });
+    }
+};

+ 28 - 0
database/migrations/2026_04_20_100221_alter_providers_set_average_rating_default_0.php

@@ -0,0 +1,28 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::table('providers', function (Blueprint $table) {
+            $table->decimal('average_rating', 2, 1)->nullable()->default(0.0)->change();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::table('providers', function (Blueprint $table) {
+            $table->decimal('average_rating', 2, 1)->nullable()->change();
+        });
+    }
+};

+ 1 - 0
routes/authRoutes/schedule.php

@@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Route;
 Route::get('/schedules', [ScheduleController::class, 'index'])->middleware('permission:config.schedule,view');
 Route::get('/schedules/grouped-by-client', [ScheduleController::class, 'groupedByClient'])->middleware('permission:config.schedule,view');
 Route::get('/schedules/finished', [ScheduleController::class, 'finished'])->middleware('permission:config.schedule,view');
+Route::get('/schedule/client-provider-blocks', [ScheduleController::class, 'clientProviderBlocks'])->middleware('permission:config.schedule,add');
 Route::get('/schedule/{id}', [ScheduleController::class, 'show'])->middleware('permission:config.schedule,view');
 Route::post('/schedule', [ScheduleController::class, 'store'])->middleware('permission:config.schedule,add');
 Route::put('/schedule/{id}', [ScheduleController::class, 'update'])->middleware('permission:config.schedule,edit');