Client.php 5.8 KB

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