This commit is contained in:
@@ -0,0 +1,198 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ambulatorio;
|
||||
|
||||
use App\Enums\Ambulatorio\AgendamentoStatus;
|
||||
use App\Models\Ambulatorio\Atendimento;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Services\Service;
|
||||
|
||||
class AlteracaoConsultasFinalizadasService extends Service
|
||||
{
|
||||
private static function getStatus()
|
||||
{
|
||||
return [
|
||||
AgendamentoStatus::FALTA_DO_PACIENTE,
|
||||
AgendamentoStatus::FALTA_DO_MEDICO,
|
||||
AgendamentoStatus::REALIZADO
|
||||
];
|
||||
}
|
||||
|
||||
public static function index()
|
||||
{
|
||||
return [
|
||||
'model' => [
|
||||
'data' => null,
|
||||
'unidadeId' => null,
|
||||
'especialidadeId' => null,
|
||||
'usuarioId' => null
|
||||
],
|
||||
'form' => [
|
||||
'unidades' => [],
|
||||
'especialidades' => [],
|
||||
'usuarios' => []
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public static function getUnidades()
|
||||
{
|
||||
$data = request()->data;
|
||||
|
||||
$agendamentos = DB::table('ambulatorio_agendamento as agendamento')
|
||||
->join('ambulatorio_jornada as jornada', 'agendamento.jornada_id', '=', 'jornada.id')
|
||||
->join('cadastro_lotacao as lotacao', 'lotacao.id', '=', 'jornada.lotacao_id')
|
||||
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', '=', 'lotacao.infraestrutura_id')
|
||||
->join('cadastro_unidade as unidade', 'unidade.id', '=', 'infraestrutura.unidade_id')
|
||||
->whereIn('agendamento.status_id', self::getStatus())
|
||||
->where('jornada.data', '=', $data)
|
||||
->select('unidade.id', 'unidade.nome_fantasia');
|
||||
|
||||
$demandaEspontanea = DB::table('ambulatorio_demanda_espontanea as demanda_espontanea')
|
||||
->join('ambulatorio_atendimento as atendimento', 'atendimento.demanda_espontanea_id', '=', 'demanda_espontanea.id')
|
||||
->join('painel_senha_fila as fila', 'fila.id', '=', 'atendimento.fila_id')
|
||||
->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', '=', 'fila.centro_custo_id')
|
||||
->join('cadastro_infraestrutura as infraestrutura', 'infraestrutura.id', '=', 'centro_custo.infraestrutura_id')
|
||||
->join('cadastro_unidade as unidade', 'unidade.id', '=', 'infraestrutura.unidade_id')
|
||||
->where('atendimento.finalizado_em', '=', $data)
|
||||
->select('unidade.id', 'unidade.nome_fantasia');
|
||||
|
||||
return $agendamentos
|
||||
->union($demandaEspontanea)
|
||||
->orderBy('nome_fantasia')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function getEspecialidades()
|
||||
{
|
||||
|
||||
$data = request()->data;
|
||||
$unidade = request()->unidade_id;
|
||||
|
||||
return DB::table('cadastro_especialidade')
|
||||
->select([
|
||||
'cadastro_especialidade.id',
|
||||
'cadastro_especialidade.nome',
|
||||
])
|
||||
->where(fn ($q) => $q
|
||||
->whereExists(fn ($q) => $q
|
||||
->select(DB::raw(1))
|
||||
->from('ambulatorio_atendimento')
|
||||
->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id')
|
||||
->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id')
|
||||
->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_jornada.lotacao_id')
|
||||
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id')
|
||||
->whereColumn('ambulatorio_jornada.especialidade_id', 'cadastro_especialidade.id')
|
||||
->whereIn('ambulatorio_agendamento.status_id', self::getStatus())
|
||||
->where('ambulatorio_jornada.data', $data)
|
||||
->where('cadastro_infraestrutura.unidade_id', $unidade)
|
||||
)
|
||||
->orWhereExists(fn ($q) => $q
|
||||
->select(DB::raw(1))
|
||||
->from('ambulatorio_atendimento')
|
||||
->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id')
|
||||
->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_demanda_espontanea.created_lotacao_id')
|
||||
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id')
|
||||
->whereColumn('ambulatorio_demanda_espontanea.especialidade_id', 'cadastro_especialidade.id')
|
||||
->whereNotNull('ambulatorio_atendimento.finalizado_em')
|
||||
->where('ambulatorio_demanda_espontanea.data', $data)
|
||||
->where('cadastro_infraestrutura.unidade_id', $unidade)
|
||||
)
|
||||
)
|
||||
->orderBy('cadastro_especialidade.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function getUsuarios()
|
||||
{
|
||||
$data = request()->data;
|
||||
$unidade = request()->unidade_id;
|
||||
$especialidade = request()->especialidade_id;
|
||||
|
||||
return DB::table('cadastro_usuario')
|
||||
->select([
|
||||
'cadastro_usuario.id',
|
||||
'cadastro_cidadao.nome',
|
||||
])
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
|
||||
->whereExists(fn ($q) => $q
|
||||
->select(DB::raw(1))
|
||||
->from('cadastro_lotacao')
|
||||
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'cadastro_lotacao.infraestrutura_id')
|
||||
->join('cadastro_endereco', 'cadastro_endereco.id', 'cadastro_infraestrutura.endereco_id')
|
||||
->join('cadastro_logradouro', 'cadastro_logradouro.id', 'cadastro_endereco.logradouro_id')
|
||||
->whereColumn('cadastro_lotacao.usuario_id', 'cadastro_usuario.id')
|
||||
->when(request()->unidade_id, fn($q) => $q->where('cadastro_infraestrutura.unidade_id', request()->unidade_id))
|
||||
->where(fn ($q) => $q
|
||||
->whereExists(fn ($q) => $q
|
||||
->select(DB::raw(1))
|
||||
->from('ambulatorio_agendamento')
|
||||
->join('ambulatorio_jornada', 'ambulatorio_jornada.id', 'ambulatorio_agendamento.jornada_id')
|
||||
->whereColumn('ambulatorio_jornada.lotacao_id', 'cadastro_lotacao.id')
|
||||
->where('ambulatorio_jornada.data', $data)
|
||||
->where('ambulatorio_jornada.especialidade_id', $especialidade)
|
||||
->where('cadastro_infraestrutura.unidade_id', $unidade)
|
||||
)
|
||||
->orWhereExists(fn ($q) => $q
|
||||
->select(DB::raw(1))
|
||||
->from('ambulatorio_demanda_espontanea')
|
||||
->whereColumn('ambulatorio_demanda_espontanea.usuario_id', 'cadastro_usuario.id')
|
||||
->where('ambulatorio_demanda_espontanea.data', $data)
|
||||
->where('ambulatorio_demanda_espontanea.especialidade_id', $especialidade)
|
||||
->where('cadastro_infraestrutura.unidade_id', $unidade)
|
||||
)
|
||||
)
|
||||
)
|
||||
->orderBy('cadastro_cidadao.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function list()
|
||||
{
|
||||
return Atendimento::whereHas('agendamento', fn($q) => $q->whereIn('status_id', self::getStatus()))
|
||||
->whereHas('agendamento.jornada.lotacao.infraestrutura', fn($q) => $q->where('unidade_id', request()->unidade_id))
|
||||
->whereHas('agendamento.jornada', fn($q) => $q->where('especialidade_id', request()->especialidade_id))
|
||||
->whereHas('agendamento.jornada.lotacao', fn($q) => $q->where('usuario_id', request()->usuario_id))
|
||||
->whereHas('agendamento.jornada', fn($q) => $q->whereDate('data', request()->data))
|
||||
->orHas('demandaEspontanea')
|
||||
->whereHas('fila.centroCusto.infraestrutura', fn($q) => $q->where('unidade_id', request()->unidade_id))
|
||||
->whereHas('demandaEspontanea', fn($q) => $q->where('especialidade_id', request()->especialidade_id))
|
||||
->whereHas('demandaEspontanea', fn($q) => $q->where('usuario_id', request()->usuario_id))
|
||||
->whereDate('finalizado_em', request()->data)
|
||||
->get()
|
||||
->map(function($atendimento) {
|
||||
if($atendimento->agendamento_id) {
|
||||
return [
|
||||
'id' => $atendimento->id,
|
||||
'nome' => $atendimento->agendamento->paciente->cidadao->nome,
|
||||
'prontuario' => $atendimento->agendamento->paciente->prontuario,
|
||||
'status' => $atendimento->agendamento->status->nome,
|
||||
'selecionado' => false
|
||||
];
|
||||
} else if($atendimento->demanda_espontanea_id) {
|
||||
return [
|
||||
'id' => $atendimento->id,
|
||||
'nome' => $atendimento->demandaEspontanea->paciente->cidadao->nome,
|
||||
'prontuario' => $atendimento->demandaEspontanea->paciente->prontuario,
|
||||
'status' => 'REALIZADO',
|
||||
'selecionado' => false
|
||||
];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static function store()
|
||||
{
|
||||
DB::transaction(function() {
|
||||
foreach(request()->all() as $id) {
|
||||
$atendimento = Atendimento::find($id);
|
||||
if($atendimento->agendamento_id) {
|
||||
$atendimento->agendamento->update([ 'status_id' => AgendamentoStatus::AGENDADO ]);
|
||||
}
|
||||
$atendimento->finalizado_em = null;
|
||||
$atendimento->finalizado_lotacao_id = null;
|
||||
$atendimento->save();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user