@@ -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 |
@@ -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 |