This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\ProntoAtendimento\Evasao;
|
||||
|
||||
use App\Enums\ProntoAtendimento\Atendimento\Desfecho;
|
||||
use App\Models\ProntoAtendimento\Atendimento\Faa;
|
||||
use App\Services\Service;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CancelamentoEvasaoService extends Service
|
||||
{
|
||||
public static function datatable($request)
|
||||
{
|
||||
$cancelamentoEvasao = DB::table('pronto_atendimento_atendimento')
|
||||
->leftJoin('painel_senha_fila_historico', function($leftJoin) {
|
||||
|
||||
$leftJoin->on('pronto_atendimento_atendimento.fila_id', '=', 'painel_senha_fila_historico.fila_id')
|
||||
->where('painel_senha_fila_historico.id', '=', function ($query) {
|
||||
|
||||
$query->select('painel_senha_fila_historico.id')
|
||||
->from('painel_senha_fila_historico')
|
||||
->whereColumn('painel_senha_fila_historico.fila_id', 'pronto_atendimento_atendimento.fila_id')
|
||||
->orderByDesc('painel_senha_fila_historico.created_at')
|
||||
->limit(1);
|
||||
});
|
||||
})
|
||||
->leftJoin('cadastro_centro_custo as fila_centro_custo', 'fila_centro_custo.id', 'painel_senha_fila_historico.created_centro_custo_id')
|
||||
->leftJoin('cadastro_setor', 'cadastro_setor.id', 'fila_centro_custo.setor_id')
|
||||
->join('pronto_atendimento_faa', 'pronto_atendimento_faa.id', 'pronto_atendimento_atendimento.faa_id')
|
||||
->join('cadastro_paciente', 'cadastro_paciente.id', 'pronto_atendimento_faa.paciente_id')
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_paciente.cidadao_id')
|
||||
->join('cadastro_centro_custo', 'cadastro_centro_custo.id', 'pronto_atendimento_atendimento.created_centro_custo_id')
|
||||
->where('pronto_atendimento_faa.desfecho_id', Desfecho::EVASAO)
|
||||
->where('pronto_atendimento_faa.updated_at', '>=', Carbon::now()->subHours(24)->toDateTimeString())
|
||||
->where('cadastro_centro_custo.infraestrutura_id', '=', $request->infraestrutura_id);
|
||||
|
||||
return $cancelamentoEvasao
|
||||
->select([
|
||||
DB::raw('DATE_FORMAT(pronto_atendimento_faa.updated_at, "%d/%m/%Y") as data'),
|
||||
DB::raw('DATE_FORMAT(pronto_atendimento_faa.updated_at, "%H:%i") as hora'),
|
||||
'cadastro_cidadao.nome',
|
||||
DB::raw('(CASE
|
||||
WHEN cadastro_cidadao.nome_mae IS NULL
|
||||
THEN "Não informado"
|
||||
ELSE cadastro_cidadao.nome_mae
|
||||
END) as nome_mae'),
|
||||
DB::raw('(CASE
|
||||
WHEN DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y") IS NULL
|
||||
THEN "Não informada"
|
||||
ELSE DATE_FORMAT(cadastro_cidadao.data_nascimento, "%d/%m/%Y")
|
||||
END) as data_nascimento'),
|
||||
'cadastro_paciente.prontuario',
|
||||
'cadastro_paciente.sequencial_desconhecido',
|
||||
'cadastro_cidadao.caracteristicas_fisicas',
|
||||
'cadastro_cidadao.preferencia_nome_id AS preferencia_nome_id',
|
||||
'cadastro_cidadao.nome_social AS nome_social',
|
||||
'pronto_atendimento_faa.id as faa_id',
|
||||
'pronto_atendimento_faa.numero as faa_numero',
|
||||
DB::raw('(CASE
|
||||
WHEN painel_senha_fila_historico.motivo_evasao IS NULL
|
||||
THEN "Não informado"
|
||||
ELSE painel_senha_fila_historico.motivo_evasao
|
||||
END) as motivo_evasao'),
|
||||
'cadastro_setor.nome as setor'
|
||||
])
|
||||
->orderByDesc('pronto_atendimento_faa.updated_at')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function cancel($request, $faaId)
|
||||
{
|
||||
self::validations();
|
||||
|
||||
DB::transaction(function () use ($request, $faaId) {
|
||||
|
||||
$faa = Faa::findOrFail($faaId);
|
||||
|
||||
$faa->update([
|
||||
'desfecho_id' => null,
|
||||
'finalizado' => false,
|
||||
'motivo_cancelamento_evasao' => $request->motivo_cancelamento_evasao,
|
||||
]);
|
||||
|
||||
$fila = $faa->atendimentos->first()->fila;
|
||||
|
||||
if (!is_null($fila)) {
|
||||
|
||||
$fila->update([
|
||||
'evadido' => false,
|
||||
'motivo_cancelamento_evasao' => $request->motivo_cancelamento_evasao,
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static function validations()
|
||||
{
|
||||
return request()->validate([
|
||||
'motivo_cancelamento_evasao' => [
|
||||
'required',
|
||||
'string',
|
||||
'between:3,500',
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user