Client.php 5.0 KB

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