Schedule.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. 'offers_meal',
  82. 'cancel_text',
  83. 'cancelled_by',
  84. ];
  85. protected $casts = [
  86. 'date' => 'date',
  87. 'code_verified' => 'boolean',
  88. 'total_amount' => 'decimal:2',
  89. 'offers_meal' => 'boolean',
  90. ];
  91. public function client()
  92. {
  93. return $this->belongsTo(Client::class)->select(
  94. 'id',
  95. 'user_id',
  96. 'document',
  97. 'average_rating',
  98. 'gateway_customer_id',
  99. 'gateway_customer_code',
  100. 'profile_media_id',
  101. );
  102. }
  103. public function provider()
  104. {
  105. return $this->belongsTo(Provider::class);
  106. }
  107. //
  108. public function address()
  109. {
  110. return $this->belongsTo(Address::class, 'address_id')->select('id', 'district');
  111. }
  112. public function customSchedule()
  113. {
  114. return $this->hasOne(CustomSchedule::class)->select('id', 'service_type_id', 'min_price', 'max_price', 'schedule_id');
  115. }
  116. public function proposals()
  117. {
  118. return $this->hasMany(ScheduleProposal::class);
  119. }
  120. public function refuses()
  121. {
  122. return $this->hasMany(ScheduleRefuse::class);
  123. }
  124. public function reviews()
  125. {
  126. return $this->hasMany(Review::class);
  127. }
  128. public function getSpecialitiesAttribute(): Collection
  129. {
  130. if ($this->schedule_type !== 'custom') {
  131. return collect();
  132. }
  133. return $this->customSchedule?->specialities
  134. ->sortBy('description')
  135. ->map(fn(CustomScheduleSpeciality $speciality) => [
  136. 'id' => $speciality->id,
  137. 'description' => $speciality->description,
  138. ])
  139. ->values() ?? collect();
  140. }
  141. //
  142. public function ensureCustomerPhone(?string $fallbackPhone = null): void
  143. {
  144. $phone = $this->client?->user?->phone ?? $fallbackPhone;
  145. $digits = preg_replace('/\D+/', '', (string) $phone) ?? '';
  146. if (strlen($digits) >= 10) {
  147. return;
  148. }
  149. throw new PaymentMissingDataException(
  150. 'Voce precisa cadastrar um numero de celular valido no seu perfil para concluir o pagamento.'
  151. );
  152. }
  153. }