345 lines
20 KiB
PHP
345 lines
20 KiB
PHP
<?php
|
|
|
|
namespace App\Services\ExameLaboratorial;
|
|
|
|
use App\Enums\ExameLaboratorial\StatusAgendamento;
|
|
use App\Models\ExameLaboratorial\AgendamentoExame\AgendamentoExame;
|
|
use App\Models\ExameLaboratorial\Parametrizacao\MensagemProtocolo;
|
|
use App\Models\Intranet\Cliente;
|
|
use App\Services\Service;
|
|
use App\Utils\MaskUtil;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class ImpressaoService 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');
|
|
|
|
$agendamentos = 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.coleta_infraestrutura_id AS coleta_infraestrutura_id',
|
|
'exame_laboratorial_agendamento.coleta_unidade_id AS coleta_unidade_id',
|
|
'exame_laboratorial_agendamento.data AS data_entrega',
|
|
DB::raw('DATE_FORMAT(exame_laboratorial_agendamento.data, "%d/%m/%Y") AS data'),
|
|
'exame_laboratorial_agendamento.ultima_menstruacao AS ultima_menstruacao',
|
|
'exame_laboratorial_agendamento.gestante AS gestante',
|
|
DB::raw('TIME_FORMAT(exame_laboratorial_agendamento.horario, "%h:%i") AS hora_inicial'),
|
|
'exame_laboratorial_agendamento.infraestrutura_id AS infraestrutura_id',
|
|
'exame_laboratorial_agendamento.jornada_id AS jornada_id',
|
|
'exame_laboratorial_agendamento.laboratorio_infraestrutura_id AS laboratorio_infraestrutura_id',
|
|
'exame_laboratorial_agendamento.laboratorio_unidade_id AS laboratorio_id',
|
|
'exame_laboratorial_agendamento.medico_id AS medico_id',
|
|
'cadastro_cidadao_medico.nome AS medico_nome',
|
|
'exame_laboratorial_agendamento.observacao AS observacao',
|
|
'exame_laboratorial_agendamento.responsavel AS responsavel',
|
|
'exame_laboratorial_agendamento.suspeita_clinica AS suspeita',
|
|
'exame_laboratorial_agendamento.ultima_menstruacao AS data_menstruacao',
|
|
'exame_laboratorial_agendamento.unidade_id AS unidade_id',
|
|
'exame_laboratorial_agendamento.urgente AS urgente',
|
|
'exame_laboratorial_agendamento.uso_medicamento AS uso_medicamento',
|
|
'parametrizacao_exame.id AS exame_grupo_id',
|
|
'parametrizacao_exame.nome AS exame_grupo_nome',
|
|
'parametrizacao_exame.sigla_exame AS sigla_exame',
|
|
DB::raw('false AS pertence_grupo'),
|
|
'exame_laboratorial_agendamento_exame.status_id AS status_id',
|
|
'exame_laboratorial_agendamento_exame.usuario_id AS usuario_id',
|
|
'exame_laboratorial_agendamento_exame.ocupacao_id AS ocupacao_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',
|
|
'cadastro_cidadao.sexo_id AS paciente_sexo_id',
|
|
DB::raw('LPAD(cadastro_paciente.prontuario,7,0) AS paciente_prontuario'),
|
|
'cadastro_paciente.id AS paciente_id',
|
|
'cadastro_infraestrutura.telefone AS telefone',
|
|
DB::raw("COALESCE(cadastro_endereco.numero, 'S/N') AS endereco_numero"),
|
|
'cadastro_logradouro.nome AS logradouro_nome',
|
|
'cadastro_logradouro.cep AS logradouro_cep',
|
|
'cadastro_logradouro_tipo.descricao AS logradouro_tipo',
|
|
'cadastro_bairro.nome AS bairro_nome',
|
|
'cadastro_infraestrutura_coleta.telefone AS telefone_coleta',
|
|
DB::raw("COALESCE(cadastro_endereco_coleta.numero, 'S/N') AS endereco_numero_coleta"),
|
|
'cadastro_logradouro_coleta.nome AS logradouro_nome_coleta',
|
|
'cadastro_logradouro_coleta.cep AS logradouro_cep_coleta',
|
|
'cadastro_bairro_coleta.nome AS bairro_nome_coleta',
|
|
'cadastro_logradouro_tipo_coleta.descricao AS logradouro_tipo_coleta',
|
|
'cadastro_unidade.nome_fantasia AS infraestrutura_nome',
|
|
'cadastro_unidade_coleta.nome_fantasia AS coleta_infraestrutura_nome',
|
|
'cadastro_infraestrutura_laboratorio.telefone AS telefone_laboratorio',
|
|
DB::raw("COALESCE(cadastro_endereco_laboratorio.numero, 'S/N') AS endereco_numero_laboratorio"),
|
|
'cadastro_logradouro_laboratorio.nome AS logradouro_nome_laboratorio',
|
|
'cadastro_logradouro_laboratorio.cep AS logradouro_cep_laboratorio',
|
|
'cadastro_bairro_laboratorio.nome AS bairro_nome_laboratorio',
|
|
'cadastro_logradouro_tipo_laboratorio.descricao AS logradouro_tipo_laboratorio',
|
|
'cadastro_unidade.nome_fantasia AS infraestrutura_nome',
|
|
'cadastro_unidade_laboratorio.nome_fantasia AS laboratorio_infraestrutura_nome',
|
|
'exame_laboratorial_agendamento.supervisor_id AS supervisor_id',
|
|
DB::raw('CASE WHEN cadastro_cidadao.nome_mae IS NOT NULL THEN cadastro_cidadao.nome_mae ELSE "NÃO INFORMADO" END AS nome_mae'),
|
|
DB::raw('CONCAT(TIMESTAMPDIFF(YEAR, cadastro_cidadao.data_nascimento, exame_laboratorial_agendamento.data), " anos") AS paciente_idade'),
|
|
DB::raw('DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") AS data_nascimento'),
|
|
'regulacao_solicitacao.numero as regulacao_solicitacao_numero',
|
|
])
|
|
->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_usuario', 'cadastro_usuario.id', 'exame_laboratorial_agendamento.medico_id')
|
|
->leftJoin('cadastro_cidadao as cadastro_cidadao_medico', 'cadastro_cidadao_medico.id', 'cadastro_usuario.cidadao_id')
|
|
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
|
->leftJoin('cadastro_infraestrutura', 'cadastro_infraestrutura.id', 'exame_laboratorial_agendamento.infraestrutura_id')
|
|
->leftJoin('cadastro_unidade', 'cadastro_unidade.id', 'cadastro_infraestrutura.unidade_id')
|
|
->leftJoin('cadastro_infraestrutura as cadastro_infraestrutura_coleta', 'cadastro_infraestrutura_coleta.id', 'exame_laboratorial_agendamento.coleta_infraestrutura_id')
|
|
->leftJoin('cadastro_unidade as cadastro_unidade_coleta', 'cadastro_unidade_coleta.id', 'cadastro_infraestrutura_coleta.unidade_id')
|
|
->leftJoin('cadastro_infraestrutura as cadastro_infraestrutura_laboratorio', 'cadastro_infraestrutura_laboratorio.id', 'exame_laboratorial_agendamento.laboratorio_infraestrutura_id')
|
|
->leftJoin('cadastro_unidade as cadastro_unidade_laboratorio', 'cadastro_unidade_laboratorio.id', 'cadastro_infraestrutura_laboratorio.unidade_id')
|
|
->leftJoin('cadastro_endereco', 'cadastro_endereco.id', 'cadastro_infraestrutura.endereco_id')
|
|
->leftJoin('cadastro_logradouro', 'cadastro_logradouro.id', 'cadastro_endereco.logradouro_id')
|
|
->leftJoin('cadastro_logradouro_tipo as cadastro_logradouro_tipo', 'cadastro_logradouro_tipo.id', 'cadastro_logradouro.tipo_id')
|
|
->leftJoin('cadastro_bairro', 'cadastro_bairro.id', 'cadastro_logradouro.bairro_id')
|
|
->leftJoin('cadastro_endereco as cadastro_endereco_coleta', 'cadastro_endereco_coleta.id', 'cadastro_infraestrutura_coleta.endereco_id')
|
|
->leftJoin('cadastro_logradouro as cadastro_logradouro_coleta', 'cadastro_logradouro_coleta.id', 'cadastro_endereco_coleta.logradouro_id')
|
|
->leftJoin('cadastro_bairro as cadastro_bairro_coleta', 'cadastro_bairro_coleta.id', 'cadastro_logradouro_coleta.bairro_id')
|
|
->leftJoin('cadastro_logradouro_tipo as cadastro_logradouro_tipo_coleta', 'cadastro_logradouro_tipo_coleta.id', 'cadastro_logradouro_coleta.tipo_id')
|
|
->leftJoin('cadastro_endereco as cadastro_endereco_laboratorio', 'cadastro_endereco_laboratorio.id', 'cadastro_infraestrutura_laboratorio.endereco_id')
|
|
->leftJoin('cadastro_logradouro as cadastro_logradouro_laboratorio', 'cadastro_logradouro_laboratorio.id', 'cadastro_endereco_laboratorio.logradouro_id')
|
|
->leftJoin('cadastro_bairro as cadastro_bairro_laboratorio', 'cadastro_bairro_laboratorio.id', 'cadastro_logradouro_laboratorio.bairro_id')
|
|
->leftJoin('regulacao_solicitacao', 'regulacao_solicitacao.id', 'exame_laboratorial_agendamento.regulacao_solicitacao_id')
|
|
->leftJoin('cadastro_logradouro_tipo as cadastro_logradouro_tipo_laboratorio', 'cadastro_logradouro_tipo_laboratorio.id', 'cadastro_logradouro_laboratorio.tipo_id')
|
|
->where('exame_laboratorial_agendamento_exame.status_id', '!=', StatusAgendamento::CANCELADO)
|
|
->groupBy([
|
|
'exame_laboratorial_agendamento.id',
|
|
]);
|
|
|
|
if ($request->agendamento_numero) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
|
|
} else {
|
|
|
|
if ($request->periodo) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.data', '>=', $request->periodo['start']);
|
|
$agendamentos->where('exame_laboratorial_agendamento.data', '<=', $request->periodo['end']);
|
|
} else {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.data', $currentDate);
|
|
}
|
|
|
|
if ($request->paciente_id) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.paciente_id', $request->paciente_id);
|
|
}
|
|
|
|
if ($request->agendamento_numero) {
|
|
|
|
$agendamentos->where('exame_laboratorial_agendamento.agendamento_numero', $request->agendamento_numero);
|
|
}
|
|
|
|
if ($request->prontuario) {
|
|
|
|
$agendamentos->where('cadastro_paciente.prontuario', $request->prontuario);
|
|
}
|
|
|
|
if ($request->data_nascimento) {
|
|
|
|
$agendamentos->where('cadastro_cidadao.data_nascimento', $request->data_nascimento);
|
|
}
|
|
}
|
|
|
|
$agendamentos = $agendamentos->get()
|
|
->map(function ($atendimento) {
|
|
|
|
$atendimento->impressoes = self::getImpressoes(
|
|
[
|
|
'agendamento' => [
|
|
'protocolo' => [
|
|
'exames_incluidos' => AgendamentoExame::where('agendamento_id', $atendimento->agendamento_id)
|
|
->get()
|
|
->map(function ($query) {
|
|
return [
|
|
'id' => $query->id,
|
|
'exame_id' => $query->exame_id,
|
|
'nome' => $query->exame ? $query->exame->nome : null,
|
|
'codigo' => $query->exame->codigo ?? null,
|
|
'valor' => $query->valor,
|
|
'status_id' => $query->status_id,
|
|
'prazo_entrega' => $query->exame->prazo_entrega ?? null,
|
|
'observacao' => $query->exame->observacao ?? null
|
|
];
|
|
})
|
|
->values(),
|
|
'data' => $atendimento->data,
|
|
'data_entrega' => $atendimento->data_entrega,
|
|
'data_nascimento' => $atendimento->data_nascimento,
|
|
'hora_exame' => $atendimento->hora_inicial ?? null,
|
|
'mensagem_protocolo' => self::getMensagem(),
|
|
'agendamento_numero' => $atendimento->agendamento_numero,
|
|
'medico_nome' => $atendimento->medico_nome,
|
|
'nome_responsavel' => $atendimento->responsavel,
|
|
'observacao' => $atendimento->observacao,
|
|
'paciente_nome' => $atendimento->paciente_nome,
|
|
'paciente_nome_social' => $atendimento->paciente_nome_social,
|
|
'prontuario' => $atendimento->paciente_prontuario,
|
|
'local_coleta' => $atendimento->coleta_infraestrutura_nome,
|
|
'laboratorio' => $atendimento->laboratorio_infraestrutura_nome,
|
|
'sistema' => self::getSistema(),
|
|
'urgente' => $atendimento->urgente,
|
|
'unidade' => $atendimento->infraestrutura_nome,
|
|
'telefone' => MaskUtil::getPhoneMasked($atendimento->telefone),
|
|
'endereco_numero' => $atendimento->endereco_numero,
|
|
'logradouro_nome' => $atendimento->logradouro_nome,
|
|
'logradouro_cep' => MaskUtil::getCepMasked($atendimento->logradouro_cep),
|
|
'logradouro_tipo' => $atendimento->logradouro_tipo,
|
|
'bairro_nome' => $atendimento->bairro_nome,
|
|
'telefone_coleta' => MaskUtil::getPhoneMasked($atendimento->telefone_coleta),
|
|
'endereco_numero_coleta' => $atendimento->endereco_numero_coleta,
|
|
'logradouro_nome_coleta' => $atendimento->logradouro_nome_coleta,
|
|
'logradouro_cep_coleta' => MaskUtil::getCepMasked($atendimento->logradouro_cep_coleta),
|
|
'logradouro_tipo_coleta' => $atendimento->logradouro_tipo_coleta,
|
|
'bairro_nome_coleta' => $atendimento->bairro_nome_coleta,
|
|
'telefone_laboratorio' => MaskUtil::getPhoneMasked($atendimento->telefone_laboratorio),
|
|
'endereco_numero_laboratorio' => $atendimento->endereco_numero_laboratorio,
|
|
'logradouro_nome_laboratorio' => $atendimento->logradouro_nome_laboratorio,
|
|
'logradouro_cep_laboratorio' => MaskUtil::getCepMasked($atendimento->logradouro_cep_laboratorio),
|
|
'logradouro_tipo_laboratorio' => $atendimento->logradouro_tipo_laboratorio,
|
|
'bairro_nome_laboratorio' => $atendimento->bairro_nome_laboratorio,
|
|
'regulacao_solicitacao_numero' => $atendimento->regulacao_solicitacao_numero,
|
|
],
|
|
'etiqueta' => [
|
|
'exames_incluidos' => AgendamentoExame::where('agendamento_id', $atendimento->agendamento_id)
|
|
->get()
|
|
->map(function ($query) {
|
|
return [
|
|
'id' => $query->id,
|
|
'exame_id' => $query->exame_id,
|
|
'nome' => $query->exame ? $query->exame->nome : null,
|
|
'codigo' => $query->exame->codigo ?? null,
|
|
'observacao' => $query->exame->observacao ?? null,
|
|
'status_id' => $query->status_id ?? null,
|
|
'valor' => $query->exame->procedimento->valor_servico_ambilatorial ?? null,
|
|
];
|
|
})
|
|
->values(),
|
|
'data_exame' => $atendimento->data,
|
|
'data_nascimento' => $atendimento->data_nascimento,
|
|
'medico_solicitante' => $atendimento->medico_nome,
|
|
'unidade' => $atendimento->infraestrutura_nome,
|
|
'laboratorio' => $atendimento->laboratorio_infraestrutura_nome,
|
|
'agendamento_numero' => $atendimento->agendamento_numero,
|
|
'paciente_nome' => $atendimento->paciente_nome,
|
|
'prontuario' => $atendimento->paciente_prontuario,
|
|
'idade' => $atendimento->paciente_idade,
|
|
'paciente_nome_social' => $atendimento->paciente_nome_social,
|
|
'sistema' => self::getSistema(),
|
|
]
|
|
]
|
|
]
|
|
);
|
|
|
|
return $atendimento;
|
|
});
|
|
|
|
return [
|
|
'model' => [
|
|
'agendamentos' => $agendamentos
|
|
],
|
|
];
|
|
}
|
|
|
|
private static function getSistema()
|
|
{
|
|
$cliente = Cliente::where('link', app('config')->get('database.default'))->firstOrFail();
|
|
|
|
return [
|
|
'ramo_atividade' => $cliente->sistema->ramoAtividade->nome ?? null,
|
|
'nome_sistema' => $cliente->sistema->nome ?? null,
|
|
'versao' => $cliente->sistema->versao ?? null,
|
|
];
|
|
}
|
|
|
|
private static function getMensagem()
|
|
{
|
|
$mensagemProtocolo = MensagemProtocolo::select('mensagem')->first();
|
|
|
|
return $mensagemProtocolo->mensagem;
|
|
|
|
}
|
|
|
|
public static function getImpressoes($data = [], $excluded = [])
|
|
{
|
|
$impressoes = [
|
|
'agendamento' => self::getimpressoesAgendamentoExames($data['agendamento'] ?? []),
|
|
];
|
|
|
|
foreach ($excluded as $key => $value) {
|
|
|
|
if (is_array($value)) {
|
|
|
|
foreach ($value as $col => $vl) {
|
|
|
|
$indice = array_search($vl, array_column($impressoes[$key], $col));
|
|
if ($indice >= 0) {
|
|
|
|
unset($impressoes[$key][$indice]);
|
|
}
|
|
}
|
|
} else {
|
|
|
|
if (array_key_exists($value, $impressoes)) {
|
|
|
|
unset($impressoes[$value]);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $impressoes;
|
|
}
|
|
|
|
public static function getimpressoesAgendamentoExames($data = [])
|
|
{
|
|
$impressoesAgendamentoExames = [];
|
|
|
|
if (isset($data['protocolo'])) {
|
|
|
|
$impressoesAgendamentoExames[] = [
|
|
'value' => 'protocolo',
|
|
'text' => 'Comprovante de agendamento',
|
|
'disabled' => false,
|
|
'data' => $data['protocolo']
|
|
];
|
|
}
|
|
|
|
if (isset($data['etiqueta'])) {
|
|
|
|
$impressoesAgendamentoExames[] = [
|
|
'value' => 'etiqueta',
|
|
'text' => 'Etiqueta',
|
|
'disabled' => false,
|
|
'data' => $data['etiqueta']
|
|
];
|
|
}
|
|
|
|
return $impressoesAgendamentoExames;
|
|
}
|
|
}
|