Client.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. use Illuminate\Support\Facades\Auth;
  10. /**
  11. * @property int $id
  12. * @property string|null $document
  13. * @property int $user_id
  14. * @property \Illuminate\Support\Carbon|null $created_at
  15. * @property \Illuminate\Support\Carbon|null $updated_at
  16. * @property \Illuminate\Support\Carbon|null $deleted_at
  17. * @property string|null $average_rating
  18. * @property int $total_services
  19. * @property string|null $gateway_customer_id
  20. * @property string|null $gateway_customer_code
  21. * @property int|null $profile_media_id
  22. * @property bool $selfie_verified
  23. * @property bool $first_access
  24. * @property string|null $idempotency_key
  25. * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ProviderClientBlock> $blockedByProviders
  26. * @property-read int|null $blocked_by_providers_count
  27. * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ClientProviderBlock> $blockedProviders
  28. * @property-read int|null $blocked_providers_count
  29. * @property-read \App\Models\Media|null $profileMedia
  30. * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Schedule> $schedules
  31. * @property-read int|null $schedules_count
  32. * @property-read \App\Models\User $user
  33. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client newModelQuery()
  34. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client newQuery()
  35. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client onlyTrashed()
  36. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client query()
  37. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereAverageRating($value)
  38. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereCreatedAt($value)
  39. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereDeletedAt($value)
  40. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereDocument($value)
  41. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereGatewayCustomerCode($value)
  42. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereGatewayCustomerId($value)
  43. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereId($value)
  44. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereIdempotencyKey($value)
  45. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereProfileMediaId($value)
  46. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereTotalServices($value)
  47. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereUpdatedAt($value)
  48. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client whereUserId($value)
  49. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client withTrashed(bool $withTrashed = true)
  50. * @method static \Illuminate\Database\Eloquent\Builder<static>|Client withoutTrashed()
  51. * @mixin \Eloquent
  52. */
  53. class Client extends Model
  54. {
  55. use HasFactory, SoftDeletes;
  56. protected $fillable = [
  57. 'document',
  58. 'gateway_customer_id',
  59. 'gateway_customer_code',
  60. 'idempotency_key',
  61. 'user_id',
  62. 'profile_media_id',
  63. 'selfie_verified',
  64. 'first_access',
  65. ];
  66. protected $casts = [
  67. 'created_at' => 'datetime',
  68. 'updated_at' => 'datetime',
  69. 'deleted_at' => 'datetime',
  70. 'selfie_verified' => 'boolean',
  71. 'first_access' => 'boolean',
  72. ];
  73. public function user(): BelongsTo
  74. {
  75. return $this->belongsTo(User::class)->select('id', 'name', 'email', 'phone');
  76. }
  77. //
  78. public function blockedByProviders(): HasMany
  79. {
  80. return $this->hasMany(ProviderClientBlock::class);
  81. }
  82. public function blockedProviders(): HasMany
  83. {
  84. return $this->hasMany(ClientProviderBlock::class);
  85. }
  86. public function profileMedia(): BelongsTo
  87. {
  88. $authenticatedUser = Auth::user();
  89. $relation = $this->belongsTo(Media::class, 'profile_media_id')
  90. ->join('clients as profile_media_clients', function ($join) {
  91. $join->on('profile_media_clients.id', '=', 'media.source_id')
  92. ->on('profile_media_clients.profile_media_id', '=', 'media.id');
  93. })
  94. ->where('media.source', 'client')
  95. ->select('media.*');
  96. if ($authenticatedUser?->canModerateClients()) {
  97. return $relation;
  98. }
  99. return $relation->where(function ($query) use ($authenticatedUser) {
  100. $query->where('profile_media_clients.selfie_verified', true);
  101. if ($authenticatedUser !== null) {
  102. $query->orWhere('profile_media_clients.user_id', $authenticatedUser->id);
  103. }
  104. });
  105. }
  106. public function schedules()
  107. {
  108. return $this->hasMany(Schedule::class);
  109. }
  110. public function updateAverageRating(float $newRating): void
  111. {
  112. $totalReviews = Review::where('reviews.origin', 'provider')
  113. ->leftJoin('schedules', 'schedules.id', '=', 'reviews.schedule_id')
  114. ->where('schedules.client_id', $this->id)
  115. ->count();
  116. if ($totalReviews === 0) {
  117. $this->average_rating = $newRating;
  118. } else {
  119. $currentTotalRating = $this->average_rating * ($totalReviews - 1);
  120. $newAverage = ($currentTotalRating + $newRating) / $totalReviews;
  121. $this->average_rating = round($newAverage, 2);
  122. }
  123. $this->save();
  124. }
  125. //
  126. public function ensureGatewayCode(): string
  127. {
  128. if (! empty($this->gateway_customer_code)) {
  129. return $this->gateway_customer_code;
  130. }
  131. $code = 'client-'.(string) \Illuminate\Support\Str::uuid();
  132. $this->forceFill(['gateway_customer_code' => $code])->save();
  133. return $code;
  134. }
  135. }