Provider.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Models;
  3. use App\Enums\ApprovalStatusEnum;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  7. use Illuminate\Database\Eloquent\Relations\HasMany;
  8. use Illuminate\Database\Eloquent\Relations\HasOne;
  9. use Illuminate\Database\Eloquent\SoftDeletes;
  10. /**
  11. * @property int $id
  12. * @property string $document
  13. * @property string|null $rg
  14. * @property int $user_id
  15. * @property float|null $average_rating
  16. * @property int $total_services
  17. * @property string|null $birth_date
  18. * @property bool $selfie_verified
  19. * @property bool $document_verified
  20. * @property string $approval_status
  21. * @property float|null $daily_price_8h
  22. * @property float|null $daily_price_6h
  23. * @property float|null $daily_price_4h
  24. * @property float|null $daily_price_2h
  25. * @property int|null $profile_media_id
  26. * @property \Illuminate\Support\Carbon|null $created_at
  27. * @property \Illuminate\Support\Carbon|null $updated_at
  28. * @property \Illuminate\Support\Carbon|null $deleted_at
  29. * @property string|null $selfie_media_base64
  30. * @property string|null $document_front_media_base64
  31. * @property string|null $document_back_media_base64
  32. * @property ApprovalStatusEnum $approval_status
  33. * @property string|null $recipient_id
  34. * @property string|null $recipient_name
  35. * @property string|null $recipient_email
  36. * @property string|null $recipient_description
  37. * @property string|null $recipient_document
  38. * @property string|null $recipient_type
  39. * @property string|null $recipient_code
  40. * @property string|null $recipient_payment_mode
  41. * @property array<array-key, mixed>|null $recipient_default_bank_account
  42. * @property array<array-key, mixed>|null $recipient_transfer_settings
  43. * @property array<array-key, mixed>|null $recipient_automatic_anticipation_settings
  44. * @property array<array-key, mixed>|null $recipient_metadata
  45. * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Address> $addresses
  46. * @property-read int|null $addresses_count
  47. * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ClientProviderBlock> $blockedByClients
  48. * @property-read int|null $blocked_by_clients_count
  49. * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ProviderClientBlock> $blockedClients
  50. * @property-read int|null $blocked_clients_count
  51. * @property-read \App\Models\Address|null $primaryAddress
  52. * @property-read \App\Models\Media|null $profileMedia
  53. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider newModelQuery()
  54. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider newQuery()
  55. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider onlyTrashed()
  56. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider query()
  57. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereApprovalStatus($value)
  58. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereAverageRating($value)
  59. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereBirthDate($value)
  60. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereCreatedAt($value)
  61. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereDailyPrice2h($value)
  62. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereDailyPrice4h($value)
  63. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereDailyPrice6h($value)
  64. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereDailyPrice8h($value)
  65. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereDeletedAt($value)
  66. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereDocument($value)
  67. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereDocumentBackMediaBase64($value)
  68. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereDocumentFrontMediaBase64($value)
  69. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereDocumentVerified($value)
  70. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereId($value)
  71. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereProfileMediaId($value)
  72. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereRecipientAutomaticAnticipationSettings($value)
  73. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereRecipientCode($value)
  74. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereRecipientDefaultBankAccount($value)
  75. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereRecipientDescription($value)
  76. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereRecipientDocument($value)
  77. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereRecipientEmail($value)
  78. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereRecipientId($value)
  79. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereRecipientMetadata($value)
  80. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereRecipientName($value)
  81. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereRecipientPaymentMode($value)
  82. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereRecipientTransferSettings($value)
  83. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereRecipientType($value)
  84. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereRg($value)
  85. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereSelfieMediaBase64($value)
  86. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereSelfieVerified($value)
  87. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereTotalServices($value)
  88. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereUpdatedAt($value)
  89. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider whereUserId($value)
  90. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider withTrashed(bool $withTrashed = true)
  91. * @method static \Illuminate\Database\Eloquent\Builder<static>|Provider withoutTrashed()
  92. * @mixin \Eloquent
  93. */
  94. class Provider extends Model
  95. {
  96. use HasFactory, SoftDeletes;
  97. protected $table = "providers";
  98. protected $guarded = ["id"];
  99. /**
  100. * Get the attributes that should be cast.
  101. *
  102. * @return array<string, string>
  103. */
  104. protected function casts(): array
  105. {
  106. return [
  107. 'birth_date' => 'date',
  108. 'selfie_verified' => 'boolean',
  109. 'document_verified' => 'boolean',
  110. 'approval_status' => ApprovalStatusEnum::class,
  111. 'average_rating' => 'decimal:1',
  112. 'daily_price_8h' => 'decimal:2',
  113. 'daily_price_6h' => 'decimal:2',
  114. 'daily_price_4h' => 'decimal:2',
  115. 'daily_price_2h' => 'decimal:2',
  116. 'total_services' => 'integer',
  117. 'recipient_default_bank_account' => 'array',
  118. 'recipient_transfer_settings' => 'array',
  119. 'recipient_automatic_anticipation_settings' => 'array',
  120. 'recipient_metadata' => 'array',
  121. ];
  122. }
  123. public function user(): BelongsTo
  124. {
  125. return $this->belongsTo(User::class, 'user_id');
  126. }
  127. /**
  128. * @return BelongsTo
  129. */
  130. public function profileMedia(): BelongsTo
  131. {
  132. return $this->belongsTo(Media::class, "profile_media_id");
  133. }
  134. /**
  135. * @return HasMany
  136. */
  137. public function addresses(): HasMany
  138. {
  139. return $this->hasMany(Address::class, 'source_id')
  140. ->where('source', 'provider');
  141. }
  142. // /**
  143. // * @return HasOne
  144. // */
  145. // public function primaryAddress(): HasOne
  146. // {
  147. // return $this->hasOne(Address::class, 'source_id')
  148. // ->where('source', 'provider')
  149. // ->where('is_primary', true);
  150. // }
  151. /**
  152. * @return HasMany
  153. */
  154. public function blockedClients()
  155. {
  156. return $this->hasMany(ProviderClientBlock::class);
  157. }
  158. /**
  159. * @return \Illuminate\Database\Eloquent\Relations\HasMany
  160. */
  161. public function blockedByClients()
  162. {
  163. return $this->hasMany(ClientProviderBlock::class);
  164. }
  165. public function updateAverageRating(float $newRating): void
  166. {
  167. $totalReviews = Review::where('reviews.origin', 'client')
  168. ->leftJoin('schedules', 'schedules.id', '=', 'reviews.schedule_id')
  169. ->where('schedules.provider_id', $this->id)
  170. ->count();
  171. if ($totalReviews === 0) {
  172. $this->average_rating = $newRating;
  173. } else {
  174. $currentTotalRating = $this->average_rating * ($totalReviews - 1);
  175. $newAverage = ($currentTotalRating + $newRating) / $totalReviews;
  176. $this->average_rating = round($newAverage, 2);
  177. }
  178. $this->save();
  179. }
  180. public function primaryAddress()
  181. {
  182. return $this->hasOne(Address::class, "source_id")
  183. ->where("source", "provider")
  184. ->orderBy("is_primary", "desc");
  185. }
  186. }