111 lines
3.4 KiB
PHP
111 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Modules\MasterData\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use Modules\MasterData\Models\Material;
|
|
use Modules\MasterData\Models\MaterialGroup;
|
|
|
|
class MaterialGroupController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$query = MaterialGroup::withCount('materials');
|
|
|
|
if ($search = $request->search) {
|
|
$query->where('name', 'like', "%{$search}%");
|
|
}
|
|
|
|
if ($status = $request->status) {
|
|
$query->where('status', $status);
|
|
}
|
|
|
|
$groups = $query->latest()->paginate(15)->withQueryString();
|
|
|
|
$allMaterials = Material::where('status', 'active')
|
|
->select('id', 'ulid', 'name', 'sku', 'unit', 'unit_cost', 'category')
|
|
->get();
|
|
|
|
return Inertia::render('MaterialLogistics::Groups/Index', [
|
|
'groups' => $groups,
|
|
'allMaterials' => $allMaterials,
|
|
'filters' => $request->only(['search', 'status']),
|
|
]);
|
|
}
|
|
|
|
public function show(MaterialGroup $materialGroup)
|
|
{
|
|
$materialGroup->load('materials:id,ulid,name,sku,unit,unit_cost,category');
|
|
|
|
return response()->json($materialGroup);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'description' => 'nullable|string',
|
|
]);
|
|
|
|
$group = MaterialGroup::create($validated);
|
|
|
|
if ($request->has('material_ids') && is_array($request->material_ids)) {
|
|
$materialIds = Material::whereIn('ulid', $request->material_ids)->pluck('id');
|
|
$group->materials()->attach($materialIds);
|
|
}
|
|
|
|
return back()->with('success', "Material group \"{$group->name}\" created.");
|
|
}
|
|
|
|
public function update(Request $request, MaterialGroup $materialGroup)
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'description' => 'nullable|string',
|
|
'status' => 'nullable|in:active,archived',
|
|
]);
|
|
|
|
$materialGroup->update($validated);
|
|
|
|
return back()->with('success', 'Group updated.');
|
|
}
|
|
|
|
public function destroy(MaterialGroup $materialGroup)
|
|
{
|
|
if ($materialGroup->projects()->exists()) {
|
|
return back()->with('error', 'Cannot delete a group that is assigned to projects. Remove it from all projects first.');
|
|
}
|
|
|
|
$name = $materialGroup->name;
|
|
$materialGroup->delete();
|
|
|
|
return back()->with('success', "Group \"{$name}\" deleted.");
|
|
}
|
|
|
|
public function addMaterial(Request $request, MaterialGroup $materialGroup)
|
|
{
|
|
$request->validate([
|
|
'material_id' => 'required|string',
|
|
]);
|
|
|
|
$material = Material::where('ulid', $request->material_id)->firstOrFail();
|
|
|
|
if ($materialGroup->materials()->where('material_id', $material->id)->exists()) {
|
|
return back()->with('error', 'Material is already in this group.');
|
|
}
|
|
|
|
$materialGroup->materials()->attach($material->id);
|
|
|
|
return back()->with('success', "{$material->name} added to group.");
|
|
}
|
|
|
|
public function removeMaterial(MaterialGroup $materialGroup, Material $material)
|
|
{
|
|
$materialGroup->materials()->detach($material->id);
|
|
|
|
return back()->with('success', "{$material->name} removed from group.");
|
|
}
|
|
}
|