Client.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 whereGatewayCustomerCode($value)
  39. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereGatewayCustomerId($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. //
  71. public function blockedByProviders(): HasMany
  72. {
  73. return $this->hasMany(ProviderClientBlock::class);
  74. }
  75. public function blockedProviders(): HasMany
  76. {
  77. return $this->hasMany(ClientProviderBlock::class);
  78. }
  79. public function profileMedia(): BelongsTo
  80. {
  81. return $this->belongsTo(Media::class, 'profile_media_id');
  82. }
  83. public function schedules()
  84. {
  85. return $this->hasMany(Schedule::class);
  86. }
  87. public function updateAverageRating(float $newRating): void
  88. {
  89. $totalReviews = Review::where('reviews.origin', 'provider')
  90. ->leftJoin('schedules', 'schedules.id', '=', 'reviews.schedule_id')
  91. ->where('schedules.client_id', $this->id)
  92. ->count();
  93. if ($totalReviews === 0) {
  94. $this->average_rating = $newRating;
  95. } else {
  96. $currentTotalRating = $this->average_rating * ($totalReviews - 1);
  97. $newAverage = ($currentTotalRating + $newRating) / $totalReviews;
  98. $this->average_rating = round($newAverage, 2);
  99. }
  100. $this->save();
  101. }
  102. //
  103. public function ensureGatewayCode(): string
  104. {
  105. if (! empty($this->gateway_customer_code)) {
  106. return $this->gateway_customer_code;
  107. }
  108. $code = 'client-'.(string) \Illuminate\Support\Str::uuid();
  109. $this->forceFill(['gateway_customer_code' => $code])->save();
  110. return $code;
  111. }
  112. }