ClientFavoriteProviderResource.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Http\Resources;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Http\Resources\Json\JsonResource;
  5. use Illuminate\Support\Facades\Storage;
  6. class ClientFavoriteProviderResource extends JsonResource
  7. {
  8. public function toArray(Request $request): array
  9. {
  10. $provider = $this->resource->relationLoaded('provider') ? $this->resource->provider : null;
  11. $profileMediaPath = $this->profile_media_path
  12. ?? ($provider?->relationLoaded('profileMedia') ? $provider->profileMedia?->path : null);
  13. $dailyPrice8h = $this->daily_price_8h ?? $provider?->daily_price_8h;
  14. $dailyPrice6h = $this->daily_price_6h ?? $provider?->daily_price_6h;
  15. $dailyPrice4h = $this->daily_price_4h ?? $provider?->daily_price_4h;
  16. $dailyPrice2h = $this->daily_price_2h ?? $provider?->daily_price_2h;
  17. return [
  18. 'id' => $this->id,
  19. 'client_id' => $this->client_id,
  20. 'provider_id' => $this->provider_id,
  21. 'provider_name' => $this->provider_name ?? ($provider?->relationLoaded('user') ? $provider->user?->name : null),
  22. 'city_name' => $this->city_name ?? null,
  23. 'average_rating' => $this->average_rating ?? $provider?->average_rating,
  24. 'profile_media_url' => $profileMediaPath
  25. ? Storage::temporaryUrl($profileMediaPath, now()->addMinutes(60))
  26. : null,
  27. 'daily_price_8h_base' => $dailyPrice8h,
  28. 'daily_price_6h_base' => $dailyPrice6h,
  29. 'daily_price_4h_base' => $dailyPrice4h,
  30. 'daily_price_2h_base' => $dailyPrice2h,
  31. 'daily_price_8h' => $this->applyCreditCardFee($dailyPrice8h),
  32. 'daily_price_6h' => $this->applyCreditCardFee($dailyPrice6h),
  33. 'daily_price_4h' => $this->applyCreditCardFee($dailyPrice4h),
  34. 'daily_price_2h' => $this->applyCreditCardFee($dailyPrice2h),
  35. 'total_services ' => $this->total_services ?? $provider?->total_services,
  36. 'notes' => $this->notes,
  37. 'created_at' => $this->created_at?->format('Y-m-d H:i'),
  38. 'updated_at' => $this->updated_at?->format('Y-m-d H:i'),
  39. ];
  40. }
  41. private function applyCreditCardFee(mixed $price): ?float
  42. {
  43. if ($price === null) {
  44. return null;
  45. }
  46. $rate = config('services.pagarme.platform_credit_card_fee_rate', 0.16);
  47. if (is_string($rate)) {
  48. $rate = str_replace(',', '.', trim($rate));
  49. }
  50. $rate = (float) $rate;
  51. $rate = $rate > 1 ? $rate / 100 : $rate;
  52. return round((float) $price * (1 + $rate), 2);
  53. }
  54. }