This commit is contained in:
254
api/app/Services/Ambulatorio/AssinaturaEletronicaService.php
Normal file
254
api/app/Services/Ambulatorio/AssinaturaEletronicaService.php
Normal file
@@ -0,0 +1,254 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Ambulatorio;
|
||||
|
||||
use App\Enums\Cadastro\SujeitoAtencao\PreferenciaNome;
|
||||
use App\Models\Ambulatorio\Atendimento;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Sexo;
|
||||
use App\Models\Cns\TermoInvalido;
|
||||
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'),
|
||||
],
|
||||
'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),
|
||||
'permissao_assinatura' => self::getPermissaoAssinatura(),
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPermissaoAssinatura()
|
||||
{
|
||||
$lotacaoLogada = self::getCurrentLotacao();
|
||||
|
||||
return DB::table('ambulatorio_parametrizacao_infraestrutura_assinatura_eletronica AS infraestrutura_assinatura_eletronica')
|
||||
->join('ambulatorio_parametrizacao_infraestrutura AS parametrizacao_infraestrutura', 'parametrizacao_infraestrutura.id', 'infraestrutura_assinatura_eletronica.parametrizacao_infraestrutura_id')
|
||||
->join('ambulatorio_parametrizacao_assinatura_eletronica as assinatura_eletronica', 'assinatura_eletronica.id', 'infraestrutura_assinatura_eletronica.parametrizacao_assinatura_eletronica_id')
|
||||
->when($lotacaoLogada->infraestrutura_id, fn ($q, $id) => $q->where('parametrizacao_infraestrutura.infraestrutura_id', $id))
|
||||
->select(
|
||||
'assinatura_eletronica.id as id'
|
||||
)
|
||||
->get();
|
||||
}
|
||||
|
||||
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)
|
||||
];
|
||||
|
||||
$atendimentoQuery = DB::table('ambulatorio_atendimento_soap')
|
||||
->select([
|
||||
'ambulatorio_atendimento_soap.id',
|
||||
'ambulatorio_atendimento_soap.atendimento_id',
|
||||
'cadastro_sexo.descricao AS sexo',
|
||||
'ambulatorio_atendimento_soap.created_at',
|
||||
'ambulatorio_atendimento_soap.created_at as data_hora',
|
||||
'ambulatorio_atendimento_soap.is_atendimento_enfermagem',
|
||||
'ambulatorio_atendimento_soap.is_atendimento_soap_assinado',
|
||||
'ocupacao.id AS ocupacao_id',
|
||||
'ocupacao.codigo AS ocupacao_codigo',
|
||||
'ocupacao.nome AS ocupacao_nome',
|
||||
'cadastro_setor.nome AS setor_nome',
|
||||
'cadastro_centro_custo.infraestrutura_id AS centro_custo_infraestrutura_id',
|
||||
'cadastro_paciente.prontuario AS paciente_prontuario',
|
||||
'cadastro_cidadao.data_nascimento AS paciente_data_nascimento',
|
||||
'cadastro_cidadao.nome_mae AS paciente_nome_mae',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
'cadastro_cidadao.preferencia_nome_id as paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.caracteristicas_fisicas AS paciente_caracteristicas_fisicas',
|
||||
'cadastro_paciente.sequencial_desconhecido_em AS paciente_sequencial_desconhecido_em',
|
||||
DB::raw("'ATENDIMENTO' as tipo"),
|
||||
])
|
||||
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id')
|
||||
->join('ambulatorio_agendamento', 'ambulatorio_agendamento.id', 'ambulatorio_atendimento.agendamento_id')
|
||||
->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_atendimento_soap.created_lotacao_id')
|
||||
->join('painel_senha_fila', 'painel_senha_fila.id', 'ambulatorio_atendimento.fila_id')
|
||||
->join('cadastro_paciente', 'cadastro_paciente.id', 'painel_senha_fila.paciente_id')
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->join('sigtap.ocupacao', 'ocupacao.id', 'cadastro_lotacao.ocupacao_id')
|
||||
->join('cadastro_centro_custo', 'cadastro_centro_custo.id', 'ambulatorio_atendimento_soap.created_centro_custo_id')
|
||||
->join('cadastro_setor', 'cadastro_setor.id', 'cadastro_centro_custo.setor_id')
|
||||
->leftJoin('cadastro_sexo', 'cadastro_sexo.id', 'cadastro_cidadao.sexo_id')
|
||||
->where('ambulatorio_atendimento_soap.is_rascunho', false)
|
||||
->where('cadastro_centro_custo.infraestrutura_id', $infraestruturaId)
|
||||
->where('cadastro_lotacao.usuario_id', $usuarioId)
|
||||
->whereBetween('ambulatorio_atendimento_soap.created_at', $periodo)
|
||||
->when($request->paciente, fn ($q, $value) => $q->where('cadastro_cidadao.nome', 'like', "{$value}%"))
|
||||
->when($request->sexo_id, fn ($q, $id) => $q->where('cadastro_cidadao.sexo_id', $id))
|
||||
->when($request->data_nascimento, fn ($q, $data) => $q
|
||||
->whereDate('cadastro_cidadao.data_nascimento', Carbon::createFromFormat('d/m/Y', $data))
|
||||
)
|
||||
->when($request->atendimento, fn ($q, $id) => $q->where('ambulatorio_atendimento_soap.atendimento_id', $id))
|
||||
->unionAll(
|
||||
DB::table('ambulatorio_atendimento_soap')
|
||||
->select([
|
||||
'ambulatorio_atendimento_soap.id',
|
||||
'ambulatorio_atendimento_soap.atendimento_id',
|
||||
'cadastro_sexo.descricao AS sexo',
|
||||
'ambulatorio_atendimento_soap.created_at',
|
||||
'ambulatorio_atendimento_soap.created_at as data_hora',
|
||||
'ambulatorio_atendimento_soap.is_atendimento_enfermagem',
|
||||
'ambulatorio_atendimento_soap.is_atendimento_soap_assinado',
|
||||
'ocupacao.id AS ocupacao_id',
|
||||
'ocupacao.codigo AS ocupacao_codigo',
|
||||
'ocupacao.nome AS ocupacao_nome',
|
||||
'cadastro_setor.nome AS setor_nome',
|
||||
'cadastro_centro_custo.infraestrutura_id AS centro_custo_infraestrutura_id',
|
||||
'cadastro_paciente.prontuario AS paciente_prontuario',
|
||||
'cadastro_cidadao.data_nascimento AS paciente_data_nascimento',
|
||||
'cadastro_cidadao.nome_mae AS paciente_nome_mae',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
'cadastro_cidadao.nome_social AS paciente_nome_social',
|
||||
'cadastro_cidadao.preferencia_nome_id as paciente_preferencia_nome_id',
|
||||
'cadastro_cidadao.caracteristicas_fisicas AS paciente_caracteristicas_fisicas',
|
||||
'cadastro_paciente.sequencial_desconhecido_em AS paciente_sequencial_desconhecido_em',
|
||||
DB::raw("'ATENDIMENTO' as tipo"),
|
||||
])
|
||||
->join('ambulatorio_atendimento', 'ambulatorio_atendimento.id', 'ambulatorio_atendimento_soap.atendimento_id')
|
||||
->join('ambulatorio_demanda_espontanea', 'ambulatorio_demanda_espontanea.id', 'ambulatorio_atendimento.demanda_espontanea_id')
|
||||
->join('cadastro_lotacao', 'cadastro_lotacao.id', 'ambulatorio_atendimento_soap.created_lotacao_id')
|
||||
->join('painel_senha_fila', 'painel_senha_fila.id', 'ambulatorio_atendimento.fila_id')
|
||||
->join('cadastro_paciente', 'cadastro_paciente.id', 'painel_senha_fila.paciente_id')
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->leftJoin('cadastro_sexo', 'cadastro_sexo.id', 'cadastro_cidadao.sexo_id')
|
||||
->join('sigtap.ocupacao', 'ocupacao.id', 'cadastro_lotacao.ocupacao_id')
|
||||
->join('cadastro_centro_custo', 'cadastro_centro_custo.id', 'ambulatorio_atendimento_soap.created_centro_custo_id')
|
||||
->join('cadastro_setor', 'cadastro_setor.id', 'cadastro_centro_custo.setor_id')
|
||||
->where('ambulatorio_atendimento_soap.is_rascunho', false)
|
||||
->where('cadastro_centro_custo.infraestrutura_id', $infraestruturaId)
|
||||
->where('cadastro_lotacao.usuario_id', $usuarioId)
|
||||
->whereBetween('ambulatorio_atendimento_soap.created_at', $periodo)
|
||||
->when($request->paciente, fn ($q, $value) => $q->where('cadastro_cidadao.nome', 'like', "{$value}%"))
|
||||
->when($request->sexo_id, fn ($q, $id) => $q->where('cadastro_cidadao.sexo_id', $id))
|
||||
->when($request->data_nascimento, fn ($q, $data) => $q
|
||||
->whereDate('cadastro_cidadao.data_nascimento', Carbon::createFromFormat('d/m/Y', $data))
|
||||
)
|
||||
->when($request->atendimento, fn ($q, $id) => $q->where('ambulatorio_atendimento_soap.atendimento_id', $id))
|
||||
);
|
||||
|
||||
$documentos = null;
|
||||
|
||||
$documentos = $atendimentoQuery;
|
||||
|
||||
$documentos = $documentos
|
||||
->orderBy('data_hora')
|
||||
->get()
|
||||
->filter(function ($documento) {
|
||||
|
||||
$assinados = ImpressaoService::getImpressoesAtendimentoAssinados($documento->id);
|
||||
|
||||
if ($assinados->isEmpty() || $assinados->count() === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$atendimento = Atendimento::where('id', $documento->atendimento_id)->first();
|
||||
$impressoes = ImpressaoService::getImpressoesAtendimento($atendimento);
|
||||
|
||||
return $impressoes->count() !== $assinados->count();
|
||||
})
|
||||
->map(function ($documento) {
|
||||
|
||||
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;
|
||||
})
|
||||
->values()
|
||||
->toArray();
|
||||
|
||||
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',
|
||||
],
|
||||
'atendimento' => [
|
||||
'nullable',
|
||||
'integer',
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user