This commit is contained in:
@@ -0,0 +1,257 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TransportePaciente\Parametrizacao;
|
||||
|
||||
use App\Enums\Sigtap\Procedimento;
|
||||
use App\Models\Cadastro\Estabelecimento\Unidade;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Usuario;
|
||||
use App\Models\TransportePaciente\Faturamento\Faturamento as FaturamentoBpa;
|
||||
use App\Models\TransportePaciente\Parametrizacao\Faturamento;
|
||||
use App\Models\TransportePaciente\Parametrizacao\Parametrizacao;
|
||||
use App\Services\Service;
|
||||
use App\Utils\DataUtil;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ParametrizacaoService extends Service
|
||||
{
|
||||
|
||||
private static function getFormData()
|
||||
{
|
||||
$now = Carbon::now();
|
||||
|
||||
return [
|
||||
'data_atual' => $now->format('d/m/Y'),
|
||||
'unidades' => Unidade::whereRaw('LENGTH(cnes) = 7')
|
||||
->orderBy('nome_fantasia')
|
||||
->get()
|
||||
->map(fn ($query) => [
|
||||
'id' => $query->id,
|
||||
'nome_fantasia' => $query->cnes . ' - ' . $query->nome_fantasia,
|
||||
'infraestruturas' => $query->infraestruturas->sortBy('nome')->map(fn ($infraestrutura) => [
|
||||
'id' => $infraestrutura->id,
|
||||
'nome' => $infraestrutura->nome,
|
||||
'principal' => $infraestrutura->principal,
|
||||
])->values()
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
private static function getModelData($parametrizacao = null)
|
||||
{
|
||||
$parametrizacao = [
|
||||
'id' => $parametrizacao->id ?? null,
|
||||
'faltas' => $parametrizacao->faltas ?? null,
|
||||
'periodo' => $parametrizacao->periodo ?? null,
|
||||
'agendar_transporte_jornada' => $parametrizacao->agendar_transporte_jornada ?? null,
|
||||
'veiculo' => $parametrizacao->veiculo ?? null,
|
||||
'comprovante_acompanhante' => $parametrizacao->comprovante_acompanhante ?? null,
|
||||
'dados_acompanhante' => $parametrizacao->dados_acompanhante ?? null,
|
||||
'campo_livre' => $parametrizacao->campo_livre ?? null,
|
||||
'exibir_observacao_paciente' => $parametrizacao->exibir_observacao_paciente ?? null,
|
||||
'exibir_observacao_transporte' => $parametrizacao->exibir_observacao_transporte ?? null,
|
||||
'horario_saida' => $parametrizacao->horario_saida ?? null,
|
||||
'mensagem_protocolo' => $parametrizacao->mensagem_protocolo ?? null,
|
||||
'faturamento' => $parametrizacao->faturamento ?? null,
|
||||
'auxilio_alimentacao' => $parametrizacao->auxilio_alimentacao ?? null,
|
||||
'faturamentos' => Faturamento::get()
|
||||
->map(fn ($query) => [
|
||||
'id' => $query->id,
|
||||
'unidade_id' => $query->unidade_id,
|
||||
'unidade_nome' => $query->unidade->cnes . ' - ' . $query->unidade->nome_fantasia,
|
||||
'infraestrutura_id' => $query->infraestrutura_id,
|
||||
'infraestrutura_nome' => $query->infraestrutura->nome,
|
||||
'usuario_id' => $query->usuario_id,
|
||||
'usuario_nome' => $query->usuario->cidadao->cns . ' - ' . $query->usuario->nome,
|
||||
'ocupacao_id' => $query->ocupacao_id,
|
||||
'ocupacao_codigo_nome' => $query->ocupacao->codigo_nome,
|
||||
]),
|
||||
'bpa_faturamento' => FaturamentoBpa::get()
|
||||
->map(fn ($bpa) =>[
|
||||
'id' => $bpa->id,
|
||||
'unidade_id' =>$bpa->lotacao_id,
|
||||
])
|
||||
];
|
||||
|
||||
return $parametrizacao;
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => self::getModelData(self::show()),
|
||||
];
|
||||
}
|
||||
|
||||
public static function show()
|
||||
{
|
||||
return Parametrizacao::first();
|
||||
}
|
||||
|
||||
public static function store()
|
||||
{
|
||||
DB::transaction(function () {
|
||||
|
||||
self::saveGeral();
|
||||
self::saveFaturamento();
|
||||
});
|
||||
|
||||
return response()->json(null, Response::HTTP_OK);
|
||||
}
|
||||
|
||||
private static function saveGeral()
|
||||
{
|
||||
$geral = Parametrizacao::first() ?? new Parametrizacao();
|
||||
|
||||
$data = request()->geral;
|
||||
|
||||
$data['exibir_observacao_transporte'] = $data['exibir_observacao_transporte'] ?? 0;
|
||||
|
||||
$geral->fill($data);
|
||||
|
||||
$geral->save();
|
||||
}
|
||||
|
||||
private static function saveFaturamento()
|
||||
{
|
||||
$ids = [];
|
||||
|
||||
foreach (request()->faturamento as $params) {
|
||||
|
||||
$faturamento = Faturamento::where('unidade_id', $params['unidade_id'])
|
||||
->where('infraestrutura_id', $params['infraestrutura_id'])
|
||||
->where('usuario_id', $params['usuario_id'])
|
||||
->first() ?? new Faturamento();
|
||||
|
||||
$faturamento->fill($params);
|
||||
$faturamento->save();
|
||||
|
||||
$ids[] = $faturamento->id;
|
||||
}
|
||||
|
||||
Faturamento::destroy(Faturamento::whereNotIn('id', $ids)->pluck('id'));
|
||||
}
|
||||
|
||||
public static function lotacoes()
|
||||
{
|
||||
$usuario = Usuario::findOrFail(request()->usuario_id);
|
||||
|
||||
$lotacoes = $usuario->lotacoesAtivas()
|
||||
->where('cadastro_lotacao.infraestrutura_id', request()->query('infraestrutura_id'))
|
||||
->whereExists(function ($query) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('sigtap.procedimento_ocupacao')
|
||||
->join('sigtap.procedimento', 'sigtap.procedimento.id', 'sigtap.procedimento_ocupacao.procedimento_id')
|
||||
->whereColumn('sigtap.procedimento_ocupacao.ocupacao_id', 'cadastro_lotacao.ocupacao_id')
|
||||
->where('sigtap.procedimento_ocupacao.competencia_id', function ($query) {
|
||||
|
||||
$query->select('sigtap.competencia.id')
|
||||
->from('sigtap.competencia')
|
||||
->orderBy('id')
|
||||
->limit(1);
|
||||
})
|
||||
->where(function ($query) {
|
||||
|
||||
if (request()->auxilio_alimentar) {
|
||||
|
||||
$query->whereIn(
|
||||
'sigtap.procedimento.codigo',
|
||||
[
|
||||
Procedimento::AJUDA_DE_CUSTO_PARA_ALIMENTACAO_DE_PACIENTE_SEM_PERNOITE,
|
||||
Procedimento::AJUDA_DE_CUSTO_PARA_ALIMENTACAO_DE_PACIENTE_SEM_PERNOITE
|
||||
]
|
||||
);
|
||||
} else {
|
||||
$query->whereIn(
|
||||
'sigtap.procedimento.codigo',
|
||||
[
|
||||
Procedimento::UNIDADE_DE_REMUNERACAO_PARA_DESLOCAMENTO_DE_PACIENTE_POR_TRANSPORTE_TERRESTRE,
|
||||
Procedimento::UNIDADE_DE_REMUNERACAO_PARA_DESLOCAMENTO_DE_ACOMPANHANTE_POR_TRANSPORTE_TERRESTRE
|
||||
]
|
||||
);
|
||||
}
|
||||
});
|
||||
})
|
||||
->with(['infraestrutura.unidade'])
|
||||
->get();
|
||||
|
||||
return [
|
||||
'lotacoes' => $lotacoes->map(fn ($lotacao) => [
|
||||
'id' => $lotacao->id,
|
||||
'unidade_id' => $lotacao->infraestrutura->unidade_id,
|
||||
'unidade_nome' => $lotacao->infraestrutura->unidade->nome_fantasia,
|
||||
'infraestrutura_id' => $lotacao->infraestrutura_id,
|
||||
'infraestrutura_nome' => $lotacao->infraestrutura->nome,
|
||||
'ocupacao' => $lotacao->ocupacao,
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public static function autocomplete()
|
||||
{
|
||||
$usuarios = DB::table('cadastro_usuario')
|
||||
->select([
|
||||
'cadastro_usuario.id',
|
||||
DB::raw('CONCAT(cadastro_cidadao.cns, " - " ,cadastro_cidadao.nome) as cidadao_nome'),
|
||||
'cadastro_cidadao.cns AS cidadao_cns',
|
||||
'cadastro_cidadao.cpf AS cidadao_cpf',
|
||||
'cadastro_usuario.registro_numero',
|
||||
])
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
|
||||
->when(request()->query('infraestrutura_id'), function($query) {
|
||||
$query->whereExists(function ($query) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('cadastro_lotacao')
|
||||
->where('cadastro_lotacao.infraestrutura_id', request()->query('infraestrutura_id'))
|
||||
->whereExists(function ($query) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('sigtap.procedimento_ocupacao')
|
||||
->join('sigtap.procedimento', 'sigtap.procedimento.id', 'sigtap.procedimento_ocupacao.procedimento_id')
|
||||
->whereColumn('sigtap.procedimento_ocupacao.ocupacao_id', 'cadastro_lotacao.ocupacao_id')
|
||||
->where('sigtap.procedimento_ocupacao.competencia_id', function ($query) {
|
||||
|
||||
$query->select('sigtap.competencia.id')
|
||||
->from('sigtap.competencia')
|
||||
->orderBy('id')
|
||||
->limit(1);
|
||||
})
|
||||
->where(function ($query) {
|
||||
|
||||
if (request()->auxilio_alimentar) {
|
||||
|
||||
$query->whereExists(function ($subquery) {
|
||||
$subquery->select(DB::raw(1))
|
||||
->from('sigtap.procedimento')
|
||||
->whereIn('sigtap.procedimento.codigo', [
|
||||
Procedimento::AJUDA_DE_CUSTO_PARA_ALIMENTACAO_DE_PACIENTE_SEM_PERNOITE,
|
||||
Procedimento::AJUDA_DE_CUSTO_PARA_ALIMENTACAO_DE_ACOMPANHANTE_SEM_PERNOITE
|
||||
]);
|
||||
});
|
||||
} else {
|
||||
|
||||
$query->whereExists(function ($subquery) {
|
||||
$subquery->select(DB::raw(1))
|
||||
->from('sigtap.procedimento')
|
||||
->whereIn('sigtap.procedimento.codigo', [
|
||||
Procedimento::UNIDADE_DE_REMUNERACAO_PARA_DESLOCAMENTO_DE_PACIENTE_POR_TRANSPORTE_TERRESTRE,
|
||||
Procedimento::UNIDADE_DE_REMUNERACAO_PARA_DESLOCAMENTO_DE_ACOMPANHANTE_POR_TRANSPORTE_TERRESTRE
|
||||
]);
|
||||
});
|
||||
}
|
||||
});
|
||||
})
|
||||
->whereColumn('cadastro_lotacao.usuario_id', 'cadastro_usuario.id');
|
||||
});
|
||||
})
|
||||
->whereNull('cadastro_usuario.deleted_at')
|
||||
->where('cadastro_usuario.bloqueado', '=', '0');
|
||||
|
||||
if (!DataUtil::isStringTrue(request()->query('with_not_regularized'))) {
|
||||
$usuarios->whereNotNull('cadastro_cidadao.cns');
|
||||
}
|
||||
|
||||
return $usuarios;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user