Schedule.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\SoftDeletes;
  6. use Illuminate\Support\Collection;
  7. /**
  8. * @property int $id
  9. * @property int $client_id
  10. * @property int|null $provider_id
  11. * @property int $address_id
  12. * @property \Illuminate\Support\Carbon $date
  13. * @property string $period_type
  14. * @property string $schedule_type
  15. * @property string $start_time
  16. * @property string $end_time
  17. * @property string $status
  18. * @property numeric $total_amount
  19. * @property string $code
  20. * @property bool $code_verified
  21. * @property \Illuminate\Support\Carbon|null $created_at
  22. * @property \Illuminate\Support\Carbon|null $updated_at
  23. * @property \Illuminate\Support\Carbon|null $deleted_at
  24. * @property bool|null $offers_meal
  25. * @property string|null $cancel_text
  26. * @property string|null $cancelled_by
  27. * @property-read \App\Models\Address $address
  28. * @property-read \App\Models\Client $client
  29. * @property-read \App\Models\CustomSchedule|null $customSchedule
  30. * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ScheduleProposal> $proposals
  31. * @property-read int|null $proposals_count
  32. * @property-read \App\Models\Provider|null $provider
  33. * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ScheduleRefuse> $refuses
  34. * @property-read int|null $refuses_count
  35. * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Review> $reviews
  36. * @property-read int|null $reviews_count
  37. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule newModelQuery()
  38. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule newQuery()
  39. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule onlyTrashed()
  40. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule query()
  41. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereAddressId($value)
  42. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereCancelText($value)
  43. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereCancelledBy($value)
  44. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereClientId($value)
  45. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereCode($value)
  46. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereCodeVerified($value)
  47. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereCreatedAt($value)
  48. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereDate($value)
  49. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereDeletedAt($value)
  50. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereEndTime($value)
  51. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereId($value)
  52. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereOffersMeal($value)
  53. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule wherePeriodType($value)
  54. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereProviderId($value)
  55. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereScheduleType($value)
  56. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereStartTime($value)
  57. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereStatus($value)
  58. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereTotalAmount($value)
  59. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule whereUpdatedAt($value)
  60. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule withTrashed(bool $withTrashed = true)
  61. * @method static \Illuminate\Database\Eloquent\Builder<static>|Schedule withoutTrashed()
  62. * @mixin \Eloquent
  63. */
  64. class Schedule extends Model
  65. {
  66. use HasFactory, SoftDeletes;
  67. protected $fillable = [
  68. 'client_id',
  69. 'provider_id',
  70. 'address_id',
  71. 'date',
  72. 'period_type',
  73. 'schedule_type',
  74. 'start_time',
  75. 'end_time',
  76. 'status',
  77. 'total_amount',
  78. 'code',
  79. 'code_verified',
  80. 'offers_meal',
  81. 'cancel_text',
  82. 'cancelled_by',
  83. ];
  84. protected $casts = [
  85. 'date' => 'date',
  86. 'code_verified' => 'boolean',
  87. 'total_amount' => 'decimal:2',
  88. 'offers_meal' => 'boolean',
  89. ];
  90. public function client()
  91. {
  92. return $this->belongsTo(Client::class)->select(
  93. 'id',
  94. 'user_id',
  95. 'document',
  96. 'average_rating',
  97. 'gateway_customer_id',
  98. 'gateway_customer_code',
  99. 'profile_media_id',
  100. );
  101. }
  102. public function provider()
  103. {
  104. return $this->belongsTo(Provider::class);
  105. }
  106. //
  107. public function address()
  108. {
  109. return $this->belongsTo(Address::class, 'address_id')->select('id', 'district');
  110. }
  111. public function customSchedule()
  112. {
  113. return $this->hasOne(CustomSchedule::class)->select('id', 'service_type_id', 'min_price', 'max_price', 'schedule_id');
  114. }
  115. public function proposals()
  116. {
  117. return $this->hasMany(ScheduleProposal::class);
  118. }
  119. public function refuses()
  120. {
  121. return $this->hasMany(ScheduleRefuse::class);
  122. }
  123. public function reviews()
  124. {
  125. return $this->hasMany(Review::class);
  126. }
  127. public function getSpecialitiesAttribute(): Collection
  128. {
  129. if ($this->schedule_type !== 'custom') {
  130. return collect();
  131. }
  132. return $this->customSchedule?->specialities
  133. ->sortBy('description')
  134. ->map(fn(CustomScheduleSpeciality $speciality) => [
  135. 'id' => $speciality->id,
  136. 'description' => $speciality->description,
  137. ])
  138. ->values() ?? collect();
  139. }
  140. //
  141. public function ensureCustomerPhone(?string $fallbackPhone = null): void
  142. {
  143. $phone = $this->client?->user?->phone ?? $fallbackPhone;
  144. $digits = preg_replace('/\D+/', '', (string) $phone) ?? '';
  145. if (strlen($digits) >= 10) {
  146. return;
  147. }
  148. throw new \InvalidArgumentException(
  149. 'Voce precisa cadastrar um numero de celular valido no seu perfil para concluir o pagamento.'
  150. );
  151. }
  152. }