|
@@ -14,6 +14,7 @@ use App\Models\ServicePackage;
|
|
|
use App\Notifications\Push\Cliente\Agendamento\PrestadorAceitouPush;
|
|
use App\Notifications\Push\Cliente\Agendamento\PrestadorAceitouPush;
|
|
|
use App\Rules\ScheduleBusinessRules;
|
|
use App\Rules\ScheduleBusinessRules;
|
|
|
use App\Services\NotificationService;
|
|
use App\Services\NotificationService;
|
|
|
|
|
+use App\Services\DistanceService;
|
|
|
use App\Services\PushNotificationService;
|
|
use App\Services\PushNotificationService;
|
|
|
use Carbon\Carbon;
|
|
use Carbon\Carbon;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\DB;
|
|
@@ -22,6 +23,8 @@ use Illuminate\Support\Facades\Storage;
|
|
|
|
|
|
|
|
class CustomScheduleService
|
|
class CustomScheduleService
|
|
|
{
|
|
{
|
|
|
|
|
+ private const NEARBY_RADIUS_KM = 20.0;
|
|
|
|
|
+
|
|
|
public function __construct(
|
|
public function __construct(
|
|
|
private readonly ZipCodeCoordinatesService $zipCodeCoordinatesService,
|
|
private readonly ZipCodeCoordinatesService $zipCodeCoordinatesService,
|
|
|
) {}
|
|
) {}
|
|
@@ -255,6 +258,10 @@ class CustomScheduleService
|
|
|
->orderBy('is_primary', 'desc')
|
|
->orderBy('is_primary', 'desc')
|
|
|
->first();
|
|
->first();
|
|
|
|
|
|
|
|
|
|
+ $providerCityId = $providerAddress?->city_id;
|
|
|
|
|
+ $providerLat = $providerAddress?->latitude !== null ? (float) $providerAddress->latitude : null;
|
|
|
|
|
+ $providerLng = $providerAddress?->longitude !== null ? (float) $providerAddress->longitude : null;
|
|
|
|
|
+
|
|
|
$opportunities = Schedule::with([
|
|
$opportunities = Schedule::with([
|
|
|
'client.user',
|
|
'client.user',
|
|
|
'client.profileMedia',
|
|
'client.profileMedia',
|
|
@@ -266,11 +273,37 @@ class CustomScheduleService
|
|
|
$join->on('schedules.id', '=', 'schedule_refuses.schedule_id')
|
|
$join->on('schedules.id', '=', 'schedule_refuses.schedule_id')
|
|
|
->where('schedule_refuses.provider_id', $providerId);
|
|
->where('schedule_refuses.provider_id', $providerId);
|
|
|
})
|
|
})
|
|
|
|
|
+ ->leftJoin('addresses as opportunity_address', 'opportunity_address.id', '=', 'schedules.address_id')
|
|
|
->whereNull('schedule_refuses.id')
|
|
->whereNull('schedule_refuses.id')
|
|
|
->where('schedules.schedule_type', 'custom')
|
|
->where('schedules.schedule_type', 'custom')
|
|
|
->where('schedules.status', 'pending')
|
|
->where('schedules.status', 'pending')
|
|
|
->whereNull('schedules.provider_id')
|
|
->whereNull('schedules.provider_id')
|
|
|
->whereDate('schedules.date', '>=', now()->toDateString())
|
|
->whereDate('schedules.date', '>=', now()->toDateString())
|
|
|
|
|
+ ->where(function ($query) use ($providerCityId, $providerLat, $providerLng) {
|
|
|
|
|
+ if ($providerCityId !== null) {
|
|
|
|
|
+ $query->where('opportunity_address.city_id', $providerCityId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($providerLat !== null && $providerLng !== null) {
|
|
|
|
|
+ $method = $providerCityId !== null ? 'orWhereRaw' : 'whereRaw';
|
|
|
|
|
+
|
|
|
|
|
+ $query->{$method}(
|
|
|
|
|
+ DistanceService::withinRadiusSqlCondition(
|
|
|
|
|
+ $providerLat,
|
|
|
|
|
+ $providerLng,
|
|
|
|
|
+ self::NEARBY_RADIUS_KM,
|
|
|
|
|
+ 'opportunity_address.latitude',
|
|
|
|
|
+ 'opportunity_address.longitude',
|
|
|
|
|
+ )
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($providerCityId === null) {
|
|
|
|
|
+ $query->whereRaw('1 = 0');
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
->select(
|
|
->select(
|
|
|
'schedules.id',
|
|
'schedules.id',
|
|
|
'schedules.client_id',
|
|
'schedules.client_id',
|