feat: implement inventory management controllers, dispatch service logic, and frontend inventory tracking dashboard
Some checks failed
Tests / PHP 8.3 (push) Has been cancelled
Tests / PHP 8.4 (push) Has been cancelled
Tests / PHP 8.5 (push) Has been cancelled

This commit is contained in:
Ajjj
2026-08-05 16:23:57 +08:00
parent 6075ac7632
commit c13f418517
4 changed files with 53 additions and 14 deletions

View File

@@ -26,7 +26,9 @@ class MaterialController extends Controller
// --- Inventory Dashboard ---
public function inventory(Request $request)
{
$warehouses = Warehouse::active()->select('id', 'ulid', 'name', 'code', 'type')
$warehouses = Warehouse::active()
->whereIn('id', $this->visibleWarehouseIdsQuery())
->select('id', 'ulid', 'name', 'code', 'type')
->with(['stock' => function ($query) {
$query->where('quantity', '>', 0);
}])
@@ -62,7 +64,8 @@ class MaterialController extends Controller
});
// Warehouse stock
$warehouseStockQuery = WarehouseStock::with([
$warehouseStockQuery = WarehouseStock::whereIn('warehouse_id', $this->visibleWarehouseIdsQuery())
->with([
'warehouse:id,ulid,name,code',
'material' => function ($query) {
$query->select('id', 'ulid', 'name', 'sku', 'unit', 'category', 'type')
@@ -79,7 +82,8 @@ class MaterialController extends Controller
->withQueryString();
// Project site stock
$siteStockQuery = ProjectInventory::with([
$siteStockQuery = ProjectInventory::whereIn('project_id', $this->visibleProjectIdsQuery())
->with([
'project:id,ulid,name,code',
'material' => function ($query) {
$query->select('id', 'ulid', 'name', 'sku', 'unit', 'category', 'type')
@@ -96,11 +100,11 @@ class MaterialController extends Controller
// Summary stats
$summary = [
'total_warehouse' => WarehouseStock::sum('quantity'),
'total_warehouse' => WarehouseStock::whereIn('warehouse_id', $this->visibleWarehouseIdsQuery())->sum('quantity'),
'total_onsite' => ProjectInventory::sum('on_hand_qty'),
'total_reserved' => ProjectInventory::sum('allocated_qty'),
'total_consumed' => ProjectInventory::sum('consumed_qty'),
'total_onsite' => ProjectInventory::whereIn('project_id', $this->visibleProjectIdsQuery())->sum('on_hand_qty'),
'total_reserved' => ProjectInventory::whereIn('project_id', $this->visibleProjectIdsQuery())->sum('allocated_qty'),
'total_consumed' => ProjectInventory::whereIn('project_id', $this->visibleProjectIdsQuery())->sum('consumed_qty'),
];
return Inertia::render('MaterialLogistics::Inventory/Index', [
@@ -116,7 +120,9 @@ class MaterialController extends Controller
// --- Inventory Operations Form Page ---
public function operations(Request $request, string $type)
{
$warehouses = Warehouse::active()->select('id', 'ulid', 'name', 'code')->get();
$warehouses = Warehouse::active()
->whereIn('id', $this->visibleWarehouseIdsQuery())
->select('id', 'ulid', 'name', 'code')->get();
$projects = Project::select('id', 'ulid', 'name', 'code')
->with(['personnel' => function ($query) {
@@ -377,4 +383,16 @@ class MaterialController extends Controller
return back()->with('success', 'Stock adjusted.');
}
private function visibleProjectIdsQuery()
{
return Project::query()->select('projects.id');
}
private function visibleWarehouseIdsQuery()
{
return Warehouse::query()
->whereIn('project_id', $this->visibleProjectIdsQuery())
->select('warehouses.id');
}
}

View File

@@ -7,12 +7,13 @@ use Illuminate\Http\Request;
use Inertia\Inertia;
use Modules\MaterialLogistics\Models\InventoryMovement;
use Modules\MaterialLogistics\Models\Warehouse;
use Modules\ProjectManagement\Models\Project;
class WarehouseController extends Controller
{
public function index(Request $request)
{
$query = Warehouse::query();
$query = Warehouse::whereIn('id', $this->visibleWarehouseIdsQuery());
if ($search = $request->search) {
$query->where(function ($q) use ($search) {
@@ -40,6 +41,8 @@ class WarehouseController extends Controller
public function show(Request $request, Warehouse $warehouse)
{
abort_unless($this->visibleWarehouseIdsQuery()->where('warehouses.id', $warehouse->id)->exists(), 403);
// Paginated stock with search & category filter
$stockQuery = $warehouse->stock()->with([
'material:id,ulid,name,sku,unit,category,type',
@@ -94,6 +97,13 @@ class WarehouseController extends Controller
]);
}
private function visibleWarehouseIdsQuery()
{
return Warehouse::query()
->whereIn('project_id', Project::query()->select('projects.id'))
->select('warehouses.id');
}
public function store(Request $request)
{
$validated = $request->validate([