2025-12-30 15:03:19 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
2026-01-26 10:37:47 +08:00
|
|
|
|
namespace App\Modules\Inventory\Controllers;
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
2026-01-26 10:37:47 +08:00
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Modules\Inventory\Models\Product;
|
|
|
|
|
|
use App\Modules\Inventory\Models\Unit;
|
2026-01-26 14:59:24 +08:00
|
|
|
|
use App\Modules\Inventory\Models\Category;
|
2025-12-30 15:03:19 +08:00
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
use Inertia\Inertia;
|
|
|
|
|
|
use Inertia\Response;
|
2026-02-02 14:39:13 +08:00
|
|
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
|
|
use App\Modules\Inventory\Exports\ProductTemplateExport;
|
|
|
|
|
|
use App\Modules\Inventory\Imports\ProductImport;
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
class ProductController extends Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
/**
|
2026-01-26 14:59:24 +08:00
|
|
|
|
* 顯示資源列表。
|
2025-12-30 15:03:19 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function index(Request $request): Response
|
|
|
|
|
|
{
|
2026-01-08 11:52:25 +08:00
|
|
|
|
$query = Product::with(['category', 'baseUnit', 'largeUnit', 'purchaseUnit']);
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
if ($request->filled('search')) {
|
|
|
|
|
|
$search = $request->search;
|
|
|
|
|
|
$query->where(function ($q) use ($search) {
|
|
|
|
|
|
$q->where('name', 'like', "%{$search}%")
|
|
|
|
|
|
->orWhere('code', 'like', "%{$search}%")
|
2026-01-29 16:13:56 +08:00
|
|
|
|
->orWhere('barcode', 'like', "%{$search}%")
|
2025-12-30 15:03:19 +08:00
|
|
|
|
->orWhere('brand', 'like', "%{$search}%");
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ($request->filled('category_id') && $request->category_id !== 'all') {
|
|
|
|
|
|
$query->where('category_id', $request->category_id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$perPage = $request->input('per_page', 10);
|
|
|
|
|
|
if (!in_array($perPage, [10, 20, 50, 100])) {
|
|
|
|
|
|
$perPage = 10;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$sortField = $request->input('sort_field', 'id');
|
|
|
|
|
|
$sortDirection = $request->input('sort_direction', 'desc');
|
|
|
|
|
|
|
2026-01-26 14:59:24 +08:00
|
|
|
|
// 定義允許的排序欄位以防止 SQL 注入
|
2026-01-08 12:00:36 +08:00
|
|
|
|
$allowedSorts = ['id', 'code', 'name', 'category_id', 'base_unit_id', 'conversion_rate'];
|
2025-12-30 15:03:19 +08:00
|
|
|
|
if (!in_array($sortField, $allowedSorts)) {
|
|
|
|
|
|
$sortField = 'id';
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!in_array(strtolower($sortDirection), ['asc', 'desc'])) {
|
|
|
|
|
|
$sortDirection = 'desc';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-26 14:59:24 +08:00
|
|
|
|
// 如果需要,分別處理關聯排序(分類名稱),或簡單的 join
|
2025-12-30 15:03:19 +08:00
|
|
|
|
if ($sortField === 'category_id') {
|
2026-01-26 14:59:24 +08:00
|
|
|
|
// 加入分類以便按名稱排序?還是僅按 ID?
|
|
|
|
|
|
// 簡單方法:目前按 ID 排序,如果使用者想要按名稱排序則 join。
|
|
|
|
|
|
// 先假設標準欄位排序。
|
2025-12-30 15:03:19 +08:00
|
|
|
|
$query->orderBy('category_id', $sortDirection);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$query->orderBy($sortField, $sortDirection);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$products = $query->paginate($perPage)->withQueryString();
|
|
|
|
|
|
|
2026-01-26 14:59:24 +08:00
|
|
|
|
$products->getCollection()->transform(function ($product) {
|
|
|
|
|
|
return (object) [
|
|
|
|
|
|
'id' => (string) $product->id,
|
|
|
|
|
|
'code' => $product->code,
|
2026-01-29 16:13:56 +08:00
|
|
|
|
'barcode' => $product->barcode,
|
2026-01-26 14:59:24 +08:00
|
|
|
|
'name' => $product->name,
|
|
|
|
|
|
'categoryId' => $product->category_id,
|
|
|
|
|
|
'category' => $product->category ? (object) [
|
|
|
|
|
|
'id' => $product->category->id,
|
|
|
|
|
|
'name' => $product->category->name,
|
|
|
|
|
|
] : null,
|
|
|
|
|
|
'brand' => $product->brand,
|
|
|
|
|
|
'specification' => $product->specification,
|
|
|
|
|
|
'baseUnitId' => $product->base_unit_id,
|
|
|
|
|
|
'baseUnit' => $product->baseUnit ? (object) [
|
|
|
|
|
|
'id' => $product->baseUnit->id,
|
|
|
|
|
|
'name' => $product->baseUnit->name,
|
|
|
|
|
|
] : null,
|
|
|
|
|
|
'largeUnitId' => $product->large_unit_id,
|
|
|
|
|
|
'largeUnit' => $product->largeUnit ? (object) [
|
|
|
|
|
|
'id' => $product->largeUnit->id,
|
|
|
|
|
|
'name' => $product->largeUnit->name,
|
|
|
|
|
|
] : null,
|
|
|
|
|
|
'purchaseUnitId' => $product->purchase_unit_id,
|
|
|
|
|
|
'purchaseUnit' => $product->purchaseUnit ? (object) [
|
|
|
|
|
|
'id' => $product->purchaseUnit->id,
|
|
|
|
|
|
'name' => $product->purchaseUnit->name,
|
|
|
|
|
|
] : null,
|
|
|
|
|
|
'conversionRate' => (float) $product->conversion_rate,
|
2026-02-03 13:17:46 +08:00
|
|
|
|
'location' => $product->location,
|
2026-01-26 14:59:24 +08:00
|
|
|
|
];
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
$categories = Category::where('is_active', true)->get();
|
2025-12-30 15:03:19 +08:00
|
|
|
|
|
|
|
|
|
|
return Inertia::render('Product/Index', [
|
|
|
|
|
|
'products' => $products,
|
2026-01-26 14:59:24 +08:00
|
|
|
|
'categories' => Category::where('is_active', true)->get()->map(fn($c) => (object)['id' => $c->id, 'name' => $c->name]),
|
|
|
|
|
|
'units' => Unit::all()->map(fn($u) => (object)['id' => (string) $u->id, 'name' => $u->name, 'code' => $u->code]),
|
2025-12-30 15:03:19 +08:00
|
|
|
|
'filters' => $request->only(['search', 'category_id', 'per_page', 'sort_field', 'sort_direction']),
|
|
|
|
|
|
]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-26 14:59:24 +08:00
|
|
|
|
* 將新建立的資源儲存到儲存體中。
|
2025-12-30 15:03:19 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function store(Request $request)
|
|
|
|
|
|
{
|
|
|
|
|
|
$validated = $request->validate([
|
2026-02-02 15:07:12 +08:00
|
|
|
|
'code' => 'required|string|min:2|max:8|unique:products,code',
|
2026-01-29 16:13:56 +08:00
|
|
|
|
'barcode' => 'required|string|unique:products,barcode',
|
2025-12-30 15:03:19 +08:00
|
|
|
|
'name' => 'required|string|max:255',
|
|
|
|
|
|
'category_id' => 'required|exists:categories,id',
|
|
|
|
|
|
'brand' => 'nullable|string|max:255',
|
|
|
|
|
|
'specification' => 'nullable|string',
|
2026-01-08 11:52:25 +08:00
|
|
|
|
|
|
|
|
|
|
'base_unit_id' => 'required|exists:units,id',
|
|
|
|
|
|
'large_unit_id' => 'nullable|exists:units,id',
|
|
|
|
|
|
'conversion_rate' => 'required_with:large_unit_id|nullable|numeric|min:0.0001',
|
|
|
|
|
|
'purchase_unit_id' => 'nullable|exists:units,id',
|
2026-02-03 13:17:46 +08:00
|
|
|
|
'location' => 'nullable|string|max:255',
|
2025-12-31 17:22:56 +08:00
|
|
|
|
], [
|
2026-01-21 16:30:50 +08:00
|
|
|
|
'code.required' => '商品代號為必填',
|
2026-02-02 15:07:12 +08:00
|
|
|
|
'code.max' => '商品代號最多 8 碼',
|
|
|
|
|
|
'code.min' => '商品代號最少 2 碼',
|
2026-01-21 16:30:50 +08:00
|
|
|
|
'code.unique' => '商品代號已存在',
|
2026-01-29 16:13:56 +08:00
|
|
|
|
'barcode.required' => '條碼編號為必填',
|
|
|
|
|
|
'barcode.unique' => '條碼編號已存在',
|
2025-12-31 17:22:56 +08:00
|
|
|
|
'name.required' => '商品名稱為必填',
|
|
|
|
|
|
'category_id.required' => '請選擇分類',
|
|
|
|
|
|
'category_id.exists' => '所選分類不存在',
|
2026-01-08 11:52:25 +08:00
|
|
|
|
'base_unit_id.required' => '基本庫存單位為必填',
|
|
|
|
|
|
'base_unit_id.exists' => '所選基本單位不存在',
|
2025-12-31 17:22:56 +08:00
|
|
|
|
'conversion_rate.required_with' => '填寫大單位時,換算率為必填',
|
|
|
|
|
|
'conversion_rate.numeric' => '換算率必須為數字',
|
|
|
|
|
|
'conversion_rate.min' => '換算率最小為 0.0001',
|
2025-12-30 15:03:19 +08:00
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
$product = Product::create($validated);
|
|
|
|
|
|
|
|
|
|
|
|
return redirect()->back()->with('success', '商品已建立');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-26 14:59:24 +08:00
|
|
|
|
* 更新儲存體中的指定資源。
|
2025-12-30 15:03:19 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function update(Request $request, Product $product)
|
|
|
|
|
|
{
|
2026-01-08 11:52:25 +08:00
|
|
|
|
$validated = $request->validate([
|
2026-02-02 15:07:12 +08:00
|
|
|
|
'code' => 'required|string|min:2|max:8|unique:products,code,' . $product->id,
|
2026-01-29 16:13:56 +08:00
|
|
|
|
'barcode' => 'required|string|unique:products,barcode,' . $product->id,
|
2025-12-30 15:03:19 +08:00
|
|
|
|
'name' => 'required|string|max:255',
|
|
|
|
|
|
'category_id' => 'required|exists:categories,id',
|
|
|
|
|
|
'brand' => 'nullable|string|max:255',
|
|
|
|
|
|
'specification' => 'nullable|string',
|
2026-01-08 11:52:25 +08:00
|
|
|
|
'base_unit_id' => 'required|exists:units,id',
|
|
|
|
|
|
'large_unit_id' => 'nullable|exists:units,id',
|
|
|
|
|
|
'conversion_rate' => 'required_with:large_unit_id|nullable|numeric|min:0.0001',
|
|
|
|
|
|
'purchase_unit_id' => 'nullable|exists:units,id',
|
2026-02-03 13:17:46 +08:00
|
|
|
|
'location' => 'nullable|string|max:255',
|
2026-01-08 11:52:25 +08:00
|
|
|
|
], [
|
2026-01-21 16:30:50 +08:00
|
|
|
|
'code.required' => '商品代號為必填',
|
2026-02-02 15:07:12 +08:00
|
|
|
|
'code.max' => '商品代號最多 8 碼',
|
|
|
|
|
|
'code.min' => '商品代號最少 2 碼',
|
2026-01-21 16:30:50 +08:00
|
|
|
|
'code.unique' => '商品代號已存在',
|
2026-01-29 16:13:56 +08:00
|
|
|
|
'barcode.required' => '條碼編號為必填',
|
|
|
|
|
|
'barcode.unique' => '條碼編號已存在',
|
2026-01-08 11:52:25 +08:00
|
|
|
|
'name.required' => '商品名稱為必填',
|
|
|
|
|
|
'category_id.required' => '請選擇分類',
|
|
|
|
|
|
'category_id.exists' => '所選分類不存在',
|
|
|
|
|
|
'base_unit_id.required' => '基本庫存單位為必填',
|
|
|
|
|
|
'base_unit_id.exists' => '所選基本單位不存在',
|
|
|
|
|
|
'conversion_rate.required_with' => '填寫大單位時,換算率為必填',
|
|
|
|
|
|
'conversion_rate.numeric' => '換算率必須為數字',
|
|
|
|
|
|
'conversion_rate.min' => '換算率最小為 0.0001',
|
2025-12-30 15:03:19 +08:00
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
$product->update($validated);
|
|
|
|
|
|
|
|
|
|
|
|
return redirect()->back()->with('success', '商品已更新');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-26 14:59:24 +08:00
|
|
|
|
* 從儲存體中移除指定資源。
|
2025-12-30 15:03:19 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function destroy(Product $product)
|
|
|
|
|
|
{
|
|
|
|
|
|
$product->delete();
|
|
|
|
|
|
|
|
|
|
|
|
return redirect()->back()->with('success', '商品已刪除');
|
|
|
|
|
|
}
|
2026-02-02 14:39:13 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 下載匯入範本
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function template()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Excel::download(new ProductTemplateExport, 'products_template.xlsx');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 匯入商品
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function import(Request $request)
|
|
|
|
|
|
{
|
|
|
|
|
|
$request->validate([
|
|
|
|
|
|
'file' => 'required|file|mimes:xlsx,xls',
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
Excel::import(new ProductImport, $request->file('file'));
|
|
|
|
|
|
return redirect()->back()->with('success', '商品匯入成功');
|
|
|
|
|
|
} catch (\Maatwebsite\Excel\Validators\ValidationException $e) {
|
|
|
|
|
|
$failures = $e->failures();
|
|
|
|
|
|
$messages = [];
|
|
|
|
|
|
foreach ($failures as $failure) {
|
|
|
|
|
|
$messages[] = '第 ' . $failure->row() . ' 行: ' . implode(', ', $failure->errors());
|
|
|
|
|
|
}
|
|
|
|
|
|
return redirect()->back()->withErrors(['file' => implode("\n", $messages)]);
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
|
return redirect()->back()->withErrors(['file' => '匯入失敗: ' . $e->getMessage()]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-12-30 15:03:19 +08:00
|
|
|
|
}
|