Client.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\SoftDeletes;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\Relations\HasMany;
  8. /**
  9. * @property int $id
  10. * @property string|null $document
  11. * @property int $user_id
  12. * @property \Illuminate\Support\Carbon|null $created_at
  13. * @property \Illuminate\Support\Carbon|null $updated_at
  14. * @property \Illuminate\Support\Carbon|null $deleted_at
  15. * @property string|null $average_rating
  16. * @property int $total_services
  17. * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ProviderClientBlock> $blockedByProviders
  18. * @property-read int|null $blocked_by_providers_count
  19. * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ClientProviderBlock> $blockedProviders
  20. * @property-read int|null $blocked_providers_count
  21. * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Schedule> $schedules
  22. * @property-read int|null $schedules_count
  23. * @property-read \App\Models\User $user
  24. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client newModelQuery()
  25. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client newQuery()
  26. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client onlyTrashed()
  27. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client query()
  28. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereAverageRating($value)
  29. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereCreatedAt($value)
  30. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereDeletedAt($value)
  31. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereDocument($value)
  32. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereId($value)
  33. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereTotalServices($value)
  34. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereUpdatedAt($value)
  35. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereUserId($value)
  36. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client withTrashed(bool $withTrashed = true)
  37. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client withoutTrashed()
  38. * @mixin \Eloquent
  39. */
  40. class Client extends Model
  41. {
  42. use HasFactory, SoftDeletes;
  43. protected $fillable = [
  44. 'document',
  45. 'user_id',
  46. ];
  47. protected $casts = [
  48. 'created_at' => 'datetime',
  49. 'updated_at' => 'datetime',
  50. 'deleted_at' => 'datetime',
  51. ];
  52. /**
  53. * @return BelongsTo
  54. */
  55. public function user(): BelongsTo
  56. {
  57. return $this->belongsTo(User::class)->select('id', 'name');
  58. }
  59. /**
  60. * @return HasMany
  61. */
  62. public function blockedByProviders(): HasMany
  63. {
  64. return $this->hasMany(ProviderClientBlock::class);
  65. }
  66. /**
  67. * @return HasMany
  68. */
  69. public function blockedProviders(): HasMany
  70. {
  71. return $this->hasMany(ClientProviderBlock::class);
  72. }
  73. public function updateAverageRating(float $newRating): void
  74. {
  75. $totalReviews = Review::where('reviews.origin', 'provider')
  76. ->leftJoin('schedules', 'schedules.id', '=', 'reviews.schedule_id')
  77. ->where('schedules.client_id', $this->id)
  78. ->count();
  79. if ($totalReviews === 0) {
  80. $this->average_rating = $newRating;
  81. } else {
  82. $currentTotalRating = $this->average_rating * ($totalReviews - 1);
  83. $newAverage = ($currentTotalRating + $newRating) / $totalReviews;
  84. $this->average_rating = round($newAverage, 2);
  85. }
  86. $this->save();
  87. }
  88. public function schedules()
  89. {
  90. return $this->hasMany(Schedule::class);
  91. }
  92. }