DashboardService.php 29 KB

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