286 lines
12 KiB
PHP
286 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Services\ExameImagem;
|
|
|
|
use App\Enums\ExameImagem\Fechamento\Status;
|
|
use App\Models\Cadastro\Estabelecimento\Unidade;
|
|
use App\Models\Cadastro\SujeitoAtencao\Lotacao;
|
|
use App\Models\Cadastro\SujeitoAtencao\Paciente;
|
|
use App\Models\Parametrizacao\Exame\Exame;
|
|
use App\Models\Cadastro\SujeitoAtencao\Sexo;
|
|
use App\Models\ExameImagem\Agendamento\Agendamento;
|
|
use App\Models\ExameImagem\Agendamento\AgendamentoExame;
|
|
use App\Models\ExameImagem\Fechamento\Faturamento;
|
|
use App\Models\ExameImagem\Fechamento\StatusFechamento;
|
|
use App\Services\Service;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class AlteracaoExameFinalizadoService extends Service
|
|
{
|
|
public static function index()
|
|
{
|
|
$now = Carbon::now();
|
|
$currentDate = $now->format('d/m/Y');
|
|
$currentLotacao = self::getCurrentLotacao();
|
|
|
|
return [
|
|
'form' => [
|
|
'data_atual' => Carbon::now()->format('d/m/Y'),
|
|
'unidades' => [],
|
|
'profissionais' => [],
|
|
'grupoExames' => [],
|
|
'exames' => [],
|
|
'itens' => [],
|
|
'infraestrutura' => $currentLotacao->infraestrutura->nome,
|
|
'status' => StatusFechamento::orderBy('id')
|
|
->get()
|
|
->map(fn ($status) => [
|
|
'id' => $status->id,
|
|
'nome' => $status->nome
|
|
]),
|
|
],
|
|
'model' => [
|
|
'periodo' => [
|
|
'start' => $currentDate,
|
|
'end' => $currentDate,
|
|
],
|
|
'agendamentos' => [],
|
|
'agendamentosNotSave' => null,
|
|
'unidade_id' => null,
|
|
'especialidade_id' => null,
|
|
'usuario_id' => null,
|
|
'profissional_id' => null,
|
|
'grupo_exame_id' => null,
|
|
'exame_id' => null,
|
|
'agendamento_numero' => null,
|
|
'prontuario' => null,
|
|
],
|
|
'additionals' => [
|
|
'periodo_min_date' => $now->copy()->subYears(130)->format('d/m/Y'),
|
|
'current_date' => $now->format('d/m/Y')
|
|
]
|
|
];
|
|
}
|
|
|
|
public static function list()
|
|
{
|
|
|
|
$agendamentos = self::getAgendamentos();
|
|
|
|
return $agendamentos;
|
|
}
|
|
|
|
public static function getUnidades()
|
|
{
|
|
|
|
$start_date = request()->data_inicio;
|
|
$end_date = request()->data_fim;
|
|
|
|
return Unidade::whereHas('agendamentosExameImagem', function ($query) use ($start_date, $end_date) {
|
|
$query->whereBetween('data', [$start_date, $end_date])
|
|
->whereHas('agendamentoExame', function ($query) {
|
|
$query->whereIn('status_id', [Status::REALIZADO, Status::FALTOU]);
|
|
});
|
|
})
|
|
->get()
|
|
->map(function ($query){
|
|
return [
|
|
'unidade_id' => $query->id,
|
|
'nome_fantasia' => $query->nome_fantasia,
|
|
];
|
|
});
|
|
}
|
|
|
|
public static function getProfissional()
|
|
{
|
|
|
|
$start_date = request()->data_inicio;
|
|
$end_date = request()->data_fim;
|
|
$unidade = request()->unidade_id;
|
|
|
|
return DB::table('cadastro_usuario as usuario')
|
|
->join('cadastro_cidadao as cidadao', 'cidadao.id', 'usuario.cidadao_id')
|
|
->whereExists(fn ($q) => $q
|
|
->select(DB::raw(1))
|
|
->from('exame_imagem_agendamento as agendamento')
|
|
->join('exame_imagem_agendamento_exame as exame', 'agendamento.id', 'exame.agendamento_id')
|
|
->whereBetween('data', [$start_date, $end_date])
|
|
->whereColumn('agendamento.profissional_id', 'usuario.id')
|
|
->whereIn('exame.status_id', [Status::REALIZADO, Status::FALTOU])
|
|
->where('agendamento.unidade_id', $unidade)
|
|
)
|
|
->select(DB::raw("
|
|
usuario.id as profissional_id,
|
|
cidadao.nome as profissional_nome
|
|
"))
|
|
->get();
|
|
}
|
|
|
|
public static function getGrupoExames()
|
|
{
|
|
$start_date = request()->data_inicio;
|
|
$end_date = request()->data_fim;
|
|
$unidade = request()->unidade_id;
|
|
$profissional = request()->profissional_id;
|
|
|
|
return DB::table('cadastro_grupo as grupo')
|
|
->join('parametrizacao_exame as parametrizacao', 'parametrizacao.grupo_tipo_id', 'grupo.id')
|
|
->whereExists(fn ($q) => $q
|
|
->select(DB::raw(1))
|
|
->from('exame_imagem_agendamento as agendamento')
|
|
->join('exame_imagem_agendamento_exame as exame', 'agendamento.id', 'exame.agendamento_id')
|
|
->whereBetween('data', [$start_date, $end_date])
|
|
->whereIn('exame.status_id', [Status::REALIZADO, Status::FALTOU])
|
|
->where('agendamento.profissional_id', $profissional)
|
|
->where('agendamento.unidade_id', $unidade)
|
|
->whereColumn('exame.exame_id', 'parametrizacao.id')
|
|
)
|
|
->whereNull('grupo.deleted_at')
|
|
->select(DB::raw("
|
|
grupo.id as grupo_exame_id,
|
|
grupo.nome as grupo_nome
|
|
"))
|
|
->get()
|
|
->unique();
|
|
}
|
|
|
|
public static function getExames()
|
|
{
|
|
$start_date = request()->data_inicio;
|
|
$end_date = request()->data_fim;
|
|
$unidade = request()->unidade_id;
|
|
$profissional = request()->profissional_id;
|
|
$grupo = request()->grupo_exame_id;
|
|
|
|
return Exame::whereHas('agendamentoExameImagem', function ($query) use ($start_date, $end_date, $unidade, $profissional) {
|
|
$query->whereIn('status_id', [Status::REALIZADO, Status::FALTOU])
|
|
->whereHas('agendamento', function ($query) use ($start_date, $end_date, $unidade, $profissional) {
|
|
$query->whereBetween('data', [$start_date, $end_date]);
|
|
$query->where('profissional_id', $profissional);
|
|
$query->where('unidade_id', $unidade);
|
|
});
|
|
})
|
|
->where('grupo_tipo_id', $grupo)
|
|
->get()
|
|
->map(function ($query){
|
|
return [
|
|
'exame_id' => $query->id,
|
|
'exame_nome' => $query->nome,
|
|
];
|
|
});
|
|
}
|
|
|
|
private static function getAgendamentos()
|
|
{
|
|
return AgendamentoExame::with([
|
|
'exame.procedimento',
|
|
'agendamento.jornada.usuario',
|
|
'agendamento.unidade',
|
|
'agendamento.medico',
|
|
'agendamento.grupo',
|
|
'paciente.cidadao',
|
|
])
|
|
->whereHas('agendamento', function ($query) {
|
|
$query->whereBetween('data', [request()->data_inicio, request()->data_fim]);
|
|
$query->where('profissional_id', request()->profissional_id);
|
|
$query->where('unidade_id', request()->unidade_id)
|
|
->when(request()->agendamento_numero, fn ($query) => $query->where('agendamento_numero', request()->agendamento_numero))
|
|
->when(request()->prontuario, fn ($query) => $query->whereHas('Paciente', function ($query) {
|
|
$query->where('prontuario', request()->prontuario);
|
|
}));
|
|
})
|
|
->when(request()->exame_id, fn ($query) => $query->where('exame_id', request()->exame_id))
|
|
->when(request()->paciente, fn ($query) => $query->where('paciente_id', request()->paciente))
|
|
->when(request()->has('status'), function ($query) {
|
|
|
|
$status = request()->status;
|
|
|
|
if ($status == Status::REALIZADO) {
|
|
$query->where('status_id', Status::REALIZADO);
|
|
} elseif ($status == Status::FALTOU) {
|
|
$query->where('status_id', Status::FALTOU);
|
|
} elseif ($status == Status::FALTA_MEDICA) {
|
|
$query->where('status_id', Status::FALTA_MEDICA);
|
|
} else {
|
|
$query->whereIn('status_id', [Status::REALIZADO, Status::FALTOU, Status::FALTA_MEDICA]);
|
|
}
|
|
})
|
|
->get()
|
|
->map(function ($query){
|
|
return [
|
|
'agendamento_id' => $query->agendamento_id,
|
|
'exame_id' => $query->exame_id,
|
|
'exame_nome' => $query->exame->nome,
|
|
'data' => (new Carbon($query->agendamento->data))->format('d/m/Y') ?? null,
|
|
'grupo_exame' => $query->agendamento->grupo->nome,
|
|
'grupo_exame_id' => $query->agendamento->exame_id,
|
|
'procedimento' => [
|
|
'id' => $query->exame->procedimento->id ?? null,
|
|
'codigo' => $query->exame->procedimento->codigo ?? null,
|
|
'nome' => $query->exame->procedimento->nome ?? null,
|
|
],
|
|
'unidade_id' => $query->agendamento->unidade->id,
|
|
'paciente_id' => $query->agendamento->paciente_id,
|
|
'profissional_id' => $query->agendamento->profissional_id,
|
|
'profissional_nome' => $query->agendamento->jornada->usuario->nome,
|
|
'status_troca' => $query->status_troca,
|
|
'status_salvo' => $query->status_id ?? null,
|
|
'status_id' => $query->status_id ?? null,
|
|
'selecionado' => false,
|
|
'agendamento_numero' => $query->agendamento->agendamento_numero,
|
|
'id' => $query->id,
|
|
'grupo_exame' => $query->agendamento->grupo->nome,
|
|
'grupo_exame_id' => $query->agendamento->exame_id,
|
|
'medico_id' => $query->agendamento->medico_id,
|
|
'medico_nome' => $query->agendamento->medico->nome,
|
|
'nome_responsavel' => $query->agendamento->nome_responsavel,
|
|
'paciente_id' => $query->paciente_id,
|
|
'paciente_nome' => $query->paciente->cidadao->nome,
|
|
'paciente' => Paciente::where('id', $query->paciente_id)
|
|
->with('unidade')
|
|
->first(),
|
|
'prontuario' => $query->paciente->prontuario,
|
|
'profissional_id' => $query->agendamento->profissional_id,
|
|
'profissional_nome' => $query->agendamento->jornada->usuario->nome,
|
|
'unidade_id' => $query->agendamento->unidade_id,
|
|
'unidade_nome' => $query->agendamento->unidade->nome_fantasia,
|
|
'urgente' => $query->urgente ?? false,
|
|
];
|
|
});
|
|
}
|
|
|
|
public static function store(Request $request)
|
|
{
|
|
return self::saveStatusExame($request);
|
|
}
|
|
|
|
private static function saveStatusExame()
|
|
{
|
|
foreach (request()->all() as $params) {
|
|
|
|
$exame = AgendamentoExame::where('agendamento_id', $params['agendamento_id'])
|
|
->where('exame_id', $params['exame_id'])
|
|
->first();
|
|
|
|
if ($params['status_id'] == Status::AGENDADO) {
|
|
|
|
$exame->status_id = Status::AGENDADO;
|
|
$exame->arquivo = null;
|
|
$exame->laudo_nome = null;
|
|
$exame->status_troca = false;
|
|
|
|
Faturamento::destroy(Faturamento::where('agendamento_exame_id', $exame->id)->pluck('id'));
|
|
|
|
} else {
|
|
|
|
$exame->status_id = $params['status_id'];
|
|
}
|
|
|
|
$exame->save();
|
|
}
|
|
}
|
|
}
|