This commit is contained in:
260
api/app/Services/TransportePaciente/Condutor/CondutorService.php
Normal file
260
api/app/Services/TransportePaciente/Condutor/CondutorService.php
Normal file
@@ -0,0 +1,260 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\TransportePaciente\Condutor;
|
||||
|
||||
use App\Exceptions\Model\DeletingModelException;
|
||||
use App\Models\Cadastro\SujeitoAtencao\CarteiraHabilitacaoCategoria;
|
||||
use App\Models\TransportePaciente\Condutor\Condutor;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class CondutorService extends Service
|
||||
{
|
||||
private static function getCarteiraHabilitacaoCategoria()
|
||||
{
|
||||
return CarteiraHabilitacaoCategoria::all();
|
||||
}
|
||||
|
||||
private static function getUsuariosCondutores($condutor = null)
|
||||
{
|
||||
if ($condutor) {
|
||||
|
||||
return DB::table('cadastro_usuario')
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
|
||||
->join('cadastro_lotacao', 'cadastro_lotacao.usuario_id', 'cadastro_usuario.id')
|
||||
->whereNull('cadastro_lotacao.deleted_at')
|
||||
->whereIn(
|
||||
'cadastro_usuario.id',
|
||||
DB::table('transporte_paciente_condutor')
|
||||
->where(
|
||||
'usuario_id',
|
||||
$condutor->usuario_id
|
||||
)
|
||||
->select('transporte_paciente_condutor.usuario_id as id')
|
||||
->get()
|
||||
->pluck('id')
|
||||
)
|
||||
->whereIn(
|
||||
'cadastro_lotacao.ocupacao_id',
|
||||
DB::table('sigtap.ocupacao')
|
||||
->where(
|
||||
'sigtap.ocupacao.nome',
|
||||
'like',
|
||||
'MOTORISTA%'
|
||||
)
|
||||
->orWhere(
|
||||
'sigtap.ocupacao.nome',
|
||||
'like',
|
||||
'CONDUTOR%'
|
||||
)
|
||||
->select('sigtap.ocupacao.id')
|
||||
->get()
|
||||
->pluck('id')
|
||||
)
|
||||
->select([
|
||||
'cadastro_usuario.id',
|
||||
'cadastro_cidadao.nome'
|
||||
])
|
||||
->groupBy('cadastro_usuario.id')
|
||||
->orderby('nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
return DB::table('cadastro_usuario')
|
||||
->join('cadastro_cidadao', 'cadastro_cidadao.id', 'cadastro_usuario.cidadao_id')
|
||||
->join('cadastro_lotacao', 'cadastro_lotacao.usuario_id', 'cadastro_usuario.id')
|
||||
->whereNull('cadastro_lotacao.deleted_at')
|
||||
->whereNotIn(
|
||||
'cadastro_usuario.id',
|
||||
DB::table('transporte_paciente_condutor')
|
||||
->select('transporte_paciente_condutor.usuario_id as id')
|
||||
->get()
|
||||
->pluck('id')
|
||||
)
|
||||
->whereIn(
|
||||
'cadastro_lotacao.ocupacao_id',
|
||||
DB::table('sigtap.ocupacao')
|
||||
->where(
|
||||
'sigtap.ocupacao.nome',
|
||||
'like',
|
||||
'MOTORISTA%'
|
||||
)
|
||||
->orWhere(
|
||||
'sigtap.ocupacao.nome',
|
||||
'like',
|
||||
'CONDUTOR%'
|
||||
)
|
||||
->select('sigtap.ocupacao.id')
|
||||
->get()
|
||||
->pluck('id')
|
||||
)
|
||||
->select([
|
||||
'cadastro_usuario.id',
|
||||
'cadastro_cidadao.nome',
|
||||
])
|
||||
->groupBy('cadastro_usuario.id')
|
||||
->orderby('nome')
|
||||
->get();
|
||||
}
|
||||
|
||||
private static function getFormData($condutor = null)
|
||||
{
|
||||
return [
|
||||
'condutores' => self::getUsuariosCondutores($condutor),
|
||||
'categorias' => self::getCarteiraHabilitacaoCategoria(),
|
||||
];
|
||||
}
|
||||
|
||||
private static function getModelData($condutor = null)
|
||||
{
|
||||
return [
|
||||
'id' => $condutor->id ?? null,
|
||||
'usuario_id' => $condutor->usuario_id ?? null,
|
||||
'categoria_cnh_id' => $condutor->categoria_cnh_id ?? null,
|
||||
'numero_cnh' => $condutor->numero_cnh ?? null,
|
||||
'bloqueado' => $condutor->bloqueado ?? null,
|
||||
'vencimento_habilitacao' => $condutor->vencimento_habilitacao ?? null,
|
||||
'matricula' => $condutor->matricula ?? null
|
||||
];
|
||||
}
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return [
|
||||
'form' => self::getFormData(),
|
||||
'model' => self::getModelData(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function show($id)
|
||||
{
|
||||
return Condutor::find($id);
|
||||
}
|
||||
|
||||
public static function update($request, $id)
|
||||
{
|
||||
self::validate($request);
|
||||
|
||||
return Condutor::where('id', $id)->update($request->all());
|
||||
}
|
||||
|
||||
public static function bloquear($id)
|
||||
{
|
||||
$condutor = self::show($id);
|
||||
|
||||
return Condutor::where('id', $id)->update(['bloqueado' => $condutor->bloqueado == 1 ? 0 : 1]);
|
||||
}
|
||||
|
||||
public static function edit($id)
|
||||
{
|
||||
$condutor = self::show($id);
|
||||
|
||||
return [
|
||||
'form' => self::getFormData($condutor),
|
||||
'model' => self::getModelData($condutor),
|
||||
];
|
||||
}
|
||||
|
||||
public static function store($request)
|
||||
{
|
||||
self::validate($request);
|
||||
|
||||
return Condutor::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()
|
||||
{
|
||||
$condutores = DB::table('transporte_paciente_condutor')
|
||||
->join(
|
||||
'cadastro_carteira_habilitacao_categoria',
|
||||
'transporte_paciente_condutor.categoria_cnh_id',
|
||||
'=',
|
||||
'cadastro_carteira_habilitacao_categoria.id'
|
||||
)->join(
|
||||
'cadastro_usuario',
|
||||
'transporte_paciente_condutor.usuario_id',
|
||||
'=',
|
||||
'cadastro_usuario.id'
|
||||
)->join(
|
||||
'cadastro_cidadao',
|
||||
'cadastro_usuario.cidadao_id',
|
||||
'=',
|
||||
'cadastro_cidadao.id'
|
||||
)->select(
|
||||
'cadastro_cidadao.nome',
|
||||
'transporte_paciente_condutor.id',
|
||||
'transporte_paciente_condutor.usuario_id',
|
||||
'transporte_paciente_condutor.numero_cnh',
|
||||
'transporte_paciente_condutor.categoria_cnh_id',
|
||||
'transporte_paciente_condutor.bloqueado',
|
||||
'cadastro_carteira_habilitacao_categoria.letra as categoria',
|
||||
DB::raw("
|
||||
CASE WHEN transporte_paciente_condutor.bloqueado THEN
|
||||
'INATIVO'
|
||||
ELSE
|
||||
'ATIVO'
|
||||
END AS status
|
||||
"),
|
||||
DB::raw("DATE_FORMAT(transporte_paciente_condutor.vencimento_habilitacao, '%d/%m/%Y') AS vencimento_habilitacao"),
|
||||
);
|
||||
|
||||
return datatables()
|
||||
->of($condutores)
|
||||
->filterColumn('transporte_paciente_condutor.bloqueado', function ($query, $keyword) {
|
||||
|
||||
$query->whereRaw("
|
||||
CASE WHEN transporte_paciente_condutor.bloqueado THEN
|
||||
'INATIVO'
|
||||
ELSE
|
||||
'ATIVO'
|
||||
END LIKE ?", ["%{$keyword}%"]
|
||||
);
|
||||
})
|
||||
->filterColumn('transporte_paciente_condutor.vencimento_habilitacao', function ($query, $keyword) {
|
||||
$query
|
||||
->whereRaw("DATE_FORMAT(transporte_paciente_condutor.vencimento_habilitacao, '%d/%m/%Y') LIKE ?", ["%{$keyword}%"])
|
||||
->orWhereRaw(
|
||||
"CONCAT(
|
||||
SUBSTR(transporte_paciente_condutor.vencimento_habilitacao,9,2),
|
||||
SUBSTR(transporte_paciente_condutor.vencimento_habilitacao,6,2),
|
||||
SUBSTR(transporte_paciente_condutor.vencimento_habilitacao,1,4)
|
||||
) like ?",
|
||||
["%{$keyword}%"]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private static function validate($request)
|
||||
{
|
||||
return $request->validate([
|
||||
'usuario_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
Rule::unique('transporte_paciente_condutor')
|
||||
->ignore($request->condutor)
|
||||
],
|
||||
'numero_cnh' => [
|
||||
'required',
|
||||
'String',
|
||||
],
|
||||
'categoria_cnh_id' => [
|
||||
'required',
|
||||
'integer',
|
||||
],
|
||||
'vencimento_habilitacao' => [
|
||||
'required',
|
||||
'date',
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user