'datetime:d/m/Y H:i', 'fim' => 'datetime:d/m/Y H:i' ]; protected $fillable = [ 'lotacao_id', 'servico_id', 'faixa_etaria_id', 'escala_tipo_id', 'inicio', 'fim', 'quantidade_atendimentos', 'status_id' ]; protected $hidden = [ 'created_at', 'created_lotacao_id', 'created_centro_custo_id', 'updated_at', 'updated_lotacao_id', 'updated_centro_custo_id', 'deleted_at', 'deleted_lotacao_id', 'deleted_centro_custo_id', ]; protected $appends = [ 'dia_semana_inicio', 'jornada_em_horas', ]; public function lotacao() { return $this->belongsTo(Lotacao::class); } public function servico() { return $this->belongsTo(Servico::class); } public function faixaEtaria() { return $this->belongsTo(FaixaEtaria::class); } public function escalaTipo() { return $this->belongsTo(EscalaTipo::class); } public function status() { return $this->belongsTo(Status::class); } public function recepcoes() { return $this->hasMany(Recepcao::class); } public function atendimentosTriagem() { return $this->hasMany(AtendimentoTriagem::class); } public function atendimentosEnfermagem() { return $this->hasMany(AtendimentoEnfermagem::class); } public function atendimentosMedico() { return $this->hasMany(AtendimentoMedico::class); } public function pacientesObservacao() { return $this->hasMany(PacienteObservacao::class); } public function pedidosExames() { return $this->hasMany(PedidoExame::class); } public function procedimentos() { return $this->hasMany(Procedimento::class); } public function getDiaSemanaInicioAttribute() { return (int) $this->inicio->format('w') + 1; } public function getJornadaEmHorasAttribute() { $interval = $this->inicio->diff($this->fim); return $interval->days === 1 ? 24 : $interval->h; } public function getIsEscalaTipoFixaAttribute() { return $this->escala_tipo_id === TipoEscala::FIXA; } public function getIsEscalaTipoDinamicaAttribute() { return $this->escala_tipo_id === TipoEscala::DINAMICA; } public function getIsEscalaTipoUnicaAttribute() { return $this->escala_tipo_id === TipoEscala::UNICA; } public function getPeriodoDescansoAttribute() { if ($this->isEscalaTipoUnica) { return []; } return [ 'inicio' => $this->fim->copy(), 'fim' => $this->fim->copy()->addHours($this->getRestHours()), ]; } private function getRestHours() { switch ($this->jornada_em_horas) { case 6: { return 18; } case 8: { return 16; } case 12: { return 36; } case 24: { return 48; } } } public function getDateTimeOfFirstAtendimentoAttribute() { $atendimentos = $this->getAtendimentos(); if ($atendimentos->isEmpty()) { return null; } return $atendimentos->sort()->first(); } public function getDateTimeOfLastAtendimentoAttribute() { $atendimentos = $this->getAtendimentos(); if ($atendimentos->isEmpty()) { return null; } return $atendimentos->sort()->last(); } private function getAtendimentos() { return $this->recepcoes->pluck('created_at') ->merge($this->atendimentosTriagem->pluck('created_at')) ->merge($this->atendimentosEnfermagem->pluck('created_at')) ->merge($this->atendimentosMedico->pluck('created_at')) ->merge($this->pacientesObservacao->pluck('created_at')) ->merge($this->pedidosExames->pluck('created_at')); } public function getIsAllowedToDeleteAttribute() { return true; } }