128 lines
4.0 KiB
PHP
128 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\PainelSenha;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\PainelSenha\EncaminhamentoRequest;
|
|
use App\Models\Cadastro\Estabelecimento\CentroCusto;
|
|
use App\Models\PainelSenha\Encaminhamento;
|
|
use Illuminate\Http\Response;
|
|
|
|
class EncaminhamentoController extends Controller
|
|
{
|
|
protected $modulo = 'painel-senha';
|
|
protected $funcionalidade = 'encaminhamento';
|
|
|
|
public function index()
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
$where = $this->getWhereFromQueryString([
|
|
'centro_custo_id_de'
|
|
]);
|
|
|
|
$encaminhamentos = $this->get(Encaminhamento::class)
|
|
->where($where)
|
|
->whereHas('centroCustoDe', function($query) {
|
|
$query->where('infraestrutura_id', $this->getCentroCustoLogado()->infraestrutura_id);
|
|
})
|
|
->whereHas('centroCustoPara', function($query) {
|
|
$query->where('infraestrutura_id', $this->getCentroCustoLogado()->infraestrutura_id);
|
|
})
|
|
->get()
|
|
->sortBy('centroCustoDe.setor.nome')
|
|
->values();
|
|
|
|
return response()->json($encaminhamentos, Response::HTTP_OK);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
return response()->json(
|
|
[
|
|
'form' => [
|
|
'setores' => CentroCusto::where('infraestrutura_id', $this->getCentroCustoLogado()->infraestrutura_id)
|
|
->get()
|
|
->map(function($query) {
|
|
return [
|
|
'id' => $query->setor->id,
|
|
'centro_custo_id' => $query->id,
|
|
'nome' => $query->setor->nome,
|
|
];
|
|
})->sortBy('nome')->values(),
|
|
],
|
|
'model' => [
|
|
'id' => null,
|
|
'centroCustoIdDe' => null,
|
|
'centroCustoIdPara' => null,
|
|
]
|
|
],
|
|
Response::HTTP_OK
|
|
);
|
|
}
|
|
|
|
public function store(EncaminhamentoRequest $request)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
return response()->json(Encaminhamento::create($request->all())->fresh(), Response::HTTP_CREATED);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
return response()->json($this->get(Encaminhamento::class, $id), Response::HTTP_OK);
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
$encaminhamento = Encaminhamento::findOrFail($id);
|
|
|
|
return response()->json(
|
|
[
|
|
'form' => [
|
|
'setores' => CentroCusto::where('infraestrutura_id', $this->getCentroCustoLogado()->infraestrutura_id)
|
|
->get()
|
|
->map(function($query) {
|
|
return [
|
|
'id' => $query->setor->id,
|
|
'centro_custo_id' => $query->id,
|
|
'nome' => $query->setor->nome,
|
|
];
|
|
})->sortBy('nome')->values(),
|
|
],
|
|
'model' => [
|
|
'id' => $encaminhamento->id,
|
|
'centro_custo_id_de' => $encaminhamento->centro_custo_id_de,
|
|
'centro_custo_id_para' => $encaminhamento->centro_custo_id_para,
|
|
],
|
|
],
|
|
Response::HTTP_OK
|
|
);
|
|
}
|
|
|
|
public function update(EncaminhamentoRequest $request, $id)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
return response()->json(tap(Encaminhamento::findOrFail($id))->update($request->all()), Response::HTTP_OK);
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
return response()->json(Encaminhamento::findOrFail($id)->delete(), Response::HTTP_NO_CONTENT);
|
|
}
|
|
|
|
private function getCentroCustoLogado()
|
|
{
|
|
return CentroCusto::findOrFail(auth()->payload()->get('centro_custo_id'));
|
|
}
|
|
}
|