Provider.php 4.1 KB

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