Provider.php 11 KB

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