ClientFavoriteProviderResource.php 2.8 KB

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