@@ -14,129 +14,129 @@ |
||
| 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 | - * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. |
|
| 65 | - * Se for nulo, ele busca a referencia da config. |
|
| 66 | - * @return array |
|
| 67 | - */ |
|
| 68 | - public function availableToday($model_type, $duration, $openingTime = null) |
|
| 69 | - { |
|
| 70 | - return $this->availableOn($model_type, Carbon::now(), $duration, $openingTime); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Retorna os horários disponiveis em um determinado dia para uma certa model. |
|
| 75 | - * |
|
| 76 | - * @param string $model_type Tipo da model |
|
| 77 | - * @param \Carbon\Carbon $today Data para o qual ele irá fazer a busca. |
|
| 78 | - * @param int $durationMinutes Serve para facilitar na hora de buscar horários livres |
|
| 79 | - * que precisem ter uma certa duração. |
|
| 80 | - * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. |
|
| 81 | - * Se for nulo, ele busca a referencia da config. |
|
| 82 | - * @return array |
|
| 83 | - */ |
|
| 84 | - public function availableOn($model_type, $today, $durationMinutes, $openingTime = null) |
|
| 85 | - { |
|
| 86 | - $openingTime = $openingTime ?? Carbon::parse(Config::get('scheduler.opening_time'))->setDateFrom($today); |
|
| 87 | - $closingTime = Carbon::parse(Config::get('scheduler.closing_time'))->setDateFrom($today); |
|
| 88 | - |
|
| 89 | - $livres = []; |
|
| 90 | - $today = Carbon::parse($today->toDateString()); |
|
| 91 | - while($openingTime <= $closingTime) |
|
| 92 | - { |
|
| 93 | - $add = true; |
|
| 94 | - |
|
| 95 | - $opening = Carbon::parse($openingTime->toDateTimeString()); |
|
| 96 | - $closing = Carbon::parse($openingTime->toDateTimeString())->addMinutes($durationMinutes); |
|
| 97 | - |
|
| 98 | - foreach (Schedule::orderBy('start_at', 'DESC')->cursor() as $schedule) { |
|
| 99 | - if($schedule->model_type != $model_type) |
|
| 100 | - continue; |
|
| 101 | - |
|
| 102 | - $start = Carbon::parse($schedule->start_at); |
|
| 103 | - $begin = Carbon::parse($start->toDateString()); |
|
| 104 | - |
|
| 105 | - if($begin->greaterThan($today)) |
|
| 106 | - break; |
|
| 107 | - |
|
| 108 | - if($begin->notEqualTo($today)) |
|
| 109 | - continue; |
|
| 110 | - |
|
| 111 | - $end = Carbon::parse($schedule->end_at); |
|
| 112 | - |
|
| 113 | - if($this->isShouldntAdd($opening, $closing, $start, $end)) |
|
| 114 | - $add = false; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - if($add && $closing->lessThanOrEqualTo($closingTime)) |
|
| 118 | - $livres[] = [ |
|
| 119 | - 'start_at' => $opening, |
|
| 120 | - 'end_at' => $closing |
|
| 121 | - ]; |
|
| 122 | - |
|
| 123 | - $openingTime->addMinutes($durationMinutes); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - return $livres; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Verifica se ele não deve ser adicionado ao array de horários livres. |
|
| 131 | - * |
|
| 132 | - * @param \Carbon\Carbon $opening |
|
| 133 | - * @param \Carbon\Carbon $closing |
|
| 134 | - * @param \Carbon\Carbon $start |
|
| 135 | - * @param \Carbon\Carbon $end |
|
| 136 | - * @return boolean |
|
| 137 | - */ |
|
| 138 | - private function isShouldntAdd($opening, $closing, $start, $end) |
|
| 139 | - { |
|
| 140 | - return $start <= $opening && $end >= $closing; |
|
| 141 | - } |
|
| 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 | + * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. |
|
| 65 | + * Se for nulo, ele busca a referencia da config. |
|
| 66 | + * @return array |
|
| 67 | + */ |
|
| 68 | + public function availableToday($model_type, $duration, $openingTime = null) |
|
| 69 | + { |
|
| 70 | + return $this->availableOn($model_type, Carbon::now(), $duration, $openingTime); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Retorna os horários disponiveis em um determinado dia para uma certa model. |
|
| 75 | + * |
|
| 76 | + * @param string $model_type Tipo da model |
|
| 77 | + * @param \Carbon\Carbon $today Data para o qual ele irá fazer a busca. |
|
| 78 | + * @param int $durationMinutes Serve para facilitar na hora de buscar horários livres |
|
| 79 | + * que precisem ter uma certa duração. |
|
| 80 | + * @param \Carbon\Carbon|null $openingTime Serve como referencia para buscar horários livres. |
|
| 81 | + * Se for nulo, ele busca a referencia da config. |
|
| 82 | + * @return array |
|
| 83 | + */ |
|
| 84 | + public function availableOn($model_type, $today, $durationMinutes, $openingTime = null) |
|
| 85 | + { |
|
| 86 | + $openingTime = $openingTime ?? Carbon::parse(Config::get('scheduler.opening_time'))->setDateFrom($today); |
|
| 87 | + $closingTime = Carbon::parse(Config::get('scheduler.closing_time'))->setDateFrom($today); |
|
| 88 | + |
|
| 89 | + $livres = []; |
|
| 90 | + $today = Carbon::parse($today->toDateString()); |
|
| 91 | + while($openingTime <= $closingTime) |
|
| 92 | + { |
|
| 93 | + $add = true; |
|
| 94 | + |
|
| 95 | + $opening = Carbon::parse($openingTime->toDateTimeString()); |
|
| 96 | + $closing = Carbon::parse($openingTime->toDateTimeString())->addMinutes($durationMinutes); |
|
| 97 | + |
|
| 98 | + foreach (Schedule::orderBy('start_at', 'DESC')->cursor() as $schedule) { |
|
| 99 | + if($schedule->model_type != $model_type) |
|
| 100 | + continue; |
|
| 101 | + |
|
| 102 | + $start = Carbon::parse($schedule->start_at); |
|
| 103 | + $begin = Carbon::parse($start->toDateString()); |
|
| 104 | + |
|
| 105 | + if($begin->greaterThan($today)) |
|
| 106 | + break; |
|
| 107 | + |
|
| 108 | + if($begin->notEqualTo($today)) |
|
| 109 | + continue; |
|
| 110 | + |
|
| 111 | + $end = Carbon::parse($schedule->end_at); |
|
| 112 | + |
|
| 113 | + if($this->isShouldntAdd($opening, $closing, $start, $end)) |
|
| 114 | + $add = false; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + if($add && $closing->lessThanOrEqualTo($closingTime)) |
|
| 118 | + $livres[] = [ |
|
| 119 | + 'start_at' => $opening, |
|
| 120 | + 'end_at' => $closing |
|
| 121 | + ]; |
|
| 122 | + |
|
| 123 | + $openingTime->addMinutes($durationMinutes); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + return $livres; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Verifica se ele não deve ser adicionado ao array de horários livres. |
|
| 131 | + * |
|
| 132 | + * @param \Carbon\Carbon $opening |
|
| 133 | + * @param \Carbon\Carbon $closing |
|
| 134 | + * @param \Carbon\Carbon $start |
|
| 135 | + * @param \Carbon\Carbon $end |
|
| 136 | + * @return boolean |
|
| 137 | + */ |
|
| 138 | + private function isShouldntAdd($opening, $closing, $start, $end) |
|
| 139 | + { |
|
| 140 | + return $start <= $opening && $end >= $closing; |
|
| 141 | + } |
|
| 142 | 142 | } |
| 143 | 143 | \ No newline at end of file |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | * |
| 29 | 29 | * @return void |
| 30 | 30 | */ |
| 31 | - public function __construct($app) |
|
| 31 | + public function __construct ($app) |
|
| 32 | 32 | { |
| 33 | 33 | $this->app = $app; |
| 34 | 34 | } |
@@ -41,9 +41,9 @@ discard block |
||
| 41 | 41 | * @param string|\Carbon\Carbon $end_at |
| 42 | 42 | * @return bool |
| 43 | 43 | */ |
| 44 | - public function hasScheduleBetween($model_type, $start_at, $end_at) |
|
| 44 | + public function hasScheduleBetween ($model_type, $start_at, $end_at) |
|
| 45 | 45 | { |
| 46 | - if(!Config::get('scheduler.enable_schedule_conflict')) |
|
| 46 | + if (!Config::get('scheduler.enable_schedule_conflict')) |
|
| 47 | 47 | return false; |
| 48 | 48 | |
| 49 | 49 | return !is_null( |
@@ -65,7 +65,7 @@ discard block |
||
| 65 | 65 | * Se for nulo, ele busca a referencia da config. |
| 66 | 66 | * @return array |
| 67 | 67 | */ |
| 68 | - public function availableToday($model_type, $duration, $openingTime = null) |
|
| 68 | + public function availableToday ($model_type, $duration, $openingTime = null) |
|
| 69 | 69 | { |
| 70 | 70 | return $this->availableOn($model_type, Carbon::now(), $duration, $openingTime); |
| 71 | 71 | } |
@@ -81,14 +81,14 @@ discard block |
||
| 81 | 81 | * Se for nulo, ele busca a referencia da config. |
| 82 | 82 | * @return array |
| 83 | 83 | */ |
| 84 | - public function availableOn($model_type, $today, $durationMinutes, $openingTime = null) |
|
| 84 | + public function availableOn ($model_type, $today, $durationMinutes, $openingTime = null) |
|
| 85 | 85 | { |
| 86 | 86 | $openingTime = $openingTime ?? Carbon::parse(Config::get('scheduler.opening_time'))->setDateFrom($today); |
| 87 | 87 | $closingTime = Carbon::parse(Config::get('scheduler.closing_time'))->setDateFrom($today); |
| 88 | 88 | |
| 89 | - $livres = []; |
|
| 89 | + $livres = [ ]; |
|
| 90 | 90 | $today = Carbon::parse($today->toDateString()); |
| 91 | - while($openingTime <= $closingTime) |
|
| 91 | + while ($openingTime <= $closingTime) |
|
| 92 | 92 | { |
| 93 | 93 | $add = true; |
| 94 | 94 | |
@@ -96,26 +96,26 @@ discard block |
||
| 96 | 96 | $closing = Carbon::parse($openingTime->toDateTimeString())->addMinutes($durationMinutes); |
| 97 | 97 | |
| 98 | 98 | foreach (Schedule::orderBy('start_at', 'DESC')->cursor() as $schedule) { |
| 99 | - if($schedule->model_type != $model_type) |
|
| 99 | + if ($schedule->model_type != $model_type) |
|
| 100 | 100 | continue; |
| 101 | 101 | |
| 102 | 102 | $start = Carbon::parse($schedule->start_at); |
| 103 | 103 | $begin = Carbon::parse($start->toDateString()); |
| 104 | 104 | |
| 105 | - if($begin->greaterThan($today)) |
|
| 105 | + if ($begin->greaterThan($today)) |
|
| 106 | 106 | break; |
| 107 | 107 | |
| 108 | - if($begin->notEqualTo($today)) |
|
| 108 | + if ($begin->notEqualTo($today)) |
|
| 109 | 109 | continue; |
| 110 | 110 | |
| 111 | 111 | $end = Carbon::parse($schedule->end_at); |
| 112 | 112 | |
| 113 | - if($this->isShouldntAdd($opening, $closing, $start, $end)) |
|
| 113 | + if ($this->isShouldntAdd($opening, $closing, $start, $end)) |
|
| 114 | 114 | $add = false; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - if($add && $closing->lessThanOrEqualTo($closingTime)) |
|
| 118 | - $livres[] = [ |
|
| 117 | + if ($add && $closing->lessThanOrEqualTo($closingTime)) |
|
| 118 | + $livres[ ] = [ |
|
| 119 | 119 | 'start_at' => $opening, |
| 120 | 120 | 'end_at' => $closing |
| 121 | 121 | ]; |
@@ -135,7 +135,7 @@ discard block |
||
| 135 | 135 | * @param \Carbon\Carbon $end |
| 136 | 136 | * @return boolean |
| 137 | 137 | */ |
| 138 | - private function isShouldntAdd($opening, $closing, $start, $end) |
|
| 138 | + private function isShouldntAdd ($opening, $closing, $start, $end) |
|
| 139 | 139 | { |
| 140 | 140 | return $start <= $opening && $end >= $closing; |
| 141 | 141 | } |
@@ -43,8 +43,9 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function hasScheduleBetween($model_type, $start_at, $end_at) |
| 45 | 45 | { |
| 46 | - if(!Config::get('scheduler.enable_schedule_conflict')) |
|
| 47 | - return false; |
|
| 46 | + if(!Config::get('scheduler.enable_schedule_conflict')) { |
|
| 47 | + return false; |
|
| 48 | + } |
|
| 48 | 49 | |
| 49 | 50 | return !is_null( |
| 50 | 51 | Schedule::latest() |
@@ -96,29 +97,34 @@ discard block |
||
| 96 | 97 | $closing = Carbon::parse($openingTime->toDateTimeString())->addMinutes($durationMinutes); |
| 97 | 98 | |
| 98 | 99 | foreach (Schedule::orderBy('start_at', 'DESC')->cursor() as $schedule) { |
| 99 | - if($schedule->model_type != $model_type) |
|
| 100 | - continue; |
|
| 100 | + if($schedule->model_type != $model_type) { |
|
| 101 | + continue; |
|
| 102 | + } |
|
| 101 | 103 | |
| 102 | 104 | $start = Carbon::parse($schedule->start_at); |
| 103 | 105 | $begin = Carbon::parse($start->toDateString()); |
| 104 | 106 | |
| 105 | - if($begin->greaterThan($today)) |
|
| 106 | - break; |
|
| 107 | + if($begin->greaterThan($today)) { |
|
| 108 | + break; |
|
| 109 | + } |
|
| 107 | 110 | |
| 108 | - if($begin->notEqualTo($today)) |
|
| 109 | - continue; |
|
| 111 | + if($begin->notEqualTo($today)) { |
|
| 112 | + continue; |
|
| 113 | + } |
|
| 110 | 114 | |
| 111 | 115 | $end = Carbon::parse($schedule->end_at); |
| 112 | 116 | |
| 113 | - if($this->isShouldntAdd($opening, $closing, $start, $end)) |
|
| 114 | - $add = false; |
|
| 117 | + if($this->isShouldntAdd($opening, $closing, $start, $end)) { |
|
| 118 | + $add = false; |
|
| 119 | + } |
|
| 115 | 120 | } |
| 116 | 121 | |
| 117 | - if($add && $closing->lessThanOrEqualTo($closingTime)) |
|
| 118 | - $livres[] = [ |
|
| 122 | + if($add && $closing->lessThanOrEqualTo($closingTime)) { |
|
| 123 | + $livres[] = [ |
|
| 119 | 124 | 'start_at' => $opening, |
| 120 | 125 | 'end_at' => $closing |
| 121 | 126 | ]; |
| 127 | + } |
|
| 122 | 128 | |
| 123 | 129 | $openingTime->addMinutes($durationMinutes); |
| 124 | 130 | } |