Files
Antonio Lopes dos Santos afec302c3e
All checks were successful
build-api-image / build (push) Successful in 51s
init commit
2026-08-04 16:52:02 -03:00

218 lines
6.6 KiB
PHP

<?php
namespace App\Services\TransportePaciente\Ponto;
use App\Exceptions\Model\DeletingModelException;
use App\Models\Cadastro\Localizacao\LogradouroTipo;
use App\Models\TransportePaciente\Ponto\Ponto;
use App\Services\Service;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
class PontoService extends Service
{
private static function getFormData()
{
$now = Carbon::now();
return [
'data_atual' => $now->format('d/m/Y'),
'tiposLogradouro' => LogradouroTipo::orderBy('descricao')->get()->map(fn ($tipo) => [
'id' => $tipo->id,
'descricao' => $tipo->descricao,
]),
];
}
private static function getModelData($ponto = null)
{
$ponto = [
'id' => $ponto->id ?? null,
'descricao' => $ponto->descricao ?? null,
'logradouro' => $ponto->logradouro ?? null,
'numero' => $ponto->numero ?? null,
'bloqueado' => $ponto->bloqueado ?? null,
'sem_numero' => $ponto && $ponto->numero == null ?? false,
'casa_paciente' => $ponto->casa_paciente ?? null
];
return $ponto;
}
public static function create()
{
return [
'form' => self::getFormData(),
'model' => self::getModelData(),
];
}
public static function show($id)
{
return Ponto::find($id);
}
public static function update($id)
{
if (!request()->casa_paciente) {
self::validate();
}
return Ponto::where('id', $id)->update(request()->all());
}
public static function bloquear($id)
{
$ponto = self::show($id);
return Ponto::where('id', $id)->update(['bloqueado' => $ponto->bloqueado == 1 ? 0 : 1]);
}
public static function edit($id)
{
return [
'form' => self::getFormData(),
'model' => self::getModelData(self::show($id)),
];
}
public static function store()
{
if (!request()->casa_paciente) {
self::validate();
}
return Ponto::create(request()->all())->fresh();
}
public static function destroy($id)
{
try {
return self::show($id)->delete();
} catch (\Throwable $th) {
throw new DeletingModelException();
}
}
public static function datatable()
{
$pontos = DB::table('transporte_paciente_ponto')
->leftJoin(
'cadastro_logradouro',
'cadastro_logradouro.id',
'=',
'transporte_paciente_ponto.logradouro_id'
)
->leftJoin(
'intranet.cadastro_municipio',
'intranet.cadastro_municipio.id',
'=',
'cadastro_logradouro.municipio_id'
)
->leftJoin(
'intranet.cadastro_estado',
'intranet.cadastro_estado.id',
'=',
'cadastro_municipio.estado_id'
)
->leftJoin(
'cadastro_bairro',
'cadastro_bairro.id',
'=',
'cadastro_logradouro.bairro_id'
)
->leftJoin(
'cadastro_logradouro_tipo',
'cadastro_logradouro_tipo.id',
'=',
'cadastro_logradouro.tipo_id'
)
->select(
'transporte_paciente_ponto.id',
DB::raw("
CASE WHEN transporte_paciente_ponto.casa_paciente THEN
'CASA DO PACIENTE'
ELSE
transporte_paciente_ponto.descricao
END AS descricao
"),
DB::raw("COALESCE(cadastro_logradouro.nome, '-') AS logradouro"),
DB::raw("
CASE WHEN transporte_paciente_ponto.casa_paciente THEN
'-'
ELSE
COALESCE(transporte_paciente_ponto.numero, 'SEM NÚMERO')
END AS numero
"),
'transporte_paciente_ponto.casa_paciente',
'transporte_paciente_ponto.bloqueado',
DB::raw("COALESCE(cadastro_bairro.nome, '-') AS bairro"),
DB::raw("COALESCE(intranet.cadastro_municipio.nome, '-') AS municipio"),
DB::raw("COALESCE(intranet.cadastro_estado.nome, '-') AS estado"),
DB::raw("COALESCE(cadastro_logradouro_tipo.descricao, '-') AS tipo"),
DB::raw("
CASE WHEN transporte_paciente_ponto.bloqueado THEN
'INATIVO'
ELSE
'ATIVO'
END AS status
"),
);
return datatables()
->of($pontos)
->filterColumn('transporte_paciente_ponto.bloqueado', function ($query, $keyword) {
$query->whereRaw("
CASE WHEN transporte_paciente_ponto.bloqueado THEN
'INATIVO'
ELSE
'ATIVO'
END LIKE ?", ["%{$keyword}%"]
);
})
->filterColumn('transporte_paciente_ponto.numero', function ($query, $keyword) {
$query
->WhereRaw(
"CASE WHEN transporte_paciente_ponto.casa_paciente THEN
'-'
ELSE
COALESCE(transporte_paciente_ponto.numero, 'SEM NÚMERO')
END like ?",
["%{$keyword}%"]
);
});
}
private static function validate()
{
return request()->validate(
[
'descricao' => [
'required',
'min:5',
'max:100',
Rule::unique('transporte_paciente_ponto')
->ignore(request()->ponto)
],
'logradouro_id' => [
'required',
'integer',
],
],
[
'descricao.unique' => 'O ponto já foi inserido anteriormente.',
'logradouro_id.required' => 'O logradouro é obrigatório.',
'descricao.required' => 'A descrição é obrigatória.',
]
);
}
}