Client.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. 'external_customer_id',
  46. 'external_customer_code',
  47. 'user_id',
  48. ];
  49. protected $casts = [
  50. 'created_at' => 'datetime',
  51. 'updated_at' => 'datetime',
  52. 'deleted_at' => 'datetime',
  53. ];
  54. /**
  55. * @return BelongsTo
  56. */
  57. public function user(): BelongsTo
  58. {
  59. return $this->belongsTo(User::class)->select('id', 'name');
  60. }
  61. /**
  62. * @return HasMany
  63. */
  64. public function blockedByProviders(): HasMany
  65. {
  66. return $this->hasMany(ProviderClientBlock::class);
  67. }
  68. /**
  69. * @return HasMany
  70. */
  71. public function blockedProviders(): HasMany
  72. {
  73. return $this->hasMany(ClientProviderBlock::class);
  74. }
  75. public function updateAverageRating(float $newRating): void
  76. {
  77. $totalReviews = Review::where('reviews.origin', 'provider')
  78. ->leftJoin('schedules', 'schedules.id', '=', 'reviews.schedule_id')
  79. ->where('schedules.client_id', $this->id)
  80. ->count();
  81. if ($totalReviews === 0) {
  82. $this->average_rating = $newRating;
  83. } else {
  84. $currentTotalRating = $this->average_rating * ($totalReviews - 1);
  85. $newAverage = ($currentTotalRating + $newRating) / $totalReviews;
  86. $this->average_rating = round($newAverage, 2);
  87. }
  88. $this->save();
  89. }
  90. public function schedules()
  91. {
  92. return $this->hasMany(Schedule::class);
  93. }
  94. }