Files
star-erp/app/Modules/Inventory/Controllers/ProductController.php

330 lines
13 KiB
PHP
Raw Normal View History

2025-12-30 15:03:19 +08:00
<?php
namespace App\Modules\Inventory\Controllers;
2025-12-30 15:03:19 +08:00
use App\Http\Controllers\Controller;
use App\Modules\Inventory\Models\Product;
use App\Modules\Inventory\Models\Unit;
use App\Modules\Inventory\Models\Category;
2025-12-30 15:03:19 +08:00
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
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
{
/**
* 顯示資源列表。
2025-12-30 15:03:19 +08:00
*/
public function index(Request $request): Response
{
$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}%")
->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');
// 定義允許的排序欄位以防止 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';
}
// 如果需要,分別處理關聯排序(分類名稱),或簡單的 join
2025-12-30 15:03:19 +08:00
if ($sortField === 'category_id') {
// 加入分類以便按名稱排序?還是僅按 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();
$products->getCollection()->transform(function ($product) {
return (object) [
'id' => (string) $product->id,
'code' => $product->code,
'barcode' => $product->barcode,
'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,
'location' => $product->location,
'cost_price' => (float) $product->cost_price,
'price' => (float) $product->price,
'member_price' => (float) $product->member_price,
'wholesale_price' => (float) $product->wholesale_price,
];
});
$categories = Category::where('is_active', true)->get();
2025-12-30 15:03:19 +08:00
return Inertia::render('Product/Index', [
'products' => $products,
'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']),
]);
}
/**
* 顯示指定的資源。
*/
public function show(Product $product): Response
{
return Inertia::render('Product/Show', [
'product' => (object) [
'id' => (string) $product->id,
'code' => $product->code,
'barcode' => $product->barcode,
'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,
'location' => $product->location,
'cost_price' => (float) $product->cost_price,
'price' => (float) $product->price,
'member_price' => (float) $product->member_price,
'wholesale_price' => (float) $product->wholesale_price,
'is_active' => (bool) $product->is_active,
]
]);
}
/**
* 顯示建立表單。
*/
public function create(): Response
{
return Inertia::render('Product/Create', [
'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
/**
* 將新建立的資源儲存到儲存體中。
2025-12-30 15:03:19 +08:00
*/
public function store(Request $request)
{
$validated = $request->validate([
'code' => 'nullable|unique:products,code',
'barcode' => 'nullable|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',
'base_unit_id' => 'required|exists:units,id',
'large_unit_id' => 'nullable|exists:units,id',
'purchase_unit_id' => 'nullable|exists:units,id',
'conversion_rate' => 'nullable|numeric|min:0',
'location' => 'nullable|string|max:255',
'cost_price' => 'nullable|numeric|min:0',
'price' => 'nullable|numeric|min:0',
'member_price' => 'nullable|numeric|min:0',
'wholesale_price' => 'nullable|numeric|min:0',
'is_active' => 'boolean',
2025-12-30 15:03:19 +08:00
]);
if (empty($validated['code'])) {
$validated['code'] = $this->generateRandomCode();
}
2025-12-30 15:03:19 +08:00
$product = Product::create($validated);
return redirect()->route('products.index')->with('success', '商品已建立');
}
/**
* 顯示編輯表單。
*/
public function edit(Product $product): Response
{
return Inertia::render('Product/Edit', [
'product' => (object) [
'id' => (string) $product->id,
'code' => $product->code,
'barcode' => $product->barcode,
'name' => $product->name,
'categoryId' => $product->category_id,
'brand' => $product->brand,
'specification' => $product->specification,
'baseUnitId' => $product->base_unit_id,
'largeUnitId' => $product->large_unit_id,
'conversionRate' => (float) $product->conversion_rate,
'purchaseUnitId' => $product->purchase_unit_id,
'location' => $product->location,
'cost_price' => (float) $product->cost_price,
'price' => (float) $product->price,
'member_price' => (float) $product->member_price,
'wholesale_price' => (float) $product->wholesale_price,
],
'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
}
/**
* 更新儲存體中的指定資源。
2025-12-30 15:03:19 +08:00
*/
public function update(Request $request, Product $product)
{
$validated = $request->validate([
'code' => 'nullable|unique:products,code,' . $product->id,
'barcode' => 'nullable|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',
'base_unit_id' => 'required|exists:units,id',
'large_unit_id' => 'nullable|exists:units,id',
'purchase_unit_id' => 'nullable|exists:units,id',
'conversion_rate' => 'nullable|numeric|min:0',
'location' => 'nullable|string|max:255',
'cost_price' => 'nullable|numeric|min:0',
'price' => 'nullable|numeric|min:0',
'member_price' => 'nullable|numeric|min:0',
'wholesale_price' => 'nullable|numeric|min:0',
'is_active' => 'boolean',
2025-12-30 15:03:19 +08:00
]);
if (empty($validated['code'])) {
$validated['code'] = $this->generateRandomCode();
}
2025-12-30 15:03:19 +08:00
$product->update($validated);
if ($request->input('from') === 'show') {
return redirect()->route('products.show', $product->id)->with('success', '商品已更新');
}
return redirect()->route('products.index')->with('success', '商品已更新');
2025-12-30 15:03:19 +08:00
}
/**
* 從儲存體中移除指定資源。
2025-12-30 15:03:19 +08:00
*/
public function destroy(Product $product)
{
$product->delete();
return redirect()->back()->with('success', '商品已刪除');
}
/**
* 下載匯入範本
*/
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()]);
}
}
/**
* 生成隨機 8 碼代號 (大寫英文+數字)
*/
private function generateRandomCode(): string
{
$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$code = '';
do {
$code = '';
for ($i = 0; $i < 8; $i++) {
$code .= $characters[rand(0, strlen($characters) - 1)];
}
} while (Product::where('code', $code)->exists());
return $code;
}
2025-12-30 15:03:19 +08:00
}