220 lines
5.0 KiB
PHP
220 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models\ProntoAtendimento\Plantao;
|
|
|
|
use App\Models\Model;
|
|
use App\Models\Geral\FaixaEtaria;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use App\Models\Cadastro\SujeitoAtencao\Lotacao;
|
|
use App\Models\Cadastro\Estabelecimento\Servico;
|
|
use App\Enums\ProntoAtendimento\Plantao\TipoEscala;
|
|
use App\Models\ProntoAtendimento\Atendimento\Enfermagem\AtendimentoEnfermagem;
|
|
use App\Models\ProntoAtendimento\Atendimento\Medico\AtendimentoMedico;
|
|
use App\Models\ProntoAtendimento\Atendimento\Observacao\PacienteObservacao;
|
|
use App\Models\ProntoAtendimento\Atendimento\Procedimento;
|
|
use App\Models\ProntoAtendimento\Atendimento\Recepcao;
|
|
use App\Models\ProntoAtendimento\Atendimento\Triagem\AtendimentoTriagem;
|
|
use App\Models\ProntoAtendimento\PedidoExame\PedidoExame;
|
|
|
|
class Plantao extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected $table = 'pronto_atendimento_plantao';
|
|
|
|
protected $casts = [
|
|
'inicio' => '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;
|
|
}
|
|
}
|