113 lines
2.8 KiB
PHP
113 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Cadastro;
|
|
|
|
use App\Exceptions\Cadastro\UnidadeExterna\DuplicatedNomeFantasiaException;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Cadastro\Estabelecimento\UnidadeExternaRequest;
|
|
use App\Models\Cadastro\UnidadeExterna;
|
|
use Illuminate\Http\Response;
|
|
|
|
class UnidadeExternaController extends Controller
|
|
{
|
|
protected $modulo = 'cadastro';
|
|
protected $funcionalidade = 'unidade-externa';
|
|
|
|
public function autocomplete()
|
|
{
|
|
$unidadesExterna = UnidadeExterna::with([]);
|
|
|
|
return datatables()->of($unidadesExterna)->toJson();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
$unidades = $this->get(UnidadeExterna::class)->orderBy('nome_fantasia')->get();
|
|
|
|
return response()->json($unidades, Response::HTTP_OK);
|
|
}
|
|
|
|
public function store(UnidadeExternaRequest $request)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
$params = $this->getParams($request);
|
|
$unidadeExterna = UnidadeExterna::where('nome_fantasia', $params['nome_fantasia'])->first();
|
|
|
|
if (!is_null($unidadeExterna)) {
|
|
|
|
throw new DuplicatedNomeFantasiaException;
|
|
}
|
|
|
|
return response()->json(UnidadeExterna::create($params)->fresh(), Response::HTTP_CREATED);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
return response()->json($this->get(UnidadeExterna::class, $id), Response::HTTP_OK);
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
$unidadeExterna = UnidadeExterna::findOrFail($id);
|
|
|
|
return response()->json(
|
|
[
|
|
'form' => [],
|
|
'model' => $unidadeExterna
|
|
],
|
|
Response::HTTP_OK
|
|
);
|
|
}
|
|
|
|
public function update(UnidadeExternaRequest $request, $id)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
$params = $this->getParams($request);
|
|
|
|
$unidadeExterna = UnidadeExterna::where('nome_fantasia', $params['nome_fantasia'])
|
|
->where('id', '<>', $id)
|
|
->first();
|
|
|
|
if (!is_null($unidadeExterna)) {
|
|
|
|
throw new DuplicatedNomeFantasiaException;
|
|
}
|
|
|
|
$unidade = tap(UnidadeExterna::findOrFail($id))->update($params);
|
|
|
|
return response()->json($unidade, Response::HTTP_OK);
|
|
}
|
|
|
|
private function getParams($request)
|
|
{
|
|
return [
|
|
'nome_fantasia' => mb_strtoupper($request->nome_fantasia),
|
|
'municipio_id' => $request->municipio_id,
|
|
];
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
UnidadeExterna::findOrFail($id)->delete();
|
|
|
|
return response()->json(null, Response::HTTP_NO_CONTENT);
|
|
}
|
|
|
|
public function datatable()
|
|
{
|
|
$unidadesExterna = UnidadeExterna::with([]);
|
|
|
|
return datatables()->of($unidadesExterna)->toJson();
|
|
}
|
|
}
|