where('type', 'kit') ->orWhere('type', 'assembly') ->latest() ->paginate(15); return Inertia::render('MaterialsCatalog::MaterialsCatalog/Index', [ 'kits' => $kits ]); } /** * Show the form for creating a new resource. */ public function create() { $rawMaterials = Material::where('type', 'single')->get(['id', 'name', 'sku', 'unit', 'unit_cost']); return Inertia::render('MaterialsCatalog::MaterialsCatalog/Form', [ 'kit' => null, 'rawMaterials' => $rawMaterials ]); } /** * Store a newly created resource in storage. */ public function store(Request $request) { $validated = $request->validate([ 'name' => 'required|string|max:255', 'sku' => 'nullable|string|unique:materials,sku', 'category' => 'nullable|string', 'description' => 'nullable|string', 'components' => 'required|array|min:1', 'components.*.component_id' => 'required|exists:materials,id', 'components.*.quantity' => 'required|numeric|min:0.001', ]); $calculatedUnitCost = 0; foreach ($validated['components'] as $comp) { $mat = Material::find($comp['component_id']); if ($mat) { $calculatedUnitCost += ($mat->unit_cost * (float)$comp['quantity']); } } $kit = Material::create([ 'name' => $validated['name'], 'sku' => $validated['sku'], 'category' => $validated['category'] ?? 'Kit', 'description' => $validated['description'], 'unit' => 'kit', 'type' => 'kit', 'unit_cost' => $calculatedUnitCost, ]); $components = []; foreach ($validated['components'] as $comp) { $components[] = new \Modules\MasterData\Models\MaterialComponent([ 'component_id' => $comp['component_id'], 'quantity' => $comp['quantity'], ]); } $kit->components()->saveMany($components); return redirect()->route('materials-catalog.kits.index') ->with('success', 'Kit created successfully.'); } /** * Show the specified resource. */ public function show(string $id) { return redirect()->back(); // Or render a separate show page if needed } /** * Show the form for editing the specified resource. */ public function edit(string $id) { $kit = Material::with('components')->findOrFail($id); $rawMaterials = Material::where('type', 'single')->get(['id', 'name', 'sku', 'unit', 'unit_cost']); return Inertia::render('MaterialsCatalog::MaterialsCatalog/Form', [ 'kit' => $kit, 'rawMaterials' => $rawMaterials ]); } /** * Update the specified resource in storage. */ public function update(Request $request, string $id) { $kit = Material::findOrFail($id); $validated = $request->validate([ 'name' => 'required|string|max:255', 'sku' => 'nullable|string|unique:materials,sku,' . $kit->id, 'category' => 'nullable|string', 'description' => 'nullable|string', 'components' => 'required|array|min:1', 'components.*.component_id' => 'required|exists:materials,id', 'components.*.quantity' => 'required|numeric|min:0.001', ]); $calculatedUnitCost = 0; foreach ($validated['components'] as $comp) { $mat = Material::find($comp['component_id']); if ($mat) { $calculatedUnitCost += ($mat->unit_cost * (float)$comp['quantity']); } } $kit->update([ 'name' => $validated['name'], 'sku' => $validated['sku'], 'category' => $validated['category'] ?? 'Kit', 'description' => $validated['description'], 'unit_cost' => $calculatedUnitCost, ]); $kit->components()->delete(); // Clear old components $components = []; foreach ($validated['components'] as $comp) { $components[] = new \Modules\MasterData\Models\MaterialComponent([ 'component_id' => $comp['component_id'], 'quantity' => $comp['quantity'], ]); } $kit->components()->saveMany($components); return redirect()->route('materials-catalog.kits.index') ->with('success', 'Kit updated successfully.'); } /** * Remove the specified resource from storage. */ public function destroy(string $id) { $kit = Material::findOrFail($id); $kit->delete(); return back()->with('success', 'Kit deleted successfully.'); } }