This commit is contained in:
@@ -0,0 +1,303 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ProntoAtendimento\AssistenciaSocial;
|
||||
|
||||
use App\Enums\Cadastro\SujeitoAtencao\PreferenciaNome;
|
||||
use App\Models\Cadastro\Estabelecimento\CentroCusto;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Sexo;
|
||||
use App\Models\ProntoAtendimento\AssistenciaSocial\AssistenciaSocial;
|
||||
use App\Models\ProntoAtendimento\AssistenciaSocial\AssistenciaSocialHistorico;
|
||||
use App\Models\ProntoAtendimento\Atendimento\Atendimento;
|
||||
use App\Models\Sigtap\Ocupacao;
|
||||
use App\Services\Service;
|
||||
use App\Utils\MaskUtil;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class AssistenciaSocialService 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,
|
||||
],
|
||||
'additionals' => [
|
||||
'periodo_min_date' => $now->copy()->subYears(130)->format('d/m/Y'),
|
||||
'current_date' => $now->format('d/m/Y')
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public static function datatable($request)
|
||||
{
|
||||
self::validateDatatableRequest($request);
|
||||
|
||||
$atendimentos = DB::table('pronto_atendimento_atendimento AS atendimento')
|
||||
->leftJoin('painel_senha_fila AS fila', 'fila.id', 'atendimento.fila_id')
|
||||
->leftJoin('painel_senha_opcao_entrada AS opcao_entrada', 'opcao_entrada.id', 'fila.opcao_entrada_id')
|
||||
->join('pronto_atendimento_faa AS faa', 'faa.id', 'atendimento.faa_id')
|
||||
->join('cadastro_paciente AS paciente', 'paciente.id', 'faa.paciente_id')
|
||||
->join('cadastro_cidadao AS cidadao', 'cidadao.id', 'paciente.cidadao_id')
|
||||
->leftJoin('cadastro_sexo AS sexo', 'sexo.id', 'cidadao.sexo_id')
|
||||
->join('pronto_atendimento_atendimento_recepcao AS recepcao', 'recepcao.atendimento_id', 'atendimento.id')
|
||||
->join('cadastro_centro_custo AS centro_custo', 'centro_custo.id', 'atendimento.created_centro_custo_id')
|
||||
->join('cadastro_infraestrutura AS infraestrutura', 'infraestrutura.id', 'centro_custo.infraestrutura_id')
|
||||
->select([
|
||||
'atendimento.id AS atendimento_id',
|
||||
'recepcao.atendido_em AS data',
|
||||
'recepcao.acompanhante AS acompanhante',
|
||||
'infraestrutura.nome as infraestrutura',
|
||||
'faa.id AS faa',
|
||||
'faa.numero AS faa_numero',
|
||||
DB::raw("CONCAT(opcao_entrada.sigla, fila.senha) AS senha"),
|
||||
'fila.created_at AS senha_data_hora_emissao',
|
||||
'sexo.descricao AS sexo',
|
||||
'paciente.prontuario AS paciente_prontuario',
|
||||
'faa.paciente_id AS paciente_id',
|
||||
'cidadao.data_nascimento AS paciente_data_nascimento',
|
||||
'cidadao.nome_mae AS paciente_nome_mae',
|
||||
'cidadao.nome AS paciente_nome',
|
||||
'cidadao.nome_social AS paciente_nome_social',
|
||||
'cidadao.preferencia_nome_id as paciente_preferencia_nome_id',
|
||||
'cidadao.caracteristicas_fisicas AS paciente_caracteristicas_fisicas',
|
||||
'paciente.sequencial_desconhecido_em AS paciente_sequencial_desconhecido_em',
|
||||
])
|
||||
->where('centro_custo.infraestrutura_id', self::getCurrentCentroCusto()->infraestrutura_id)
|
||||
->whereBetween('recepcao.atendido_em', [
|
||||
Carbon::createFromFormat('d/m/Y', $request->periodo['start'])->setTime(0, 0, 0),
|
||||
Carbon::createFromFormat('d/m/Y', $request->periodo['end'])->setTime(23, 59, 59)
|
||||
])
|
||||
->when($request->sexo_id, function ($query, $value) {
|
||||
|
||||
$query->where('cidadao.sexo_id', $value);
|
||||
})
|
||||
->when($request->paciente, function ($query, $value) {
|
||||
|
||||
$query->where('cidadao.nome', 'like', "{$value}%");
|
||||
})
|
||||
->when($request->data_nascimento, function ($query, $value) {
|
||||
|
||||
$query->whereDate('cidadao.data_nascimento', Carbon::createFromFormat('d/m/Y', $value));
|
||||
})
|
||||
->when($request->faa, function ($query, $value) {
|
||||
|
||||
$query->where('faa.numero', $value);
|
||||
})
|
||||
->whereNull('faa.deleted_at')
|
||||
->get()
|
||||
->map(function ($atendimento) {
|
||||
|
||||
if (is_null($atendimento->paciente_nome_mae)) {
|
||||
|
||||
$atendimento->paciente_nome_mae = 'Não informado';
|
||||
}
|
||||
|
||||
if (is_null($atendimento->paciente_caracteristicas_fisicas)) {
|
||||
|
||||
$atendimento->paciente_caracteristicas_fisicas = 'Não informado';
|
||||
}
|
||||
|
||||
$atendimento->paciente_is_desconhecido = !is_null($atendimento->paciente_sequencial_desconhecido_em);
|
||||
$atendimento->data = (new Carbon($atendimento->data))->format('d/m/Y');
|
||||
|
||||
$atendimento->paciente_prefere_nome_social = $atendimento
|
||||
->paciente_preferencia_nome_id === PreferenciaNome::NOME_SOCIAL;
|
||||
|
||||
$atendimento->paciente_data_nascimento = is_null($atendimento->paciente_data_nascimento)
|
||||
? 'Não informada'
|
||||
: (new Carbon($atendimento->paciente_data_nascimento))->format('d/m/Y');
|
||||
|
||||
unset(
|
||||
$atendimento->acompanhante,
|
||||
$atendimento->infraestrutura,
|
||||
$atendimento->senha,
|
||||
$atendimento->senha_data_hora_emissao,
|
||||
$atendimento->paciente_preferencia_nome_id,
|
||||
$atendimento->paciente_sequencial_desconhecido_em
|
||||
);
|
||||
|
||||
return $atendimento;
|
||||
});
|
||||
|
||||
return $atendimentos;
|
||||
}
|
||||
|
||||
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',
|
||||
],
|
||||
'faa' => [
|
||||
'nullable',
|
||||
'integer',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
|
||||
$assistencia = Atendimento::where('id', $id)
|
||||
->orderBy('created_at', 'desc')
|
||||
->firstOrFail();
|
||||
|
||||
$centroCustroLogado = self::getCentroCustoLogado();
|
||||
$currentLotacao = self::getCurrentLotacao();
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'unidade_logada' => $centroCustroLogado->infraestrutura->unidade->id,
|
||||
'local_atendimento_logado' => $centroCustroLogado->infraestrutura->id,
|
||||
'usuario' => $currentLotacao->usuario->cidadao->nome,
|
||||
'usuario_sistema_id' => $currentLotacao->usuario->id,
|
||||
'ocupacao_id' => $currentLotacao->ocupacao->id,
|
||||
'ocupacao_codigo' => $currentLotacao->ocupacao->codigo,
|
||||
'ocupacao_nome' => $currentLotacao->ocupacao->nome,
|
||||
'data_atual' => Carbon::now()->format('d/m/Y'),
|
||||
],
|
||||
'model' => [
|
||||
'anotacoes' => [
|
||||
'atendimento_id' => intval($assistencia->id),
|
||||
'usuario_id' => $assistencia->usuario_id,
|
||||
'paciente_id' => $assistencia->faa->paciente_id,
|
||||
'anotacao' => null,
|
||||
'data_anotacao' => $assistencia->data_anotacao,
|
||||
'prontuario' => $assistencia->faa->paciente->prontuario,
|
||||
'nome' => $assistencia->faa->paciente->cidadao->nome,
|
||||
'data_nascimento' => $assistencia->faa->paciente->cidadao->data_nascimento,
|
||||
'cns' => $assistencia->faa->paciente->cidadao->cns ? MaskUtil::getCnsMasked($assistencia->faa->paciente->cidadao->cns) : 'NÃO INFORMADO',
|
||||
'cpf' => $assistencia->faa->paciente->cidadao->cpf ? MaskUtil::getCpfMasked($assistencia->faa->paciente->cidadao->cpf) : 'NÃO INFORMADO',
|
||||
'sexo' => $assistencia->faa->paciente->cidadao->sexo->descricao,
|
||||
'data_nascimento' => $assistencia->faa->paciente->cidadao->data_nascimento
|
||||
? $assistencia->faa->paciente->cidadao->data_nascimento->format('d/m/Y')
|
||||
: 'NÃO INFORMADA',
|
||||
'idade' => $assistencia->faa->paciente->cidadao->data_nascimento
|
||||
? "{$assistencia->faa->paciente->cidadao->idade['anos']} ano(s), "
|
||||
. "{$assistencia->faa->paciente->cidadao->idade['meses']} mês(es) e "
|
||||
. "{$assistencia->faa->paciente->cidadao->idade['dias']} dia(s) "
|
||||
: 'NÃO INFORMADA',
|
||||
'idade_paciente' => $assistencia->faa->paciente->cidadao->data_nascimento ? [
|
||||
'anos' => $assistencia->faa->paciente->cidadao->idade['anos'] ?? 'Não informada',
|
||||
'meses' => $assistencia->faa->paciente->cidadao->idade['meses'] ?? 'Não informada',
|
||||
'dias' => $assistencia->faa->paciente->cidadao->idade['dias'] ?? 'Não informada',
|
||||
] : null,
|
||||
'nome_social' => $assistencia->faa->paciente->cidadao->nome_social,
|
||||
'preferencia_nome_id' => $assistencia->faa->paciente->cidadao->preferencia_nome_id ?? null,
|
||||
'nome_mae' => $assistencia->faa->paciente->cidadao->nome_mae ?? 'NÃO INFORMADO',
|
||||
'telefones' => $assistencia->faa->paciente->cidadao->telefones->map(fn ($t) => MaskUtil::getPhoneMasked($t->numero))->all(),
|
||||
'historico' => AssistenciaSocialHistorico::whereNotNull('anotacao')
|
||||
->where('atendimento_id', $assistencia->id)
|
||||
->orderBy('created_at', 'desc')
|
||||
->get()
|
||||
->map(function ($query){
|
||||
return [
|
||||
'data' => (new Carbon($query->created_at))->format('d/m/Y') ?? null,
|
||||
'horario' => (new Carbon($query->created_at))->format('H:i') ?? null,
|
||||
'anotacao' => $query->anotacao,
|
||||
'profissional' => [
|
||||
'nome' => $query->usuario->cidadao->nome,
|
||||
'usuario_id' => $query->usuario_id,
|
||||
'ocupacao_id' => $query->ocupacao_id,
|
||||
'cbo' => Ocupacao::findOrFail($query['ocupacao_id'])->nome,
|
||||
],
|
||||
];
|
||||
})
|
||||
->values(),
|
||||
]
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
private static function getCentroCustoLogado()
|
||||
{
|
||||
return CentroCusto::findOrFail(auth()->payload()->get('centro_custo_id'));
|
||||
}
|
||||
|
||||
public static function store(Request $request)
|
||||
{
|
||||
DB::transaction(function () use ($request) {
|
||||
|
||||
self::save($request);
|
||||
});
|
||||
}
|
||||
|
||||
private static function save($request)
|
||||
{
|
||||
$ids = [];
|
||||
|
||||
$atendimento_id = $request->anotacoes['atendimento_id'];
|
||||
$usuario_id = $request->anotacoes['usuario_id'];
|
||||
$paciente_id = $request->anotacoes['paciente_id'];
|
||||
$ocupacao_id = $request->anotacoes['ocupacao_id'];
|
||||
$anotacao = $request->anotacoes['anotacao'];
|
||||
$data_anotacao = $request->anotacoes['data_anotacao'];
|
||||
|
||||
$assistenciaSocial = AssistenciaSocial::where('atendimento_id', $atendimento_id)
|
||||
->first() ?? new AssistenciaSocial;
|
||||
|
||||
$assistenciaSocial->atendimento_id = $atendimento_id;
|
||||
$assistenciaSocial->paciente_id = $paciente_id;
|
||||
$assistenciaSocial->usuario_id = $usuario_id;
|
||||
$assistenciaSocial->ocupacao_id = $ocupacao_id;
|
||||
$assistenciaSocial->anotacao = $anotacao;
|
||||
$assistenciaSocial->data_anotacao = $data_anotacao;
|
||||
|
||||
$assistenciaSocial->save();
|
||||
|
||||
$ids[] = $assistenciaSocial->id;
|
||||
|
||||
$historico = new AssistenciaSocialHistorico();
|
||||
|
||||
$historico->assistencia_id = $assistenciaSocial['id'];
|
||||
$historico->atendimento_id = $assistenciaSocial['atendimento_id'];
|
||||
$historico->usuario_id = $assistenciaSocial['usuario_id'];
|
||||
$historico->paciente_id = $assistenciaSocial['paciente_id'];
|
||||
$historico->ocupacao_id = $assistenciaSocial['ocupacao_id'];
|
||||
$historico->anotacao = $assistenciaSocial['anotacao'];
|
||||
|
||||
$historico->save();
|
||||
$idsHistorico[] = $historico->id;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user