init commit
All checks were successful
build-api-image / build (push) Successful in 51s

This commit is contained in:
Antonio Lopes dos Santos
2026-08-04 16:52:02 -03:00
commit afec302c3e
11594 changed files with 1636728 additions and 0 deletions

View File

@@ -0,0 +1,265 @@
<?php
namespace App\Services\Cadastro;
use App\Exceptions\Cadastro\UnidadeManutencao\InfraestruturaExistenteException;
use App\Models\Cadastro\UnidadeExecutanteManutencao;
use App\Services\Service;
use App\Utils\MaskUtil;
use DB;
class UnidadeExecutanteManutencaoService extends Service
{
public static function datatable()
{
$unidades = DB::table('cadastro_unidade_executante_manutencao as unidade_executante_manutencao')
->leftJoin('cadastro_fornecedor', function ($join) {
$join->on('unidade_executante_manutencao.fornecedor_id', 'cadastro_fornecedor.id')
->where('unidade_executante_manutencao.is_prestador', 1);
})
->leftJoin('cadastro_infraestrutura', function ($join) {
$join->on('unidade_executante_manutencao.infraestrutura_id', 'cadastro_infraestrutura.id')
->where('unidade_executante_manutencao.is_prestador', '!=', 1);
})
->leftJoin('cadastro_unidade', 'cadastro_infraestrutura.unidade_id', 'cadastro_unidade.id')
->leftJoin('cadastro_usuario', 'unidade_executante_manutencao.usuario_id', 'cadastro_usuario.id')
->leftJoin('cadastro_cidadao', 'cadastro_usuario.cidadao_id', 'cadastro_cidadao.id')
->select(
'unidade_executante_manutencao.id',
DB::raw('
CASE WHEN
unidade_executante_manutencao.is_prestador = 1
THEN
"PRESTADOR" ELSE "UNIDADE"
END as is_prestador
'),
'unidade_executante_manutencao.status',
DB::raw('
COALESCE(
cadastro_fornecedor.nome_fantasia,
cadastro_unidade.nome_fantasia
) as nome
'),
DB::raw('
COALESCE(
cadastro_fornecedor.id,
cadastro_unidade.id
) as unidade_prestador_id
'),
DB::raw('
CASE WHEN
cadastro_cidadao.nome IS NOT NULL
THEN
cadastro_cidadao.nome ELSE "NÃO INFORMADO"
END as usuario_nome
'),
DB::raw('
CASE
WHEN EXISTS (
SELECT 1
FROM cadastro_servico_manutencao_prestador_fornecedor
WHERE cadastro_servico_manutencao_prestador_fornecedor.unidade_executante_manutencao_id = unidade_executante_manutencao.id
)
THEN TRUE
ELSE FALSE
END as allow_destroy
'),
DB::raw('
CASE WHEN
unidade_executante_manutencao.status = 1
THEN
TRUE ELSE FALSE
END as allow_disable
'),
'cadastro_infraestrutura.telefone',
)
->orderBy('nome')
->get();
$unidades->map(function ($item) {
$item->telefone = MaskUtil::getPhoneMasked($item->telefone) ?? 'NÃO INFORMADO';
});
return $unidades;
}
public static function create()
{
return [
'model' => [
'unidade_prestador_id' => null,
'usuario_id' => null,
'is_prestador' => false,
'is_profissional_executante' => false
]
];
}
public static function store()
{
self::validate();
$data = self::prepareSave(request()->all());
if ($data['is_prestador']) {
$checaUnidade = UnidadeExecutanteManutencao::where('fornecedor_id', $data['fornecedor_id'])->first();
} else {
$checaUnidade = UnidadeExecutanteManutencao::where('infraestrutura_id', $data['infraestrutura_id'])->first();
}
if (!is_null($checaUnidade) && is_null($checaUnidade->usuario_id) && is_null($data['usuario_id'])) {
throw new InfraestruturaExistenteException;
}
$unidadeExecutanteManutencao = UnidadeExecutanteManutencao::create($data);
return $unidadeExecutanteManutencao->fresh();
}
public static function edit($id)
{
$unidadeExecutanteManutencao = UnidadeExecutanteManutencao::findOrFail($id);
if ($unidadeExecutanteManutencao->is_prestador) {
$unidadePrestador = (object) [
'id' => $unidadeExecutanteManutencao->id,
'nome_fantasia' => $unidadeExecutanteManutencao->getFornecedor->nome_fantasia
];
} else {
$unidadePrestador = (object) [
'id' => $unidadeExecutanteManutencao->getInfraestrutura->id,
'nome_fantasia' => $unidadeExecutanteManutencao->getInfraestrutura->unidade->nome_fantasia
];
}
if ($unidadeExecutanteManutencao->usuario_id) {
$usuario = $unidadeExecutanteManutencao->getUsuario()
->leftJoin('cadastro_cidadao as cidadao', 'cidadao.id', 'cadastro_usuario.cidadao_id')
->select('cadastro_usuario.id', 'cidadao.nome as cidadao_nome')
->first();
$usuario = (object) ['id' => $usuario->id, 'cidadao_nome' => $usuario->cidadao_nome];
$isProfissionalExecutante = true;
} else {
$isProfissionalExecutante = false;
}
return [
'model' => [
'unidade_prestador_id' => $unidadePrestador,
'usuario_id' => $usuario ?? null,
'is_prestador' => $unidadeExecutanteManutencao->is_prestador,
'is_profissional_executante' => $isProfissionalExecutante,
]
];
}
public static function update($id)
{
self::validate();
$registro = UnidadeExecutanteManutencao::findOrFail($id);
$data = self::prepareSave(request()->all());
if (request()->has('status')) {
$registro->update(['status' => $data['status']]);
return;
} else {
$unidadeExecutanteManutencao = tap($registro)->update($data);
}
return $unidadeExecutanteManutencao->fresh();
}
public static function prepareSave($data)
{
$dataStore = [];
if (isset($data['status'])) {
$dataStore['status'] = (bool) $data['status'];
} else {
if ($data['is_prestador']) {
$dataStore['fornecedor_id'] = $data['unidade_prestador_id']['id'];
} else {
$dataStore['infraestrutura_id'] = $data['unidade_prestador_id']['id'];
}
$dataStore['usuario_id'] = $data['usuario_id']['id'] ?? null;
$dataStore['is_prestador'] = (bool) $data['is_prestador'];
}
return $dataStore;
}
public static function validate()
{
$rule = [
'unidade_prestador_id' => [
'required',
],
];
if (request()->has('status')) {
$rule['unidade_prestador_id'] = 'nullable';
}
request()->validate($rule);
}
public static function destroy($id)
{
UnidadeExecutanteManutencao::findOrFail($id)->delete();
return null;
}
public static function autocompleteUnidade()
{
$unidades = DB::table('cadastro_unidade as unidade')
->join('cadastro_infraestrutura', 'cadastro_infraestrutura.unidade_id', 'unidade.id')
->whereNull('unidade.deleted_at')
->where('cadastro_infraestrutura.ativo', 1)
->distinct()
->selectRaw("
cadastro_infraestrutura.id,
unidade.nome_fantasia
")
->groupBy(
'unidade.nome_fantasia',
);
return $unidades;
}
public static function autocompleteFornecedor()
{
$fornecedor = DB::table('cadastro_fornecedor as fornecedor')
->distinct()
->selectRaw("
fornecedor.id,
fornecedor.nome_fantasia
")
->groupBy(
'fornecedor.nome_fantasia',
);
return $fornecedor;
}
}