實作產品與庫存匯入邏輯 (ProductImport, InventoryImport) 並更新相關 Service 與 Controller
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 56s

This commit is contained in:
2026-03-02 11:58:04 +08:00
parent 649af40919
commit 7dac2d1f77
7 changed files with 138 additions and 114 deletions

View File

@@ -16,6 +16,12 @@ use App\Modules\Inventory\Imports\ProductImport;
class ProductController extends Controller
{
protected $productService;
public function __construct(\App\Modules\Inventory\Contracts\ProductServiceInterface $productService)
{
$this->productService = $productService;
}
/**
* 顯示資源列表。
*/
@@ -195,15 +201,7 @@ class ProductController extends Controller
'is_active' => 'boolean',
]);
if (empty($validated['code'])) {
$validated['code'] = $this->generateRandomCode();
}
if (empty($validated['barcode'])) {
$validated['barcode'] = $this->generateRandomBarcode();
}
$product = Product::create($validated);
$product = $this->productService->createProduct($validated);
return redirect()->route('products.index')->with('success', '商品已建立');
}
@@ -262,15 +260,7 @@ class ProductController extends Controller
'is_active' => 'boolean',
]);
if (empty($validated['code'])) {
$validated['code'] = $this->generateRandomCode();
}
if (empty($validated['barcode'])) {
$validated['barcode'] = $this->generateRandomBarcode();
}
$product->update($validated);
$this->productService->updateProduct($product, $validated);
if ($request->input('from') === 'show') {
return redirect()->route('products.show', $product->id)->with('success', '商品已更新');
@@ -320,39 +310,4 @@ class ProductController extends Controller
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;
}
/**
* 生成隨機 13 碼條碼 (純數字)
*/
private function generateRandomBarcode(): string
{
$barcode = '';
do {
$barcode = '';
for ($i = 0; $i < 13; $i++) {
$barcode .= rand(0, 9);
}
} while (Product::where('barcode', $barcode)->exists());
return $barcode;
}
}