142 lines
3.6 KiB
PHP
142 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Parametrizacao\KitExame;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Parametrizacao\KitExame\ItemRequest;
|
|
use App\Models\Parametrizacao\KitExame\Item;
|
|
use App\Models\Parametrizacao\KitExame\KitExame;
|
|
use App\Models\Parametrizacao\KitExame\Tipo;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class KitExameController extends Controller
|
|
{
|
|
protected $modulo = 'parametrizacao';
|
|
protected $funcionalidade = 'kit-exame';
|
|
|
|
public function datatable()
|
|
{
|
|
$kits = DB::table('parametrizacao_kit_exame')
|
|
->join(
|
|
'parametrizacao_kit_exame_tipo',
|
|
'parametrizacao_kit_exame_tipo.id',
|
|
'parametrizacao_kit_exame.tipo_id'
|
|
)
|
|
->select(
|
|
'parametrizacao_kit_exame.id',
|
|
'parametrizacao_kit_exame.nome',
|
|
'parametrizacao_kit_exame_tipo.nome as tipo'
|
|
)
|
|
->whereNull('parametrizacao_kit_exame.deleted_at');
|
|
|
|
return datatables()->of($kits)->skipTotalRecords()->toJson();
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
$kits = $this->get(KitExame::class)->orderBy('nome')->get();
|
|
|
|
return response()->json($kits, Response::HTTP_OK);
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
$kit = new KitExame;
|
|
$kit->fill(array_fill_keys($kit->getFillable(), null));
|
|
|
|
return response()->json(
|
|
[
|
|
'form' => $this->getFormData(),
|
|
'model' => $kit->makeHidden('id')->load('itens')
|
|
],
|
|
Response::HTTP_OK
|
|
);
|
|
}
|
|
|
|
public function store(ItemRequest $request)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
$kit = new KitExame;
|
|
$params = $request->all();
|
|
|
|
DB::transaction(function () use ($kit, $params) {
|
|
|
|
$kit->fill($params)->save();
|
|
$kit->itens()->createMany($params['itens']);
|
|
});
|
|
|
|
return response()->json($kit->fresh(), Response::HTTP_CREATED);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
return response()->json($this->get(KitExame::class, $id), Response::HTTP_OK);
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
$kit = KitExame::findOrFail($id);
|
|
|
|
$kit->itens->each(function ($item) {
|
|
|
|
$item->exame->procedimento_nome = $item->exame->procedimento->nome ?? null;
|
|
|
|
$item->exame->unsetRelation('tipo')->unsetRelation('procedimento');
|
|
$item->unsetRelation('kit');
|
|
});
|
|
|
|
return response()->json(
|
|
[
|
|
'form' => $this->getFormData(),
|
|
'model' => $kit->makeHidden('id')->unsetRelation('tipo'),
|
|
],
|
|
Response::HTTP_OK
|
|
);
|
|
}
|
|
|
|
private function getFormData()
|
|
{
|
|
return [
|
|
'tipos' => Tipo::orderBy('nome')->get()
|
|
];
|
|
}
|
|
|
|
public function update(ItemRequest $request, $id)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
$kit = KitExame::findOrFail($id);
|
|
$params = $request->only(['nome', 'itens']);
|
|
|
|
DB::transaction(function () use ($kit, $params) {
|
|
|
|
$kit->update($params);
|
|
|
|
Item::destroy($kit->itens->pluck('id'));
|
|
|
|
$kit->itens()->createMany($params['itens']);
|
|
});
|
|
|
|
return response()->json($kit->fresh(), Response::HTTP_CREATED);
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$this->auth(__FUNCTION__);
|
|
|
|
KitExame::findOrFail($id)->delete();
|
|
|
|
return response()->json(null, Response::HTTP_NO_CONTENT);
|
|
}
|
|
}
|