Client.php 4.1 KB

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