This commit is contained in:
321
api/app/Services/PainelSenha/AvisoService.php
Normal file
321
api/app/Services/PainelSenha/AvisoService.php
Normal file
@@ -0,0 +1,321 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\PainelSenha;
|
||||
|
||||
use App\Events\PainelSenha\AvisoEvent;
|
||||
use App\Http\Requests\AvisoRequest;
|
||||
use App\Models\Geral\DiaSemana;
|
||||
use App\Models\PainelSenha\Aviso;
|
||||
use App\Models\PainelSenha\AvisoAgendamento;
|
||||
use App\Models\PainelSenha\AvisoUnidade;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Carbon\CarbonInterval;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class AvisoService extends Service
|
||||
{
|
||||
public static function datatable()
|
||||
{
|
||||
$avisos = DB::table('painel_senha_aviso')
|
||||
->select(
|
||||
'painel_senha_aviso.id',
|
||||
'painel_senha_aviso.mensagem',
|
||||
DB::raw('GROUP_CONCAT(cadastro_unidade.nome_fantasia) as unidades'),
|
||||
DB::raw('GROUP_CONCAT(cadastro_unidade.id) as ids_unidade'),
|
||||
'painel_senha_aviso.is_ativo',
|
||||
)
|
||||
->join('painel_senha_aviso_unidade', 'painel_senha_aviso_unidade.aviso_id', 'painel_senha_aviso.id')
|
||||
->leftJoin('cadastro_unidade', 'cadastro_unidade.id', 'painel_senha_aviso_unidade.unidade_id')
|
||||
->groupBy('painel_senha_aviso.id', 'painel_senha_aviso.mensagem', 'painel_senha_aviso.is_ativo')
|
||||
->get();
|
||||
|
||||
return datatables()
|
||||
->of($avisos)
|
||||
->toJson();
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'additionals' => self::getAdditionalsData(),
|
||||
'form' => self::getFormData(),
|
||||
'model' => [
|
||||
'mensagem' => null,
|
||||
'unidades' => null,
|
||||
'is_ativo' => true,
|
||||
'unidades' => [],
|
||||
'periodo' => [
|
||||
'start' => null,
|
||||
'end' => null,
|
||||
],
|
||||
'hora_inicial' => null,
|
||||
'hora_final' => null,
|
||||
'dias_semana' => [],
|
||||
'intervalo' => null,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
$aviso = DB::table('painel_senha_aviso')
|
||||
->select(
|
||||
'painel_senha_aviso.id as aviso_id',
|
||||
'painel_senha_aviso.mensagem',
|
||||
DB::raw('GROUP_CONCAT(painel_senha_aviso_unidade.unidade_id) as unidades'),
|
||||
'painel_senha_aviso.is_ativo',
|
||||
'painel_senha_aviso.data_inicial',
|
||||
'painel_senha_aviso.data_final',
|
||||
'painel_senha_aviso.hora_inicial',
|
||||
'painel_senha_aviso.hora_final',
|
||||
'painel_senha_aviso.dias_semana',
|
||||
'painel_senha_aviso.intervalo',
|
||||
)
|
||||
->join('painel_senha_aviso_unidade', 'painel_senha_aviso_unidade.aviso_id', 'painel_senha_aviso.id')
|
||||
->groupBy('aviso_id', 'painel_senha_aviso.mensagem')
|
||||
->where('aviso_id', $id)
|
||||
->first('aviso_id');
|
||||
|
||||
if(!isset($aviso)) {
|
||||
return response()->json([
|
||||
'message' => 'Não foi possível localizar esse aviso!'
|
||||
], Response::HTTP_BAD_REQUEST);
|
||||
}
|
||||
|
||||
$dados = [
|
||||
'additionals' => self::getAdditionalsData(),
|
||||
'form' => self::getFormData(),
|
||||
'model' => [
|
||||
'mensagem' => $aviso->mensagem,
|
||||
'unidades' => array_map('intval', explode(',', $aviso->unidades)),
|
||||
'is_ativo' => (bool) $aviso->is_ativo,
|
||||
'periodo' => [
|
||||
'start' => $aviso->data_inicial,
|
||||
'end' => $aviso->data_final,
|
||||
],
|
||||
'hora_inicial' => $aviso->hora_inicial,
|
||||
'hora_final' => $aviso->hora_final,
|
||||
'dias_semana' => explode(',', $aviso->dias_semana),
|
||||
'intervalo' => $aviso->intervalo,
|
||||
]
|
||||
];
|
||||
|
||||
return response()->json($dados, Response::HTTP_OK);
|
||||
}
|
||||
|
||||
public static function store(AvisoRequest $request, $avisoId = null): array
|
||||
{
|
||||
$params = $request->all();
|
||||
|
||||
$params['data_inicial'] = Carbon::parse($params["periodo"]["start"])->format('Y-m-d');;
|
||||
$params['data_final'] = Carbon::parse($params["periodo"]["end"])->format('Y-m-d');;
|
||||
$params['dias_semana'] = implode(',', $params['dias_semana']);
|
||||
|
||||
unset($params['periodo']);
|
||||
|
||||
$recorrencias = self::calculaRecorrencias($params);
|
||||
|
||||
if($recorrencias->isEmpty()) {
|
||||
return [['message' => 'Nenhuma data/horário foram contemplados para esse período. Por favor, aumente a data ou os dias da semana!'], Response::HTTP_BAD_REQUEST];
|
||||
}
|
||||
|
||||
if($avisoId) {
|
||||
|
||||
$aviso = Aviso::where('id', $avisoId)->update([
|
||||
"mensagem" => $params["mensagem"],
|
||||
"is_ativo" => $params["is_ativo"],
|
||||
"data_inicial" => $params["data_inicial"],
|
||||
"data_final" => $params["data_final"],
|
||||
"hora_inicial" => $params["hora_inicial"],
|
||||
"hora_final" => $params["hora_final"],
|
||||
"dias_semana" => $params["dias_semana"],
|
||||
"intervalo" => $params["intervalo"],
|
||||
]);
|
||||
|
||||
} else {
|
||||
$aviso = Aviso::create($params);
|
||||
}
|
||||
|
||||
$avisoId = $aviso->id ?? $avisoId;
|
||||
|
||||
self::insereRecorrenciaAgendamentos($recorrencias, $avisoId);
|
||||
self::insereUnidades($params, $avisoId);
|
||||
|
||||
return [$aviso, Response::HTTP_OK];
|
||||
}
|
||||
|
||||
public static function update($id, AvisoRequest $request): array
|
||||
{
|
||||
$aviso = Aviso::find($id);
|
||||
|
||||
if(!isset($aviso)) {
|
||||
return [['message' => 'Não foi possível atualizar esse aviso'], Response::HTTP_BAD_REQUEST];
|
||||
}
|
||||
|
||||
AvisoAgendamento::where('aviso_id', $id)->delete();
|
||||
AvisoUnidade::where('aviso_id', $id)->delete();
|
||||
|
||||
return self::store($request, $id);
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
AvisoAgendamento::where('aviso_id', $id)->delete();
|
||||
AvisoUnidade::where('aviso_id', $id)->delete();
|
||||
Aviso::where('id', $id)->delete();
|
||||
|
||||
return [[], Response::HTTP_OK];
|
||||
}
|
||||
|
||||
public static function send()
|
||||
{
|
||||
$agora = Carbon::now();
|
||||
$params = request()->all();
|
||||
|
||||
$aviso = Aviso::find($params['id']);
|
||||
|
||||
if(!$aviso) {
|
||||
return [['message' => 'Não foi possível enviar aviso. Pois o mesmo não foi localizado!'], Response::HTTP_BAD_REQUEST];
|
||||
}
|
||||
|
||||
AvisoEvent::dispatch($params['id'], $params);
|
||||
|
||||
AvisoAgendamento::insert([
|
||||
"aviso_id" => $aviso->id,
|
||||
"data_hora_executada" => $agora,
|
||||
"is_envio_manual" => true,
|
||||
"created_at" => $agora,
|
||||
]);
|
||||
|
||||
|
||||
return [$aviso, Response::HTTP_OK];
|
||||
}
|
||||
|
||||
public static function mudarStatus($id) {
|
||||
|
||||
$aviso = Aviso::find($id);
|
||||
|
||||
if(!$aviso) {
|
||||
return [['message' => 'Não foi possível mudar o status do aviso. Pois o mesmo não foi localizado!'], Response::HTTP_BAD_REQUEST];
|
||||
}
|
||||
|
||||
$statusAtual = !!$aviso->is_ativo;
|
||||
$statusModificado = !$statusAtual;
|
||||
|
||||
$aviso->is_ativo = $statusModificado;
|
||||
$aviso->save();
|
||||
|
||||
return [$aviso, Response::HTTP_OK];
|
||||
}
|
||||
|
||||
private static function getAdditionalsData()
|
||||
{
|
||||
return [
|
||||
'min_data' => Carbon::now()->format('d/m/Y'),
|
||||
'max_data' => Carbon::now()->addYear()->format('d/m/Y'),
|
||||
];
|
||||
}
|
||||
|
||||
private static function getFormData()
|
||||
{
|
||||
return [
|
||||
'unidades' => DB::table('cadastro_unidade')
|
||||
->select([
|
||||
'id',
|
||||
'nome_fantasia as nome',
|
||||
])
|
||||
->orderBy('nome')
|
||||
->get(),
|
||||
'dias_semana' => DiaSemana::get()
|
||||
->map(fn ($feriado) => [
|
||||
'id' => $feriado->id,
|
||||
'descricao' => $feriado->descricao,
|
||||
'disabled' => false,
|
||||
]),
|
||||
'intervalos' => [
|
||||
[
|
||||
'valor' => 15,
|
||||
'descricao' => '15 minutos',
|
||||
],
|
||||
[
|
||||
'valor' => 30,
|
||||
'descricao' => '30 minutos',
|
||||
],
|
||||
[
|
||||
'valor' => 60,
|
||||
'descricao' => '1 hora',
|
||||
],
|
||||
[
|
||||
'valor' => 120,
|
||||
'descricao' => '2 horas',
|
||||
],
|
||||
[
|
||||
'valor' => 180,
|
||||
'descricao' => '3 horas',
|
||||
],
|
||||
[
|
||||
'valor' => 240,
|
||||
'descricao' => '4 horas',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private static function calculaRecorrencias($params)
|
||||
{
|
||||
$recorrencias = collect();
|
||||
$start = Carbon::parse($params['data_inicial'] . ' ' . $params['hora_inicial']);
|
||||
$end = Carbon::parse($params['data_final'] . ' ' . $params['hora_final']);
|
||||
|
||||
$diasSemana = is_array($params['dias_semana']) ? $params['dias_semana'] : explode(',', $params['dias_semana']);
|
||||
|
||||
$intervalo = CarbonInterval::minutes($params['intervalo']);
|
||||
|
||||
while ($start <= $end) {
|
||||
if (in_array($start->dayOfWeek + 1, $diasSemana)) {
|
||||
if ($start->format('H:i:s') <= $end->format('H:i:s')) {
|
||||
$recorrencias->push($start->format('Y-m-d H:i:s'));
|
||||
}
|
||||
}
|
||||
|
||||
$start->add($intervalo);
|
||||
|
||||
if ($start->greaterThan($end)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $recorrencias;
|
||||
}
|
||||
|
||||
private static function insereRecorrenciaAgendamentos($recorrencias, $avisoId)
|
||||
{
|
||||
$agora = Carbon::now();
|
||||
|
||||
$agendamentos = $recorrencias->map(fn($data) => [
|
||||
"aviso_id" => $avisoId,
|
||||
"data_hora_agendada" => $data,
|
||||
"data_hora_executada" => null,
|
||||
"is_envio_manual" => false,
|
||||
"created_at" => $agora
|
||||
])->toArray();
|
||||
|
||||
foreach (array_chunk($agendamentos, 500) as $chunk) {
|
||||
AvisoAgendamento::insert($chunk);
|
||||
}
|
||||
}
|
||||
|
||||
private static function insereUnidades($params, $avisoId)
|
||||
{
|
||||
$unidades = collect($params["unidades"]);
|
||||
|
||||
$dados = $unidades->map(fn($unidade) => [
|
||||
"aviso_id" => $avisoId,
|
||||
"unidade_id" => $unidade
|
||||
])->toArray();
|
||||
|
||||
AvisoUnidade::insert($dados);
|
||||
}
|
||||
}
|
||||
113
api/app/Services/PainelSenha/ChamadaService.php
Normal file
113
api/app/Services/PainelSenha/ChamadaService.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\PainelSenha;
|
||||
|
||||
use App\Events\PainelSenha\ChamadaEvent;
|
||||
use App\Models\Cadastro\Estabelecimento\Sala;
|
||||
use App\Models\PainelSenha\Chamada;
|
||||
use App\Models\PainelSenha\Fila;
|
||||
use App\Models\PainelSenha\ParametrizacaoInfraestrutura;
|
||||
use App\Models\PainelSenha\Tela;
|
||||
use App\Services\Service;
|
||||
use Exception;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class ChamadaService extends Service
|
||||
{
|
||||
public static function index($params)
|
||||
{
|
||||
return Chamada::where('tela_id', $params['tela_id'])
|
||||
->when($params['modulo_ids'] ?? null, fn ($q, $moduloIds) => $q
|
||||
->whereHas('fila', fn ($q) => $q
|
||||
->whereHas('opcaoEntrada', fn ($q) => $q
|
||||
->whereIn('modulo_id', $moduloIds)
|
||||
)
|
||||
)
|
||||
)
|
||||
->orderByDesc('created_at')
|
||||
->take(4)
|
||||
->get()
|
||||
->map(fn ($chamada) => [
|
||||
'id' => $chamada->id,
|
||||
'paciente' => [
|
||||
'cidadao' => [
|
||||
'preferencia_nome_id' => $chamada->paciente->cidadao->preferencia_nome_id ?? null,
|
||||
'nome' => $chamada->paciente->cidadao->nome ?? null,
|
||||
'nome_social' => $chamada->paciente->cidadao->nome_social ?? null,
|
||||
],
|
||||
],
|
||||
'sala' => [
|
||||
'nome' => $chamada->sala->nome ?? null,
|
||||
],
|
||||
'senha' => $chamada->fila->senha ?? null,
|
||||
]);
|
||||
}
|
||||
|
||||
public static function store()
|
||||
{
|
||||
self::validate();
|
||||
|
||||
$fila = Fila::findOrFail(request()->fila_id);
|
||||
|
||||
$chamada = Chamada::create([
|
||||
'fila_id' => $fila->id,
|
||||
'status_id' => $fila->status_id,
|
||||
'tela_id' => request()->tela_id,
|
||||
'sala_id' => request()->sala_id,
|
||||
'paciente_id' => $fila->paciente_id ?? null,
|
||||
]);
|
||||
|
||||
FilaAtendimentoService::dispatch($fila->opcaoEntradaAtual->id, $fila->centro_custo_id);
|
||||
|
||||
$parametrizacaoInfraestrutura = ParametrizacaoInfraestrutura::where(
|
||||
'infraestrutura_id',
|
||||
$fila->centroCusto->infraestrutura_id
|
||||
)->first();
|
||||
|
||||
$moduloIds = is_null($parametrizacaoInfraestrutura)
|
||||
? collect()
|
||||
: $parametrizacaoInfraestrutura->modulos->pluck('id');
|
||||
|
||||
try {
|
||||
$paramsChamada = [
|
||||
'tela_id' => $chamada->tela_id,
|
||||
'modulo_ids' => null,
|
||||
];
|
||||
|
||||
$moduloIds->each(function ($moduloId) use ($paramsChamada) {
|
||||
$paramsChamada['modulo_ids'] = [$moduloId];
|
||||
|
||||
ChamadaEvent::dispatch(
|
||||
$paramsChamada['tela_id'],
|
||||
$moduloId,
|
||||
self::index($paramsChamada)
|
||||
);
|
||||
});
|
||||
} catch (Exception $error) {
|
||||
// TODO: Gravar exception em um arquivo.
|
||||
}
|
||||
|
||||
return $chamada;
|
||||
}
|
||||
|
||||
private static function validate()
|
||||
{
|
||||
request()->validate([
|
||||
'fila_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists(Fila::class, 'id'),
|
||||
],
|
||||
'tela_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists(Tela::class, 'id'),
|
||||
],
|
||||
'sala_id' => [
|
||||
'nullable',
|
||||
'integer',
|
||||
Rule::exists(Sala::class, 'id'),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
665
api/app/Services/PainelSenha/FilaAtendimentoService.php
Normal file
665
api/app/Services/PainelSenha/FilaAtendimentoService.php
Normal file
@@ -0,0 +1,665 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\PainelSenha;
|
||||
|
||||
use App\Enums\Administracao\Modulo;
|
||||
use App\Enums\PainelSenha\Status;
|
||||
use App\Events\PainelSenha\FilaEvent;
|
||||
use App\Models\Cadastro\Estabelecimento\CentroCusto;
|
||||
use App\Models\Cadastro\SujeitoAtencao\Paciente;
|
||||
use App\Models\PainelSenha\CentroCustoCompartilhado;
|
||||
use App\Models\PainelSenha\Fila;
|
||||
use App\Models\PainelSenha\OpcaoEntrada;
|
||||
use App\Models\PainelSenha\Prioridade;
|
||||
use App\Services\Administracao\ModuloService;
|
||||
use App\Services\Farmacia\FilaAtendimentoService AS FarmaciaFilaAtendimentoService;
|
||||
use App\Services\Geral\AgeService;
|
||||
use App\Services\Imunizacao\FilaAtendimentoService AS ImunizacaoFilaAtendimentoService;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class FilaAtendimentoService extends Service
|
||||
{
|
||||
public static function index($centroCusto = null)
|
||||
{
|
||||
$centroCusto = $centroCusto ?? self::getCurrentCentroCusto();
|
||||
|
||||
return DB::table('painel_senha_fila')
|
||||
->select([
|
||||
'painel_senha_fila.id',
|
||||
'painel_senha_opcao_entrada.id AS opcao_entrada_id',
|
||||
'painel_senha_opcao_entrada.nome AS opcao_entrada_nome',
|
||||
'painel_senha_opcao_entrada_destino.id AS opcao_entrada_destino_id',
|
||||
'painel_senha_status.id AS status_id',
|
||||
'painel_senha_status.nome AS status_nome',
|
||||
'painel_senha_prioridade.id AS prioridade_id',
|
||||
'painel_senha_prioridade.nome AS prioridade_nome',
|
||||
'cadastro_paciente.id AS paciente_id',
|
||||
'cadastro_cidadao.nome AS paciente_nome',
|
||||
DB::raw("DATE_FORMAT(cadastro_cidadao.data_nascimento, '%d/%m/%Y') AS paciente_data_nascimento"),
|
||||
DB::raw("LPAD(cadastro_paciente.prontuario, 6, '0') AS paciente_prontuario"),
|
||||
DB::raw("CONCAT(painel_senha_opcao_entrada.sigla, painel_senha_fila.senha) AS senha"),
|
||||
DB::raw("DATE_FORMAT(painel_senha_fila.created_at, '%H:%i') AS horario_chegada"),
|
||||
'painel_senha_fila.emitido_em',
|
||||
])
|
||||
->addSelect([
|
||||
'chamadas' => function ($query) {
|
||||
$query->select(DB::raw("COALESCE(COUNT(painel_senha_chamada.id), 0)"))
|
||||
->from('painel_senha_chamada')
|
||||
->whereColumn('painel_senha_chamada.fila_id', 'painel_senha_fila.id')
|
||||
->whereColumn('painel_senha_chamada.status_id', 'painel_senha_fila.status_id');
|
||||
},
|
||||
])
|
||||
->join('painel_senha_opcao_entrada', 'painel_senha_opcao_entrada.id', 'painel_senha_fila.opcao_entrada_id')
|
||||
->join('painel_senha_status', 'painel_senha_status.id', 'painel_senha_fila.status_id')
|
||||
->leftJoin('painel_senha_prioridade', 'painel_senha_prioridade.id', 'painel_senha_fila.prioridade_id')
|
||||
->leftJoin('cadastro_paciente', 'cadastro_paciente.id', 'painel_senha_fila.paciente_id')
|
||||
->leftJoin('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->leftJoin('painel_senha_opcao_entrada AS painel_senha_opcao_entrada_destino', 'painel_senha_opcao_entrada_destino.id', 'painel_senha_fila.opcao_entrada_destino_id')
|
||||
->whereNull('painel_senha_fila.deleted_at')
|
||||
->where('painel_senha_fila.evadido', false)
|
||||
->where('painel_senha_fila.finalizado', false)
|
||||
->where(fn ($q) => $q
|
||||
->where(fn ($q) => $q
|
||||
->whereNull('painel_senha_fila.opcao_entrada_destino_id')
|
||||
->where('painel_senha_opcao_entrada.modulo_id', ModuloService::getId(Modulo::PAINEL_SENHA))
|
||||
->where(fn ($q) => $q
|
||||
->where('painel_senha_opcao_entrada.centro_custo_id', $centroCusto->id)
|
||||
->orWhereExists(fn ($q) => $q->select(DB::raw(1))
|
||||
->from('painel_senha_centro_custo_compartilhado')
|
||||
->where('painel_senha_centro_custo_compartilhado.centro_custo_id', $centroCusto->id)
|
||||
->whereColumn('painel_senha_centro_custo_compartilhado.opcao_entrada_id', 'painel_senha_opcao_entrada.id')
|
||||
)
|
||||
)
|
||||
)
|
||||
->orWhere(fn ($q) => $q
|
||||
->whereNotNull('painel_senha_fila.opcao_entrada_destino_id')
|
||||
->where('painel_senha_opcao_entrada_destino.modulo_id', ModuloService::getId(Modulo::PAINEL_SENHA))
|
||||
->where(fn ($q) => $q
|
||||
->where('painel_senha_opcao_entrada_destino.centro_custo_id', $centroCusto->id)
|
||||
->orWhereExists(fn ($q) => $q->select(DB::raw(1))
|
||||
->from('painel_senha_centro_custo_compartilhado')
|
||||
->where('painel_senha_centro_custo_compartilhado.centro_custo_id', $centroCusto->id)
|
||||
->whereColumn('painel_senha_centro_custo_compartilhado.opcao_entrada_id', 'painel_senha_opcao_entrada_destino.id')
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
->orderByDesc('painel_senha_prioridade.grau')
|
||||
->orderBy('painel_senha_fila.atualizado_em')
|
||||
->get()
|
||||
->map(function($query) use ($centroCusto) {
|
||||
$query->tempo_espera = self::getTempoEspera($query->id, $centroCusto);
|
||||
$query->paciente_idade = isset($query->paciente_data_nascimento) ? AgeService::getFormattedAge(Carbon::createFromFormat('d/m/Y', $query->paciente_data_nascimento)) : null;
|
||||
|
||||
return $query;
|
||||
});
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'form' => [
|
||||
'opcoes_entrada' => self::getOpcoesEntrada(),
|
||||
'prioridades' => self::getPrioridades(),
|
||||
],
|
||||
'model' => [
|
||||
'paciente_id' => null,
|
||||
'opcao_entrada_id' => null,
|
||||
'prioridade_id' => null,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function createEncaminhamento($filaId)
|
||||
{
|
||||
$fila = Fila::findOrFail($filaId);
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'modulos_encaminhamento' => self::getModulosEncaminhamento($fila),
|
||||
'centros_custo_encaminhamento' => [],
|
||||
'opcoes_entrada_encaminhamento' => [],
|
||||
'prioridades' => Prioridade::orderBy('nome')->get(['id', 'nome']),
|
||||
],
|
||||
'model' => [
|
||||
'modulo_encaminhamento_id' => null,
|
||||
'centro_custo_encaminhamento_id' => null,
|
||||
'opcao_entrada_encaminhamento_id' => null,
|
||||
'prioridade_id' => $fila->prioridade_id ?? null,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function listEncaminhamento()
|
||||
{
|
||||
$params = (object) request()->all();
|
||||
|
||||
switch (request()->listar) {
|
||||
|
||||
case 'centros-custo-encaminhamento': {
|
||||
|
||||
return [
|
||||
'centros_custo_encaminhamento' => self::getCentrosCustoEncaminhamento($params),
|
||||
'opcoes_entrada_encaminhamento' => [],
|
||||
];
|
||||
}
|
||||
case 'opcoes-entrada-encaminhamento': {
|
||||
|
||||
return [
|
||||
'opcoes_entrada_encaminhamento' => self::getOpcoesEntradaEncaminhamento($params),
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static function getModulosEncaminhamento($fila)
|
||||
{
|
||||
return DB::table('administracao_modulo AS modulo')
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('painel_senha_opcao_entrada AS opcao_entrada')
|
||||
->join(
|
||||
'painel_senha_opcao_entrada_destino AS opcao_entrada_destino',
|
||||
'opcao_entrada_destino.opcao_entrada_destino_id',
|
||||
'opcao_entrada.id'
|
||||
)
|
||||
->whereColumn('opcao_entrada.modulo_id', 'modulo.id')
|
||||
->where('opcao_entrada_destino.opcao_entrada_origem_id', $fila->opcaoEntradaAtual->id)
|
||||
)
|
||||
->select(
|
||||
'modulo.id',
|
||||
'modulo.nome'
|
||||
)
|
||||
->orderBy('modulo.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getCentrosCustoEncaminhamento($params)
|
||||
{
|
||||
return DB::table('cadastro_centro_custo AS centro_custo')
|
||||
->join('cadastro_setor AS setor', 'setor.id', 'centro_custo.setor_id')
|
||||
->whereExists(fn ($q) => $q
|
||||
->selectRaw(1)
|
||||
->from('painel_senha_opcao_entrada AS opcao_entrada')
|
||||
->join(
|
||||
'painel_senha_opcao_entrada_destino AS opcao_entrada_destino',
|
||||
'opcao_entrada_destino.opcao_entrada_destino_id',
|
||||
'opcao_entrada.id'
|
||||
)
|
||||
->whereColumn('opcao_entrada.centro_custo_id', 'centro_custo.id')
|
||||
->where('opcao_entrada_destino.opcao_entrada_origem_id', $params->opcao_entrada_origem_id)
|
||||
->where('opcao_entrada.modulo_id', $params->modulo_id)
|
||||
)
|
||||
->select(
|
||||
'centro_custo.id',
|
||||
'setor.nome AS setor_nome'
|
||||
)
|
||||
->orderBy('setor.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getOpcoesEntradaEncaminhamento($params)
|
||||
{
|
||||
return DB::table('painel_senha_opcao_entrada AS opcao_entrada')
|
||||
->join(
|
||||
'painel_senha_opcao_entrada_destino AS opcao_entrada_destino',
|
||||
'opcao_entrada_destino.opcao_entrada_destino_id',
|
||||
'opcao_entrada.id'
|
||||
)
|
||||
->where('opcao_entrada_destino.opcao_entrada_origem_id', $params->opcao_entrada_origem_id)
|
||||
->where('opcao_entrada.modulo_id', $params->modulo_id)
|
||||
->where('opcao_entrada.centro_custo_id', $params->centro_custo_id)
|
||||
->select(
|
||||
'opcao_entrada.id',
|
||||
'opcao_entrada.nome',
|
||||
'opcao_entrada.prioridade_obrigatoria'
|
||||
)
|
||||
->orderBy('opcao_entrada.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function store()
|
||||
{
|
||||
self::validateStore();
|
||||
|
||||
$centroCusto = self::getCurrentCentroCusto();
|
||||
|
||||
$senha = (
|
||||
Fila::where('opcao_entrada_id', request()->opcao_entrada_id)
|
||||
->whereRaw('emitido_em = CURRENT_DATE')
|
||||
->max('senha') ?? 0
|
||||
) + 1;
|
||||
|
||||
$fila = Fila::create([
|
||||
'paciente_id' => request()->paciente_id,
|
||||
'centro_custo_id' => $centroCusto->id,
|
||||
'opcao_entrada_id' => request()->opcao_entrada_id,
|
||||
'prioridade_id' => request()->prioridade_id,
|
||||
'status_id' => Status::EM_ESPERA,
|
||||
'senha' => $senha,
|
||||
])->fresh();
|
||||
|
||||
self::dispatch($fila->opcaoEntradaAtual->id, $fila->centro_custo_id);
|
||||
|
||||
return $fila;
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
$fila = Fila::findOrFail($id);
|
||||
|
||||
return [
|
||||
'form' => [
|
||||
'opcoes_entrada' => self::getOpcoesEntrada(),
|
||||
'prioridades' => self::getPrioridades(),
|
||||
],
|
||||
'model' => [
|
||||
'id' => $fila->id,
|
||||
'paciente' => $fila->paciente ? [
|
||||
'id' => $fila->paciente->id,
|
||||
'nome' => $fila->paciente->cidadao->nome,
|
||||
'cns' => $fila->paciente->cidadao->cns,
|
||||
] : null,
|
||||
'opcao_entrada_id' => $fila->opcao_entrada_id,
|
||||
'prioridade_id' => $fila->prioridade_id,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function update($id)
|
||||
{
|
||||
self::validateUpdate();
|
||||
|
||||
$fila = Fila::findOrFail($id);
|
||||
|
||||
$fila->update([
|
||||
'paciente_id' => request()->paciente_id,
|
||||
'prioridade_id' => request()->prioridade_id,
|
||||
]);
|
||||
|
||||
self::dispatch($fila->opcaoEntradaAtual->id, $fila->centro_custo_id);
|
||||
|
||||
return $fila->fresh();
|
||||
}
|
||||
|
||||
public static function list()
|
||||
{
|
||||
return [
|
||||
'form' => [
|
||||
'tempos_espera' => self::getTemposEspera(),
|
||||
'opcoes_entrada' => self::getOpcoesEntrada(),
|
||||
'prioridades' => self::getPrioridades(),
|
||||
'status' => self::getStatus(),
|
||||
'salas' => self::getSalas(),
|
||||
'telas' => self::getTelas(),
|
||||
],
|
||||
'model' => [
|
||||
'tempo_espera_id' => null,
|
||||
'opcao_entrada_id' => null,
|
||||
'prioridade_id' => null,
|
||||
'status_id' => Status::EM_ESPERA,
|
||||
'fila' => self::index(),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function autocompletePaciente()
|
||||
{
|
||||
return DB::table('cadastro_paciente')
|
||||
->select([
|
||||
'cadastro_paciente.id',
|
||||
'cadastro_cidadao.nome',
|
||||
'cadastro_cidadao.cns',
|
||||
])
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id');
|
||||
}
|
||||
|
||||
public static function evade($id)
|
||||
{
|
||||
$fila = Fila::findOrFail($id);
|
||||
|
||||
self::validateEvade();
|
||||
|
||||
$fila->update([
|
||||
'evadido' => true,
|
||||
'motivo_evasao' => request()->motivo,
|
||||
'motivo_cancelamento_evasao' => null,
|
||||
'finalizado' => true,
|
||||
]);
|
||||
|
||||
self::dispatch($fila->opcaoEntradaAtual->id, $fila->centro_custo_id);
|
||||
}
|
||||
|
||||
public static function answer($id)
|
||||
{
|
||||
$fila = Fila::findOrFail($id);
|
||||
|
||||
$fila->update(['status_id' => Status::EM_ATENDIMENTO]);
|
||||
|
||||
self::dispatch($fila->opcaoEntradaAtual->id, $fila->centro_custo_id);
|
||||
}
|
||||
|
||||
public static function sendBack($id)
|
||||
{
|
||||
$fila = Fila::findOrFail($id);
|
||||
|
||||
$fila->update(['status_id' => Status::EM_ESPERA]);
|
||||
|
||||
self::dispatch($fila->opcaoEntradaAtual->id, $fila->centro_custo_id);
|
||||
}
|
||||
|
||||
public static function forward($id)
|
||||
{
|
||||
$fila = Fila::findOrFail($id);
|
||||
|
||||
self::validateForward();
|
||||
|
||||
$opcaoEntradaFrom = $fila->opcaoEntradaAtual;
|
||||
$centroCustoFrom = CentroCusto::findOrFail($fila->centro_custo_id);
|
||||
|
||||
$opcaoEntradaTo = OpcaoEntrada::findOrFail(request()->opcao_entrada_encaminhamento_id);
|
||||
$centroCustoTo = $opcaoEntradaTo->centroCusto;
|
||||
|
||||
$fila->update([
|
||||
'centro_custo_id' => $centroCustoTo->id,
|
||||
'opcao_entrada_destino_id' => $opcaoEntradaTo->id,
|
||||
'prioridade_id' => request()->prioridade_id,
|
||||
'status_id' => Status::EM_ESPERA,
|
||||
]);
|
||||
|
||||
self::dispatch($opcaoEntradaFrom->id, $centroCustoFrom->id);
|
||||
|
||||
if ($opcaoEntradaTo->modulo_id === ModuloService::getId(Modulo::PAINEL_SENHA)) {
|
||||
|
||||
self::dispatch($opcaoEntradaTo->id, $centroCustoTo->id);
|
||||
}
|
||||
else if ($opcaoEntradaTo->modulo_id === ModuloService::getId(Modulo::FARMACIA)) {
|
||||
|
||||
FarmaciaFilaAtendimentoService::dispatch($opcaoEntradaTo->id, $centroCustoTo->id);
|
||||
}
|
||||
else if ($opcaoEntradaTo->modulo_id === ModuloService::getId(Modulo::IMUNIZACAO)) {
|
||||
|
||||
ImunizacaoFilaAtendimentoService::dispatch($opcaoEntradaTo->id, $centroCustoTo->id);
|
||||
}
|
||||
}
|
||||
|
||||
public static function finalize($id)
|
||||
{
|
||||
$fila = Fila::findOrFail($id);
|
||||
|
||||
$fila->update(['finalizado' => true]);
|
||||
|
||||
self::dispatch($fila->opcaoEntradaAtual->id, $fila->centro_custo_id);
|
||||
}
|
||||
|
||||
public static function dispatch($opcaoEntradaId, $centroCustoId)
|
||||
{
|
||||
$centroCusto = CentroCusto::findOrFail($centroCustoId);
|
||||
|
||||
FilaEvent::dispatch(
|
||||
$centroCusto->setor->tipo_id,
|
||||
$centroCusto->id,
|
||||
Modulo::PAINEL_SENHA,
|
||||
self::index($centroCusto)
|
||||
);
|
||||
|
||||
CentroCustoCompartilhado::where('opcao_entrada_id', $opcaoEntradaId)
|
||||
->get()
|
||||
->each(function ($item) {
|
||||
$centroCusto = CentroCusto::findOrFail($item->centro_custo_id);
|
||||
|
||||
FilaEvent::dispatch(
|
||||
$centroCusto->setor->tipo_id,
|
||||
$centroCusto->id,
|
||||
Modulo::PAINEL_SENHA,
|
||||
self::index($centroCusto)
|
||||
);
|
||||
});
|
||||
|
||||
CentroCustoCompartilhado::where('centro_custo_id', $centroCustoId)
|
||||
->get()
|
||||
->each(function ($item) {
|
||||
$centroCusto = OpcaoEntrada::findOrFail($item->opcao_entrada_id)->centroCusto;
|
||||
|
||||
FilaEvent::dispatch(
|
||||
$centroCusto->setor->tipo_id,
|
||||
$centroCusto->id,
|
||||
Modulo::PAINEL_SENHA,
|
||||
self::index($centroCusto)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private static function validateStore()
|
||||
{
|
||||
request()->validate([
|
||||
'paciente_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists(Paciente::class, 'id'),
|
||||
],
|
||||
'opcao_entrada_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists(OpcaoEntrada::class, 'id'),
|
||||
],
|
||||
'prioridade_id' => [
|
||||
'nullable',
|
||||
Rule::requiredIf(fn () => OpcaoEntrada::find(request()->opcao_entrada_id)
|
||||
->prioridade_obrigatoria ?? false
|
||||
),
|
||||
Rule::exists(Prioridade::class, 'id'),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private static function validateUpdate()
|
||||
{
|
||||
request()->validate([
|
||||
'paciente_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists(Paciente::class, 'id'),
|
||||
],
|
||||
'prioridade_id' => [
|
||||
'nullable',
|
||||
Rule::requiredIf(fn () => OpcaoEntrada::find(request()->opcao_entrada_id)
|
||||
->prioridade_obrigatoria ?? false
|
||||
),
|
||||
Rule::exists(Prioridade::class, 'id'),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private static function validateForward()
|
||||
{
|
||||
request()->validate([
|
||||
'opcao_entrada_encaminhamento_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists(OpcaoEntrada::class, 'id'),
|
||||
],
|
||||
'prioridade_id' => [
|
||||
'nullable',
|
||||
Rule::requiredIf(fn () => OpcaoEntrada::find(request()->opcao_entrada_encaminhamento_id)
|
||||
->prioridade_obrigatoria ?? false
|
||||
),
|
||||
Rule::exists(Prioridade::class, 'id'),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private static function validateEvade()
|
||||
{
|
||||
request()->validate([
|
||||
'motivo' => [
|
||||
'nullable',
|
||||
'string',
|
||||
'max:500',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private static function getTempoEspera($filaId, $centroCusto)
|
||||
{
|
||||
$tempoEsperaTotal = DB::table('painel_senha_fila_historico')
|
||||
->select(
|
||||
'painel_senha_fila_historico.created_at',
|
||||
'painel_senha_fila_historico.status_id'
|
||||
)
|
||||
->where('painel_senha_fila_historico.fila_id', $filaId)
|
||||
->distinct()
|
||||
->get()
|
||||
->reduce(function ($count, $query) use ($filaId) {
|
||||
if ($query->status_id === Status::EM_ESPERA) {
|
||||
$proximo = DB::table('painel_senha_fila_historico')
|
||||
->where('fila_id', $filaId)
|
||||
->where('created_at', '>', $query->created_at)
|
||||
->select('created_at')
|
||||
->first();
|
||||
|
||||
return $count + Carbon::create($query->created_at)->diffInMinutes($proximo->created_at ?? Carbon::now());
|
||||
}
|
||||
|
||||
return $count;
|
||||
}, 1);
|
||||
|
||||
$tempoEspera = DB::table('painel_senha_tempo_espera')
|
||||
->select(
|
||||
'id',
|
||||
'cor'
|
||||
)
|
||||
->whereNull('deleted_at')
|
||||
->where('tempo', '<=', $tempoEsperaTotal)
|
||||
->where('infraestrutura_id', $centroCusto->infraestrutura_id)
|
||||
->orderByDesc('tempo')
|
||||
->first();
|
||||
|
||||
if (is_null($tempoEspera)) {
|
||||
$tempoEspera = DB::table('painel_senha_tempo_espera')
|
||||
->select(
|
||||
'id',
|
||||
'cor'
|
||||
)
|
||||
->whereNull('deleted_at')
|
||||
->where('tempo', '>=', $tempoEsperaTotal)
|
||||
->where('infraestrutura_id', $centroCusto->infraestrutura_id)
|
||||
->orderBy('tempo')
|
||||
->first();
|
||||
}
|
||||
|
||||
$tempoEsperaTotal = Carbon::now()->subMinutes($tempoEsperaTotal)->diff(Carbon::now());
|
||||
|
||||
return [
|
||||
'id' => $tempoEspera->id ?? null,
|
||||
'cor' => $tempoEspera->cor ?? null,
|
||||
'total' => [
|
||||
'dias' => (int) $tempoEsperaTotal->format('%d') > 0 ? (int) $tempoEsperaTotal->format('%d') : null,
|
||||
'horas' => (int) $tempoEsperaTotal->format('%h') ?? null,
|
||||
'minutos' => (int) $tempoEsperaTotal->format('%i') ?? null,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private static function getTemposEspera()
|
||||
{
|
||||
return DB::table('painel_senha_tempo_espera')
|
||||
->select([
|
||||
'painel_senha_tempo_espera.id',
|
||||
DB::raw("CONCAT(painel_senha_tempo_espera.tempo, ' MINUTO(S)') AS minutos"),
|
||||
])
|
||||
->whereNull('painel_senha_tempo_espera.deleted_at')
|
||||
->whereExists(fn ($q) => $q->select(DB::raw(1))
|
||||
->from('cadastro_infraestrutura')
|
||||
->whereNull('cadastro_infraestrutura.deleted_at')
|
||||
->whereExists(fn ($q) => $q->select(DB::raw(1))
|
||||
->from('cadastro_centro_custo')
|
||||
->whereNull('cadastro_centro_custo.deleted_at')
|
||||
->where('cadastro_centro_custo.id', self::getCurrentCentroCusto()->id)
|
||||
->whereColumn('cadastro_centro_custo.infraestrutura_id', 'cadastro_infraestrutura.id')
|
||||
)
|
||||
->whereColumn('cadastro_infraestrutura.id', 'painel_senha_tempo_espera.infraestrutura_id')
|
||||
)
|
||||
->orderBy('painel_senha_tempo_espera.tempo')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getOpcoesEntrada()
|
||||
{
|
||||
$centroCusto = self::getCurrentCentroCusto();
|
||||
|
||||
return DB::table('painel_senha_opcao_entrada')
|
||||
->select([
|
||||
'painel_senha_opcao_entrada.id',
|
||||
'painel_senha_opcao_entrada.nome',
|
||||
'painel_senha_opcao_entrada.prioridade_obrigatoria',
|
||||
])
|
||||
->whereNull('painel_senha_opcao_entrada.deleted_at')
|
||||
->where('painel_senha_opcao_entrada.modulo_id', ModuloService::getId(Modulo::PAINEL_SENHA))
|
||||
->where(fn ($q) => $q->where('painel_senha_opcao_entrada.centro_custo_id', $centroCusto->id)
|
||||
->orWhereExists(fn ($q) => $q->select(DB::raw(1))
|
||||
->from('painel_senha_centro_custo_compartilhado')
|
||||
->where('painel_senha_centro_custo_compartilhado.centro_custo_id', $centroCusto->id)
|
||||
->whereColumn('painel_senha_centro_custo_compartilhado.opcao_entrada_id', 'painel_senha_opcao_entrada.id')
|
||||
)
|
||||
)
|
||||
->orderBy('painel_senha_opcao_entrada.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getPrioridades()
|
||||
{
|
||||
return DB::table('painel_senha_prioridade')
|
||||
->select([
|
||||
'painel_senha_prioridade.id',
|
||||
'painel_senha_prioridade.nome',
|
||||
])
|
||||
->orderBy('painel_senha_prioridade.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getStatus()
|
||||
{
|
||||
return DB::table('painel_senha_status')
|
||||
->select([
|
||||
'id',
|
||||
'nome',
|
||||
])
|
||||
->orderBy('painel_senha_status.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getSalas()
|
||||
{
|
||||
return DB::table('cadastro_sala')
|
||||
->select([
|
||||
'cadastro_sala.id',
|
||||
'cadastro_sala.nome',
|
||||
])
|
||||
->whereNull('cadastro_sala.deleted_at')
|
||||
->where('centro_custo_id', self::getCurrentCentroCusto()->id)
|
||||
->orderBy('cadastro_sala.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getTelas()
|
||||
{
|
||||
return DB::table('painel_senha_tela')
|
||||
->select([
|
||||
'painel_senha_tela.id',
|
||||
'painel_senha_tela.nome',
|
||||
])
|
||||
->whereNull('painel_senha_tela.deleted_at')
|
||||
->whereExists(fn ($q) => $q->select(DB::raw(1))
|
||||
->from('painel_senha_opcao_chamada')
|
||||
->whereNull('painel_senha_opcao_chamada.deleted_at')
|
||||
->where('painel_senha_opcao_chamada.centro_custo_id', self::getCurrentCentroCusto()->id)
|
||||
->whereColumn('painel_senha_opcao_chamada.tela_id', 'painel_senha_tela.id')
|
||||
)
|
||||
->orderBy('painel_senha_tela.nome')
|
||||
->get();
|
||||
}
|
||||
}
|
||||
145
api/app/Services/PainelSenha/FilaService.php
Normal file
145
api/app/Services/PainelSenha/FilaService.php
Normal file
@@ -0,0 +1,145 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\PainelSenha;
|
||||
|
||||
use App\Enums\Administracao\Modulo;
|
||||
use App\Enums\PainelSenha\Status;
|
||||
use App\Models\PainelSenha\Fila;
|
||||
use App\Models\PainelSenha\OpcaoEntrada;
|
||||
use App\Models\PainelSenha\Prioridade;
|
||||
use App\Models\PainelSenha\TempoEspera;
|
||||
use App\Services\Administracao\ModuloService;
|
||||
use App\Services\Ambulatorio\FilaService as AmbulatorioFilaService;
|
||||
use App\Services\Farmacia\FilaAtendimentoService as FarmaciaFilaAtendimentoService;
|
||||
use App\Services\Imunizacao\FilaAtendimentoService as ImunizacaoFilaAtendimentoService;
|
||||
use App\Services\PainelSenha\FilaAtendimentoService as PainelSenhaFilaAtendimentoService;
|
||||
use App\Services\Service;
|
||||
|
||||
class FilaService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
return Fila::query();
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'form' => [
|
||||
'opcoes_entrada' => OpcaoEntrada::orderBy('nome')
|
||||
->whereHas('centroCusto', function($query) {
|
||||
$query->where('id', self::getCurrentCentroCusto()->id);
|
||||
})
|
||||
->get()
|
||||
->map(function($query) {
|
||||
return [
|
||||
'id' => $query->id,
|
||||
'nome' => $query->nome,
|
||||
'prioridade_obrigatoria' => $query->prioridade_obrigatoria
|
||||
];
|
||||
}),
|
||||
'prioridades' => Prioridade::orderBy('nome')
|
||||
->get()
|
||||
->map(function($query) {
|
||||
return [
|
||||
'id' => $query->id,
|
||||
'nome' => $query->nome,
|
||||
];
|
||||
}),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function store($request)
|
||||
{
|
||||
$fila = new Fila();
|
||||
|
||||
$tempoEspera = TempoEspera::orderBy('tempo')
|
||||
->first();
|
||||
|
||||
$senha = (
|
||||
$fila->where('opcao_entrada_id', $request['opcao_entrada_id'])
|
||||
->whereRaw('emitido_em = CURRENT_DATE')
|
||||
->max('senha') ?? 0
|
||||
) + 1;
|
||||
|
||||
$params = [
|
||||
'opcao_entrada_id' => $request['opcao_entrada_id'],
|
||||
'centro_custo_id' => $request['centro_custo_id'],
|
||||
'tempo_espera_id' => $tempoEspera->id ?? null,
|
||||
'prioridade_id' => $request['prioridade_id'] ?? null,
|
||||
'status_id' => Status::EM_ESPERA,
|
||||
'senha' => $senha,
|
||||
];
|
||||
|
||||
$fila = $fila->create($params)->fresh();
|
||||
|
||||
if ($fila->opcaoEntrada->modulo_id === ModuloService::getId(Modulo::PAINEL_SENHA)) {
|
||||
|
||||
PainelSenhaFilaAtendimentoService::dispatch($fila->opcaoEntradaAtual->id, $fila->centro_custo_id);
|
||||
}
|
||||
else if ($fila->opcaoEntrada->modulo_id === ModuloService::getId(Modulo::AMBULATORIO)) {
|
||||
|
||||
AmbulatorioFilaService::dispatch($fila->centro_custo_id);
|
||||
}
|
||||
else if ($fila->opcaoEntrada->modulo_id === ModuloService::getId(Modulo::IMUNIZACAO)) {
|
||||
|
||||
ImunizacaoFilaAtendimentoService::dispatch($fila->opcaoEntradaAtual->id, $fila->centro_custo_id);
|
||||
}
|
||||
else if ($fila->opcaoEntrada->modulo_id === ModuloService::getId(Modulo::FARMACIA)) {
|
||||
|
||||
FarmaciaFilaAtendimentoService::dispatch($fila->opcaoEntradaAtual->id, $fila->centro_custo_id);
|
||||
}
|
||||
|
||||
return $fila;
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
return self::get(Fila::class, $id);
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
self::show($id)->delete();
|
||||
}
|
||||
|
||||
public static function answer($id)
|
||||
{
|
||||
$fila = self::show($id);
|
||||
|
||||
$params = [
|
||||
'status_id' => Status::EM_ATENDIMENTO
|
||||
];
|
||||
|
||||
return tap($fila)->update($params)
|
||||
->fresh();
|
||||
}
|
||||
|
||||
public static function forward($request, $id)
|
||||
{
|
||||
$fila = self::show($id);
|
||||
|
||||
$params = [
|
||||
'centro_custo_id' => $request['centro_custo_id_para'],
|
||||
'status_id' => Status::EM_ESPERA,
|
||||
'prioridade_id' => $request['prioridade_id'] ?? null,
|
||||
];
|
||||
|
||||
return tap($fila)->update($params)
|
||||
->fresh();
|
||||
}
|
||||
|
||||
public static function evade($request, $id)
|
||||
{
|
||||
$fila = self::show($id);
|
||||
|
||||
$params = [
|
||||
'evadido' => true,
|
||||
'motivo_evasao' => $request['motivo_evasao'],
|
||||
];
|
||||
|
||||
return tap($fila)->update($params)
|
||||
->fresh();
|
||||
}
|
||||
}
|
||||
333
api/app/Services/PainelSenha/OpcaoEntradaService.php
Normal file
333
api/app/Services/PainelSenha/OpcaoEntradaService.php
Normal file
@@ -0,0 +1,333 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\PainelSenha;
|
||||
|
||||
use App\Enums\Administracao\Modulo;
|
||||
use App\Models\PainelSenha\CentroCustoCompartilhado;
|
||||
use App\Models\PainelSenha\OpcaoEntrada;
|
||||
use App\Models\PainelSenha\OpcaoEntradaDestino;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class OpcaoEntradaService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
return DB::table('painel_senha_opcao_entrada')
|
||||
->select([
|
||||
'painel_senha_opcao_entrada.id',
|
||||
'painel_senha_opcao_entrada.nome AS opcao_entrada_nome',
|
||||
'painel_senha_opcao_entrada.sigla AS opcao_entrada_sigla',
|
||||
'cadastro_setor.nome AS setor_nome',
|
||||
'administracao_modulo.nome AS modulo_nome',
|
||||
])
|
||||
->join('cadastro_centro_custo', 'cadastro_centro_custo.id', 'painel_senha_opcao_entrada.centro_custo_id')
|
||||
->join('cadastro_setor', 'cadastro_setor.id', 'cadastro_centro_custo.setor_id')
|
||||
->join('administracao_modulo', 'administracao_modulo.id', 'painel_senha_opcao_entrada.modulo_id')
|
||||
->whereNull('painel_senha_opcao_entrada.deleted_at')
|
||||
->where('cadastro_centro_custo.infraestrutura_id', self::getCurrentCentroCusto()->infraestrutura_id)
|
||||
->orderBy('painel_senha_opcao_entrada.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => self::getModelData(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function store($request)
|
||||
{
|
||||
self::validate($request);
|
||||
|
||||
$opcaoEntrada = null;
|
||||
|
||||
DB::transaction(function() use ($request, &$opcaoEntrada) {
|
||||
$opcaoEntrada = OpcaoEntrada::create($request->all())->fresh();
|
||||
|
||||
foreach ($request->centros_custo_compartilhado as $value) {
|
||||
CentroCustoCompartilhado::updateOrCreate(
|
||||
[
|
||||
'opcao_entrada_id' => $opcaoEntrada->id,
|
||||
'centro_custo_id' => $value['centro_custo_id'],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($request->opcoes_entrada_destino as $value) {
|
||||
OpcaoEntradaDestino::updateOrCreate(
|
||||
[
|
||||
'opcao_entrada_origem_id' => $opcaoEntrada->id,
|
||||
'opcao_entrada_destino_id' => $value['opcao_entrada_id'],
|
||||
]
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
return $opcaoEntrada;
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
return OpcaoEntrada::find($id);
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
return [
|
||||
'form' => self::getFormData($id),
|
||||
'model' => self::getModelData($id),
|
||||
];
|
||||
}
|
||||
|
||||
public static function update($request, $id)
|
||||
{
|
||||
self::validate($request);
|
||||
|
||||
$opcaoEntrada = tap(self::show($id));
|
||||
|
||||
DB::transaction(function() use ($request, &$opcaoEntrada) {
|
||||
$opcaoEntrada = $opcaoEntrada->update($request->all());
|
||||
|
||||
$centrosCustoCompartilhadoIds = collect($request->centros_custo_compartilhado)->pluck('id')->all();
|
||||
$opcoesEntradaDestinoIds = collect($request->opcoes_entrada_destino)->pluck('id')->all();
|
||||
|
||||
CentroCustoCompartilhado::where('opcao_entrada_id', $opcaoEntrada->id)
|
||||
->whereNotIn('id', $centrosCustoCompartilhadoIds)
|
||||
->delete();
|
||||
|
||||
OpcaoEntradaDestino::where('opcao_entrada_origem_id', $opcaoEntrada->id)
|
||||
->whereNotIn('id', $opcoesEntradaDestinoIds)
|
||||
->delete();
|
||||
|
||||
foreach ($request->centros_custo_compartilhado as $value) {
|
||||
CentroCustoCompartilhado::updateOrCreate(
|
||||
['id' => $value['id']],
|
||||
[
|
||||
'opcao_entrada_id' => $opcaoEntrada->id,
|
||||
'centro_custo_id' => $value['centro_custo_id'],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
foreach ($request->opcoes_entrada_destino as $value) {
|
||||
OpcaoEntradaDestino::updateOrCreate(
|
||||
['id' => $value['id']],
|
||||
[
|
||||
'opcao_entrada_origem_id' => $opcaoEntrada->id,
|
||||
'opcao_entrada_destino_id' => $value['opcao_entrada_id'],
|
||||
]
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
return $opcaoEntrada;
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
return self::show($id)->delete();
|
||||
}
|
||||
|
||||
public static function list()
|
||||
{
|
||||
switch (request()->get('listar')) {
|
||||
case 'centros-custo':
|
||||
return self::getCentrosCusto(request()->get('modulo_id'), request()->get('opcao_entrada_id'));
|
||||
break;
|
||||
|
||||
case 'opcoes-entrada':
|
||||
return self::getOpcoesEntrada(request()->get('modulo_id'), request()->get('centro_custo_id'), request()->get('opcao_entrada_id'));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private static function validate($request)
|
||||
{
|
||||
return $request->validate([
|
||||
'centro_custo_id' => [
|
||||
'required',
|
||||
Rule::unique('\App\Models\PainelSenha\OpcaoEntrada')->where(function ($query) use ($request) {
|
||||
return $query
|
||||
->where('nome', $request->nome)
|
||||
->where('sigla', $request->sigla)
|
||||
->where('modulo_id', $request->modulo_id)
|
||||
->where('faixa_etaria_id', $request->faixa_etaria_id)
|
||||
->whereNull('deleted_at');
|
||||
})->ignore($request->opcao_entrada),
|
||||
],
|
||||
'modulo_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
],
|
||||
'nome' => [
|
||||
'required',
|
||||
'min:3',
|
||||
'max:100',
|
||||
],
|
||||
'sigla' => [
|
||||
'required',
|
||||
'min:2',
|
||||
'max:2',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private static function getFormData($opcaoEntradaId = null)
|
||||
{
|
||||
return [
|
||||
'centros_custo' => self::getCentrosCusto(),
|
||||
'modulos' => self::getModulos(),
|
||||
'faixas_etarias' => self::getFaixasEtarias(),
|
||||
'centro_custo_compartilhado' => [
|
||||
'centros_custo' => self::getCentrosCusto(),
|
||||
],
|
||||
'opcao_entrada_destino' => [
|
||||
'modulos' => self::getModulos(true, $opcaoEntradaId),
|
||||
'centros_custo' => [],
|
||||
'opcoes_entrada' => [],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private static function getModelData($id = null)
|
||||
{
|
||||
$opcaoEntrada = self::show($id);
|
||||
|
||||
$centrosCustoCompartilhado = DB::table('painel_senha_centro_custo_compartilhado')
|
||||
->select([
|
||||
'painel_senha_centro_custo_compartilhado.id',
|
||||
'cadastro_centro_custo.id AS centro_custo_id',
|
||||
'cadastro_setor.nome AS setor_nome',
|
||||
])
|
||||
->join('cadastro_centro_custo', 'cadastro_centro_custo.id', 'painel_senha_centro_custo_compartilhado.centro_custo_id')
|
||||
->join('cadastro_setor', 'cadastro_setor.id', 'cadastro_centro_custo.setor_id')
|
||||
->where('painel_senha_centro_custo_compartilhado.opcao_entrada_id', $opcaoEntrada->id ?? null)
|
||||
->orderBy('cadastro_setor.nome')
|
||||
->get();
|
||||
|
||||
$opcoesEntradaDestino = DB::table('painel_senha_opcao_entrada_destino')
|
||||
->select([
|
||||
'painel_senha_opcao_entrada_destino.id',
|
||||
'administracao_modulo.id AS modulo_id',
|
||||
'administracao_modulo.nome AS modulo_nome',
|
||||
'cadastro_centro_custo.id AS centro_custo_id',
|
||||
'cadastro_setor.nome AS setor_nome',
|
||||
'opcao_entrada_destino.id AS opcao_entrada_id',
|
||||
'opcao_entrada_destino.nome AS opcao_entrada_nome',
|
||||
])
|
||||
->join('painel_senha_opcao_entrada', 'painel_senha_opcao_entrada.id', 'painel_senha_opcao_entrada_destino.opcao_entrada_destino_id')
|
||||
->join('administracao_modulo', 'administracao_modulo.id', 'painel_senha_opcao_entrada.modulo_id')
|
||||
->join('cadastro_centro_custo', 'cadastro_centro_custo.id', 'painel_senha_opcao_entrada.centro_custo_id')
|
||||
->join('cadastro_setor', 'cadastro_setor.id', 'cadastro_centro_custo.setor_id')
|
||||
->join('painel_senha_opcao_entrada AS opcao_entrada_destino', 'opcao_entrada_destino.id', 'painel_senha_opcao_entrada_destino.opcao_entrada_destino_id')
|
||||
->where('painel_senha_opcao_entrada_destino.opcao_entrada_origem_id', $opcaoEntrada->id ?? null)
|
||||
->orderBy('opcao_entrada_destino.nome')
|
||||
->get();
|
||||
|
||||
return [
|
||||
'id' => $opcaoEntrada->id ?? null,
|
||||
'centro_custo_id' => $opcaoEntrada->centro_custo_id ?? null,
|
||||
'modulo_id' => $opcaoEntrada->modulo_id ?? null,
|
||||
'faixa_etaria_id' => $opcaoEntrada->faixa_etaria_id ?? null,
|
||||
'nome' => $opcaoEntrada->nome ?? null,
|
||||
'prioridade_obrigatoria' => $opcaoEntrada->prioridade_obrigatoria ?? false,
|
||||
'sigla' => $opcaoEntrada->sigla ?? null,
|
||||
'centro_custo_compartilhado' => [
|
||||
'centro_custo_id' => null,
|
||||
],
|
||||
'opcao_entrada_destino' => [
|
||||
'centro_custo_id' => null,
|
||||
'modulo_id' => null,
|
||||
'opcao_entrada_id' => null,
|
||||
],
|
||||
'centros_custo_compartilhado' => $centrosCustoCompartilhado ?? [],
|
||||
'opcoes_entrada_destino' => $opcoesEntradaDestino ?? [],
|
||||
];
|
||||
}
|
||||
|
||||
private static function getModulos($hasOpcaoEntrada = false, $opcaoEntradaId = null)
|
||||
{
|
||||
return DB::table('administracao_modulo')
|
||||
->select([
|
||||
'administracao_modulo.id',
|
||||
'administracao_modulo.nome',
|
||||
'administracao_modulo.identificador',
|
||||
])
|
||||
->whereExists(fn ($q) => $q->select(DB::raw(1))
|
||||
->from('painel_senha_parametrizacao_infraestrutura_modulo')
|
||||
->join(
|
||||
'painel_senha_parametrizacao_infraestrutura',
|
||||
'painel_senha_parametrizacao_infraestrutura.id',
|
||||
'painel_senha_parametrizacao_infraestrutura_modulo.parametrizacao_infraestrutura_id'
|
||||
)
|
||||
->where('painel_senha_parametrizacao_infraestrutura.infraestrutura_id', self::getCurrentCentroCusto()->infraestrutura_id)
|
||||
->whereColumn('painel_senha_parametrizacao_infraestrutura_modulo.modulo_id', 'administracao_modulo.id')
|
||||
)
|
||||
->when($hasOpcaoEntrada, fn ($q) => $q->whereIn('administracao_modulo.identificador', [Modulo::FARMACIA, Modulo::IMUNIZACAO, Modulo::PAINEL_SENHA])
|
||||
->whereExists(fn ($q) => $q->select(DB::raw(1))
|
||||
->from('painel_senha_opcao_entrada')
|
||||
->join('cadastro_centro_custo', 'cadastro_centro_custo.id', 'painel_senha_opcao_entrada.centro_custo_id')
|
||||
->whereNull('painel_senha_opcao_entrada.deleted_at')
|
||||
->where('cadastro_centro_custo.infraestrutura_id', self::getCurrentCentroCusto()->infraestrutura_id)
|
||||
->when($opcaoEntradaId, fn ($q) => $q->where('painel_senha_opcao_entrada.id', '<>', $opcaoEntradaId))
|
||||
->whereColumn('painel_senha_opcao_entrada.modulo_id', 'administracao_modulo.id')
|
||||
)
|
||||
)
|
||||
->orderBy('administracao_modulo.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getCentrosCusto($moduloId = null, $opcaoEntradaId = null)
|
||||
{
|
||||
return DB::table('cadastro_centro_custo')
|
||||
->select([
|
||||
'cadastro_centro_custo.id AS centro_custo_id',
|
||||
'cadastro_setor.nome AS setor_nome',
|
||||
])
|
||||
->join('cadastro_setor', 'cadastro_setor.id', 'cadastro_centro_custo.setor_id')
|
||||
->whereNull('cadastro_centro_custo.deleted_at')
|
||||
->when($moduloId, function ($query) use ($moduloId, $opcaoEntradaId) {
|
||||
$query->whereExists(function($query) use ($moduloId, $opcaoEntradaId) {
|
||||
$query->select(DB::raw(1))
|
||||
->from('painel_senha_opcao_entrada')
|
||||
->whereNull('painel_senha_opcao_entrada.deleted_at')
|
||||
->where('painel_senha_opcao_entrada.modulo_id', $moduloId)
|
||||
->when($opcaoEntradaId, fn ($q) => $q->where('painel_senha_opcao_entrada.id', '<>', $opcaoEntradaId))
|
||||
->whereColumn('painel_senha_opcao_entrada.centro_custo_id', 'cadastro_centro_custo.id');
|
||||
});
|
||||
})
|
||||
->where('cadastro_centro_custo.infraestrutura_id', self::getCurrentCentroCusto()->infraestrutura_id)
|
||||
->orderBy('cadastro_setor.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getOpcoesEntrada($moduloId, $centroCustoId, $opcaoEntradaId)
|
||||
{
|
||||
return DB::table('painel_senha_opcao_entrada')
|
||||
->select([
|
||||
'painel_senha_opcao_entrada.id',
|
||||
'painel_senha_opcao_entrada.nome',
|
||||
])
|
||||
->whereNull('painel_senha_opcao_entrada.deleted_at')
|
||||
->where('painel_senha_opcao_entrada.modulo_id', $moduloId)
|
||||
->where('painel_senha_opcao_entrada.centro_custo_id', $centroCustoId)
|
||||
->where('painel_senha_opcao_entrada.id', '<>', $opcaoEntradaId)
|
||||
->orderBy('painel_senha_opcao_entrada.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getFaixasEtarias()
|
||||
{
|
||||
return DB::table('geral_faixa_etaria')
|
||||
->select([
|
||||
'geral_faixa_etaria.id',
|
||||
'geral_faixa_etaria.nome',
|
||||
])
|
||||
->orderBy('geral_faixa_etaria.nome')
|
||||
->get();
|
||||
}
|
||||
}
|
||||
114
api/app/Services/PainelSenha/PrioridadeService.php
Normal file
114
api/app/Services/PainelSenha/PrioridadeService.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\PainelSenha;
|
||||
|
||||
use App\Models\PainelSenha\Prioridade;
|
||||
use App\Models\PainelSenha\PrioridadeTipo;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class PrioridadeService extends Service
|
||||
{
|
||||
public static function index()
|
||||
{
|
||||
return DB::table('painel_senha_prioridade')
|
||||
->select([
|
||||
'painel_senha_prioridade.id',
|
||||
'painel_senha_prioridade_tipo.nome as tipo_nome',
|
||||
'painel_senha_prioridade.nome',
|
||||
'painel_senha_prioridade.grau',
|
||||
])
|
||||
->leftJoin('painel_senha_prioridade_tipo', 'painel_senha_prioridade_tipo.id', 'painel_senha_prioridade.tipo_id')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => self::getModelData(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function store($request)
|
||||
{
|
||||
self::validate($request);
|
||||
|
||||
return Prioridade::create($request->all())->fresh();
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
return Prioridade::find($id);
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => self::getModelData($id),
|
||||
];
|
||||
}
|
||||
|
||||
public static function update($request, $id)
|
||||
{
|
||||
self::validate($request);
|
||||
|
||||
return tap(self::show($id))->update($request->all());
|
||||
}
|
||||
|
||||
public static function destroy($id)
|
||||
{
|
||||
return self::show($id)->delete();
|
||||
}
|
||||
|
||||
private static function validate($request)
|
||||
{
|
||||
return $request->validate([
|
||||
'tipo_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::exists(PrioridadeTipo::class, 'id'),
|
||||
],
|
||||
'nome' => [
|
||||
'required',
|
||||
'string',
|
||||
'min:3',
|
||||
'max:100',
|
||||
Rule::unique('\App\Models\PainelSenha\Prioridade')->where(function ($query) use ($request) {
|
||||
return $query->where('nome', $request->nome);
|
||||
})->ignore($request->prioridade),
|
||||
],
|
||||
'grau' => [
|
||||
'required',
|
||||
'integer',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private static function getFormData()
|
||||
{
|
||||
return [
|
||||
'tipos' => DB::table('painel_senha_prioridade_tipo')
|
||||
->select([
|
||||
'id',
|
||||
'nome',
|
||||
])
|
||||
->orderBy('nome')
|
||||
->get(),
|
||||
];
|
||||
}
|
||||
|
||||
private static function getModelData($id = null)
|
||||
{
|
||||
$prioridade = self::show($id);
|
||||
|
||||
return [
|
||||
'id' => $prioridade->id ?? null,
|
||||
'tipo_id' => $prioridade->tipo_id ?? null,
|
||||
'nome' => $prioridade->nome ?? null,
|
||||
'grau' => $prioridade->grau ?? null,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user