DashboardService.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. <?php
  2. namespace App\Services;
  3. use App\Enums\UserTypeEnum;
  4. use App\Models\Address;
  5. use App\Models\Client;
  6. use App\Models\ClientFavoriteProvider;
  7. use App\Models\ClientPaymentMethod;
  8. use App\Models\Provider;
  9. use App\Models\ProviderSpeciality;
  10. use App\Models\Review;
  11. use App\Models\Schedule;
  12. use App\Models\ScheduleProposal;
  13. use App\Models\Speciality;
  14. use App\Models\Notification;
  15. use App\Rules\ScheduleBusinessRules;
  16. use App\Services\DistanceService;
  17. use Illuminate\Auth\Access\AuthorizationException;
  18. use Illuminate\Support\Facades\Auth;
  19. use Illuminate\Support\Facades\DB;
  20. use Illuminate\Support\Facades\Storage;
  21. class DashboardService
  22. {
  23. public function __construct(
  24. private readonly CustomScheduleService $customScheduleService,
  25. ) {}
  26. public function dadosDashboardCliente(): array
  27. {
  28. $user = Auth::user();
  29. if ($user->type !== UserTypeEnum::CLIENT) {
  30. throw new AuthorizationException('Apenas clientes podem acessar este recurso.');
  31. }
  32. $cliente = Client::with('profileMedia')->where('user_id', $user->id)->first();
  33. $headerBar = [
  34. 'rating' => $cliente->average_rating,
  35. 'total_ratings' => Review::where('reviews.origin', 'provider')
  36. ->leftJoin('schedules', 'schedules.id', '=', 'reviews.schedule_id')
  37. ->where('schedules.client_id', $cliente->id)
  38. ->count(),
  39. 'total_services' => $cliente->total_services,
  40. ];
  41. $address = Address::where('source', 'client')
  42. ->where('source_id', $cliente->id)
  43. ->with(['city', 'state'])
  44. ->select('id', 'source', 'source_id', 'address', 'number', 'district', 'nickname', 'address_type', 'city_id', 'state_id', 'is_primary')
  45. ->first();
  46. $summaryInfos = [
  47. 'name' => $user->name,
  48. 'profile_photo' => $cliente->profileMedia?->path
  49. ? Storage::temporaryUrl($cliente->profileMedia->path, now()->addMinutes(60))
  50. : null,
  51. 'address' => $address,
  52. 'pending_services' => Schedule::where('client_id', $cliente->id)
  53. ->where('status', 'pending')
  54. ->count(),
  55. ];
  56. $nextSchedules = Schedule::with('address:district,address,source_id,source,id,address_type')
  57. ->where('schedules.client_id', $cliente->id)
  58. ->whereIn('schedules.status', ['accepted', 'paid'])
  59. ->whereDate('schedules.date', '>=', now()->toDateString())
  60. ->leftJoin('providers', 'providers.id', '=', 'schedules.provider_id')
  61. ->leftJoin('users as provider_user', 'provider_user.id', '=', 'providers.user_id')
  62. ->leftJoin('custom_schedules', 'custom_schedules.schedule_id', '=', 'schedules.id')
  63. ->leftJoin('media as provider_media', 'provider_media.id', '=', 'providers.profile_media_id')
  64. ->where('schedules.date', '>=', now()->toDateString())
  65. ->select(
  66. 'schedules.id',
  67. 'schedules.provider_id',
  68. 'provider_user.name as provider_name',
  69. 'schedules.date',
  70. 'schedules.start_time',
  71. 'schedules.end_time',
  72. 'schedules.total_amount',
  73. 'schedules.period_type',
  74. 'schedules.schedule_type',
  75. 'schedules.address_id',
  76. 'custom_schedules.address_type as custom_address_type',
  77. 'provider_media.path as provider_photo_path',
  78. )
  79. ->orderBy('schedules.date', 'asc')
  80. ->limit(5)
  81. ->get();
  82. $nextSchedules->each(function ($item) {
  83. $item->provider_photo = $item->provider_photo_path
  84. ? Storage::temporaryUrl($item->provider_photo_path, now()->addMinutes(60))
  85. : null;
  86. unset($item->provider_photo_path);
  87. });
  88. $lastDoneSchedules = Schedule::where('schedules.client_id', $cliente->id)
  89. ->where('schedules.status', 'finished')
  90. ->leftJoin('providers', 'providers.id', '=', 'schedules.provider_id')
  91. ->leftJoin('users as provider_user', 'provider_user.id', '=', 'providers.user_id')
  92. ->leftJoin('addresses as provider_address', function ($join) {
  93. $join->on('provider_address.source_id', '=', 'providers.id')
  94. ->where('provider_address.source', 'provider')
  95. ->orderBy('provider_address.is_primary', 'desc');
  96. })
  97. ->leftJoin('media as provider_media', 'provider_media.id', '=', 'providers.profile_media_id')
  98. ->select(
  99. 'schedules.id',
  100. 'schedules.provider_id',
  101. 'provider_user.name as provider_name',
  102. 'provider_address.district as provider_district',
  103. 'provider_media.path as provider_photo_path',
  104. )
  105. ->orderBy('schedules.date', 'desc')
  106. ->limit(5)
  107. ->get();
  108. $lastDoneSchedules->each(function ($item) {
  109. $item->provider_photo = $item->provider_photo_path
  110. ? Storage::temporaryUrl($item->provider_photo_path, now()->addMinutes(60))
  111. : null;
  112. unset($item->provider_photo_path);
  113. });
  114. $favoriteProviders = ClientFavoriteProvider::where('client_favorite_providers.client_id', $cliente->id)
  115. ->leftJoin('providers', 'providers.id', '=', 'client_favorite_providers.provider_id')
  116. ->leftJoin('users as provider_user', 'provider_user.id', '=', 'providers.user_id')
  117. ->leftJoin('addresses as provider_address', function ($join) {
  118. $join->on('provider_address.source_id', '=', 'providers.id')
  119. ->where('provider_address.source', 'provider')
  120. ->orderBy('provider_address.is_primary', 'desc');
  121. })
  122. ->leftJoin('media as provider_media', 'provider_media.id', '=', 'providers.profile_media_id')
  123. ->select(
  124. 'providers.id as provider_id',
  125. 'provider_user.name as provider_name',
  126. 'providers.average_rating',
  127. 'provider_address.district as provider_district',
  128. 'provider_media.path as provider_photo_path',
  129. )
  130. ->orderBy('client_favorite_providers.created_at', 'desc')
  131. ->limit(5)
  132. ->get();
  133. $favoriteProviders->each(function ($item) {
  134. $item->provider_photo = $item->provider_photo_path
  135. ? Storage::temporaryUrl($item->provider_photo_path, now()->addMinutes(60))
  136. : null;
  137. unset($item->provider_photo_path);
  138. });
  139. $blockedProviderIds = ScheduleBusinessRules::getBlockedProviderIdsForClient($cliente->id);
  140. $providersWithWorkingDays = ScheduleBusinessRules::getProviderIdsWithWorkingDays();
  141. $clientPrimaryAddress = Address::where('source', 'client')
  142. ->where('source_id', $cliente->id)
  143. ->where('is_primary', true)
  144. ->first();
  145. $providersCloseDistanceSelect = $this->distanceSelect(
  146. $clientPrimaryAddress?->latitude !== null ? (float) $clientPrimaryAddress->latitude : null,
  147. $clientPrimaryAddress?->longitude !== null ? (float) $clientPrimaryAddress->longitude : null,
  148. );
  149. $providersClose = Provider::leftJoin('users as provider_user', 'provider_user.id', '=', 'providers.user_id')
  150. ->leftJoin(DB::raw("
  151. (
  152. SELECT DISTINCT ON (source_id)
  153. *
  154. FROM addresses
  155. WHERE source = 'provider'
  156. AND deleted_at IS NULL
  157. ORDER BY source_id, is_primary DESC
  158. ) as provider_address
  159. "), 'provider_address.source_id', '=', 'providers.id')
  160. ->leftJoin('media as provider_media', 'provider_media.id', '=', 'providers.profile_media_id')
  161. ->whereNotNull('provider_address.id')
  162. ->where('provider_address.city_id', $clientPrimaryAddress?->city_id)
  163. ->whereNotIn('providers.id', $blockedProviderIds)
  164. ->whereIn('providers.id', $providersWithWorkingDays)
  165. ->whereNull('providers.deleted_at')
  166. ->select(
  167. 'providers.id as provider_id',
  168. 'provider_user.name as provider_name',
  169. 'provider_address.id as address_id',
  170. 'provider_address.district',
  171. 'provider_address.latitude as provider_latitude',
  172. 'provider_address.longitude as provider_longitude',
  173. 'providers.average_rating',
  174. 'providers.total_services',
  175. 'providers.daily_price_8h',
  176. 'providers.daily_price_6h',
  177. 'providers.daily_price_4h',
  178. 'providers.daily_price_2h',
  179. DB::raw("(
  180. SELECT COUNT(*)
  181. FROM reviews
  182. LEFT JOIN schedules ON schedules.id = reviews.schedule_id
  183. WHERE reviews.origin = 'provider'
  184. AND schedules.provider_id = providers.id
  185. ) as total_reviews"),
  186. $providersCloseDistanceSelect,
  187. 'provider_media.path as provider_photo_path',
  188. )
  189. ->get();
  190. $providersClose->each(function ($item) {
  191. $item->provider_photo = $item->provider_photo_path
  192. ? Storage::temporaryUrl($item->provider_photo_path, now()->addMinutes(60))
  193. : null;
  194. unset($item->provider_photo_path);
  195. });
  196. $pendingSchedules = Schedule::with('address:district,address,number,source_id,source,id,address_type')
  197. ->where('schedules.client_id', $cliente->id)
  198. ->whereIn('schedules.status', ['pending', 'accepted'])
  199. ->where('schedules.schedule_type', 'default')
  200. ->leftJoin('providers', 'providers.id', '=', 'schedules.provider_id')
  201. ->leftJoin('users as provider_user', 'provider_user.id', '=', 'providers.user_id')
  202. ->leftJoin('media as provider_media', 'provider_media.id', '=', 'providers.profile_media_id')
  203. ->select(
  204. 'schedules.id',
  205. 'schedules.provider_id',
  206. 'provider_user.name as provider_name',
  207. 'schedules.date',
  208. 'schedules.address_id',
  209. 'schedules.status',
  210. 'schedules.total_amount',
  211. 'schedules.start_time',
  212. 'schedules.end_time',
  213. DB::raw("CASE
  214. WHEN (now() - schedules.created_at) < INTERVAL '1 hour' THEN CONCAT(ROUND(EXTRACT(EPOCH FROM (now() - schedules.created_at)) / 60), 'min')
  215. WHEN (now() - schedules.created_at) < INTERVAL '1 day' THEN CONCAT(ROUND(EXTRACT(EPOCH FROM (now() - schedules.created_at)) / 3600), 'h')
  216. ELSE CONCAT(ROUND(EXTRACT(EPOCH FROM (now() - schedules.created_at)) / 86400), 'd')
  217. END as time_since_request"),
  218. 'provider_media.path as provider_photo_path',
  219. )
  220. ->orderBy('schedules.date', 'asc')
  221. ->get();
  222. $pendingSchedules->each(function ($item) {
  223. $item->provider_photo = $item->provider_photo_path
  224. ? Storage::temporaryUrl($item->provider_photo_path, now()->addMinutes(60))
  225. : null;
  226. unset($item->provider_photo_path);
  227. });
  228. $proposalsDistanceSelect = DistanceService::sqlExpression(
  229. $clientPrimaryAddress?->latitude !== null ? (float) $clientPrimaryAddress->latitude : null,
  230. $clientPrimaryAddress?->longitude !== null ? (float) $clientPrimaryAddress->longitude : null,
  231. );
  232. $schedulesProposals = ScheduleProposal::query()
  233. ->leftJoin('schedules', 'schedule_proposals.schedule_id', '=', 'schedules.id')
  234. ->leftJoin('providers', 'schedule_proposals.provider_id', '=', 'providers.id')
  235. ->leftJoin('users', 'providers.user_id', '=', 'users.id')
  236. ->leftJoin(DB::raw("(
  237. SELECT DISTINCT ON (source_id)
  238. *
  239. FROM addresses
  240. WHERE source = 'provider'
  241. AND deleted_at IS NULL
  242. ORDER BY source_id, is_primary DESC
  243. ) as provider_address"), 'provider_address.source_id', '=', 'providers.id')
  244. ->leftJoin('media as provider_media', 'provider_media.id', '=', 'providers.profile_media_id')
  245. ->where('schedules.client_id', $cliente->id)
  246. ->where('schedules.schedule_type', 'custom')
  247. ->where('schedules.status', 'pending')
  248. ->whereNull('schedules.deleted_at')
  249. ->whereDate('schedules.date', '>=', now()->toDateString())
  250. ->orderBy('schedule_proposals.created_at', 'desc')
  251. ->select([
  252. 'schedule_proposals.id',
  253. DB::raw("DATE_PART('year', AGE(providers.birth_date)) as idade"),
  254. 'providers.id as provider_id',
  255. 'schedules.id as schedule_id',
  256. 'schedules.date',
  257. 'schedules.start_time',
  258. 'schedules.end_time',
  259. 'schedules.period_type',
  260. 'schedules.total_amount',
  261. 'providers.daily_price_8h',
  262. 'providers.average_rating',
  263. 'providers.total_services',
  264. 'users.name as provider_name',
  265. $proposalsDistanceSelect,
  266. 'provider_media.path as provider_photo_path',
  267. ])
  268. ->get();
  269. $schedulesProposals->each(function ($item) {
  270. $item->provider_photo = $item->provider_photo_path
  271. ? Storage::temporaryUrl($item->provider_photo_path, now()->addMinutes(60))
  272. : null;
  273. unset($item->provider_photo_path);
  274. });
  275. $todaySchedules = Schedule::with('address:district,address,number,source_id,source,id,address_type')
  276. ->where('schedules.client_id', $cliente->id)
  277. ->whereIn('schedules.status', ['accepted', 'paid', 'started', 'finished'])
  278. ->whereDate('schedules.date', now()->toDateString())
  279. ->leftJoin('providers', 'providers.id', '=', 'schedules.provider_id')
  280. ->leftJoin('users as provider_user', 'provider_user.id', '=', 'providers.user_id')
  281. ->leftJoin('media', 'media.id', '=', 'providers.profile_media_id')
  282. ->select(
  283. 'schedules.id',
  284. 'schedules.provider_id',
  285. 'provider_user.name as provider_name',
  286. 'schedules.date',
  287. 'schedules.start_time',
  288. 'schedules.end_time',
  289. 'schedules.total_amount',
  290. 'schedules.period_type',
  291. 'schedules.schedule_type',
  292. 'schedules.address_id',
  293. 'schedules.status',
  294. 'schedules.code_verified',
  295. 'schedules.code',
  296. 'media.path as provider_photo',
  297. DB::raw("EXISTS(
  298. SELECT 1 FROM reviews
  299. WHERE reviews.schedule_id = schedules.id
  300. AND reviews.origin = 'client'
  301. AND reviews.origin_id = {$cliente->id}
  302. AND reviews.deleted_at IS NULL
  303. ) as client_reviewed"),
  304. )
  305. ->orderBy('schedules.start_time', 'asc')
  306. ->get()
  307. ->map(function ($item) {
  308. $item->provider_photo = $item->provider_photo
  309. ? Storage::temporaryUrl($item->provider_photo, now()->addMinutes(60))
  310. : null;
  311. return $item;
  312. });
  313. $notifications = Notification::where('user_id', $user->id)
  314. ->orderBy('read', 'asc')
  315. ->orderBy('created_at', 'desc')
  316. ->limit(10)
  317. ->get()
  318. ->map(function ($notification) {
  319. return [
  320. 'id' => $notification->id,
  321. 'title' => $notification->title,
  322. 'description' => $notification->description,
  323. 'time' => $notification->created_at->diffForHumans(),
  324. 'read' => $notification->read,
  325. 'avatar' => '/icons/avatar.svg',
  326. ];
  327. });
  328. $hasPaymentMethods = ClientPaymentMethod::where('client_id', $cliente->id)->exists();
  329. return [
  330. 'headerBar' => $headerBar,
  331. 'summaryInfos' => $summaryInfos,
  332. 'pendingSchedules' => $pendingSchedules,
  333. 'nextSchedules' => $nextSchedules,
  334. 'lastDoneSchedules' => $lastDoneSchedules,
  335. 'favoriteProviders' => $favoriteProviders,
  336. 'providersClose' => $providersClose,
  337. 'todaySchedules' => $todaySchedules,
  338. 'schedulesProposals' => $schedulesProposals,
  339. 'notifications' => $notifications,
  340. 'has_payment_methods' => $hasPaymentMethods,
  341. ];
  342. }
  343. public function getScheduleClienteDetails(int $scheduleId): array
  344. {
  345. $user = Auth::user();
  346. $cliente = Client::where('user_id', $user->id)->firstOrFail();
  347. $schedule = Schedule::where('schedules.id', $scheduleId)
  348. ->where('schedules.client_id', $cliente->id)
  349. ->leftJoin('providers', 'providers.id', '=', 'schedules.provider_id')
  350. ->leftJoin('users as provider_user', 'provider_user.id', '=', 'providers.user_id')
  351. ->leftJoin('custom_schedules', 'custom_schedules.schedule_id', '=', 'schedules.id')
  352. ->leftJoin('media', 'media.id', '=', 'providers.profile_media_id')
  353. ->select(
  354. 'schedules.provider_id',
  355. 'provider_user.name as provider_name',
  356. 'providers.birth_date as provider_birth_date',
  357. 'media.path as provider_photo',
  358. 'custom_schedules.offers_meal',
  359. )
  360. ->firstOrFail();
  361. $allSpecialities = Speciality::where('active', true)
  362. ->select('id', 'description')
  363. ->orderBy('description')
  364. ->get();
  365. $providerSpecialityIds = ProviderSpeciality::where('provider_id', $schedule->provider_id)
  366. ->pluck('speciality_id')
  367. ->all();
  368. $specialities = $allSpecialities->map(fn($sp) => [
  369. 'id' => $sp->id,
  370. 'description' => $sp->description,
  371. 'has_speciality' => in_array($sp->id, $providerSpecialityIds),
  372. ])->values();
  373. return [
  374. 'provider_name' => $schedule->provider_name,
  375. 'provider_birth_date' => $schedule->provider_birth_date,
  376. 'provider_photo' => $schedule->provider_photo
  377. ? Storage::temporaryUrl($schedule->provider_photo, now()->addMinutes(60))
  378. : null,
  379. 'offers_meal' => $schedule->offers_meal,
  380. 'specialities' => $specialities,
  381. ];
  382. }
  383. public function dadosDashboardPrestador(): array
  384. {
  385. $user = Auth::user();
  386. if ($user->type !== UserTypeEnum::PROVIDER) {
  387. throw new AuthorizationException('Apenas prestadores podem acessar este recurso.');
  388. }
  389. $provider = Provider::with('profileMedia')->where('user_id', $user->id)->first();
  390. $headerBar = [
  391. 'rating' => $provider->average_rating,
  392. 'total_ratings' => Review::where('reviews.origin', 'client')->leftJoin('schedules', 'schedules.id', '=', 'reviews.schedule_id')->where('schedules.provider_id', $provider->id)->count(),
  393. 'total_services' => $provider->total_services,
  394. ];
  395. $address = Address::where('source', 'provider')->where('source_id', $provider->id)->with(['city', 'state'])->first();
  396. $summaryInfos = [
  397. 'name' => $user->name,
  398. 'profile_photo' => $provider->profileMedia?->path
  399. ? Storage::temporaryUrl($provider->profileMedia->path, now()->addMinutes(60))
  400. : null,
  401. 'address' => $address,
  402. 'pending_services' => Schedule::where('provider_id', $provider->id)->where('status', 'pending')->count(),
  403. ];
  404. $priceSuggestedAvg = Provider::where('user_id', '!=', $user->id)
  405. ->whereNotNull('daily_price_8h')
  406. ->pluck('daily_price_8h')
  407. ->avg();
  408. $priceActual = $provider->daily_price_8h;
  409. $priceSuggested = [
  410. 'average_price' => $priceSuggestedAvg,
  411. 'your_price' => $priceActual,
  412. ];
  413. $solicitations = Schedule::with('address:district,source_id,source,id,latitude,longitude')
  414. ->where('schedules.provider_id', $provider->id)
  415. ->where('schedules.status', 'pending')
  416. ->leftJoin('clients', 'clients.id', '=', 'schedules.client_id')
  417. ->leftJoin('users as client_user', 'client_user.id', '=', 'clients.user_id')
  418. ->leftJoin('media as client_media', 'client_media.id', '=', 'clients.profile_media_id')
  419. ->leftJoin('custom_schedules', 'custom_schedules.schedule_id', '=', 'schedules.id')
  420. ->select(
  421. 'schedules.id',
  422. 'client_user.name as client_name',
  423. 'clients.average_rating',
  424. 'schedules.date',
  425. DB::raw("TO_CHAR(schedules.date, 'DD/MM/YYYY') as formatted_date"),
  426. 'schedules.start_time',
  427. 'schedules.end_time',
  428. 'schedules.total_amount',
  429. 'schedules.period_type',
  430. 'schedules.schedule_type',
  431. 'schedules.address_id',
  432. 'schedules.status',
  433. 'custom_schedules.offers_meal',
  434. 'client_media.path as customer_photo_path',
  435. DB::raw("CASE
  436. WHEN (now() - schedules.created_at) < INTERVAL '1 day' THEN CONCAT(ROUND(EXTRACT(EPOCH FROM (now() - schedules.created_at)) / 3600), ' hours ago')
  437. ELSE CONCAT(ROUND(EXTRACT(EPOCH FROM (now() - schedules.created_at)) / 86400), ' days ago')
  438. END as time_since_request"),
  439. )
  440. ->orderBy('schedules.date', 'asc')
  441. ->get();
  442. $providerLat = $address?->latitude !== null ? (float) $address->latitude : null;
  443. $providerLng = $address?->longitude !== null ? (float) $address->longitude : null;
  444. $solicitations->each(function ($solicitation) use ($providerLat, $providerLng) {
  445. $solicitation->customer_photo = $solicitation->customer_photo_path
  446. ? Storage::temporaryUrl($solicitation->customer_photo_path, now()->addMinutes(60))
  447. : null;
  448. unset($solicitation->customer_photo_path);
  449. $solicitation->distance_km = DistanceService::calculate(
  450. $providerLat,
  451. $providerLng,
  452. $solicitation->address?->latitude !== null ? (float) $solicitation->address->latitude : null,
  453. $solicitation->address?->longitude !== null ? (float) $solicitation->address->longitude : null,
  454. );
  455. });
  456. $todayServices = Schedule::with('address:district,address,number,source_id,source,id')
  457. ->where('schedules.provider_id', $provider->id)
  458. ->whereIn('schedules.status', ['accepted', 'paid', 'started', 'finished'])
  459. ->whereDate('schedules.date', now()->toDateString())
  460. ->leftJoin('clients', 'clients.id', '=', 'schedules.client_id')
  461. ->leftJoin('users as client_user', 'client_user.id', '=', 'clients.user_id')
  462. ->leftJoin('media as client_media', 'client_media.id', '=', 'clients.profile_media_id')
  463. ->leftJoin('custom_schedules', 'custom_schedules.schedule_id', '=', 'schedules.id')
  464. ->select(
  465. 'schedules.id',
  466. 'schedules.client_id',
  467. 'client_user.name as client_name',
  468. 'schedules.date',
  469. 'schedules.start_time',
  470. 'schedules.end_time',
  471. 'schedules.total_amount',
  472. 'schedules.period_type',
  473. 'schedules.address_id',
  474. 'schedules.schedule_type',
  475. 'schedules.status',
  476. 'schedules.code_verified',
  477. 'schedules.code',
  478. 'custom_schedules.offers_meal',
  479. 'client_media.path as customer_photo_path',
  480. DB::raw("EXISTS(
  481. SELECT 1 FROM reviews
  482. WHERE reviews.schedule_id = schedules.id
  483. AND reviews.origin = 'provider'
  484. AND reviews.origin_id = {$provider->id}
  485. AND reviews.deleted_at IS NULL
  486. ) as provider_reviewed"),
  487. )
  488. ->orderBy('schedules.start_time', 'asc')
  489. ->get();
  490. $todayServices->each(function ($s) {
  491. $s->customer_photo = $s->customer_photo_path
  492. ? Storage::temporaryUrl($s->customer_photo_path, now()->addMinutes(60))
  493. : null;
  494. unset($s->customer_photo_path);
  495. });
  496. $nextSchedules = Schedule::with('address:district,address,number,source_id,source,id,latitude,longitude')
  497. ->where('schedules.provider_id', $provider->id)
  498. ->whereIn('schedules.status', ['accepted', 'paid'])
  499. ->whereDate('schedules.date', '>=', now()->toDateString())
  500. ->leftJoin('clients', 'clients.id', '=', 'schedules.client_id')
  501. ->leftJoin('users as client_user', 'client_user.id', '=', 'clients.user_id')
  502. ->leftJoin('media as client_media', 'client_media.id', '=', 'clients.profile_media_id')
  503. ->leftJoin('custom_schedules', 'custom_schedules.schedule_id', '=', 'schedules.id')
  504. ->select(
  505. 'schedules.id',
  506. 'client_user.name as client_name',
  507. 'schedules.date',
  508. 'schedules.start_time',
  509. 'schedules.end_time',
  510. 'schedules.total_amount',
  511. 'schedules.period_type',
  512. 'schedules.address_id',
  513. 'schedules.schedule_type',
  514. 'schedules.status',
  515. 'custom_schedules.offers_meal',
  516. 'client_media.path as customer_photo_path',
  517. )
  518. ->orderBy('schedules.date', 'asc')
  519. ->get();
  520. $nextSchedules->each(function ($schedule) use ($providerLat, $providerLng) {
  521. $schedule->customer_photo = $schedule->customer_photo_path
  522. ? Storage::temporaryUrl($schedule->customer_photo_path, now()->addMinutes(60))
  523. : null;
  524. unset($schedule->customer_photo_path);
  525. $schedule->distance_km = DistanceService::calculate(
  526. $providerLat,
  527. $providerLng,
  528. $schedule->address?->latitude !== null ? (float) $schedule->address->latitude : null,
  529. $schedule->address?->longitude !== null ? (float) $schedule->address->longitude : null,
  530. );
  531. });
  532. // $opportunities = Schedule::with('address:district,source_id,source,id')
  533. // ->where('schedules.schedule_type', 'custom')
  534. // ->where('schedules.status', 'pending')
  535. // ->whereDate('schedules.date', '>=', now()->toDateString())
  536. // ->leftJoin('clients', 'clients.id', '=', 'schedules.client_id')
  537. // ->leftJoin('users as client_user', 'client_user.id', '=', 'clients.user_id')
  538. // ->leftJoin('custom_schedules', 'custom_schedules.schedule_id', '=', 'schedules.id')
  539. // ->select(
  540. // 'schedules.id',
  541. // 'custom_schedules.id as custom_schedule_id',
  542. // 'client_user.name as client_name',
  543. // 'clients.average_rating',
  544. // 'schedules.date',
  545. // 'schedules.start_time',
  546. // 'schedules.end_time',
  547. // 'schedules.period_type',
  548. // 'schedules.schedule_type',
  549. // 'schedules.address_id',
  550. // 'custom_schedules.address_type',
  551. // DB::raw("CASE
  552. // WHEN schedules.period_type = '2' THEN {$provider->daily_price_2h}
  553. // WHEN schedules.period_type = '4' THEN {$provider->daily_price_4h}
  554. // WHEN schedules.period_type = '6' THEN {$provider->daily_price_6h}
  555. // WHEN schedules.period_type = '8' THEN {$provider->daily_price_8h}
  556. // ELSE 0
  557. // END as total_amount"),
  558. // )
  559. // ->orderBy('schedules.date', 'asc')
  560. // ->get();
  561. $notifications = Notification::where('user_id', $user->id)
  562. ->orderBy('read', 'asc')
  563. ->orderBy('created_at', 'desc')
  564. ->limit(10)
  565. ->get()
  566. ->map(function ($notification) {
  567. return [
  568. 'id' => $notification->id,
  569. 'title' => $notification->title,
  570. 'description' => $notification->description,
  571. 'time' => $notification->created_at->diffForHumans(),
  572. 'read' => $notification->read,
  573. 'avatar' => '/icons/avatar.svg',
  574. ];
  575. });
  576. $opportunities = $this->customScheduleService->getAvailableOpportunities($provider->id);
  577. $opportunities->each(function ($o) {
  578. $o->customer_photo = $o->client?->profileMedia?->path
  579. ? Storage::temporaryUrl($o->client->profileMedia->path, now()->addMinutes(60))
  580. : null;
  581. });
  582. return [
  583. 'headerBar' => $headerBar,
  584. 'summaryInfos' => $summaryInfos,
  585. 'priceSuggested' => $priceSuggested,
  586. 'solicitations' => $solicitations,
  587. 'todayServices' => $todayServices,
  588. 'nextSchedules' => $nextSchedules,
  589. 'opportunities' => $opportunities,
  590. 'notifications' => $notifications,
  591. ];
  592. }
  593. private function distanceSelect(?float $clientLatitude, ?float $clientLongitude): \Illuminate\Contracts\Database\Query\Expression
  594. {
  595. return DistanceService::sqlExpression($clientLatitude, $clientLongitude);
  596. }
  597. }