This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\ProntoAtendimento\Parametrizacao;
|
||||
|
||||
use App\Enums\Sigtap\Ocupacao;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\ProntoAtendimento\Parametrizacao\ParametrizacaoRequest;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Usuario;
|
||||
use App\Models\ProntoAtendimento\Atendimento\Medico\AtendimentoMedicoTipo;
|
||||
use App\Models\ProntoAtendimento\Parametrizacao\AtendimentoMedico;
|
||||
use App\Models\ProntoAtendimento\Parametrizacao\AtendimentoMedicoTipo as ParametrizacaoAtendimentoMedicoTipo;
|
||||
use App\Models\ProntoAtendimento\Parametrizacao\FaturamentoExameLaboratorial;
|
||||
use App\Models\ProntoAtendimento\Parametrizacao\Parametrizacao;
|
||||
use App\Models\ProntoAtendimento\Parametrizacao\ServicoEnfermagem;
|
||||
use App\Services\Sigtap\ProcedimentoService;
|
||||
use App\Utils\DataUtil;
|
||||
use DB;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class ParametrizacaoController extends Controller
|
||||
{
|
||||
protected $modulo = 'pronto-atendimento';
|
||||
protected $funcionalidade = 'parametrizacao';
|
||||
|
||||
public function autocompleteServicoEnfermagem()
|
||||
{
|
||||
$procedimento = new ProcedimentoService([
|
||||
'only_servicos_enfermagem' => true,
|
||||
'search_by_apelido' => DataUtil::isStringTrue(request()->query('search_by_apelido')),
|
||||
]);
|
||||
|
||||
return datatables()->of($procedimento->getProcedimentosBuilder())->toJson();
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$this->auth(__FUNCTION__);
|
||||
|
||||
$parametrizacao = Parametrizacao::first();
|
||||
|
||||
return response()->json(
|
||||
[
|
||||
'form' => $this->getFormData(),
|
||||
'model' => [
|
||||
'servicos_enfermagem' => ServicoEnfermagem::all(),
|
||||
'atendimento_medico' => $this->getAtendimentoMedicoModel(),
|
||||
'faturamento_automatico_enfermagem' => $parametrizacao->faturamento_automatico_enfermagem ?? false,
|
||||
'assinatura_eletronica' => $parametrizacao->assinatura_eletronica ?? false,
|
||||
'lotacoes_exame_laboratorial' => FaturamentoExameLaboratorial::all()->map(fn($param) => [
|
||||
'lotacao_id' => $param->lotacao_id,
|
||||
'usuario_nome' => $param->lotacao()->withTrashed()->first()->usuario->cidadao->nome,
|
||||
'unidade_nome_fantasia' => $param->lotacao()->withTrashed()->first()->infraestrutura->unidade->nome_fantasia,
|
||||
'infraestrutura_nome' => $param->lotacao()->withTrashed()->first()->infraestrutura->nome,
|
||||
'ocupacao_descricao' => $param->lotacao()->withTrashed()->first()->ocupacao->codigo . ' - '
|
||||
. $param->lotacao()->withTrashed()->first()->ocupacao->nome
|
||||
]),
|
||||
'tempo_adicional_plantao' => $parametrizacao->tempo_adicional_plantao ?? null,
|
||||
],
|
||||
'additionals' => $this->getAdditionalsData()
|
||||
],
|
||||
Response::HTTP_OK
|
||||
);
|
||||
}
|
||||
|
||||
public function getOcupacoes($infraestruturaId, $usuarioId)
|
||||
{
|
||||
$ocupacoes = Usuario::findOrFail($usuarioId)->lotacoes()
|
||||
->where('infraestrutura_id', $infraestruturaId)
|
||||
->get()
|
||||
->map(fn ($lotacao) => [
|
||||
'lotacao_id' => $lotacao->id,
|
||||
'descricao' => $lotacao->ocupacao->codigo . " - " . $lotacao->ocupacao->nome,
|
||||
])
|
||||
->sortBy('descricao')
|
||||
->values();
|
||||
|
||||
return response()->json($ocupacoes, Response::HTTP_OK);
|
||||
}
|
||||
|
||||
private function getFormData()
|
||||
{
|
||||
return [
|
||||
'atendimento_medico' => [
|
||||
'tipos' => AtendimentoMedicoTipo::orderBy('nome')->get(),
|
||||
],
|
||||
'unidades' => Unidade::orderBy('nome_fantasia')
|
||||
->get()
|
||||
->map(fn ($query) => [
|
||||
'id' => $query->id,
|
||||
'nome_fantasia' => $query->nome_fantasia,
|
||||
'infraestruturas' => $query->infraestruturas
|
||||
->map(fn ($query) => [
|
||||
'id' => $query->id,
|
||||
'nome' => $query->nome,
|
||||
])
|
||||
->sortBy('nome')
|
||||
->values(),
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
private function getAdditionalsData()
|
||||
{
|
||||
return [
|
||||
'servicos_enfermagem' => [
|
||||
'cbos_permitidos' => [
|
||||
Ocupacao::ENFERMEIRO,
|
||||
Ocupacao::TECNICO_ENFERMAGEM,
|
||||
Ocupacao::AUXILIAR_ENFERMAGEM,
|
||||
]
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function getAtendimentoMedicoModel()
|
||||
{
|
||||
$parametrizacao = AtendimentoMedico::first();
|
||||
|
||||
return [
|
||||
'procedimento_padrao' => $parametrizacao->procedimento_padrao ?? null,
|
||||
'tipos' => ParametrizacaoAtendimentoMedicoTipo::all()
|
||||
->map(fn ($tipo) => [
|
||||
'tipo' => [
|
||||
'id' => $tipo->tipo->id,
|
||||
'nome' => $tipo->tipo->nome,
|
||||
],
|
||||
'unidade' => [
|
||||
'id' => $tipo->infraestrutura->unidade->id,
|
||||
'nome_fantasia' => $tipo->infraestrutura->unidade->nome_fantasia,
|
||||
'infraestrutura' => [
|
||||
'id' => $tipo->infraestrutura->id,
|
||||
'nome' => $tipo->infraestrutura->nome,
|
||||
],
|
||||
],
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public function store(ParametrizacaoRequest $request)
|
||||
{
|
||||
$this->auth(__FUNCTION__);
|
||||
|
||||
DB::transaction(function () use ($request) {
|
||||
|
||||
$this->saveServicosEnfermagem($request);
|
||||
$this->saveAtendimentoMedico($request);
|
||||
$this->saveParametrizacao($request);
|
||||
$this->saveLotacoesExameLaboratorial($request);
|
||||
});
|
||||
|
||||
return response()->json(null, Response::HTTP_OK);
|
||||
}
|
||||
|
||||
private function saveServicosEnfermagem($request)
|
||||
{
|
||||
$ids = [];
|
||||
|
||||
foreach ($request->servicos_enfermagem as $params) {
|
||||
|
||||
$tipo = ServicoEnfermagem::where('procedimento_codigo', $params['procedimento_codigo'])
|
||||
->first() ?? new ServicoEnfermagem();
|
||||
|
||||
$tipo->fill($params);
|
||||
$tipo->save();
|
||||
|
||||
$ids[] = $tipo->id;
|
||||
}
|
||||
|
||||
ServicoEnfermagem::destroy(ServicoEnfermagem::whereNotIn('id', $ids)->pluck('id'));
|
||||
}
|
||||
|
||||
private function saveAtendimentoMedico($request)
|
||||
{
|
||||
$atendimentoMedico = AtendimentoMedico::first() ?? new AtendimentoMedico();
|
||||
$atendimentoMedico->fill($request->atendimento_medico);
|
||||
$atendimentoMedico->save();
|
||||
|
||||
$ids = [];
|
||||
|
||||
foreach ($request->atendimento_medico['tipos'] as $params) {
|
||||
|
||||
$tipo = ParametrizacaoAtendimentoMedicoTipo::where('infraestrutura_id', $params['infraestrutura_id'])
|
||||
->first() ?? new ParametrizacaoAtendimentoMedicoTipo();
|
||||
|
||||
$tipo->fill($params);
|
||||
$tipo->save();
|
||||
|
||||
$ids[] = $tipo->id;
|
||||
}
|
||||
|
||||
ParametrizacaoAtendimentoMedicoTipo::destroy(
|
||||
ParametrizacaoAtendimentoMedicoTipo::whereNotIn('id', $ids)->pluck('id')
|
||||
);
|
||||
}
|
||||
|
||||
private function saveParametrizacao($request)
|
||||
{
|
||||
$paramatrizacao = Parametrizacao::first();
|
||||
|
||||
if (is_null($paramatrizacao)) {
|
||||
|
||||
Parametrizacao::create($request->all());
|
||||
}
|
||||
else {
|
||||
|
||||
$paramatrizacao->update($request->all());
|
||||
}
|
||||
}
|
||||
|
||||
private function saveLotacoesExameLaboratorial($request)
|
||||
{
|
||||
$ids = [];
|
||||
|
||||
foreach ($request->lotacoes_exame_laboratorial as $lotacaoId) {
|
||||
|
||||
$ids[] = (FaturamentoExameLaboratorial::where('lotacao_id', $lotacaoId)
|
||||
->first() ?? FaturamentoExameLaboratorial::create(['lotacao_id' => $lotacaoId]))->id;
|
||||
}
|
||||
|
||||
FaturamentoExameLaboratorial::destroy(FaturamentoExameLaboratorial::whereNotIn('id', $ids)->pluck('id'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user