This commit is contained in:
239
api/app/Services/WebService/Laboratorio/Cliente/ExameService.php
Normal file
239
api/app/Services/WebService/Laboratorio/Cliente/ExameService.php
Normal file
@@ -0,0 +1,239 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\WebService\Laboratorio\Cliente;
|
||||
|
||||
use App\Enums\WebService\Laboratorio\Cliente\PedidoStatus;
|
||||
use App\Exceptions\WebService\Laboratorio\Cliente\Exame\CannotDeleteExameException;
|
||||
use App\Exceptions\WebService\Laboratorio\Cliente\Exame\ExameNotFoundException;
|
||||
use App\Models\Parametrizacao\Exame\Exame as CadastroExame;
|
||||
use App\Models\WebService\Laboratorio\Cliente\Exame;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class ExameService extends Service
|
||||
{
|
||||
public static function index($laboratorioId)
|
||||
{
|
||||
$laboratorio = LaboratorioService::getLaboratorioModel($laboratorioId);
|
||||
|
||||
return DB::table('web_service_laboratorio_cliente_exame AS exame')
|
||||
->where('exame.laboratorio_id', $laboratorio->id)
|
||||
->select([
|
||||
'exame.id',
|
||||
'exame.conectasus_exame_id',
|
||||
'exame.nome',
|
||||
])
|
||||
->orderBy('exame.nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => [
|
||||
'laboratorio_id' => null,
|
||||
'conectasus_exame' => null,
|
||||
'nome' => null,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function store($request, $laboratorioId)
|
||||
{
|
||||
$laboratorio = LaboratorioService::getLaboratorioModel($laboratorioId);
|
||||
|
||||
self::validate($request);
|
||||
|
||||
return Exame::create(self::getExameData($request, $laboratorio))->fresh();
|
||||
}
|
||||
|
||||
public static function show($laboratorioId, $id)
|
||||
{
|
||||
$laboratorio = LaboratorioService::getLaboratorioModel($laboratorioId);
|
||||
|
||||
return self::getExameModel($id, $laboratorio);
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
$exame = self::getExameModel($id);
|
||||
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => [
|
||||
'laboratorio_id' => $exame->laboratorio_id,
|
||||
'conectasus_exame' => $exame->conectasusExame ? (object) [
|
||||
'id' => $exame->conectasusExame->id,
|
||||
'nome' => $exame->conectasusExame->nome,
|
||||
] : null,
|
||||
'nome' => $exame->nome,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private static function getFormData()
|
||||
{
|
||||
return [
|
||||
'laboratorios' => self::getLaboratorios(),
|
||||
];
|
||||
}
|
||||
|
||||
private static function getLaboratorios()
|
||||
{
|
||||
return DB::table('web_service_laboratorio_cliente_laboratorio AS laboratorio')
|
||||
->join(
|
||||
'cadastro_infraestrutura AS conectasus_infraestrutura',
|
||||
'conectasus_infraestrutura.id',
|
||||
'laboratorio.conectasus_infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade AS conectasus_unidade',
|
||||
'conectasus_unidade.id',
|
||||
'conectasus_infraestrutura.unidade_id'
|
||||
)
|
||||
->select([
|
||||
'laboratorio.id',
|
||||
DB::raw("CONCAT(
|
||||
conectasus_unidade.nome_fantasia,
|
||||
IF(conectasus_infraestrutura.nome IS NOT NULL,
|
||||
CONCAT(' - ', conectasus_infraestrutura.nome),
|
||||
''
|
||||
)
|
||||
) AS descricao")
|
||||
])
|
||||
->orderByRaw("CONCAT(
|
||||
conectasus_unidade.nome_fantasia,
|
||||
IF(conectasus_infraestrutura.nome IS NOT NULL,
|
||||
CONCAT(' - ', conectasus_infraestrutura.nome),
|
||||
''
|
||||
)
|
||||
)")
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function update($request, $laboratorioId, $id)
|
||||
{
|
||||
$laboratorio = LaboratorioService::getLaboratorioModel($laboratorioId);
|
||||
$exame = self::getExameModel($id, $laboratorio);
|
||||
|
||||
self::validate($request);
|
||||
|
||||
return tap($exame)->update(self::getExameData($request, $laboratorio))->fresh();
|
||||
}
|
||||
|
||||
private static function getExameData($request, $laboratorio)
|
||||
{
|
||||
$data = $request->only(['conectasus_exame_id', 'nome']);
|
||||
$data['laboratorio_id'] = $laboratorio->id;
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function destroy($laboratorioId, $id)
|
||||
{
|
||||
$laboratorio = LaboratorioService::getLaboratorioModel($laboratorioId);
|
||||
$exame = self::getExameModel($id, $laboratorio);
|
||||
|
||||
if (self::hasPedidoEmProcessamento($exame, $laboratorio)) {
|
||||
|
||||
throw new CannotDeleteExameException();
|
||||
}
|
||||
|
||||
$exame->delete();
|
||||
}
|
||||
|
||||
private static function hasPedidoEmProcessamento($exame, $laboratorio)
|
||||
{
|
||||
return $exame->conectasusExame()
|
||||
->whereHas('agendamentoExameLaboratorial', fn ($q) => $q
|
||||
->whereHas('agendamento', fn ($q) => $q
|
||||
->where('laboratorio_infraestrutura_id', $laboratorio->conectasus_infraestrutura_id)
|
||||
->where('web_service_laboratorio_cliente_pedido_status_id', '<>', PedidoStatus::AGUARDANDO_PROCESSAMENTO)
|
||||
)
|
||||
)
|
||||
->exists();
|
||||
}
|
||||
|
||||
public static function datatable()
|
||||
{
|
||||
return DB::table('web_service_laboratorio_cliente_exame AS exame')
|
||||
->join(
|
||||
'web_service_laboratorio_cliente_laboratorio AS laboratorio',
|
||||
'laboratorio.id',
|
||||
'exame.laboratorio_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_infraestrutura AS conectasus_infraestrutura',
|
||||
'conectasus_infraestrutura.id',
|
||||
'laboratorio.conectasus_infraestrutura_id'
|
||||
)
|
||||
->join(
|
||||
'cadastro_unidade AS conectasus_unidade',
|
||||
'conectasus_unidade.id',
|
||||
'conectasus_infraestrutura.unidade_id'
|
||||
)
|
||||
->leftJoin(
|
||||
'parametrizacao_exame AS conectasus_exame',
|
||||
'conectasus_exame.id',
|
||||
'exame.conectasus_exame_id'
|
||||
)
|
||||
->select([
|
||||
'exame.id',
|
||||
'exame.nome',
|
||||
'conectasus_exame.nome as conectasus_exame_nome',
|
||||
'exame.laboratorio_id',
|
||||
DB::raw("CONCAT(
|
||||
conectasus_unidade.nome_fantasia,
|
||||
IF(conectasus_infraestrutura.nome IS NOT NULL,
|
||||
CONCAT(' - ', conectasus_infraestrutura.nome),
|
||||
''
|
||||
)
|
||||
) AS laboratorio_descricao")
|
||||
]);
|
||||
}
|
||||
|
||||
private static function validate($request)
|
||||
{
|
||||
$request->validate(
|
||||
[
|
||||
'conectasus_exame_id' => [
|
||||
'nullable',
|
||||
'present',
|
||||
'integer',
|
||||
Rule::exists(CadastroExame::class, 'id'),
|
||||
Rule::unique(Exame::class, 'conectasus_exame_id')
|
||||
->where('laboratorio_id', $request->route('laboratorioId'))
|
||||
->ignore($request->route('id')),
|
||||
],
|
||||
'nome' => [
|
||||
'required',
|
||||
'string',
|
||||
'between:3,200',
|
||||
Rule::unique(Exame::class, 'nome')
|
||||
->where('laboratorio_id', $request->route('laboratorioId'))
|
||||
->ignore($request->route('id')),
|
||||
],
|
||||
],
|
||||
[
|
||||
'conectasus_exame_id.exists' => 'O campo Exame (ConectaSUS) selecionado é inválido.',
|
||||
'conectasus_exame_id.unique' => 'O campo Exame (ConectaSUS) já está em uso para este laboratório.',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
private static function getExameModel($id, $laboratorio = null)
|
||||
{
|
||||
$exame = Exame::when($laboratorio, fn ($q) => $q->where('laboratorio_id', $laboratorio->id))
|
||||
->where('id', $id)
|
||||
->first();
|
||||
|
||||
if (is_null($exame)) {
|
||||
|
||||
throw new ExameNotFoundException();
|
||||
}
|
||||
|
||||
return $exame;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user