147 lines
4.5 KiB
PHP
147 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cadastro;
|
|
|
|
use App\Models\Cadastro\OrigemProgramatica;
|
|
use App\Services\Service;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class OrigemProgramaticaService extends Service
|
|
{
|
|
public static function create()
|
|
{
|
|
return [
|
|
'form' => self::getFormData(),
|
|
'model' => self::getModelData(),
|
|
];
|
|
}
|
|
|
|
public static function store($request)
|
|
{
|
|
self::validate($request);
|
|
|
|
return OrigemProgramatica::create($request->all())->fresh();
|
|
}
|
|
|
|
public static function show($id)
|
|
{
|
|
return OrigemProgramatica::find($id);
|
|
}
|
|
|
|
public static function edit($id)
|
|
{
|
|
return [
|
|
'form' => self::getFormData(),
|
|
'model' => self::getModelData($id),
|
|
];
|
|
}
|
|
|
|
public static function update($request, $id)
|
|
{
|
|
self::validate($request);
|
|
|
|
return self::show($id)->update($request->all());
|
|
}
|
|
|
|
public static function destroy($id)
|
|
{
|
|
return self::show($id)->delete();
|
|
}
|
|
|
|
public static function datatable()
|
|
{
|
|
return DB::table('cadastro_origem_programatica')
|
|
->select([
|
|
'cadastro_origem_programatica.id',
|
|
'cadastro_origem_programatica_programa_tipo.nome AS programa_tipo_nome',
|
|
'cadastro_origem_programatica_fornecimento_tipo.nome AS fornecimento_tipo_nome',
|
|
'cadastro_origem_programatica_estrutura_apoio.nome AS estrutura_apoio_nome',
|
|
'cadastro_origem_programatica.nome',
|
|
DB::raw("CASE WHEN cadastro_origem_programatica.ativo THEN 'Sim' ELSE 'Não' END AS ativo"),
|
|
])
|
|
->join('cadastro_origem_programatica_programa_tipo', 'cadastro_origem_programatica_programa_tipo.id', '=', 'cadastro_origem_programatica.programa_tipo_id')
|
|
->join('cadastro_origem_programatica_fornecimento_tipo', 'cadastro_origem_programatica_fornecimento_tipo.id', '=', 'cadastro_origem_programatica.fornecimento_tipo_id')
|
|
->join('cadastro_origem_programatica_estrutura_apoio', 'cadastro_origem_programatica_estrutura_apoio.id', '=', 'cadastro_origem_programatica.estrutura_apoio_id');
|
|
}
|
|
|
|
public static function autocomplete()
|
|
{
|
|
return DB::table('cadastro_origem_programatica')
|
|
->select([
|
|
'id',
|
|
'nome',
|
|
])
|
|
->where('ativo', true);
|
|
}
|
|
|
|
private static function getFormData()
|
|
{
|
|
return [
|
|
'programa_tipos' => DB::table('cadastro_origem_programatica_programa_tipo')
|
|
->select([
|
|
'id',
|
|
'nome',
|
|
])
|
|
->orderBy('nome')
|
|
->get(),
|
|
'fornecimento_tipos' => DB::table('cadastro_origem_programatica_fornecimento_tipo')
|
|
->select([
|
|
'id',
|
|
'nome',
|
|
])
|
|
->orderBy('nome')
|
|
->get(),
|
|
'estruturas_apoio' => DB::table('cadastro_origem_programatica_estrutura_apoio')
|
|
->select([
|
|
'id',
|
|
'nome',
|
|
])
|
|
->orderBy('nome')
|
|
->get(),
|
|
];
|
|
}
|
|
|
|
private static function getModelData($id = null)
|
|
{
|
|
$origemProgramatica = self::show($id);
|
|
|
|
return [
|
|
'id' => $origemProgramatica->id ?? null,
|
|
'programa_tipo_id' => $origemProgramatica->programa_tipo_id ?? null,
|
|
'fornecimento_tipo_id' => $origemProgramatica->fornecimento_tipo_id ?? null,
|
|
'estrutura_apoio_id' => $origemProgramatica->estrutura_apoio_id ?? null,
|
|
'nome' => $origemProgramatica->nome ?? null,
|
|
'ativo' => $origemProgramatica->ativo ?? true,
|
|
];
|
|
}
|
|
|
|
private static function validate($request)
|
|
{
|
|
return $request->validate([
|
|
'nome' => [
|
|
'required',
|
|
'string',
|
|
'between:3,300',
|
|
Rule::unique('cadastro_origem_programatica')->ignore($request->origem_programatica),
|
|
],
|
|
'programa_tipo_id' => [
|
|
'required',
|
|
'integer',
|
|
],
|
|
'fornecimento_tipo_id' => [
|
|
'required',
|
|
'integer',
|
|
],
|
|
'estrutura_apoio_id' => [
|
|
'required',
|
|
'integer',
|
|
],
|
|
'ativo' => [
|
|
'required',
|
|
'boolean',
|
|
],
|
|
]);
|
|
}
|
|
}
|