diff --git a/app/Http/Controllers/Admin/MaintenanceController.php b/app/Http/Controllers/Admin/MaintenanceController.php index f4d25de..4e5e750 100644 --- a/app/Http/Controllers/Admin/MaintenanceController.php +++ b/app/Http/Controllers/Admin/MaintenanceController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Models\Machine\Machine; use App\Models\Machine\MaintenanceRecord; +use App\Models\System\Company; use App\Traits\ImageHandler; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -21,6 +22,9 @@ class MaintenanceController extends Controller { $this->authorize('viewAny', MaintenanceRecord::class); + $currentUser = auth()->user(); + $companyId = trim((string) $request->input('company_id', '')); + $query = MaintenanceRecord::with(['machine', 'user', 'company']) ->whereHas('machine') // 確保僅顯示該帳號「看得見」的機台紀錄,避開因權限隔離導致的 null 報錯 ->latest('maintenance_at'); @@ -38,9 +42,16 @@ class MaintenanceController extends Controller $query->where('category', $request->category); } - $records = $query->paginate(15)->withQueryString(); + if ($currentUser->isSystemAdmin() && $companyId !== '') { + $query->where('company_id', $companyId); + } - return view('admin.maintenance.index', compact('records')); + $records = $query->paginate(15)->withQueryString(); + $companies = $currentUser->isSystemAdmin() + ? Company::select('id', 'name', 'code')->orderBy('name')->get() + : collect(); + + return view('admin.maintenance.index', compact('records', 'companies')); } /** diff --git a/app/Http/Controllers/Admin/WarehouseController.php b/app/Http/Controllers/Admin/WarehouseController.php index 5ee1e9c..ccbe366 100644 --- a/app/Http/Controllers/Admin/WarehouseController.php +++ b/app/Http/Controllers/Admin/WarehouseController.php @@ -26,36 +26,54 @@ class WarehouseController extends Controller public function index(Request $request) { + $currentUser = Auth::user(); + $companyId = trim((string) $request->input('company_id', '')); + $search = $request->input('search'); + $type = $request->input('type'); + + $applyWarehouseFilters = function ($query) use ($currentUser, $companyId, $search, $type) { + if ($currentUser->isSystemAdmin() && $companyId !== '') { + $query->where('company_id', $companyId); + } + + if ($search) { + $query->where(function ($q) use ($search) { + $q->where('name', 'like', "%{$search}%") + ->orWhere('address', 'like', "%{$search}%"); + }); + } + + if ($type) { + $query->where('type', $type); + } + + return $query; + }; + $query = Warehouse::query() ->with(['company']) ->withCount('stocks as products_count') ->withSum('stocks as total_stock', 'quantity'); - if ($search = $request->input('search')) { - $query->where(function ($q) use ($search) { - $q->where('name', 'like', "%{$search}%") - ->orWhere('address', 'like', "%{$search}%"); - }); - } - - if ($request->filled('type')) { - $query->where('type', $request->input('type')); - } + $applyWarehouseFilters($query); $warehouses = $query->orderBy('type')->orderBy('name') ->paginate($request->input('per_page', 10)) ->withQueryString(); + $statsWarehouseQuery = fn() => $applyWarehouseFilters(Warehouse::query()); + $stats = [ - 'total' => Warehouse::count(), - 'main' => Warehouse::main()->count(), - 'branch' => Warehouse::branch()->count(), + 'total' => $statsWarehouseQuery()->count(), + 'main' => $statsWarehouseQuery()->main()->count(), + 'branch' => $statsWarehouseQuery()->branch()->count(), 'low_stock' => WarehouseStock::lowStock() - ->whereHas('warehouse', fn($q) => $q)->count(), + ->whereHas('warehouse', fn($q) => $applyWarehouseFilters($q)) + ->count(), ]; $companies = collect(); - if (Auth::user()->isSystemAdmin()) { + if ($currentUser->isSystemAdmin()) { $companies = \App\Models\System\Company::active()->orderBy('name')->get(); } @@ -149,23 +167,39 @@ class WarehouseController extends Controller public function inventory(Request $request) { $tab = $request->input('tab', 'stock'); - $warehouses = Warehouse::active()->orderBy('name')->get(); + $currentUser = Auth::user(); + $companyId = trim((string) $request->input('company_id', '')); + $applyCompanyFilter = fn($query) => $query->when( + $currentUser->isSystemAdmin() && $companyId !== '', + fn($q) => $q->where('company_id', $companyId) + ); + + $warehouses = $applyCompanyFilter(Warehouse::active()) + ->orderBy('name') + ->get(); + $companies = $currentUser->isSystemAdmin() + ? \App\Models\System\Company::active()->orderBy('name')->get() + : collect(); $isAjax = $request->ajax(); - $data = compact('warehouses', 'tab'); + $data = compact('warehouses', 'companies', 'tab'); // 1. Current Stocks (stock) if (!$isAjax || $tab === 'stock') { $warehouse_ids = $request->input('warehouse_ids'); - $inventory_warehouses = Warehouse::active() + $inventory_warehouses = $applyCompanyFilter(Warehouse::active()) ->when($warehouse_ids, fn($q) => $q->whereIn('id', (array)$warehouse_ids)) ->orderBy('name') - ->get(['id', 'name', 'type']); + ->get(['id', 'name', 'type', 'company_id']); $query = Product::with(['stocks' => function($q) { $q->select('id', 'product_id', 'warehouse_id', 'quantity', 'safety_stock'); }, 'translations']); + if ($currentUser->isSystemAdmin() && $companyId !== '') { + $query->where('company_id', $companyId); + } + if ($warehouse_ids) { $query->whereHas('stocks', fn($q) => $q->whereIn('warehouse_id', (array)$warehouse_ids)); } @@ -185,6 +219,10 @@ class WarehouseController extends Controller ->withCount('items') ->latest(); + if ($currentUser->isSystemAdmin() && $companyId !== '') { + $query->where('company_id', $companyId); + } + if ($request->filled('status')) { $query->where('status', $request->input('status')); } @@ -206,7 +244,10 @@ class WarehouseController extends Controller $data['stockInDefaultEnd'] = ''; } $data['orders'] = $query->paginate($request->input('per_page', 10), ['*'], 'order_page')->withQueryString(); - $data['stock_in_products'] = Product::with('translations')->orderBy('name')->get(['id', 'name', 'image_url', 'name_dictionary_key']); + $data['stock_in_products'] = Product::with('translations') + ->when($currentUser->isSystemAdmin() && $companyId !== '', fn($q) => $q->where('company_id', $companyId)) + ->orderBy('name') + ->get(['id', 'name', 'image_url', 'name_dictionary_key']); } // 3. Movement History (movements) @@ -215,6 +256,10 @@ class WarehouseController extends Controller ->orderBy('created_at', 'desc') ->orderBy('id', 'desc'); + if ($currentUser->isSystemAdmin() && $companyId !== '') { + $query->where('company_id', $companyId); + } + if ($request->filled('warehouse_id')) { $query->where('warehouse_id', $request->input('warehouse_id')); } @@ -397,10 +442,17 @@ class WarehouseController extends Controller public function transfers(Request $request) { + $currentUser = Auth::user(); + $companyId = trim((string) $request->input('company_id', '')); + $isSystemAdmin = $currentUser->isSystemAdmin(); + $query = TransferOrder::with(['fromWarehouse:id,name', 'toWarehouse:id,name', 'fromMachine:id,name,serial_no', 'creator:id,name']) ->withCount('items') ->latest(); + if ($isSystemAdmin && $companyId !== '') { + $query->where('company_id', $companyId); + } if ($request->filled('type')) { $query->where('type', $request->input('type')); } @@ -431,14 +483,22 @@ class WarehouseController extends Controller ]); } - $warehouses = Warehouse::active()->orderBy('name')->get(['id', 'name']); - $machines = Machine::orderBy('name')->get(['id', 'name', 'serial_no']); - $products = Product::with('translations')->orderBy('name')->get(['id', 'name', 'image_url', 'name_dictionary_key']); + $companies = $isSystemAdmin + ? \App\Models\System\Company::active()->orderBy('name')->get() + : collect(); + $applyCompanyFilter = fn($query) => $query->when( + $isSystemAdmin && $companyId !== '', + fn($q) => $q->where('company_id', $companyId) + ); + + $warehouses = $applyCompanyFilter(Warehouse::active())->orderBy('name')->get(['id', 'name']); + $machines = $applyCompanyFilter(Machine::query())->orderBy('name')->get(['id', 'name', 'serial_no']); + $products = $applyCompanyFilter(Product::with('translations'))->orderBy('name')->get(['id', 'name', 'image_url', 'name_dictionary_key']); $defaultStart = ''; $defaultEnd = ''; - return view('admin.warehouses.transfers', compact('orders', 'warehouses', 'machines', 'products', 'defaultStart', 'defaultEnd')); + return view('admin.warehouses.transfers', compact('orders', 'warehouses', 'machines', 'products', 'companies', 'defaultStart', 'defaultEnd')); } /** @@ -625,11 +685,19 @@ class WarehouseController extends Controller public function machineInventory(Request $request) { + $currentUser = Auth::user(); + $companyId = trim((string) $request->input('company_id', '')); + $isSystemAdmin = $currentUser->isSystemAdmin(); + $query = Machine::with(['slots' => fn($q) => $q->with('product:id,name,image_url')->orderByRaw('CAST(slot_no AS UNSIGNED) ASC')]) ->withCount('slots as total_slots') ->withSum('slots as total_stock', 'stock') ->withSum('slots as total_capacity', 'max_stock'); + if ($isSystemAdmin && $companyId !== '') { + $query->where('company_id', $companyId); + } + if ($search = $request->input('search')) { $query->where(function ($q) use ($search) { $q->where('name', 'like', "%{$search}%") @@ -650,7 +718,11 @@ class WarehouseController extends Controller ]); } - return view('admin.warehouses.machine-inventory', compact('machines')); + $companies = $isSystemAdmin + ? \App\Models\System\Company::active()->orderBy('name')->get() + : collect(); + + return view('admin.warehouses.machine-inventory', compact('machines', 'companies')); } @@ -718,10 +790,17 @@ class WarehouseController extends Controller public function replenishments(Request $request) { + $currentUser = Auth::user(); + $companyId = trim((string) $request->input('company_id', '')); + $isSystemAdmin = $currentUser->isSystemAdmin(); + $query = ReplenishmentOrder::with(['warehouse:id,name', 'machine:id,name,serial_no', 'assignee:id,name', 'creator:id,name']) ->withCount('items') ->latest(); + if ($isSystemAdmin && $companyId !== '') { + $query->where('company_id', $companyId); + } if ($request->filled('status')) { $query->where('status', $request->input('status')); } @@ -749,20 +828,30 @@ class WarehouseController extends Controller ]); } - $warehouses = Warehouse::active()->orderBy('name')->get(['id', 'name']); - $machines = Machine::orderBy('name')->get(['id', 'name', 'serial_no']); - $products = Product::with('translations')->orderBy('name')->get(['id', 'name', 'image_url', 'name_dictionary_key']); + $companies = $isSystemAdmin + ? \App\Models\System\Company::active()->orderBy('name')->get() + : collect(); + $applyCompanyFilter = fn($query) => $query->when( + $isSystemAdmin && $companyId !== '', + fn($q) => $q->where('company_id', $companyId) + ); + + $warehouses = $applyCompanyFilter(Warehouse::active())->orderBy('name')->get(['id', 'name']); + $machines = $applyCompanyFilter(Machine::query())->orderBy('name')->get(['id', 'name', 'serial_no']); + $products = $applyCompanyFilter(Product::with('translations'))->orderBy('name')->get(['id', 'name', 'image_url', 'name_dictionary_key']); $userQuery = \App\Models\System\User::where('status', 1)->orderBy('name'); - if (!Auth::user()->isSystemAdmin()) { - $userQuery->where('company_id', Auth::user()->company_id); + if (!$isSystemAdmin) { + $userQuery->where('company_id', $currentUser->company_id); + } elseif ($companyId !== '') { + $userQuery->where('company_id', $companyId); } $users = $userQuery->get(['id', 'name']); $defaultStart = ''; $defaultEnd = ''; - return view('admin.warehouses.replenishments', compact('orders', 'warehouses', 'machines', 'products', 'users', 'defaultStart', 'defaultEnd')); + return view('admin.warehouses.replenishments', compact('orders', 'warehouses', 'machines', 'products', 'users', 'companies', 'defaultStart', 'defaultEnd')); } /** @@ -1479,4 +1568,3 @@ class WarehouseController extends Controller return view('admin.warehouses.print.replenishment', compact('order', 'items')); } } - diff --git a/lang/en.json b/lang/en.json index a97c215..a732410 100644 --- a/lang/en.json +++ b/lang/en.json @@ -1064,6 +1064,7 @@ "Machine to Warehouse": "Machine to Warehouse", "Machine to Warehouse Return": "Machine to Warehouse Return", "Machine to warehouse returns": "Machine to warehouse returns", + "M2W": "M2W", "Machine updated successfully.": "Machine updated successfully.", "Machine-Specific Webhook": "Machine-Specific Webhook", "Machines": "Machines", @@ -1261,6 +1262,7 @@ "No pickup codes found": "No pickup codes found", "No product data matching search criteria": "No product data matching search criteria", "No product record found": "No product record found", + "No products found": "No products found", "No products found in this warehouse": "No products found in this warehouse", "No products found matching your criteria.": "No products found matching your criteria.", "No records found": "No records found", @@ -2227,6 +2229,7 @@ "Warehouse to Warehouse": "Warehouse to Warehouse", "Warehouse to Warehouse Transfer": "Warehouse to Warehouse Transfer", "Warehouse to warehouse transfers": "Warehouse to warehouse transfers", + "W2W": "W2W", "Warehouse updated successfully": "Warehouse updated successfully", "Warehouse updated successfully.": "Warehouse updated successfully.", "Warning": "Warning", diff --git a/lang/ja.json b/lang/ja.json index b2032d4..1c076fb 100644 --- a/lang/ja.json +++ b/lang/ja.json @@ -1064,6 +1064,7 @@ "Machine to Warehouse": "機器から倉庫", "Machine to Warehouse Return": "自販機から倉庫への返品", "Machine to warehouse returns": "機器から倉庫への返却", + "M2W": "機器から倉庫", "Machine updated successfully.": "機器が正常に更新されました。", "Machine-Specific Webhook": "機器専用 Webhook", "Machines": "機器管理", @@ -1261,6 +1262,7 @@ "No pickup codes found": "受取コードが見つかりません", "No product data matching search criteria": "検索条件に一致する商品データはありません", "No product record found": "商品記録が見つかりません", + "No products found": "商品が見つかりません", "No products found in this warehouse": "この倉庫に商品は見つかりません", "No products found matching your criteria.": "条件に一致する商品が見つかりません。", "No records found": "記録が見つかりません", @@ -2227,6 +2229,7 @@ "Warehouse to Warehouse": "倉庫から倉庫", "Warehouse to Warehouse Transfer": "倉庫間移動", "Warehouse to warehouse transfers": "倉庫から倉庫への在庫移動", + "W2W": "倉庫間", "Warehouse updated successfully": "倉庫が正常に更新されました", "Warehouse updated successfully.": "倉庫が正常に更新されました。", "Warning": "期限間近", diff --git a/lang/zh_TW.json b/lang/zh_TW.json index 39e84da..728130d 100644 --- a/lang/zh_TW.json +++ b/lang/zh_TW.json @@ -1064,6 +1064,7 @@ "Machine to Warehouse": "機台對倉庫", "Machine to Warehouse Return": "機台退庫至倉庫", "Machine to warehouse returns": "機台退回倉庫", + "M2W": "機對倉", "Machine updated successfully.": "機台更新成功。", "Machine-Specific Webhook": "機台專屬 Webhook", "Machines": "機台管理", @@ -1261,6 +1262,7 @@ "No pickup codes found": "找不到取貨碼", "No product data matching search criteria": "沒有符合搜尋條件的商品資料", "No product record found": "無商品紀錄", + "No products found": "未找到商品", "No products found in this warehouse": "此倉庫目前無商品庫存", "No products found matching your criteria.": "找不到符合條件的商品。", "No records found": "未找到相關紀錄", @@ -2227,6 +2229,7 @@ "Warehouse to Warehouse": "倉庫對倉庫", "Warehouse to Warehouse Transfer": "倉庫至倉庫調撥", "Warehouse to warehouse transfers": "倉庫對倉庫調撥", + "W2W": "倉對倉", "Warehouse updated successfully": "倉庫更新成功", "Warehouse updated successfully.": "倉庫已成功更新。", "Warning": "警告", diff --git a/resources/views/admin/machines/index.blade.php b/resources/views/admin/machines/index.blade.php index 1e0da8a..b35bd0c 100644 --- a/resources/views/admin/machines/index.blade.php +++ b/resources/views/admin/machines/index.blade.php @@ -467,7 +467,8 @@ @if(auth()->user()->isSystemAdmin())
| {{ __('Slot') }} | +{{ __('Product') }} | +{{ __('Barcode') }} | +{{ __('Stock Level') }} / {{ __('Max Stock') }} | +{{ __('Expiry') }} | +
|---|---|---|---|---|
| + + | +
+
+
+
+
+
+
+
+
+
+
+ |
+ + + | +
+
+
+ /
+
+
+ |
+ + + | +
{{ __('Total Warehouses') }}
{{ $stats['total'] }}
@@ -174,7 +183,14 @@ {{-- Filters --}}{{ __('No machines found') }}
+{{ __('No movement records found') }}
+{{ __('No movement records found') }}
{{ __('No movement records found') }}
+{{ __('No stock-in orders found') }}
+{{ __('No products found') }}
+{{ __('No replenishment orders found') }}
+{{ __('No replenishment orders found') }}
+{{ __('No replenishment orders found') }}
+{{ __('No transfer orders found') }}
+{{ __('No transfer orders found') }}
+{{ __('No transfer orders found') }}
+