150 lines
4.7 KiB
PHP
150 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Setting;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\ReferenceUnit;
|
|
use App\Rules\UniqueInSchema;
|
|
use App\Services\Setting\UnitService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controllers\HasMiddleware;
|
|
use Illuminate\Routing\Controllers\Middleware;
|
|
use Yajra\DataTables\DataTables;
|
|
|
|
class UnitController implements HasMiddleware
|
|
{
|
|
protected $title = 'Unit';
|
|
protected $template = 'modules.setting.unit';
|
|
protected $route = 'modules.pengaturan.unit';
|
|
protected $unitService;
|
|
|
|
public function __construct(UnitService $unitService)
|
|
{
|
|
$this->unitService = $unitService;
|
|
}
|
|
|
|
public static function middleware(): array
|
|
{
|
|
return [
|
|
//new Middleware('permission:/setting/unit'),
|
|
];
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
|
|
// Pass the list of categories to the view
|
|
$data['categories'] = $this->unitService->getCategories();
|
|
$data['route'] = $this->route;
|
|
$data['title'] = $this->title;
|
|
|
|
return view($this->template.'.index',$data);
|
|
}
|
|
|
|
|
|
public function create()
|
|
{
|
|
$categories = $this->unitService->getCategories();
|
|
return view($this->template.'.create', compact('categories'));
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'category' => 'required',
|
|
'code' => [
|
|
'required',
|
|
new UniqueInSchema('reference', 'unit', 'code')
|
|
],
|
|
'name' => 'required',
|
|
'description' => 'nullable',
|
|
'row_num' => 'required|numeric',
|
|
]);
|
|
|
|
try {
|
|
$this->unitService->create($request->all());
|
|
|
|
return redirect()->route($this->route.'.index')->with('success', 'Unit berhasil ditambahkan.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'Unit gagal disimpan. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function edit($id)
|
|
{
|
|
$unit = $this->unitService->find($id);
|
|
$categories = $this->unitService->getCategories();
|
|
return view($this->template.'.edit', compact('unit', 'categories'));
|
|
}
|
|
|
|
public function update(Request $request, ReferenceUnit $unit)
|
|
{
|
|
$request->validate([
|
|
'category' => 'required',
|
|
'code' => [
|
|
'required',
|
|
new UniqueInSchema('reference', 'unit', 'code', $unit->id)
|
|
],
|
|
'name' => 'required',
|
|
'description' => 'nullable',
|
|
'row_num' => 'required|numeric',
|
|
]);
|
|
|
|
try {
|
|
$this->unitService->update($unit, $request->all());
|
|
|
|
return redirect()->route($this->route.'.index')->with('success', 'Unit berhasil diperbarui.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'Unit gagal diperbarui. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function destroy(ReferenceUnit $unit)
|
|
{
|
|
try {
|
|
$this->unitService->delete($unit);
|
|
|
|
return redirect()->route($this->route.'.index')->with('success', 'Unit berhasil dihapus.');
|
|
} catch (\Exception $e) {
|
|
return back()->withErrors(['error' => 'Unit gagal dihapus. Silakan coba lagi. Error: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function grid(Request $request)
|
|
{
|
|
$units = $this->unitService->getAll();
|
|
|
|
// Apply category filter if a category is selected
|
|
if ($request->has('category') && $request->category != '' && $request->category != 'SEMUA KATEGORI') {
|
|
$units = $units->where('category', $request->category);
|
|
}
|
|
|
|
$data = $units;
|
|
$_data = [];
|
|
|
|
foreach ($data as $key => $row) {
|
|
|
|
$btn = '<a href="' . route($this->route.'.edit', $row->id) . '" class="btn btn-sm btn-primary">Edit</a>';
|
|
$btn .= ' <form method="POST" action="' . route($this->route.'.destroy', $row->id) . '" style="display: inline">';
|
|
$btn .= csrf_field();
|
|
$btn .= method_field('DELETE');
|
|
$btn .= '<button type="submit" class="btn btn-sm btn-danger" onclick="return confirm(\'Are you sure?\')">Delete</button>';
|
|
$btn .= '</form>';
|
|
|
|
$_data[] = [
|
|
'no' => $key+1,
|
|
'no_baris' => @$row->row_num,
|
|
'code' => $row->code,
|
|
'name' => $row->name,
|
|
'description' => $row->description,
|
|
'no_baris' => $row->row_num,
|
|
'category' => $row->category,
|
|
'action' => @$btn,
|
|
];
|
|
|
|
}
|
|
|
|
return response()->json($_data);
|
|
}
|
|
}
|