101 lines
3.2 KiB
PHP
101 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin\Master\RawMaterial;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\Master\RawMaterial\RawMaterialRequest;
|
|
use App\Http\Requests\PaginatedRequest;
|
|
use App\Models\RawMaterial;
|
|
use App\Services\Admin\Master\RawMaterial\RawMaterialService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
|
|
class RawMaterialController extends Controller
|
|
{
|
|
public function __construct(
|
|
private RawMaterialService $service,
|
|
) {}
|
|
|
|
public function index(PaginatedRequest $request): Response
|
|
{
|
|
return Inertia::render('admin/master/raw-material/index', [
|
|
'rawMaterials' => $this->service->paginated(
|
|
...$request->validatedWithDefaults(),
|
|
filters: $request->only(['is_active', 'stock', 'name']),
|
|
),
|
|
'rawMaterialNames' => $this->service->getNames(),
|
|
'filters' => $request->only(['is_active', 'stock', 'name']),
|
|
'highlight' => $request->input('highlight'),
|
|
]);
|
|
}
|
|
|
|
public function create(): Response
|
|
{
|
|
return Inertia::render('admin/master/raw-material/create');
|
|
}
|
|
|
|
public function store(RawMaterialRequest $request): RedirectResponse
|
|
{
|
|
return $this->handleAction(
|
|
fn () => $this->service->store($request->validated()),
|
|
'Bahan baku berhasil ditambahkan.',
|
|
'admin.master.raw-materials.index',
|
|
'admin.master.raw-materials.create'
|
|
);
|
|
}
|
|
|
|
public function edit(RawMaterial $rawMaterial): Response
|
|
{
|
|
return Inertia::render('admin/master/raw-material/edit', [
|
|
'rawMaterial' => $this->service->getForEdit($rawMaterial),
|
|
]);
|
|
}
|
|
|
|
public function update(RawMaterialRequest $request, RawMaterial $rawMaterial): RedirectResponse
|
|
{
|
|
try {
|
|
$this->service->update($rawMaterial, $request->validated());
|
|
} catch (ValidationException $e) {
|
|
return back()->withErrors($e->errors());
|
|
}
|
|
|
|
Inertia::flash('toast', ['type' => 'success', 'message' => 'Bahan baku berhasil diperbarui.']);
|
|
|
|
return to_route('admin.master.raw-materials.index');
|
|
}
|
|
|
|
public function destroy(RawMaterial $rawMaterial): RedirectResponse
|
|
{
|
|
return $this->handleAction(
|
|
fn () => $this->service->destroy($rawMaterial),
|
|
'Bahan baku berhasil dihapus.',
|
|
'admin.master.raw-materials.index'
|
|
);
|
|
}
|
|
|
|
public function toggleStatus(RawMaterial $rawMaterial): RedirectResponse
|
|
{
|
|
$this->service->toggleStatus($rawMaterial);
|
|
$status = $rawMaterial->fresh()->is_active ? 'Aktif' : 'Non Aktif';
|
|
|
|
return Inertia::flash('toast', ['type' => 'success', 'message' => "Status bahan baku berhasil diubah menjadi {$status}."])->back();
|
|
}
|
|
|
|
public function variants(RawMaterial $rawMaterial): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'variants' => $this->service->getVariants($rawMaterial),
|
|
]);
|
|
}
|
|
|
|
public function activeMaterials(): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'materials' => $this->service->getActiveMaterials(),
|
|
]);
|
|
}
|
|
}
|