Client.php 4.7 KB

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