Client.php 4.1 KB

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