390 lines
19 KiB
PHP
390 lines
19 KiB
PHP
<?php
|
|
|
|
namespace App\Services\ProntoAtendimento;
|
|
|
|
use App\Enums\Cadastro\Medicamento\TipoReceita;
|
|
use App\Enums\Cadastro\SujeitoAtencao\PreferenciaNome;
|
|
use App\Models\Cadastro\SujeitoAtencao\Sexo;
|
|
use App\Models\Cns\TermoInvalido;
|
|
use App\Models\ProntoAtendimento\Atendimento\Medico\Receita;
|
|
use App\Services\Service;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class AssinaturaEletronicaService extends Service
|
|
{
|
|
public static function list()
|
|
{
|
|
$now = Carbon::now();
|
|
$currentDate = $now->format('d/m/Y');
|
|
$currentLotacao = self::getCurrentLotacao();
|
|
|
|
return [
|
|
'form' => [
|
|
'unidade' => $currentLotacao->infraestrutura->unidade->nome_fantasia,
|
|
'infraestrutura' => $currentLotacao->infraestrutura->nome,
|
|
'sexos' => Sexo::orderBy('descricao')->get(['id', 'descricao'])->makeHidden('identificador'),
|
|
'tipos_documento' => [
|
|
['value' => 'atendimento', 'text' => 'ATENDIMENTO'],
|
|
['value' => 'receita_medica', 'text' => 'RECEITA MÉDICA'],
|
|
],
|
|
],
|
|
'model' => [
|
|
'periodo' => [
|
|
'start' => $currentDate,
|
|
'end' => $currentDate,
|
|
],
|
|
'sexo_id' => null,
|
|
'paciente' => null,
|
|
'data_nascimento' => null,
|
|
'faa' => null,
|
|
'tipo_documento' => null,
|
|
],
|
|
'additionals' => [
|
|
'periodo_min_date' => $now->copy()->subYears(130)->format('d/m/Y'),
|
|
'current_date' => $now->format('d/m/Y'),
|
|
'termos_invalidos' => TermoInvalido::all()->map(fn ($termo) => $termo->descricao),
|
|
]
|
|
];
|
|
}
|
|
|
|
public static function datatable($request)
|
|
{
|
|
self::validateDatatableRequest($request);
|
|
|
|
$infraestruturaId = self::getCurrentCentroCusto()->infraestrutura_id;
|
|
$usuarioId = self::getCurrentLotacao()->usuario_id;
|
|
|
|
$periodo = [
|
|
Carbon::createFromFormat('d/m/Y', $request->periodo['start'])->setTime(0, 0, 0),
|
|
Carbon::createFromFormat('d/m/Y', $request->periodo['end'])->setTime(23, 59, 59)
|
|
];
|
|
|
|
$triagemQuery = DB::table('pronto_atendimento_atendimento_triagem as triagem')
|
|
->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'triagem.created_centro_custo_id')
|
|
->join('cadastro_lotacao as lotacao', 'lotacao.id', 'triagem.created_lotacao_id')
|
|
->join('pronto_atendimento_atendimento as atendimento', 'atendimento.id', 'triagem.atendimento_id')
|
|
->join('pronto_atendimento_faa as faa', 'faa.id', 'atendimento.faa_id')
|
|
->join('cadastro_paciente as paciente', 'paciente.id', 'faa.paciente_id')
|
|
->join('cadastro_cidadao as cidadao', 'cidadao.id', 'paciente.cidadao_id')
|
|
->leftJoin('cadastro_sexo as sexo', 'sexo.id', 'cidadao.sexo_id')
|
|
->whereNull('triagem.deleted_at')
|
|
->where('triagem.is_faa_assinada', false)
|
|
->where('centro_custo.infraestrutura_id', $infraestruturaId)
|
|
->where('lotacao.usuario_id', $usuarioId)
|
|
->whereBetween('triagem.created_at', $periodo)
|
|
->when($request->paciente, fn ($q, $value) => $q->where('cidadao.nome', 'like', "{$value}%"))
|
|
->when($request->sexo_id, fn ($q, $id) => $q->where('cidadao.sexo_id', $id))
|
|
->when($request->data_nascimento, fn ($q, $data) => $q
|
|
->whereDate('cidadao.data_nascimento', Carbon::createFromFormat('d/m/Y', $data))
|
|
)
|
|
->when($request->faa, fn ($q, $id) => $q->where('faa.numero', $id))
|
|
->select(DB::raw("
|
|
atendimento.id as atendimento_id,
|
|
triagem.id,
|
|
'triagem' as tipo_identificador,
|
|
'TRIAGEM' as tipo,
|
|
triagem.created_at as data_hora,
|
|
faa.id AS faa,
|
|
faa.numero AS faa_numero,
|
|
sexo.descricao AS sexo,
|
|
paciente.prontuario AS paciente_prontuario,
|
|
cidadao.data_nascimento AS paciente_data_nascimento,
|
|
cidadao.nome_mae AS paciente_nome_mae,
|
|
cidadao.nome AS paciente_nome,
|
|
cidadao.nome_social AS paciente_nome_social,
|
|
cidadao.preferencia_nome_id as paciente_preferencia_nome_id,
|
|
cidadao.caracteristicas_fisicas AS paciente_caracteristicas_fisicas,
|
|
paciente.sequencial_desconhecido_em AS paciente_sequencial_desconhecido_em,
|
|
0 as tem_receita_medica_comum,
|
|
0 as tem_receita_medica_especial
|
|
"));
|
|
|
|
$medicoQuery = DB::table('pronto_atendimento_atendimento_medico as medico')
|
|
->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'medico.created_centro_custo_id')
|
|
->join('cadastro_lotacao as lotacao', 'lotacao.id', 'medico.created_lotacao_id')
|
|
->join('pronto_atendimento_atendimento as atendimento', 'atendimento.id', 'medico.atendimento_id')
|
|
->join('pronto_atendimento_faa as faa', 'faa.id', 'atendimento.faa_id')
|
|
->join('cadastro_paciente as paciente', 'paciente.id', 'faa.paciente_id')
|
|
->join('cadastro_cidadao as cidadao', 'cidadao.id', 'paciente.cidadao_id')
|
|
->leftJoin('cadastro_sexo as sexo', 'sexo.id', 'cidadao.sexo_id')
|
|
->whereNull('medico.deleted_at')
|
|
->where('medico.is_faa_assinada', false)
|
|
->where('centro_custo.infraestrutura_id', $infraestruturaId)
|
|
->where('lotacao.usuario_id', $usuarioId)
|
|
->whereBetween('medico.created_at', $periodo)
|
|
->when($request->paciente, fn ($q, $value) => $q->where('cidadao.nome', 'like', "{$value}%"))
|
|
->when($request->sexo_id, fn ($q, $id) => $q->where('cidadao.sexo_id', $id))
|
|
->when($request->data_nascimento, fn ($q, $data) => $q
|
|
->whereDate('cidadao.data_nascimento', Carbon::createFromFormat('d/m/Y', $data))
|
|
)
|
|
->when($request->faa, fn ($q, $id) => $q->where('faa.numero', $id))
|
|
->select(DB::raw("
|
|
atendimento.id as atendimento_id,
|
|
medico.id,
|
|
'medico' as tipo_identificador,
|
|
CONCAT('MÉDICO', IF(medico.is_observacao = 1,' (OBSERVAÇÃO)', '')) as tipo,
|
|
medico.created_at as data_hora,
|
|
faa.id AS faa,
|
|
faa.numero AS faa_numero,
|
|
sexo.descricao AS sexo,
|
|
paciente.prontuario AS paciente_prontuario,
|
|
cidadao.data_nascimento AS paciente_data_nascimento,
|
|
cidadao.nome_mae AS paciente_nome_mae,
|
|
cidadao.nome AS paciente_nome,
|
|
cidadao.nome_social AS paciente_nome_social,
|
|
cidadao.preferencia_nome_id as paciente_preferencia_nome_id,
|
|
cidadao.caracteristicas_fisicas AS paciente_caracteristicas_fisicas,
|
|
paciente.sequencial_desconhecido_em AS paciente_sequencial_desconhecido_em,
|
|
0 as tem_receita_medica_comum,
|
|
0 as tem_receita_medica_especial
|
|
"));
|
|
|
|
$enfermagemQuery = DB::table('pronto_atendimento_atendimento_enfermagem as enfermagem')
|
|
->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'enfermagem.created_centro_custo_id')
|
|
->join('cadastro_lotacao as lotacao', 'lotacao.id', 'enfermagem.created_lotacao_id')
|
|
->join('pronto_atendimento_atendimento as atendimento', 'atendimento.id', 'enfermagem.atendimento_id')
|
|
->join('pronto_atendimento_faa as faa', 'faa.id', 'atendimento.faa_id')
|
|
->join('cadastro_paciente as paciente', 'paciente.id', 'faa.paciente_id')
|
|
->join('cadastro_cidadao as cidadao', 'cidadao.id', 'paciente.cidadao_id')
|
|
->leftJoin('cadastro_sexo as sexo', 'sexo.id', 'cidadao.sexo_id')
|
|
->whereNull('enfermagem.deleted_at')
|
|
->where('enfermagem.is_faa_assinada', false)
|
|
->where('centro_custo.infraestrutura_id', $infraestruturaId)
|
|
->where('lotacao.usuario_id', $usuarioId)
|
|
->whereBetween('enfermagem.created_at', $periodo)
|
|
->when($request->paciente, fn ($q, $value) => $q->where('cidadao.nome', 'like', "{$value}%"))
|
|
->when($request->sexo_id, fn ($q, $id) => $q->where('cidadao.sexo_id', $id))
|
|
->when($request->data_nascimento, fn ($q, $data) => $q
|
|
->whereDate('cidadao.data_nascimento', Carbon::createFromFormat('d/m/Y', $data))
|
|
)
|
|
->when($request->faa, fn ($q, $id) => $q->where('faa.numero', $id))
|
|
->select(DB::raw("
|
|
atendimento.id as atendimento_id,
|
|
enfermagem.id,
|
|
'enfermagem' as tipo_identificador,
|
|
'ENFERMAGEM' as tipo,
|
|
enfermagem.created_at as data_hora,
|
|
faa.id AS faa,
|
|
faa.numero AS faa_numero,
|
|
sexo.descricao AS sexo,
|
|
paciente.prontuario AS paciente_prontuario,
|
|
cidadao.data_nascimento AS paciente_data_nascimento,
|
|
cidadao.nome_mae AS paciente_nome_mae,
|
|
cidadao.nome AS paciente_nome,
|
|
cidadao.nome_social AS paciente_nome_social,
|
|
cidadao.preferencia_nome_id as paciente_preferencia_nome_id,
|
|
cidadao.caracteristicas_fisicas AS paciente_caracteristicas_fisicas,
|
|
paciente.sequencial_desconhecido_em AS paciente_sequencial_desconhecido_em,
|
|
0 as tem_receita_medica_comum,
|
|
0 as tem_receita_medica_especial
|
|
"));
|
|
|
|
$observacaoQuery = DB::table('pronto_atendimento_paciente_observacao as observacao')
|
|
->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'observacao.created_centro_custo_id')
|
|
->join('cadastro_lotacao as lotacao', 'lotacao.id', 'observacao.created_lotacao_id')
|
|
->join('pronto_atendimento_atendimento as atendimento', 'atendimento.id', 'observacao.atendimento_id')
|
|
->join('pronto_atendimento_faa as faa', 'faa.id', 'atendimento.faa_id')
|
|
->join('cadastro_paciente as paciente', 'paciente.id', 'faa.paciente_id')
|
|
->join('cadastro_cidadao as cidadao', 'cidadao.id', 'paciente.cidadao_id')
|
|
->leftJoin('cadastro_sexo as sexo', 'sexo.id', 'cidadao.sexo_id')
|
|
->whereNull('observacao.deleted_at')
|
|
->where('observacao.is_faa_assinada', false)
|
|
->where('centro_custo.infraestrutura_id', $infraestruturaId)
|
|
->where('lotacao.usuario_id', $usuarioId)
|
|
->whereBetween('observacao.created_at', $periodo)
|
|
->when($request->paciente, fn ($q, $value) => $q->where('cidadao.nome', 'like', "{$value}%"))
|
|
->when($request->sexo_id, fn ($q, $id) => $q->where('cidadao.sexo_id', $id))
|
|
->when($request->data_nascimento, fn ($q, $data) => $q
|
|
->whereDate('cidadao.data_nascimento', Carbon::createFromFormat('d/m/Y', $data))
|
|
)
|
|
->when($request->faa, fn ($q, $id) => $q->where('faa.numero', $id))
|
|
->select(DB::raw("
|
|
atendimento.id as atendimento_id,
|
|
observacao.id,
|
|
'observacao' as tipo_identificador,
|
|
'ENFERMAGEM (OBSERVAÇÃO)' as tipo,
|
|
observacao.created_at as data_hora,
|
|
faa.id AS faa,
|
|
faa.numero AS faa_numero,
|
|
sexo.descricao AS sexo,
|
|
paciente.prontuario AS paciente_prontuario,
|
|
cidadao.data_nascimento AS paciente_data_nascimento,
|
|
cidadao.nome_mae AS paciente_nome_mae,
|
|
cidadao.nome AS paciente_nome,
|
|
cidadao.nome_social AS paciente_nome_social,
|
|
cidadao.preferencia_nome_id as paciente_preferencia_nome_id,
|
|
cidadao.caracteristicas_fisicas AS paciente_caracteristicas_fisicas,
|
|
paciente.sequencial_desconhecido_em AS paciente_sequencial_desconhecido_em,
|
|
0 as tem_receita_medica_comum,
|
|
0 as tem_receita_medica_especial
|
|
"));
|
|
|
|
$receitaMedicaQuery = DB::table('pronto_atendimento_atendimento_medico_receita as receita')
|
|
->join('cadastro_centro_custo as centro_custo', 'centro_custo.id', 'receita.created_centro_custo_id')
|
|
->join('cadastro_lotacao as lotacao', 'lotacao.id', 'receita.created_lotacao_id')
|
|
->join('pronto_atendimento_atendimento_medico as medico', 'medico.id', 'receita.atendimento_medico_id')
|
|
->join('pronto_atendimento_atendimento as atendimento', 'atendimento.id', 'medico.atendimento_id')
|
|
->join('pronto_atendimento_faa as faa', 'faa.id', 'atendimento.faa_id')
|
|
->join('cadastro_paciente as paciente', 'paciente.id', 'faa.paciente_id')
|
|
->join('cadastro_cidadao as cidadao', 'cidadao.id', 'paciente.cidadao_id')
|
|
->leftJoin('cadastro_sexo as sexo', 'sexo.id', 'cidadao.sexo_id')
|
|
->whereNull('receita.deleted_at')
|
|
->where('receita.is_assinada', false)
|
|
->where('centro_custo.infraestrutura_id', $infraestruturaId)
|
|
->where('lotacao.usuario_id', $usuarioId)
|
|
->whereBetween('receita.created_at', $periodo)
|
|
->when($request->paciente, fn ($q, $value) => $q->where('cidadao.nome', 'like', "{$value}%"))
|
|
->when($request->sexo_id, fn ($q, $id) => $q->where('cidadao.sexo_id', $id))
|
|
->when($request->data_nascimento, fn ($q, $data) => $q
|
|
->whereDate('cidadao.data_nascimento', Carbon::createFromFormat('d/m/Y', $data))
|
|
)
|
|
->when($request->faa, fn ($q, $id) => $q->where('faa.numero', $id))
|
|
->select(DB::raw("
|
|
atendimento.id as atendimento_id,
|
|
receita.id,
|
|
'receita_medica' as tipo_identificador,
|
|
CONCAT('MÉDICO', IF(medico.is_observacao = 1,' (OBSERVAÇÃO)', ''), ' - RECEITA MÉDICA') as tipo,
|
|
receita.created_at as data_hora,
|
|
faa.id AS faa,
|
|
faa.numero AS faa_numero,
|
|
sexo.descricao AS sexo,
|
|
paciente.prontuario AS paciente_prontuario,
|
|
cidadao.data_nascimento AS paciente_data_nascimento,
|
|
cidadao.nome_mae AS paciente_nome_mae,
|
|
cidadao.nome AS paciente_nome,
|
|
cidadao.nome_social AS paciente_nome_social,
|
|
cidadao.preferencia_nome_id as paciente_preferencia_nome_id,
|
|
cidadao.caracteristicas_fisicas AS paciente_caracteristicas_fisicas,
|
|
paciente.sequencial_desconhecido_em AS paciente_sequencial_desconhecido_em
|
|
"))
|
|
->addSelect([
|
|
'tem_receita_medica_comum' => function ($query) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico_receita_medicamento as receita_medicamento')
|
|
->join('cadastro_medicamento as medicamento', 'medicamento.id', 'receita_medicamento.medicamento_id')
|
|
->whereColumn('receita_medicamento.receita_id', 'receita.id')
|
|
->whereNull('receita_medicamento.deleted_at')
|
|
->where('medicamento.receita_tipo_id', TipoReceita::COMUM);
|
|
},
|
|
'tem_receita_medica_especial' => function ($query) {
|
|
|
|
$query->select(DB::raw(1))
|
|
->from('pronto_atendimento_atendimento_medico_receita_medicamento as receita_medicamento')
|
|
->join('cadastro_medicamento as medicamento', 'medicamento.id', 'receita_medicamento.medicamento_id')
|
|
->whereColumn('receita_medicamento.receita_id', 'receita.id')
|
|
->whereNull('receita_medicamento.deleted_at')
|
|
->whereIn('medicamento.receita_tipo_id', [
|
|
TipoReceita::ESPECIAL,
|
|
TipoReceita::ESPECIAL_AMARELA,
|
|
TipoReceita::ESPECIAL_AZUL,
|
|
]);
|
|
},
|
|
]);
|
|
|
|
$documentos = null;
|
|
|
|
if ($request->tipo_documento == 'atendimento') {
|
|
|
|
$documentos = $triagemQuery
|
|
->unionAll($medicoQuery)
|
|
->unionAll($enfermagemQuery)
|
|
->unionAll($observacaoQuery);
|
|
}
|
|
else if ($request->tipo_documento == 'receita_medica') {
|
|
|
|
$documentos = $receitaMedicaQuery;
|
|
}
|
|
else {
|
|
|
|
$documentos = $triagemQuery
|
|
->unionAll($medicoQuery)
|
|
->unionAll($enfermagemQuery)
|
|
->unionAll($observacaoQuery)
|
|
->unionAll($receitaMedicaQuery);
|
|
}
|
|
|
|
$documentos = $documentos
|
|
->orderBy('data_hora')
|
|
->get()
|
|
->map(function ($documento) {
|
|
|
|
if ($documento->tipo_identificador == 'receita_medica') {
|
|
|
|
$documento->tem_receita_medica_comum = (bool) $documento->tem_receita_medica_comum;
|
|
$documento->tem_receita_medica_especial = (bool) $documento->tem_receita_medica_especial;
|
|
}
|
|
else {
|
|
|
|
unset($documento->tem_receita_medica_comum, $documento->tem_receita_medica_especial);
|
|
}
|
|
|
|
if (is_null($documento->paciente_nome_mae)) {
|
|
|
|
$documento->paciente_nome_mae = 'Não informado';
|
|
}
|
|
|
|
if (is_null($documento->paciente_caracteristicas_fisicas)) {
|
|
|
|
$documento->paciente_caracteristicas_fisicas = 'Não informado';
|
|
}
|
|
|
|
$documento->paciente_is_desconhecido = !is_null($documento->paciente_sequencial_desconhecido_em);
|
|
$documento->data_hora = (new Carbon($documento->data_hora))->format('d/m/Y H:i');
|
|
|
|
$documento->paciente_prefere_nome_social = $documento
|
|
->paciente_preferencia_nome_id === PreferenciaNome::NOME_SOCIAL;
|
|
|
|
$documento->paciente_data_nascimento = is_null($documento->paciente_data_nascimento)
|
|
? 'Não informada'
|
|
: (new Carbon($documento->paciente_data_nascimento))->format('d/m/Y');
|
|
|
|
unset(
|
|
$documento->paciente_preferencia_nome_id,
|
|
$documento->paciente_sequencial_desconhecido_em
|
|
);
|
|
|
|
return $documento;
|
|
});
|
|
|
|
return $documentos;
|
|
}
|
|
|
|
private static function validateDatatableRequest($request)
|
|
{
|
|
$minDate = Carbon::now()->subYears(130)->format('Y-m-d');
|
|
|
|
$request->validate([
|
|
'periodo.start' => [
|
|
'required',
|
|
'date_format:d/m/Y',
|
|
"after_or_equal:{$minDate}",
|
|
'before_or_equal:periodo.end',
|
|
],
|
|
'periodo.end' => [
|
|
'required',
|
|
'date_format:d/m/Y',
|
|
'after_or_equal:periodo.start',
|
|
'before_or_equal:now',
|
|
],
|
|
'sexo_id' => [
|
|
'nullable',
|
|
'integer',
|
|
],
|
|
'paciente' => [
|
|
'nullable',
|
|
'string',
|
|
],
|
|
'data_nascimento' => [
|
|
'nullable',
|
|
'date_format:d/m/Y',
|
|
],
|
|
'faa' => [
|
|
'nullable',
|
|
'integer',
|
|
],
|
|
]);
|
|
}
|
|
}
|