Provider.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. use Illuminate\Database\Eloquent\Relations\HasMany;
  7. use Illuminate\Database\Eloquent\Relations\HasOne;
  8. use Illuminate\Database\Eloquent\SoftDeletes;
  9. /**
  10. * @property int $id
  11. * @property string $document
  12. * @property string|null $rg
  13. * @property int $user_id
  14. * @property float|null $average_rating
  15. * @property int $total_services
  16. * @property string|null $birth_date
  17. * @property bool $selfie_verified
  18. * @property bool $document_verified
  19. * @property bool $is_approved
  20. * @property float|null $daily_price_8h
  21. * @property float|null $daily_price_6h
  22. * @property float|null $daily_price_4h
  23. * @property float|null $daily_price_2h
  24. * @property int|null $profile_media_id
  25. * @property \Illuminate\Support\Carbon|null $created_at
  26. * @property \Illuminate\Support\Carbon|null $updated_at
  27. * @property \Illuminate\Support\Carbon|null $deleted_at
  28. * @property-read \App\Models\User $user
  29. * @property-read \App\Models\Media|null $profileMedia
  30. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider newModelQuery()
  31. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider newQuery()
  32. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider onlyTrashed()
  33. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider query()
  34. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider withTrashed(bool $withTrashed = true)
  35. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider withoutTrashed()
  36. * @mixin \Eloquent
  37. */
  38. class Provider extends Model
  39. {
  40. use HasFactory, SoftDeletes;
  41. protected $table = "providers";
  42. protected $guarded = ["id"];
  43. /**
  44. * Get the attributes that should be cast.
  45. *
  46. * @return array<string, string>
  47. */
  48. protected function casts(): array
  49. {
  50. return [
  51. "birth_date" => "date",
  52. "selfie_verified" => "boolean",
  53. "document_verified" => "boolean",
  54. "is_approved" => "boolean",
  55. "average_rating" => "decimal:1",
  56. "daily_price_8h" => "decimal:2",
  57. "daily_price_6h" => "decimal:2",
  58. "daily_price_4h" => "decimal:2",
  59. "daily_price_2h" => "decimal:2",
  60. "total_services" => "integer",
  61. ];
  62. }
  63. /**
  64. * @return BelongsTo
  65. */
  66. public function user(): BelongsTo
  67. {
  68. return $this->belongsTo(User::class, "user_id");
  69. }
  70. /**
  71. * @return BelongsTo
  72. */
  73. public function profileMedia(): BelongsTo
  74. {
  75. return $this->belongsTo(Media::class, "profile_media_id");
  76. }
  77. /**
  78. * @return HasMany
  79. */
  80. public function addresses(): HasMany
  81. {
  82. return $this->hasMany(Address::class, 'source_id')
  83. ->where('source', 'provider');
  84. }
  85. /**
  86. * @return HasOne
  87. */
  88. public function primaryAddress(): HasOne
  89. {
  90. return $this->hasOne(Address::class, 'source_id')
  91. ->where('source', 'provider')
  92. ->where('is_primary', true);
  93. }
  94. /**
  95. * @return HasMany
  96. */
  97. public function blockedClients()
  98. {
  99. return $this->hasMany(ProviderClientBlock::class);
  100. }
  101. /**
  102. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  103. */
  104. public function blockedByClients()
  105. {
  106. return $this->hasMany(ClientProviderBlock::class);
  107. }
  108. public function updateAverageRating(float $newRating): void
  109. {
  110. $totalReviews = Review::where('reviews.origin', 'client')
  111. ->leftJoin('schedules', 'schedules.id', '=', 'reviews.schedule_id')
  112. ->where('schedules.provider_id', $this->id)
  113. ->count();
  114. if ($totalReviews === 0) {
  115. $this->average_rating = $newRating;
  116. } else {
  117. $currentTotalRating = $this->average_rating * ($totalReviews - 1);
  118. $newAverage = ($currentTotalRating + $newRating) / $totalReviews;
  119. $this->average_rating = round($newAverage, 2);
  120. }
  121. $this->save();
  122. }
  123. }