dstpabuaran.com/app/Http/Controllers/Admin/Master/RawMaterial/RawMaterialController.php
Yoga Pangestu b85bbb1ba4 feat: enhance purchase and restock management with variant support
- Added existing_material_ids to PurchaseForEdit and RestockForEdit types.
- Introduced RawMaterialVariant and ProductVariantForRestock types for better variant handling.
- Updated PurchaseCreate and PurchaseEdit components to fetch and display raw material variants.
- Enhanced RestockCreate and RestockEdit components to manage product variants dynamically.
- Modified TransactionCreate and TransactionEdit components to support product variants.
- Added routes for fetching active products and raw materials.
2026-08-20 10:48:24 +07:00

103 lines
3.3 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';
Inertia::flash('toast', ['type' => 'success', 'message' => "Status bahan baku berhasil diubah menjadi {$status}."]);
return 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(),
]);
}
}