139 lines
4.2 KiB
PHP
139 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace Modules\MaterialsCatalog\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use Modules\MasterData\Models\Material;
|
|
|
|
class ItemController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$query = Material::where('type', 'single');
|
|
|
|
if ($search = $request->search) {
|
|
$query->where(function ($q) use ($search) {
|
|
$q->where('name', 'like', "%{$search}%")
|
|
->orWhere('sku', 'like', "%{$search}%")
|
|
->orWhere('category', 'like', "%{$search}%");
|
|
});
|
|
}
|
|
|
|
$items = $query->latest()
|
|
->paginate(15)
|
|
->withQueryString();
|
|
|
|
return Inertia::render('MaterialsCatalog::Items/Index', [
|
|
'items' => $items,
|
|
'filters' => $request->only(['search'])
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
return Inertia::render('MaterialsCatalog::Items/Form', [
|
|
'item' => null
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 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',
|
|
'unit' => 'required|string|max:50',
|
|
'unit_cost' => 'nullable|numeric|min:0',
|
|
]);
|
|
|
|
Material::create([
|
|
'name' => $validated['name'],
|
|
'sku' => $validated['sku'],
|
|
'category' => $validated['category'] ?? 'General',
|
|
'description' => $validated['description'],
|
|
'unit' => $validated['unit'],
|
|
'unit_cost' => $validated['unit_cost'] ?? 0,
|
|
'type' => 'single',
|
|
'status' => 'active',
|
|
]);
|
|
|
|
return redirect()->route('materials-catalog.items.index')
|
|
->with('success', 'Material Item created successfully.');
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(string $id)
|
|
{
|
|
$item = Material::findOrFail($id);
|
|
|
|
return Inertia::render('MaterialsCatalog::Items/Form', [
|
|
'item' => $item
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, string $id)
|
|
{
|
|
$item = Material::findOrFail($id);
|
|
|
|
if (auth()->check() && !is_null(auth()->user()->contractor_id) && $item->contractor_id !== auth()->user()->contractor_id) {
|
|
abort(403, 'Unauthorized to modify this material item.');
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'sku' => 'nullable|string|unique:materials,sku,' . $item->id,
|
|
'category' => 'nullable|string',
|
|
'description' => 'nullable|string',
|
|
'unit' => 'required|string|max:50',
|
|
'unit_cost' => 'nullable|numeric|min:0',
|
|
'status' => 'required|string|in:active,inactive',
|
|
]);
|
|
|
|
$item->update([
|
|
'name' => $validated['name'],
|
|
'sku' => $validated['sku'],
|
|
'category' => $validated['category'] ?? 'General',
|
|
'description' => $validated['description'],
|
|
'unit' => $validated['unit'],
|
|
'unit_cost' => $validated['unit_cost'] ?? 0,
|
|
'status' => $validated['status'],
|
|
]);
|
|
|
|
return redirect()->route('materials-catalog.items.index')
|
|
->with('success', 'Material Item updated successfully.');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
$item = Material::findOrFail($id);
|
|
|
|
if (auth()->check() && !is_null(auth()->user()->contractor_id) && $item->contractor_id !== auth()->user()->contractor_id) {
|
|
abort(403, 'Unauthorized to modify this material item.');
|
|
}
|
|
|
|
$item->delete();
|
|
|
|
return back()->with('success', 'Material Item deleted successfully.');
|
|
}
|
|
}
|