chore: update document approval workflow and bug fixes
This commit is contained in:
138
Modules/MaterialsCatalog/app/Http/Controllers/ItemController.php
Normal file
138
Modules/MaterialsCatalog/app/Http/Controllers/ItemController.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?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.');
|
||||
}
|
||||
}
|
||||
169
Modules/MaterialsCatalog/app/Http/Controllers/KitController.php
Normal file
169
Modules/MaterialsCatalog/app/Http/Controllers/KitController.php
Normal file
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\MaterialsCatalog\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Modules\MasterData\Models\Material;
|
||||
|
||||
class KitController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$kits = Material::with('components.component')
|
||||
->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.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\MaterialsCatalog\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class MaterialsCatalogController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return view('materialscatalog::index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return view('materialscatalog::create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request) {}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
return view('materialscatalog::show');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
return view('materialscatalog::edit');
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, $id) {}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id) {}
|
||||
}
|
||||
Reference in New Issue
Block a user