177 lines
7.6 KiB
PHP
177 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\RegulacaoDivulgacao;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\RegulacaoDivulgacao\FilaService;
|
|
use App\Utils\MaskUtil;
|
|
use Illuminate\Http\Response;
|
|
|
|
class FilaController extends Controller
|
|
{
|
|
public function list()
|
|
{
|
|
return response()->json(FilaService::list(), Response::HTTP_OK);
|
|
}
|
|
|
|
public function datatableSolicitacaoEspera()
|
|
{
|
|
$tempoMedioPrevistoAtendimento = FilaService::getTempoMedioPrevistoAtendimento();
|
|
|
|
return datatables()
|
|
->of(FilaService::datatableSolicitacaoEspera())
|
|
->filterColumn('paciente', function ($query, $keyword) {
|
|
$query->where(fn ($q) => $q
|
|
->whereRaw("paciente_cidadao.nome like ?", ["%{$keyword}%"])
|
|
->orWhereRaw("paciente_cidadao.cns like ?", ["%{$keyword}%"])
|
|
->orWhere(fn ($q) => $q
|
|
->whereRaw("paciente_cidadao.cpf like ?", ["%{$keyword}%"])
|
|
->orWhereRaw(
|
|
"CONCAT(
|
|
SUBSTR(paciente_cidadao.cpf, 1, 3),
|
|
'.',
|
|
SUBSTR(paciente_cidadao.cpf, 4, 3),
|
|
'.',
|
|
SUBSTR(paciente_cidadao.cpf, 7, 3),
|
|
'-',
|
|
SUBSTR(paciente_cidadao.cpf, 10, 2)
|
|
) like ?",
|
|
["%{$keyword}%"]
|
|
)
|
|
)
|
|
->orWhereRaw("DATE_FORMAT(paciente_cidadao.data_nascimento, '%d/%m/%Y') like ?", ["%{$keyword}%"])
|
|
->orWhereRaw("LPAD(paciente.prontuario, 7, 0) like ?", ["%{$keyword}%"])
|
|
->orWhereRaw("solicitacao.numero like ?", ["%{$keyword}%"])
|
|
);
|
|
})
|
|
->editColumn('tempo_espera', function ($solicitacao) {
|
|
|
|
if ($solicitacao->tempo_espera === 0) {
|
|
|
|
return 'MESMO DIA';
|
|
}
|
|
|
|
$meses = floor($solicitacao->tempo_espera / 30);
|
|
$dias = $solicitacao->tempo_espera % 30;
|
|
|
|
$mesesText = $meses > 0 ? ($meses > 1 ? "{$meses} meses" : "{$meses} mês") : null;
|
|
$diasText = $dias > 0 ? ($dias > 1 ? "{$dias} dias" : "{$dias} dia") : null;
|
|
|
|
return is_null($mesesText)
|
|
? $diasText
|
|
: $mesesText . (is_null($diasText) ? '' : " e {$diasText}");
|
|
})
|
|
->editColumn('allow_update', fn ($s) => (bool) $s->allow_update)
|
|
->editColumn('itens', fn ($s) => explode('#', $s->itens))
|
|
->editColumn('paciente_nome', fn ($s) => MaskUtil::maskNome($s->paciente_nome))
|
|
->editColumn('paciente_cns', fn ($s) => MaskUtil::maskDocumento($s->paciente_cns))
|
|
->editColumn('paciente_cpf', fn ($s) => MaskUtil::maskDocumento($s->paciente_cpf))
|
|
->editColumn('tempo_medio_previsto_atendimento', function ($solicitacao) use ($tempoMedioPrevistoAtendimento) {
|
|
$itens = explode('#', $solicitacao->itens_ids);
|
|
|
|
foreach ($itens as &$item) {
|
|
$aux = explode('|', $item);
|
|
|
|
if ($aux[0] === 'EXAME') {
|
|
$item = $tempoMedioPrevistoAtendimento['exames'][$aux[1]] ?? null;
|
|
}
|
|
if ($aux[0] === 'PROCEDIMENTO') {
|
|
$item = $tempoMedioPrevistoAtendimento['procedimentos_cirurgicos'][$aux[1]] ?? null;
|
|
}
|
|
if ($aux[0] === 'ESPECIALIDADE') {
|
|
$item = $tempoMedioPrevistoAtendimento['especialidades'][$aux[1]] ?? null;
|
|
}
|
|
}
|
|
|
|
unset($item);
|
|
|
|
return $itens;
|
|
})
|
|
->removeColumn([
|
|
'paciente_data_nascimento'
|
|
])
|
|
->skipTotalRecords()
|
|
->toJson();
|
|
}
|
|
|
|
public function datatableSolicitacaoAtendida()
|
|
{
|
|
$tempoMedioPrevistoAtendimento = FilaService::getTempoMedioPrevistoAtendimento();
|
|
|
|
return datatables()
|
|
->of(FilaService::datatableSolicitacaoAtendida())
|
|
->filterColumn('paciente', function ($query, $keyword) {
|
|
$query->where(fn ($q) => $q
|
|
->whereRaw("paciente_cidadao.nome like ?", ["%{$keyword}%"])
|
|
->orWhereRaw("paciente_cidadao.cns like ?", ["%{$keyword}%"])
|
|
->orWhere(fn ($q) => $q
|
|
->whereRaw("paciente_cidadao.cpf like ?", ["%{$keyword}%"])
|
|
->orWhereRaw(
|
|
"CONCAT(
|
|
SUBSTR(paciente_cidadao.cpf, 1, 3),
|
|
'.',
|
|
SUBSTR(paciente_cidadao.cpf, 4, 3),
|
|
'.',
|
|
SUBSTR(paciente_cidadao.cpf, 7, 3),
|
|
'-',
|
|
SUBSTR(paciente_cidadao.cpf, 10, 2)
|
|
) like ?",
|
|
["%{$keyword}%"]
|
|
)
|
|
)
|
|
->orWhereRaw("DATE_FORMAT(paciente_cidadao.data_nascimento, '%d/%m/%Y') like ?", ["%{$keyword}%"])
|
|
->orWhereRaw("LPAD(paciente.prontuario, 7, 0) like ?", ["%{$keyword}%"])
|
|
->orWhereRaw("solicitacao.numero like ?", ["%{$keyword}%"])
|
|
);
|
|
})
|
|
->editColumn('tempo_espera', function ($solicitacao) {
|
|
|
|
if ($solicitacao->tempo_espera === 0) {
|
|
|
|
return 'MESMO DIA';
|
|
}
|
|
|
|
$meses = floor($solicitacao->tempo_espera / 30);
|
|
$dias = $solicitacao->tempo_espera % 30;
|
|
|
|
$mesesText = $meses > 0 ? ($meses > 1 ? "{$meses} meses" : "{$meses} mês") : null;
|
|
$diasText = $dias > 0 ? ($dias > 1 ? "{$dias} dias" : "{$dias} dia") : null;
|
|
|
|
return is_null($mesesText)
|
|
? $diasText
|
|
: $mesesText . (is_null($diasText) ? '' : " e {$diasText}");
|
|
})
|
|
->editColumn('allow_update', fn ($s) => (bool) $s->allow_update)
|
|
->editColumn('itens', fn ($s) => explode('#', $s->itens))
|
|
->editColumn('paciente_nome', fn ($s) => MaskUtil::maskNome($s->paciente_nome))
|
|
->editColumn('paciente_cns', fn ($s) => MaskUtil::maskDocumento($s->paciente_cns))
|
|
->editColumn('paciente_cpf', fn ($s) => MaskUtil::maskDocumento($s->paciente_cpf))
|
|
->editColumn('tempo_medio_previsto_atendimento', function ($solicitacao) use ($tempoMedioPrevistoAtendimento) {
|
|
$itens = explode('#', $solicitacao->itens_ids);
|
|
|
|
foreach ($itens as &$item) {
|
|
$aux = explode('|', $item);
|
|
|
|
if ($aux[0] === 'EXAME') {
|
|
$item = $tempoMedioPrevistoAtendimento['exames'][$aux[1]] ?? null;
|
|
}
|
|
if ($aux[0] === 'PROCEDIMENTO') {
|
|
$item = $tempoMedioPrevistoAtendimento['procedimentos_cirurgicos'][$aux[1]] ?? null;
|
|
}
|
|
if ($aux[0] === 'ESPECIALIDADE') {
|
|
$item = $tempoMedioPrevistoAtendimento['especialidades'][$aux[1]] ?? null;
|
|
}
|
|
}
|
|
|
|
unset($item);
|
|
|
|
return $itens;
|
|
})
|
|
->removeColumn([
|
|
'paciente_data_nascimento'
|
|
])
|
|
->skipTotalRecords()
|
|
->toJson();
|
|
}
|
|
}
|