114 lines
3.4 KiB
PHP
114 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Farmacia;
|
|
|
|
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'),
|
|
],
|
|
]);
|
|
}
|
|
}
|