Schedule.php 5.8 KB

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