This commit is contained in:
240
api/app/Services/ExameLaboratorial/ImpressaoExameService.php
Normal file
240
api/app/Services/ExameLaboratorial/ImpressaoExameService.php
Normal file
@@ -0,0 +1,240 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ExameLaboratorial;
|
||||
|
||||
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
||||
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ImpressaoExameService extends Service
|
||||
{
|
||||
|
||||
public static function create()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'periodo' => [
|
||||
'start' => $currentDate,
|
||||
'end' => $currentDate,
|
||||
],
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function list(Request $request)
|
||||
{
|
||||
$now = Carbon::now();
|
||||
$currentDate = $now->format('Y-m-d');
|
||||
|
||||
$agendamentosComGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
'exame_laboratorial_agendamento.data AS data',
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%d/%m/%Y") AS data_coleta'),
|
||||
DB::raw('CASE WHEN cadastro_cidadao_mae.nome_mae IS NOT NULL THEN cadastro_cidadao_mae.nome_mae ELSE "NÃO INFORMADO" END AS nome_mae'),
|
||||
'cadastro_grupo.id AS exame_grupo_id',
|
||||
'cadastro_grupo.nome AS exame_grupo_nome',
|
||||
DB::raw('true AS pertence_grupo'),
|
||||
DB::raw('true AS mapa_agrupado'),
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'),
|
||||
'exame_laboratorial_agendamento_exame.assinatura_profissional_id AS assinatura_profissional_id',
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_cidadao as cadastro_cidadao_mae', 'cadastro_cidadao_mae.id', 'cadastro_paciente.cidadao_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->whereNotNull('parametrizacao_exame.grupo_tipo_id')
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.id',
|
||||
'cadastro_grupo.id',
|
||||
'exame_laboratorial_agendamento_exame.assinatura_profissional_id',
|
||||
]);
|
||||
|
||||
$agendamentosComGrupo->where('parametrizacao_exame.mapa_agrupado', true);
|
||||
$agendamentosComGrupo->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::ASSINADO);
|
||||
|
||||
if ($request->periodo) {
|
||||
|
||||
$agendamentosComGrupo->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
|
||||
$agendamentosComGrupo->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
|
||||
} else {
|
||||
|
||||
$agendamentosComGrupo->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
if ($request->paciente_id) {
|
||||
|
||||
$agendamentosComGrupo->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id);
|
||||
}
|
||||
|
||||
if ($request->agendamento_numero) {
|
||||
|
||||
$agendamentosComGrupo->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
|
||||
}
|
||||
|
||||
if ($request->prontuario) {
|
||||
|
||||
$agendamentosComGrupo->where('cadastro_paciente.prontuario', $request->prontuario);
|
||||
}
|
||||
|
||||
if ($request->exame_id && $request->pertence_grupo) {
|
||||
|
||||
$agendamentosComGrupo->where('cadastro_grupo.id', $request->exame_id);
|
||||
}
|
||||
|
||||
if ($request->data_nascimento) {
|
||||
|
||||
$agendamentosComGrupo->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
|
||||
}
|
||||
|
||||
$agendamentosComGrupo = $agendamentosComGrupo->get()->toArray();
|
||||
|
||||
$agendamentosSemMapaAgrupado = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
'exame_laboratorial_agendamento.data AS data',
|
||||
DB::raw('CASE WHEN cadastro_cidadao_mae.nome_mae IS NOT NULL THEN cadastro_cidadao_mae.nome_mae ELSE "NÃO INFORMADO" END AS nome_mae'),
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%d/%m/%Y") AS data_coleta'),
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
'parametrizacao_exame.nome AS exame_grupo_nome',
|
||||
DB::raw('true AS pertence_grupo'),
|
||||
DB::raw('false AS mapa_agrupado'),
|
||||
'exame_laboratorial_agendamento_exame.assinatura_profissional_id AS assinatura_profissional_id',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_grupo', 'cadastro_grupo.id', 'parametrizacao_exame.grupo_tipo_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_cidadao as cadastro_cidadao_mae', 'cadastro_cidadao_mae.id', 'cadastro_paciente.cidadao_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->whereNotNull('parametrizacao_exame.grupo_tipo_id')
|
||||
->groupBy([
|
||||
'exame_laboratorial_agendamento.id',
|
||||
'parametrizacao_exame.id',
|
||||
]);
|
||||
|
||||
$agendamentosSemMapaAgrupado->where(function ($query) {
|
||||
|
||||
$query->where('parametrizacao_exame.mapa_agrupado', false)
|
||||
->orWhereNull('parametrizacao_exame.mapa_agrupado');
|
||||
});
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::ASSINADO);
|
||||
|
||||
if ($request->periodo) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
|
||||
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
|
||||
} else {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
if ($request->paciente_id) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id);
|
||||
}
|
||||
|
||||
if ($request->agendamento_numero) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
|
||||
}
|
||||
|
||||
if ($request->prontuario) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('cadastro_paciente.prontuario', $request->prontuario);
|
||||
}
|
||||
|
||||
if ($request->data_nascimento) {
|
||||
|
||||
$agendamentosSemMapaAgrupado->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
|
||||
}
|
||||
|
||||
$agendamentosSemMapaAgrupado = $agendamentosSemMapaAgrupado->get()->toArray();
|
||||
|
||||
$agendamentosSemGrupo = DB::table('exame_laboratorial_agendamento_exame')
|
||||
->select([
|
||||
'exame_laboratorial_agendamento.id AS agendamento_id',
|
||||
'exame_laboratorial_agendamento.agendamento_numero AS agendamento_numero',
|
||||
'exame_laboratorial_agendamento.data AS data',
|
||||
DB::raw('CASE WHEN cadastro_cidadao_mae.nome_mae IS NOT NULL THEN cadastro_cidadao_mae.nome_mae ELSE "NÃO INFORMADO" END AS nome_mae'),
|
||||
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
||||
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento_exame.data_coleta, "%d/%m/%Y") AS data_coleta'),
|
||||
'parametrizacao_exame.id AS exame_grupo_id',
|
||||
'parametrizacao_exame.nome AS exame_grupo_nome',
|
||||
DB::raw('false AS pertence_grupo'),
|
||||
'exame_laboratorial_agendamento_exame.assinatura_profissional_id AS assinatura_profissional_id',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.preferencia_nome_id AS paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) as paciente_prontuario'),
|
||||
])
|
||||
->leftJoin('exame_laboratorial_agendamento', 'exame_laboratorial_agendamento.id', 'exame_laboratorial_agendamento_exame.agendamento_id')
|
||||
->leftJoin('parametrizacao_exame', 'parametrizacao_exame.id', 'exame_laboratorial_agendamento_exame.exame_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'exame_laboratorial_agendamento.paciente_id')
|
||||
->leftJoin('cadastro_cidadao as cadastro_cidadao_mae', 'cadastro_cidadao_mae.id', 'cadastro_paciente.cidadao_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->whereNull('parametrizacao_exame.grupo_tipo_id');
|
||||
|
||||
if ($request->periodo) {
|
||||
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
|
||||
} else {
|
||||
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.data', $currentDate);
|
||||
}
|
||||
|
||||
if ($request->paciente_id) {
|
||||
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id);
|
||||
}
|
||||
|
||||
if ($request->agendamento_numero) {
|
||||
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
|
||||
}
|
||||
|
||||
if ($request->prontuario) {
|
||||
|
||||
$agendamentosSemGrupo->where('cadastro_paciente.prontuario', $request->prontuario);
|
||||
}
|
||||
|
||||
if ($request->data_nascimento) {
|
||||
|
||||
$agendamentosSemGrupo->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
|
||||
}
|
||||
|
||||
$agendamentosSemGrupo->where('exame_laboratorial_agendamento_exame.status_id', StatusAgendamento::ASSINADO);
|
||||
|
||||
$agendamentosSemGrupo = $agendamentosSemGrupo->get()->toArray();
|
||||
|
||||
$agendamentos = array_merge($agendamentosComGrupo, $agendamentosSemGrupo, $agendamentosSemMapaAgrupado);
|
||||
|
||||
return [
|
||||
'model' => [
|
||||
'agendamentos' => $agendamentos,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user