@@ -14,64 +14,64 @@ |
||
| 14 | 14 | |
| 15 | 15 | class Schedule extends Model |
| 16 | 16 | { |
| 17 | - use SoftDeletes; |
|
| 17 | + use SoftDeletes; |
|
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | - * Os atributos que podem ser atribuíveis em massa. |
|
| 21 | - * |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - protected $fillable = [ |
|
| 25 | - 'model_type', 'model_id', 'start_at', 'end_at', 'status' |
|
| 26 | - ]; |
|
| 20 | + * Os atributos que podem ser atribuíveis em massa. |
|
| 21 | + * |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + protected $fillable = [ |
|
| 25 | + 'model_type', 'model_id', 'start_at', 'end_at', 'status' |
|
| 26 | + ]; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Os atributos que devem ser transformados para data. |
|
| 30 | - * |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - protected $dates = [ |
|
| 34 | - 'start_at', 'end_at', 'deleted_at' |
|
| 35 | - ]; |
|
| 28 | + /** |
|
| 29 | + * Os atributos que devem ser transformados para data. |
|
| 30 | + * |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + protected $dates = [ |
|
| 34 | + 'start_at', 'end_at', 'deleted_at' |
|
| 35 | + ]; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Seta um status para o horário agendado. |
|
| 39 | - * |
|
| 40 | - * @param int|string $status Pode ser passado o ID do status ou seu nome para seta-lo no horário. |
|
| 41 | - */ |
|
| 42 | - public function setStatus($name) |
|
| 43 | - { |
|
| 44 | - $this->fill($this->parseStatusKey($name))->save(); |
|
| 45 | - } |
|
| 37 | + /** |
|
| 38 | + * Seta um status para o horário agendado. |
|
| 39 | + * |
|
| 40 | + * @param int|string $status Pode ser passado o ID do status ou seu nome para seta-lo no horário. |
|
| 41 | + */ |
|
| 42 | + public function setStatus($name) |
|
| 43 | + { |
|
| 44 | + $this->fill($this->parseStatusKey($name))->save(); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Retorna o ID do status caso passem o nome do status. |
|
| 49 | - * |
|
| 50 | - * @param int|string $key ID ou o nome do status. |
|
| 51 | - * @return array |
|
| 52 | - */ |
|
| 53 | - public function parseStatusKey($key) |
|
| 54 | - { |
|
| 55 | - if(is_int($key)) |
|
| 56 | - return ['status' => $key]; |
|
| 47 | + /** |
|
| 48 | + * Retorna o ID do status caso passem o nome do status. |
|
| 49 | + * |
|
| 50 | + * @param int|string $key ID ou o nome do status. |
|
| 51 | + * @return array |
|
| 52 | + */ |
|
| 53 | + public function parseStatusKey($key) |
|
| 54 | + { |
|
| 55 | + if(is_int($key)) |
|
| 56 | + return ['status' => $key]; |
|
| 57 | 57 | |
| 58 | - $status = ScheduleStatus::where('name', $key)->first(); |
|
| 58 | + $status = ScheduleStatus::where('name', $key)->first(); |
|
| 59 | 59 | |
| 60 | - if(is_null($status)) |
|
| 61 | - throw (new ModelNotFound)->setValues(ScheduleStatus::class); |
|
| 60 | + if(is_null($status)) |
|
| 61 | + throw (new ModelNotFound)->setValues(ScheduleStatus::class); |
|
| 62 | 62 | |
| 63 | - return ['status' => $status->id]; |
|
| 64 | - } |
|
| 63 | + return ['status' => $status->id]; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Escopo de uma consulta que busca horarios pela data de início. |
|
| 68 | - * |
|
| 69 | - * @param \Illuminate\Database\Eloquent\Builder $query |
|
| 70 | - * @param \Carbon\Carbon|string $start_at |
|
| 71 | - * @return \Illuminate\Database\Eloquent\Builder |
|
| 72 | - */ |
|
| 73 | - public function scopeByStartAt($query, $start_at) |
|
| 74 | - { |
|
| 75 | - return $query->where('start_at', $start_at); |
|
| 76 | - } |
|
| 66 | + /** |
|
| 67 | + * Escopo de uma consulta que busca horarios pela data de início. |
|
| 68 | + * |
|
| 69 | + * @param \Illuminate\Database\Eloquent\Builder $query |
|
| 70 | + * @param \Carbon\Carbon|string $start_at |
|
| 71 | + * @return \Illuminate\Database\Eloquent\Builder |
|
| 72 | + */ |
|
| 73 | + public function scopeByStartAt($query, $start_at) |
|
| 74 | + { |
|
| 75 | + return $query->where('start_at', $start_at); |
|
| 76 | + } |
|
| 77 | 77 | } |
| 78 | 78 | \ No newline at end of file |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | * |
| 40 | 40 | * @param int|string $status Pode ser passado o ID do status ou seu nome para seta-lo no horário. |
| 41 | 41 | */ |
| 42 | - public function setStatus($name) |
|
| 42 | + public function setStatus ($name) |
|
| 43 | 43 | { |
| 44 | 44 | $this->fill($this->parseStatusKey($name))->save(); |
| 45 | 45 | } |
@@ -50,17 +50,17 @@ discard block |
||
| 50 | 50 | * @param int|string $key ID ou o nome do status. |
| 51 | 51 | * @return array |
| 52 | 52 | */ |
| 53 | - public function parseStatusKey($key) |
|
| 53 | + public function parseStatusKey ($key) |
|
| 54 | 54 | { |
| 55 | - if(is_int($key)) |
|
| 56 | - return ['status' => $key]; |
|
| 55 | + if (is_int($key)) |
|
| 56 | + return [ 'status' => $key ]; |
|
| 57 | 57 | |
| 58 | 58 | $status = ScheduleStatus::where('name', $key)->first(); |
| 59 | 59 | |
| 60 | - if(is_null($status)) |
|
| 60 | + if (is_null($status)) |
|
| 61 | 61 | throw (new ModelNotFound)->setValues(ScheduleStatus::class); |
| 62 | 62 | |
| 63 | - return ['status' => $status->id]; |
|
| 63 | + return [ 'status' => $status->id ]; |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** |
@@ -70,7 +70,7 @@ discard block |
||
| 70 | 70 | * @param \Carbon\Carbon|string $start_at |
| 71 | 71 | * @return \Illuminate\Database\Eloquent\Builder |
| 72 | 72 | */ |
| 73 | - public function scopeByStartAt($query, $start_at) |
|
| 73 | + public function scopeByStartAt ($query, $start_at) |
|
| 74 | 74 | { |
| 75 | 75 | return $query->where('start_at', $start_at); |
| 76 | 76 | } |
@@ -35,21 +35,21 @@ discard block |
||
| 35 | 35 | * @param string $relation |
| 36 | 36 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
| 37 | 37 | */ |
| 38 | - abstract public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null); |
|
| 38 | + abstract public function belongsTo ($related, $foreignKey = null, $ownerKey = null, $relation = null); |
|
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * Get the value of the model's primary key. |
| 42 | 42 | * |
| 43 | 43 | * @return mixed |
| 44 | 44 | */ |
| 45 | - abstract public function getKey(); |
|
| 45 | + abstract public function getKey (); |
|
| 46 | 46 | |
| 47 | 47 | /** |
| 48 | 48 | * Retorna apenas os horários que possuem o mesmo [model_type] do [parent] dessa [trait]. |
| 49 | 49 | * |
| 50 | 50 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
| 51 | 51 | */ |
| 52 | - public function schedules() |
|
| 52 | + public function schedules () |
|
| 53 | 53 | { |
| 54 | 54 | return $this->belongsTo(Config::get('scheduler.schedules_table'), 'model_id'); |
| 55 | 55 | } |
@@ -67,21 +67,21 @@ discard block |
||
| 67 | 67 | * @throws \H4ad\Scheduler\Exceptions\EndCantBeforeStart |
| 68 | 68 | * @throws \H4ad\Scheduler\Exceptions\CantAddWithSameStartAt |
| 69 | 69 | */ |
| 70 | - public function addSchedule($start_at, $end_at = null, $status = null) |
|
| 70 | + public function addSchedule ($start_at, $end_at = null, $status = null) |
|
| 71 | 71 | { |
| 72 | - if(!Config::get('scheduler.enable_schedule_without_end') && is_null($end_at)) |
|
| 72 | + if (!Config::get('scheduler.enable_schedule_without_end') && is_null($end_at)) |
|
| 73 | 73 | throw new CantAddWithoutEnd; |
| 74 | 74 | |
| 75 | - $start_at = $this->parseToCarbon($start_at); |
|
| 75 | + $start_at = $this->parseToCarbon($start_at); |
|
| 76 | 76 | |
| 77 | - if(!is_null($end_at)) { |
|
| 77 | + if (!is_null($end_at)) { |
|
| 78 | 78 | $end_at = $this->parseToCarbon($end_at, $start_at); |
| 79 | 79 | |
| 80 | - if($start_at->greaterThan($end_at)) |
|
| 80 | + if ($start_at->greaterThan($end_at)) |
|
| 81 | 81 | throw new EndCantBeforeStart; |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | - if(Scheduler::hasScheduleBetween(self::class, $start_at, $end_at ?? $start_at)) |
|
| 84 | + if (Scheduler::hasScheduleBetween(self::class, $start_at, $end_at ?? $start_at)) |
|
| 85 | 85 | throw new CantAddWithSameStartAt; |
| 86 | 86 | |
| 87 | 87 | $model_id = $this->getKey(); |
@@ -97,12 +97,12 @@ discard block |
||
| 97 | 97 | * @param \Carbon\Carbon $reference Data de referencia quando o [date] é inteiro. |
| 98 | 98 | * @return \Carbon\Carbon |
| 99 | 99 | */ |
| 100 | - public function parseToCarbon($date, $reference = null) |
|
| 100 | + public function parseToCarbon ($date, $reference = null) |
|
| 101 | 101 | { |
| 102 | - if(is_string($date)) |
|
| 102 | + if (is_string($date)) |
|
| 103 | 103 | return Carbon::parse($date); |
| 104 | 104 | |
| 105 | - if(is_int($date) && !is_null($reference)) |
|
| 105 | + if (is_int($date) && !is_null($reference)) |
|
| 106 | 106 | return Carbon::parse($reference->toDateTimeString())->addMinutes($date); |
| 107 | 107 | |
| 108 | 108 | return $date; |
@@ -114,9 +114,9 @@ discard block |
||
| 114 | 114 | * @param \Carbon\Carbon|string|int $value Valor que representará a data ou o id a ser buscado. |
| 115 | 115 | * @return \H4ad\Scheduler\Models\Schedule|null |
| 116 | 116 | */ |
| 117 | - public function parseToSchedule($value) |
|
| 117 | + public function parseToSchedule ($value) |
|
| 118 | 118 | { |
| 119 | - if(is_int($value)) |
|
| 119 | + if (is_int($value)) |
|
| 120 | 120 | return Schedule::find($value); |
| 121 | 121 | |
| 122 | 122 | return Schedule::byStartAt($value)->first(); |
@@ -134,17 +134,17 @@ discard block |
||
| 134 | 134 | * @throws \H4ad\Scheduler\Exceptions\CantRemoveByDate |
| 135 | 135 | * @throws \H4ad\Scheduler\Exceptions\ModelNotFound |
| 136 | 136 | */ |
| 137 | - public function removeSchedule($schedule) |
|
| 137 | + public function removeSchedule ($schedule) |
|
| 138 | 138 | { |
| 139 | - if(!Config::get('scheduler.enable_schedule_conflict') && !is_int($schedule)) |
|
| 139 | + if (!Config::get('scheduler.enable_schedule_conflict') && !is_int($schedule)) |
|
| 140 | 140 | throw new CantRemoveByDate; |
| 141 | 141 | |
| 142 | 142 | $schedule = $this->parseToSchedule($schedule); |
| 143 | 143 | |
| 144 | - if(!($schedule instanceof Model)) |
|
| 144 | + if (!($schedule instanceof Model)) |
|
| 145 | 145 | throw (new ModelNotFound)->setValues(Schedule::class); |
| 146 | 146 | |
| 147 | - if($schedule->model_type != self::class || $schedule->model_id != $this->getKey()) |
|
| 147 | + if ($schedule->model_type != self::class || $schedule->model_id != $this->getKey()) |
|
| 148 | 148 | throw new DoesNotBelong; |
| 149 | 149 | |
| 150 | 150 | return $schedule->delete(); |
@@ -69,20 +69,23 @@ discard block |
||
| 69 | 69 | */ |
| 70 | 70 | public function addSchedule($start_at, $end_at = null, $status = null) |
| 71 | 71 | { |
| 72 | - if(!Config::get('scheduler.enable_schedule_without_end') && is_null($end_at)) |
|
| 73 | - throw new CantAddWithoutEnd; |
|
| 72 | + if(!Config::get('scheduler.enable_schedule_without_end') && is_null($end_at)) { |
|
| 73 | + throw new CantAddWithoutEnd; |
|
| 74 | + } |
|
| 74 | 75 | |
| 75 | 76 | $start_at = $this->parseToCarbon($start_at); |
| 76 | 77 | |
| 77 | 78 | if(!is_null($end_at)) { |
| 78 | 79 | $end_at = $this->parseToCarbon($end_at, $start_at); |
| 79 | 80 | |
| 80 | - if($start_at->greaterThan($end_at)) |
|
| 81 | - throw new EndCantBeforeStart; |
|
| 81 | + if($start_at->greaterThan($end_at)) { |
|
| 82 | + throw new EndCantBeforeStart; |
|
| 83 | + } |
|
| 82 | 84 | } |
| 83 | 85 | |
| 84 | - if(Scheduler::hasScheduleBetween(self::class, $start_at, $end_at ?? $start_at)) |
|
| 85 | - throw new CantAddWithSameStartAt; |
|
| 86 | + if(Scheduler::hasScheduleBetween(self::class, $start_at, $end_at ?? $start_at)) { |
|
| 87 | + throw new CantAddWithSameStartAt; |
|
| 88 | + } |
|
| 86 | 89 | |
| 87 | 90 | $model_id = $this->getKey(); |
| 88 | 91 | $model_type = self::class; |
@@ -99,11 +102,13 @@ discard block |
||
| 99 | 102 | */ |
| 100 | 103 | public function parseToCarbon($date, $reference = null) |
| 101 | 104 | { |
| 102 | - if(is_string($date)) |
|
| 103 | - return Carbon::parse($date); |
|
| 105 | + if(is_string($date)) { |
|
| 106 | + return Carbon::parse($date); |
|
| 107 | + } |
|
| 104 | 108 | |
| 105 | - if(is_int($date) && !is_null($reference)) |
|
| 106 | - return Carbon::parse($reference->toDateTimeString())->addMinutes($date); |
|
| 109 | + if(is_int($date) && !is_null($reference)) { |
|
| 110 | + return Carbon::parse($reference->toDateTimeString())->addMinutes($date); |
|
| 111 | + } |
|
| 107 | 112 | |
| 108 | 113 | return $date; |
| 109 | 114 | } |
@@ -116,8 +121,9 @@ discard block |
||
| 116 | 121 | */ |
| 117 | 122 | public function parseToSchedule($value) |
| 118 | 123 | { |
| 119 | - if(is_int($value)) |
|
| 120 | - return Schedule::find($value); |
|
| 124 | + if(is_int($value)) { |
|
| 125 | + return Schedule::find($value); |
|
| 126 | + } |
|
| 121 | 127 | |
| 122 | 128 | return Schedule::byStartAt($value)->first(); |
| 123 | 129 | } |
@@ -136,16 +142,19 @@ discard block |
||
| 136 | 142 | */ |
| 137 | 143 | public function removeSchedule($schedule) |
| 138 | 144 | { |
| 139 | - if(!Config::get('scheduler.enable_schedule_conflict') && !is_int($schedule)) |
|
| 140 | - throw new CantRemoveByDate; |
|
| 145 | + if(!Config::get('scheduler.enable_schedule_conflict') && !is_int($schedule)) { |
|
| 146 | + throw new CantRemoveByDate; |
|
| 147 | + } |
|
| 141 | 148 | |
| 142 | 149 | $schedule = $this->parseToSchedule($schedule); |
| 143 | 150 | |
| 144 | - if(!($schedule instanceof Model)) |
|
| 145 | - throw (new ModelNotFound)->setValues(Schedule::class); |
|
| 151 | + if(!($schedule instanceof Model)) { |
|
| 152 | + throw (new ModelNotFound)->setValues(Schedule::class); |
|
| 153 | + } |
|
| 146 | 154 | |
| 147 | - if($schedule->model_type != self::class || $schedule->model_id != $this->getKey()) |
|
| 148 | - throw new DoesNotBelong; |
|
| 155 | + if($schedule->model_type != self::class || $schedule->model_id != $this->getKey()) { |
|
| 156 | + throw new DoesNotBelong; |
|
| 157 | + } |
|
| 149 | 158 | |
| 150 | 159 | return $schedule->delete(); |
| 151 | 160 | } |
@@ -14,108 +14,108 @@ |
||
| 14 | 14 | |
| 15 | 15 | class Scheduler |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Laravel application |
|
| 19 | - * |
|
| 20 | - * @var \Illuminate\Foundation\Application |
|
| 21 | - */ |
|
| 22 | - public $app; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Create a new confide instance. |
|
| 26 | - * |
|
| 27 | - * @param \Illuminate\Foundation\Application $app |
|
| 28 | - * |
|
| 29 | - * @return void |
|
| 30 | - */ |
|
| 31 | - public function __construct($app) |
|
| 32 | - { |
|
| 33 | - $this->app = $app; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Escopo de uma consulta que busca horarios pela data de início. |
|
| 38 | - * |
|
| 39 | - * @param string $model_type |
|
| 40 | - * @param string|\Carbon\Carbon $start_at |
|
| 41 | - * @param string|\Carbon\Carbon $end_at |
|
| 42 | - * @return bool |
|
| 43 | - */ |
|
| 44 | - public function hasScheduleBetween($model_type, $start_at, $end_at) |
|
| 45 | - { |
|
| 46 | - if(!Config::get('scheduler.enable_schedule_conflict')) |
|
| 47 | - return false; |
|
| 48 | - |
|
| 49 | - return !is_null( |
|
| 50 | - Schedule::latest() |
|
| 51 | - ->where('model_type', $model_type) |
|
| 52 | - ->where('start_at', '>=', $start_at) |
|
| 53 | - ->where('end_at', '<=', $end_at) |
|
| 54 | - ->first() |
|
| 55 | - ); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Retorna os horários disponiveis hoje para uma determinada model. |
|
| 60 | - * . |
|
| 61 | - * @param string $model_type Tipo da model |
|
| 62 | - * @param int $duration Serve para facilitar na hora de buscar horários livres |
|
| 63 | - * que precisem ter uma certa duração. |
|
| 64 | - * @return array |
|
| 65 | - */ |
|
| 66 | - public function availableToday($model_type, $duration) |
|
| 67 | - { |
|
| 68 | - return $this->availableOn($model_type, Carbon::now(), $duration); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Retorna os horários disponiveis em um determinado dia para uma certa model. |
|
| 73 | - * |
|
| 74 | - * @param string $model_type Tipo da model |
|
| 75 | - * @param \Carbon\Carbon $today Data para o qual ele irá fazer a busca. |
|
| 76 | - * @param int $durationMinutes Serve para facilitar na hora de buscar horários livres |
|
| 77 | - * que precisem ter uma certa duração. |
|
| 78 | - * @return array |
|
| 79 | - */ |
|
| 80 | - public function availableOn($model_type, $today, $durationMinutes) |
|
| 81 | - { |
|
| 82 | - $openingTime = Carbon::parse(Config::get('scheduler.opening_time'))->setDateFrom($today); |
|
| 83 | - $closingTime = Carbon::parse(Config::get('scheduler.closing_time'))->setDateFrom($today); |
|
| 84 | - |
|
| 85 | - $livres = []; |
|
| 86 | - $today = Carbon::parse($today->toDateString()); |
|
| 87 | - while($openingTime <= $closingTime) |
|
| 88 | - { |
|
| 89 | - $add = true; |
|
| 90 | - |
|
| 91 | - foreach (Schedule::orderBy('start_at', 'DESC')->cursor() as $schedule) { |
|
| 92 | - if($schedule->model_type != $model_type) |
|
| 93 | - continue; |
|
| 94 | - |
|
| 95 | - $start = Carbon::parse($schedule->start_at); |
|
| 96 | - $begin = Carbon::parse($start->toDateString()); |
|
| 97 | - |
|
| 98 | - if($begin->greaterThan($today)) |
|
| 99 | - break; |
|
| 100 | - |
|
| 101 | - if($begin->notEqualTo($today)) |
|
| 102 | - continue; |
|
| 103 | - |
|
| 104 | - $end = Carbon::parse($schedule->end_at); |
|
| 105 | - if($start <= Carbon::parse($openingTime->toDateTimeString()) |
|
| 106 | - && $end >= Carbon::parse($openingTime->toDateTimeString())->addMinutes($durationMinutes)) |
|
| 107 | - $add = false; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - if($add) |
|
| 111 | - $livres[] = [ |
|
| 112 | - 'start_at' => Carbon::parse($openingTime->toDateTimeString()), |
|
| 113 | - 'end_at' => Carbon::parse($openingTime->toDateTimeString())->addMinutes($durationMinutes) |
|
| 114 | - ]; |
|
| 115 | - |
|
| 116 | - $openingTime->addMinutes($durationMinutes); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - return $livres; |
|
| 120 | - } |
|
| 17 | + /** |
|
| 18 | + * Laravel application |
|
| 19 | + * |
|
| 20 | + * @var \Illuminate\Foundation\Application |
|
| 21 | + */ |
|
| 22 | + public $app; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Create a new confide instance. |
|
| 26 | + * |
|
| 27 | + * @param \Illuminate\Foundation\Application $app |
|
| 28 | + * |
|
| 29 | + * @return void |
|
| 30 | + */ |
|
| 31 | + public function __construct($app) |
|
| 32 | + { |
|
| 33 | + $this->app = $app; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Escopo de uma consulta que busca horarios pela data de início. |
|
| 38 | + * |
|
| 39 | + * @param string $model_type |
|
| 40 | + * @param string|\Carbon\Carbon $start_at |
|
| 41 | + * @param string|\Carbon\Carbon $end_at |
|
| 42 | + * @return bool |
|
| 43 | + */ |
|
| 44 | + public function hasScheduleBetween($model_type, $start_at, $end_at) |
|
| 45 | + { |
|
| 46 | + if(!Config::get('scheduler.enable_schedule_conflict')) |
|
| 47 | + return false; |
|
| 48 | + |
|
| 49 | + return !is_null( |
|
| 50 | + Schedule::latest() |
|
| 51 | + ->where('model_type', $model_type) |
|
| 52 | + ->where('start_at', '>=', $start_at) |
|
| 53 | + ->where('end_at', '<=', $end_at) |
|
| 54 | + ->first() |
|
| 55 | + ); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Retorna os horários disponiveis hoje para uma determinada model. |
|
| 60 | + * . |
|
| 61 | + * @param string $model_type Tipo da model |
|
| 62 | + * @param int $duration Serve para facilitar na hora de buscar horários livres |
|
| 63 | + * que precisem ter uma certa duração. |
|
| 64 | + * @return array |
|
| 65 | + */ |
|
| 66 | + public function availableToday($model_type, $duration) |
|
| 67 | + { |
|
| 68 | + return $this->availableOn($model_type, Carbon::now(), $duration); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Retorna os horários disponiveis em um determinado dia para uma certa model. |
|
| 73 | + * |
|
| 74 | + * @param string $model_type Tipo da model |
|
| 75 | + * @param \Carbon\Carbon $today Data para o qual ele irá fazer a busca. |
|
| 76 | + * @param int $durationMinutes Serve para facilitar na hora de buscar horários livres |
|
| 77 | + * que precisem ter uma certa duração. |
|
| 78 | + * @return array |
|
| 79 | + */ |
|
| 80 | + public function availableOn($model_type, $today, $durationMinutes) |
|
| 81 | + { |
|
| 82 | + $openingTime = Carbon::parse(Config::get('scheduler.opening_time'))->setDateFrom($today); |
|
| 83 | + $closingTime = Carbon::parse(Config::get('scheduler.closing_time'))->setDateFrom($today); |
|
| 84 | + |
|
| 85 | + $livres = []; |
|
| 86 | + $today = Carbon::parse($today->toDateString()); |
|
| 87 | + while($openingTime <= $closingTime) |
|
| 88 | + { |
|
| 89 | + $add = true; |
|
| 90 | + |
|
| 91 | + foreach (Schedule::orderBy('start_at', 'DESC')->cursor() as $schedule) { |
|
| 92 | + if($schedule->model_type != $model_type) |
|
| 93 | + continue; |
|
| 94 | + |
|
| 95 | + $start = Carbon::parse($schedule->start_at); |
|
| 96 | + $begin = Carbon::parse($start->toDateString()); |
|
| 97 | + |
|
| 98 | + if($begin->greaterThan($today)) |
|
| 99 | + break; |
|
| 100 | + |
|
| 101 | + if($begin->notEqualTo($today)) |
|
| 102 | + continue; |
|
| 103 | + |
|
| 104 | + $end = Carbon::parse($schedule->end_at); |
|
| 105 | + if($start <= Carbon::parse($openingTime->toDateTimeString()) |
|
| 106 | + && $end >= Carbon::parse($openingTime->toDateTimeString())->addMinutes($durationMinutes)) |
|
| 107 | + $add = false; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + if($add) |
|
| 111 | + $livres[] = [ |
|
| 112 | + 'start_at' => Carbon::parse($openingTime->toDateTimeString()), |
|
| 113 | + 'end_at' => Carbon::parse($openingTime->toDateTimeString())->addMinutes($durationMinutes) |
|
| 114 | + ]; |
|
| 115 | + |
|
| 116 | + $openingTime->addMinutes($durationMinutes); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + return $livres; |
|
| 120 | + } |
|
| 121 | 121 | } |
| 122 | 122 | \ No newline at end of file |