Schedule.php 6.5 KB

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