Client.php 4.4 KB

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