Compare commits
7 Commits
6bfdd92347
...
590580e20a
| Author | SHA1 | Date | |
|---|---|---|---|
| 590580e20a | |||
| c2e0ff726d | |||
| 5e542752ba | |||
| f22df90e01 | |||
| e018b75783 | |||
| 200d1989bd | |||
| 6c259859cf |
@@ -1086,3 +1086,77 @@ import { Pencil } from 'lucide-react';
|
||||
5. ✅ **安全性**:統一的權限控制確保資料安全
|
||||
|
||||
當你在開發或審查 Star ERP 客戶端後台的 UI 時,請務必參考此規範!
|
||||
|
||||
---
|
||||
|
||||
## 15. 批次匯入彈窗規範 (Batch Import Dialog)
|
||||
|
||||
為了確保系統中所有批次匯入功能(如:商品、庫存、客戶)的體驗一致,必須遵循以下 UI 結構與樣式。
|
||||
|
||||
### 15.1 標題結構
|
||||
|
||||
- **樣式**:保持簡潔,僅使用文字標題,不帶額外圖示。
|
||||
- **文字**:統一為「匯入XXXX資料」。
|
||||
|
||||
```tsx
|
||||
<DialogHeader>
|
||||
<DialogTitle>匯入商品資料</DialogTitle>
|
||||
<DialogDescription>
|
||||
請先下載範本,填寫完畢後上傳檔案進行批次處理。
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
```
|
||||
|
||||
### 15.2 分步引導區塊 (Step-by-Step Guide)
|
||||
|
||||
匯入流程必須分為三個清晰的步驟區塊:
|
||||
|
||||
#### 步驟 1:取得匯入範本
|
||||
- **容器樣式**:`bg-gray-50 rounded-lg border border-gray-100 p-4 space-y-2`
|
||||
- **標題圖示**:`<FileSpreadsheet className="w-4 h-4 text-green-600" />`
|
||||
- **下載按鈕**:`variant="outline" size="sm" className="w-full sm:w-auto button-outlined-primary"`,並明確標註 `.xlsx`。
|
||||
|
||||
#### 步驟 2:設定資訊 (選甜)
|
||||
- **容器樣式**:`space-y-2`
|
||||
- **標題圖示**:`<Info className="w-4 h-4 text-primary-main" />`
|
||||
- **欄位樣式**:使用標準 `Input`,標籤文字使用 `text-sm text-gray-700`。
|
||||
- **預設值**:若有備註欄位,應提供合適的預設值(例如:「Excel 匯入」)。
|
||||
|
||||
#### 步驟 3:上傳填寫後的檔案
|
||||
- **容器樣式**:`space-y-2`
|
||||
- **標題圖示**:`<FileUp className="w-4 h-4 text-blue-600" />`
|
||||
- **Input 樣式**:`type="file"`,並開啟 `cursor-pointer`。
|
||||
|
||||
### 15.3 規則說明面板 (Accordion Rules)
|
||||
|
||||
詳細的填寫說明必須收納於 `Accordion` 中,避免干擾主流程:
|
||||
|
||||
- **樣式**:標準灰色邊框,不使用特殊背景色 (如琥珀色)。
|
||||
- **容器**:`className="w-full border rounded-lg px-2"`
|
||||
- **觸發文字**:`text-sm text-gray-500`
|
||||
|
||||
```tsx
|
||||
<Accordion type="single" collapsible className="w-full border rounded-lg px-2">
|
||||
<AccordionItem value="rules" className="border-b-0">
|
||||
<AccordionTrigger className="text-sm text-gray-500 hover:no-underline py-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<Info className="h-4 w-4" />
|
||||
匯入規則與提示
|
||||
</div>
|
||||
</AccordionTrigger>
|
||||
<AccordionContent>
|
||||
<div className="text-sm text-gray-600 space-y-2 pb-2 pl-6">
|
||||
<ul className="list-disc space-y-1">
|
||||
<li>使用加粗文字標註關鍵欄位:<span className="font-medium text-gray-700">關鍵字</span></li>
|
||||
<li>說明文字簡潔明瞭。</li>
|
||||
</ul>
|
||||
</div>
|
||||
</AccordionContent>
|
||||
</AccordionItem>
|
||||
</Accordion>
|
||||
```
|
||||
|
||||
### 15.4 底部操作 (Footer)
|
||||
|
||||
- **取消按鈕**:`variant="outline"`,且為 `button-outlined-primary`。
|
||||
- **提交按鈕**:`button-filled-primary`,且在處理中時顯示 `Loader2`。
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -28,3 +28,4 @@ Thumbs.db
|
||||
/docs/pptx_build
|
||||
/docs/presentation
|
||||
docs/Monthly_Report_2026_01.pptx
|
||||
docs/f6_1770350984272.xlsx
|
||||
|
||||
@@ -22,7 +22,6 @@ class ProductSyncController extends Controller
|
||||
'external_pos_id' => 'required|string',
|
||||
'name' => 'required|string',
|
||||
'price' => 'nullable|numeric',
|
||||
'sku' => 'nullable|string',
|
||||
'barcode' => 'nullable|string',
|
||||
'category' => 'nullable|string',
|
||||
'unit' => 'nullable|string',
|
||||
|
||||
@@ -12,6 +12,10 @@ use App\Modules\Inventory\Models\Product;
|
||||
use App\Modules\Inventory\Models\Inventory;
|
||||
use App\Modules\Inventory\Models\InventoryTransaction;
|
||||
use App\Modules\Inventory\Models\WarehouseProductSafetyStock;
|
||||
use App\Modules\Inventory\Imports\InventoryImport;
|
||||
use App\Modules\Inventory\Exports\InventoryTemplateExport;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
use App\Modules\Core\Contracts\CoreServiceInterface;
|
||||
|
||||
@@ -49,12 +53,18 @@ class InventoryController extends Controller
|
||||
->pluck('safety_stock', 'product_id')
|
||||
->mapWithKeys(fn($val, $key) => [(string)$key => (float)$val]);
|
||||
|
||||
// 3. 準備 inventories (批號分組)
|
||||
$items = $warehouse->inventories()
|
||||
->with(['product.baseUnit', 'lastIncomingTransaction', 'lastOutgoingTransaction'])
|
||||
->get();
|
||||
|
||||
$inventories = $items->groupBy('product_id')->map(function ($batchItems) use ($safetyStockMap) {
|
||||
// 判斷是否為販賣機並調整分組
|
||||
$isVending = $warehouse->type === 'vending';
|
||||
|
||||
$inventories = $items->groupBy(function ($item) use ($isVending) {
|
||||
return $isVending
|
||||
? $item->product_id . '-' . ($item->location ?? 'NO-SLOT')
|
||||
: $item->product_id;
|
||||
})->map(function ($batchItems) use ($safetyStockMap, $isVending) {
|
||||
$firstItem = $batchItems->first();
|
||||
$product = $firstItem->product;
|
||||
$totalQuantity = $batchItems->sum('quantity');
|
||||
@@ -94,6 +104,7 @@ class InventoryController extends Controller
|
||||
'safetyStock' => null, // 批號層級不再有安全庫存
|
||||
'status' => '正常',
|
||||
'batchNumber' => $inv->batch_number ?? 'BATCH-' . $inv->id,
|
||||
'location' => $inv->location,
|
||||
'expiryDate' => $inv->expiry_date ? $inv->expiry_date->format('Y-m-d') : null,
|
||||
'lastInboundDate' => $inv->lastIncomingTransaction ? ($inv->lastIncomingTransaction->actual_time ? $inv->lastIncomingTransaction->actual_time->format('Y-m-d') : $inv->lastIncomingTransaction->created_at->format('Y-m-d')) : null,
|
||||
'lastOutboundDate' => $inv->lastOutgoingTransaction ? ($inv->lastOutgoingTransaction->actual_time ? $inv->lastOutgoingTransaction->actual_time->format('Y-m-d') : $inv->lastOutgoingTransaction->created_at->format('Y-m-d')) : null,
|
||||
@@ -163,10 +174,11 @@ class InventoryController extends Controller
|
||||
'items.*.productId' => 'required|exists:products,id',
|
||||
'items.*.quantity' => 'required|numeric|min:0.01',
|
||||
'items.*.unit_cost' => 'nullable|numeric|min:0', // 新增成本驗證
|
||||
'items.*.batchMode' => 'required|in:existing,new',
|
||||
'items.*.batchMode' => 'required|in:existing,new,none',
|
||||
'items.*.inventoryId' => 'required_if:items.*.batchMode,existing|nullable|exists:inventories,id',
|
||||
'items.*.originCountry' => 'required_if:items.*.batchMode,new|nullable|string|max:2',
|
||||
'items.*.expiryDate' => 'nullable|date',
|
||||
'items.*.location' => 'nullable|string|max:50',
|
||||
]);
|
||||
|
||||
return DB::transaction(function () use ($validated, $warehouse) {
|
||||
@@ -188,6 +200,26 @@ class InventoryController extends Controller
|
||||
if (isset($item['unit_cost'])) {
|
||||
$inventory->unit_cost = $item['unit_cost'];
|
||||
}
|
||||
} elseif ($item['batchMode'] === 'none') {
|
||||
// 模式 C:不使用批號 (自動累加至 NO-BATCH)
|
||||
$inventory = $warehouse->inventories()->withTrashed()->firstOrNew(
|
||||
[
|
||||
'product_id' => $item['productId'],
|
||||
'batch_number' => 'NO-BATCH'
|
||||
],
|
||||
[
|
||||
'quantity' => 0,
|
||||
'unit_cost' => $item['unit_cost'] ?? 0,
|
||||
'total_value' => 0,
|
||||
'arrival_date' => $validated['inboundDate'],
|
||||
'expiry_date' => null,
|
||||
'origin_country' => 'TW',
|
||||
]
|
||||
);
|
||||
|
||||
if ($inventory->trashed()) {
|
||||
$inventory->restore();
|
||||
}
|
||||
} else {
|
||||
// 模式 B:建立新批號
|
||||
$originCountry = $item['originCountry'] ?? 'TW';
|
||||
@@ -209,6 +241,7 @@ class InventoryController extends Controller
|
||||
'quantity' => 0,
|
||||
'unit_cost' => $item['unit_cost'] ?? 0, // 新增
|
||||
'total_value' => 0, // 稍後計算
|
||||
'location' => $item['location'] ?? null,
|
||||
'arrival_date' => $validated['inboundDate'],
|
||||
'expiry_date' => $item['expiryDate'] ?? null,
|
||||
'origin_country' => $originCountry,
|
||||
@@ -583,4 +616,35 @@ class InventoryController extends Controller
|
||||
|
||||
return redirect()->back()->with('error', '未提供查詢參數');
|
||||
}
|
||||
|
||||
/**
|
||||
* 匯入入庫
|
||||
*/
|
||||
public function import(Request $request, Warehouse $warehouse)
|
||||
{
|
||||
$request->validate([
|
||||
'file' => 'required|mimes:xlsx,xls,csv',
|
||||
'inboundDate' => 'required|date',
|
||||
'notes' => 'nullable|string',
|
||||
]);
|
||||
|
||||
try {
|
||||
Excel::import(
|
||||
new InventoryImport($warehouse, $request->inboundDate, $request->notes),
|
||||
$request->file('file')
|
||||
);
|
||||
|
||||
return back()->with('success', '庫存資料匯入成功');
|
||||
} catch (\Exception $e) {
|
||||
return back()->withErrors(['file' => '匯入過程中發生錯誤: ' . $e->getMessage()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 下載匯入範本 (.xlsx)
|
||||
*/
|
||||
public function template()
|
||||
{
|
||||
return Excel::download(new InventoryTemplateExport, '庫存匯入範本.xlsx');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +31,51 @@ class SafetyStockController extends Controller
|
||||
];
|
||||
});
|
||||
|
||||
// 準備現有庫存列表 (用於庫存量對比)
|
||||
// 獲取現有庫存 (用於抓取「已在倉庫中」的商品)
|
||||
$inventoryProductIds = Inventory::where('warehouse_id', $warehouse->id)->pluck('product_id')->unique();
|
||||
|
||||
// 準備安全庫存設定列表 (從資料庫讀取)
|
||||
$existingSettings = WarehouseProductSafetyStock::where('warehouse_id', $warehouse->id)
|
||||
->with(['product.category', 'product.baseUnit'])
|
||||
->get();
|
||||
|
||||
$existingProductIds = $existingSettings->pluck('product_id')->toArray();
|
||||
|
||||
// 找出:有庫存但是「還沒設定過安全庫存」的商品
|
||||
$missingProductIds = $inventoryProductIds->diff($existingProductIds);
|
||||
|
||||
$missingProducts = Product::whereIn('id', $missingProductIds)
|
||||
->with(['category', 'baseUnit'])
|
||||
->get();
|
||||
|
||||
// 合併:已設定的 + 有庫存未設定的 (預設值 0)
|
||||
$safetyStockSettings = $existingSettings->map(function ($setting) {
|
||||
return [
|
||||
'id' => (string) $setting->id,
|
||||
'warehouseId' => (string) $setting->warehouse_id,
|
||||
'productId' => (string) $setting->product_id,
|
||||
'productName' => $setting->product->name,
|
||||
'productType' => $setting->product->category ? $setting->product->category->name : '其他',
|
||||
'safetyStock' => (float) $setting->safety_stock,
|
||||
'unit' => $setting->product->baseUnit?->name ?? '個',
|
||||
'updatedAt' => $setting->updated_at->toIso8601String(),
|
||||
'isNew' => false, // 標記為舊有設定
|
||||
];
|
||||
})->concat($missingProducts->map(function ($product) use ($warehouse) {
|
||||
return [
|
||||
'id' => 'temp_' . $product->id, // 暫時 ID
|
||||
'warehouseId' => (string) $warehouse->id,
|
||||
'productId' => (string) $product->id,
|
||||
'productName' => $product->name,
|
||||
'productType' => $product->category ? $product->category->name : '其他',
|
||||
'safetyStock' => 0, // 預設 0
|
||||
'unit' => $product->baseUnit?->name ?? '個',
|
||||
'updatedAt' => now()->toIso8601String(),
|
||||
'isNew' => true, // 標記為建議新增
|
||||
];
|
||||
}))->values();
|
||||
|
||||
// 原本的 inventories 映射 (供顯示對比)
|
||||
$inventories = Inventory::where('warehouse_id', $warehouse->id)
|
||||
->select('product_id', DB::raw('SUM(quantity) as total_quantity'))
|
||||
->groupBy('product_id')
|
||||
@@ -43,23 +87,6 @@ class SafetyStockController extends Controller
|
||||
];
|
||||
});
|
||||
|
||||
// 準備安全庫存設定列表 (從新表格讀取)
|
||||
$safetyStockSettings = WarehouseProductSafetyStock::where('warehouse_id', $warehouse->id)
|
||||
->with(['product.category', 'product.baseUnit'])
|
||||
->get()
|
||||
->map(function ($setting) {
|
||||
return [
|
||||
'id' => (string) $setting->id,
|
||||
'warehouseId' => (string) $setting->warehouse_id,
|
||||
'productId' => (string) $setting->product_id,
|
||||
'productName' => $setting->product->name,
|
||||
'productType' => $setting->product->category ? $setting->product->category->name : '其他',
|
||||
'safetyStock' => (float) $setting->safety_stock,
|
||||
'unit' => $setting->product->baseUnit?->name ?? '個',
|
||||
'updatedAt' => $setting->updated_at->toIso8601String(),
|
||||
];
|
||||
});
|
||||
|
||||
return Inertia::render('Warehouse/SafetyStockSettings', [
|
||||
'warehouse' => $warehouse,
|
||||
'safetyStockSettings' => $safetyStockSettings,
|
||||
|
||||
@@ -212,7 +212,8 @@ class TransferOrderController extends Controller
|
||||
return [
|
||||
'product_id' => (string) $inv->product_id,
|
||||
'product_name' => $inv->product->name,
|
||||
'product_code' => $inv->product->code, // Added code
|
||||
'product_code' => $inv->product->code,
|
||||
'product_barcode' => $inv->product->barcode,
|
||||
'batch_number' => $inv->batch_number,
|
||||
'quantity' => (float) $inv->quantity,
|
||||
'unit_cost' => (float) $inv->unit_cost,
|
||||
|
||||
@@ -123,6 +123,7 @@ class WarehouseController extends Controller
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'code' => 'required|string|max:20|unique:warehouses,code',
|
||||
'name' => 'required|string|max:50',
|
||||
'address' => 'nullable|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
@@ -131,14 +132,6 @@ class WarehouseController extends Controller
|
||||
'driver_name' => 'nullable|string|max:50',
|
||||
]);
|
||||
|
||||
// 自動產生代碼
|
||||
$prefix = 'WH';
|
||||
$lastWarehouse = Warehouse::latest('id')->first();
|
||||
$nextId = $lastWarehouse ? $lastWarehouse->id + 1 : 1;
|
||||
$code = $prefix . str_pad($nextId, 3, '0', STR_PAD_LEFT);
|
||||
|
||||
$validated['code'] = $code;
|
||||
|
||||
Warehouse::create($validated);
|
||||
|
||||
return redirect()->back()->with('success', '倉庫已建立');
|
||||
@@ -147,6 +140,7 @@ class WarehouseController extends Controller
|
||||
public function update(Request $request, Warehouse $warehouse)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'code' => 'required|string|max:20|unique:warehouses,code,' . $warehouse->id,
|
||||
'name' => 'required|string|max:50',
|
||||
'address' => 'nullable|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
@@ -162,8 +156,9 @@ class WarehouseController extends Controller
|
||||
|
||||
public function destroy(Warehouse $warehouse)
|
||||
{
|
||||
// 檢查是否有相關聯的採購單
|
||||
if ($warehouse->purchaseOrders()->exists()) {
|
||||
// 檢查是否有相關聯的採購單 (跨模組檢查,不使用模型關聯以符合解耦規範)
|
||||
$hasPurchaseOrders = \App\Modules\Procurement\Models\PurchaseOrder::where('warehouse_id', $warehouse->id)->exists();
|
||||
if ($hasPurchaseOrders) {
|
||||
return redirect()->back()->with('error', '無法刪除:該倉庫有相關聯的採購單,請先處理採購單。');
|
||||
}
|
||||
|
||||
|
||||
87
app/Modules/Inventory/Exports/InventoryTemplateExport.php
Normal file
87
app/Modules/Inventory/Exports/InventoryTemplateExport.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Exports;
|
||||
|
||||
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
|
||||
use Maatwebsite\Excel\Concerns\FromArray;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
use Maatwebsite\Excel\Concerns\WithTitle;
|
||||
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
||||
|
||||
class InventoryTemplateExport implements WithMultipleSheets
|
||||
{
|
||||
public function sheets(): array
|
||||
{
|
||||
return [
|
||||
new InventoryDataSheet(),
|
||||
new InventoryInstructionSheet(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class InventoryDataSheet implements FromArray, WithHeadings, WithTitle, ShouldAutoSize
|
||||
{
|
||||
public function array(): array
|
||||
{
|
||||
// 資料分頁保持完全空白
|
||||
return [];
|
||||
}
|
||||
|
||||
public function headings(): array
|
||||
{
|
||||
return [
|
||||
'商品條碼',
|
||||
'商品代號',
|
||||
'商品名稱',
|
||||
'數量',
|
||||
'入庫單價',
|
||||
'儲位/貨道',
|
||||
'批號',
|
||||
'產地',
|
||||
'效期',
|
||||
];
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
{
|
||||
return '資料填寫';
|
||||
}
|
||||
}
|
||||
|
||||
class InventoryInstructionSheet implements FromArray, WithHeadings, WithTitle, ShouldAutoSize
|
||||
{
|
||||
public function array(): array
|
||||
{
|
||||
return [
|
||||
['商品條碼', '擇一輸入', '系統會「優先」依據條碼匹配商品。若有填寫,條碼必須存在於系統中'],
|
||||
['商品代號', '擇一輸入', '若條碼未填寫,系統會依據代號匹配商品'],
|
||||
['商品名稱', '選填', '僅供對照參考,匯入時系統會自動忽略此欄位內容'],
|
||||
['數量', '必填', '入庫的商品數量,須為大於 0 的數字'],
|
||||
['入庫單價', '選填', '未填寫時將預設使用商品的「採購成本價」'],
|
||||
['儲位/貨道', '選填', '一般倉庫請填寫「儲位(位址)」,販賣機倉庫請填寫「貨道編號」(如: A1)'],
|
||||
['批號', '選填', '如需批次控管請填寫,若留空系統會自動標記為 "NO-BATCH"'],
|
||||
['產地', '選填', '商品的生產地資訊 (如:TW)'],
|
||||
['效期', '選填', '格式請務必使用 YYYY-MM-DD (例如: 2026-12-31)'],
|
||||
['', '', ''],
|
||||
['倉庫類型參考', '', '系統支援以下倉庫性質:'],
|
||||
['標準倉', '', '一般總倉、儲備倉'],
|
||||
['生產倉', '', '加工廠、中央廚房、原材料存放處'],
|
||||
['門市倉', '', '前台通路、店舖銷售現場'],
|
||||
['販賣機', '', 'IoT 自動販賣機設備,建議搭配「貨道」填寫'],
|
||||
['', '', ''],
|
||||
['匹配與匯入規則', '', '1. 系統會優先比對「商品條碼」,其次為「商品代號」。'],
|
||||
['', '', '2. 庫存將匯入至您在匯入前於系統介面所選擇的目標倉庫。'],
|
||||
['', '', '3. 若需區分不同貨道或批次,請分行填寫。'],
|
||||
];
|
||||
}
|
||||
|
||||
public function headings(): array
|
||||
{
|
||||
return ['欄位名稱', '必要性', '填寫說明'];
|
||||
}
|
||||
|
||||
public function title(): string
|
||||
{
|
||||
return '填寫規則';
|
||||
}
|
||||
}
|
||||
137
app/Modules/Inventory/Imports/InventoryImport.php
Normal file
137
app/Modules/Inventory/Imports/InventoryImport.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Imports;
|
||||
|
||||
use App\Modules\Inventory\Models\Product;
|
||||
use App\Modules\Inventory\Models\Inventory;
|
||||
use App\Modules\Inventory\Models\Warehouse;
|
||||
use Maatwebsite\Excel\Concerns\ToModel;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
||||
use Maatwebsite\Excel\Concerns\WithValidation;
|
||||
use Maatwebsite\Excel\Concerns\WithMapping;
|
||||
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
|
||||
use Maatwebsite\Excel\Imports\HeadingRowFormatter;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class InventoryImport implements ToModel, WithHeadingRow, WithValidation, WithMapping, SkipsEmptyRows
|
||||
{
|
||||
private $warehouse;
|
||||
private $inboundDate;
|
||||
private $notes;
|
||||
|
||||
public function __construct(Warehouse $warehouse, string $inboundDate, ?string $notes = null)
|
||||
{
|
||||
HeadingRowFormatter::default('none');
|
||||
$this->warehouse = $warehouse;
|
||||
$this->inboundDate = $inboundDate;
|
||||
$this->notes = $notes;
|
||||
}
|
||||
|
||||
public function map($row): array
|
||||
{
|
||||
// 處理條碼或代號為字串
|
||||
if (isset($row['商品條碼'])) {
|
||||
$row['商品條碼'] = (string) $row['商品條碼'];
|
||||
}
|
||||
if (isset($row['商品代號'])) {
|
||||
$row['商品代號'] = (string) $row['商品代號'];
|
||||
}
|
||||
if (isset($row['儲位/貨道'])) {
|
||||
$row['儲位/貨道'] = (string) $row['儲位/貨道'];
|
||||
}
|
||||
return $row;
|
||||
}
|
||||
|
||||
public function model(array $row)
|
||||
{
|
||||
// 查找商品
|
||||
$product = null;
|
||||
if (!empty($row['商品條碼'])) {
|
||||
$product = Product::where('barcode', $row['商品條碼'])->first();
|
||||
}
|
||||
if (!$product && !empty($row['商品代號'])) {
|
||||
$product = Product::where('code', $row['商品代號'])->first();
|
||||
}
|
||||
|
||||
if (!$product) {
|
||||
return null; // 透過 Validation 攔截
|
||||
}
|
||||
|
||||
$quantity = (float) $row['數量'];
|
||||
$unitCost = isset($row['入庫單價']) ? (float) $row['入庫單價'] : ($product->cost_price ?? 0);
|
||||
$location = $row['儲位/貨道'] ?? null;
|
||||
|
||||
// 批號邏輯:若 Excel 留空則使用 NO-BATCH
|
||||
$batchNumber = !empty($row['批號']) ? $row['批號'] : 'NO-BATCH';
|
||||
$originCountry = $row['產地'] ?? 'TW';
|
||||
$expiryDate = !empty($row['效期']) ? $row['效期'] : null;
|
||||
|
||||
return DB::transaction(function () use ($product, $quantity, $unitCost, $location, $batchNumber, $originCountry, $expiryDate) {
|
||||
// 使用與 InventoryController 相同的 firstOrNew 邏輯
|
||||
$inventory = $this->warehouse->inventories()->withTrashed()->firstOrNew(
|
||||
[
|
||||
'product_id' => $product->id,
|
||||
'batch_number' => $batchNumber,
|
||||
'location' => $location, // 加入儲位/貨道作為區分關鍵字
|
||||
],
|
||||
[
|
||||
'quantity' => 0,
|
||||
'unit_cost' => $unitCost,
|
||||
'total_value' => 0,
|
||||
'arrival_date' => $this->inboundDate,
|
||||
'expiry_date' => $expiryDate,
|
||||
'origin_country' => $originCountry,
|
||||
]
|
||||
);
|
||||
|
||||
if ($inventory->trashed()) {
|
||||
$inventory->restore();
|
||||
}
|
||||
|
||||
// 更新數量
|
||||
$oldQty = $inventory->quantity;
|
||||
$inventory->quantity += $quantity;
|
||||
|
||||
// 更新單價與總價值
|
||||
$inventory->unit_cost = $unitCost;
|
||||
$inventory->total_value = $inventory->quantity * $unitCost;
|
||||
$inventory->save();
|
||||
|
||||
// 記錄交易歷史
|
||||
$inventory->transactions()->create([
|
||||
'warehouse_id' => $this->warehouse->id,
|
||||
'product_id' => $product->id,
|
||||
'batch_number' => $inventory->batch_number,
|
||||
'quantity' => $quantity,
|
||||
'unit_cost' => $unitCost,
|
||||
'type' => '手動入庫',
|
||||
'reason' => 'Excel 匯入入庫',
|
||||
'balance_before' => $oldQty,
|
||||
'balance_after' => $inventory->quantity,
|
||||
'actual_time' => $this->inboundDate,
|
||||
'notes' => $this->notes,
|
||||
'expiry_date' => $inventory->expiry_date,
|
||||
]);
|
||||
|
||||
return $inventory;
|
||||
});
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'商品條碼' => ['nullable', 'string'],
|
||||
'商品代號' => ['nullable', 'string'],
|
||||
'數量' => [
|
||||
'required_with:商品條碼,商品代號', // 只有在有商品資訊時,數量才是必填
|
||||
'numeric',
|
||||
'min:0' // 允許數量為 0
|
||||
],
|
||||
'入庫單價' => ['nullable', 'numeric', 'min:0'],
|
||||
'儲位/貨道' => ['nullable', 'string', 'max:50'],
|
||||
'批號' => ['nullable', 'string'],
|
||||
'效期' => ['nullable', 'date'],
|
||||
'產地' => ['nullable', 'string', 'max:2'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,6 @@ class Product extends Model
|
||||
protected $fillable = [
|
||||
'code',
|
||||
'barcode',
|
||||
'sku',
|
||||
'name',
|
||||
'external_pos_id',
|
||||
'category_id',
|
||||
|
||||
@@ -56,6 +56,8 @@ Route::middleware('auth')->group(function () {
|
||||
Route::middleware('permission:inventory.adjust')->group(function () {
|
||||
Route::get('/warehouses/{warehouse}/inventory/create', [InventoryController::class, 'create'])->name('warehouses.inventory.create');
|
||||
Route::post('/warehouses/{warehouse}/inventory', [InventoryController::class, 'store'])->name('warehouses.inventory.store');
|
||||
Route::get('/warehouses/inventory/template', [InventoryController::class, 'template'])->name('warehouses.inventory.template');
|
||||
Route::post('/warehouses/{warehouse}/inventory/import', [InventoryController::class, 'import'])->name('warehouses.inventory.import');
|
||||
Route::get('/warehouses/{warehouse}/inventory/{inventoryId}/edit', [InventoryController::class, 'edit'])->name('warehouses.inventory.edit');
|
||||
Route::put('/warehouses/{warehouse}/inventory/{inventoryId}', [InventoryController::class, 'update'])->name('warehouses.inventory.update');
|
||||
Route::delete('/warehouses/{warehouse}/inventory/{inventoryId}', [InventoryController::class, 'destroy'])->name('warehouses.inventory.destroy');
|
||||
|
||||
@@ -38,12 +38,11 @@ class ProductService
|
||||
// Map allowed fields
|
||||
$product->name = $data['name'];
|
||||
$product->barcode = $data['barcode'] ?? $product->barcode;
|
||||
$product->sku = $data['sku'] ?? $product->sku; // Maybe allow SKU update?
|
||||
$product->price = $data['price'] ?? 0;
|
||||
|
||||
// Generate Code if missing (use sku or external_id)
|
||||
// Generate Code if missing (use code or external_id)
|
||||
if (empty($product->code)) {
|
||||
$product->code = $data['code'] ?? ($product->sku ?? $product->external_pos_id);
|
||||
$product->code = $data['code'] ?? $product->external_pos_id;
|
||||
}
|
||||
|
||||
// Handle Category (Default: 未分類)
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('inventories', function (Blueprint $table) {
|
||||
// 不刪除舊索引(以免外鍵報錯),直接建立新的、更精確的唯一索引
|
||||
// 這樣「商品+批號+貨道」的組合就會被視為唯一,達成多貨道支援
|
||||
$table->unique(['warehouse_id', 'product_id', 'batch_number', 'location'], 'warehouse_product_batch_location_unique');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('inventories', function (Blueprint $table) {
|
||||
$table->dropUnique('warehouse_product_batch_location_unique');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
// 最直接的做法:暫時關閉外鍵檢查,然後強制刪除報錯的索引
|
||||
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
|
||||
|
||||
try {
|
||||
// 直接下達 SQL 指令刪除索引
|
||||
DB::statement('ALTER TABLE inventories DROP INDEX warehouse_product_batch_unique;');
|
||||
} catch (\Exception $e) {
|
||||
// 索引不存在則跳過
|
||||
}
|
||||
|
||||
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
|
||||
|
||||
DB::statement('ALTER TABLE inventories ADD UNIQUE INDEX warehouse_product_batch_unique (warehouse_id, product_id, batch_number);');
|
||||
|
||||
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('products', function (Blueprint $table) {
|
||||
$table->dropColumn('sku');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('products', function (Blueprint $table) {
|
||||
//
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -1,10 +1,11 @@
|
||||
import { useState } from "react";
|
||||
import { Pencil, Eye, Trash2 } from "lucide-react";
|
||||
import { Button } from "@/Components/ui/button";
|
||||
import { Link, useForm } from "@inertiajs/react";
|
||||
import { Link, useForm, usePage } from "@inertiajs/react";
|
||||
import type { PurchaseOrder } from "@/types/purchase-order";
|
||||
import { toast } from "sonner";
|
||||
import { Can } from "@/Components/Permission/Can";
|
||||
import { PageProps } from "@/types/global";
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
@@ -21,6 +22,18 @@ export function PurchaseOrderActions({
|
||||
}: { order: PurchaseOrder }) {
|
||||
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
|
||||
const { delete: destroy, processing } = useForm({});
|
||||
const { auth } = usePage<PageProps>().props;
|
||||
const permissions = auth.user?.permissions || [];
|
||||
const isSuperAdmin = auth.user?.roles?.some((r: any) => r.name === 'super-admin');
|
||||
|
||||
const canApprove = isSuperAdmin || permissions.includes('purchase_orders.approve');
|
||||
const canEdit = isSuperAdmin || permissions.includes('purchase_orders.edit');
|
||||
|
||||
// 編輯按鈕顯示邏輯:
|
||||
// 1. 草稿狀態 + 編輯權限
|
||||
// 2. 待核准狀態 + 核准權限 (讓主管能直接改)
|
||||
const showEditButton = (order.status === 'draft' && canEdit) ||
|
||||
(order.status === 'pending' && canApprove);
|
||||
|
||||
const handleConfirmDelete = () => {
|
||||
// @ts-ignore
|
||||
@@ -45,20 +58,18 @@ export function PurchaseOrderActions({
|
||||
<Eye className="h-4 w-4" />
|
||||
</Button>
|
||||
</Link>
|
||||
<Can permission="purchase_orders.edit">
|
||||
{order.status === 'draft' && (
|
||||
<Link href={`/purchase-orders/${order.id}/edit`}>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="button-outlined-primary"
|
||||
title="編輯"
|
||||
>
|
||||
<Pencil className="h-4 w-4" />
|
||||
</Button>
|
||||
</Link>
|
||||
)}
|
||||
</Can>
|
||||
{showEditButton && (
|
||||
<Link href={`/purchase-orders/${order.id}/edit`}>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="button-outlined-primary"
|
||||
title="編輯"
|
||||
>
|
||||
<Pencil className="h-4 w-4" />
|
||||
</Button>
|
||||
</Link>
|
||||
)}
|
||||
<Can permission="purchase_orders.delete">
|
||||
{order.status === 'draft' && (
|
||||
<Button
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
import React from "react";
|
||||
import { useForm } from "@inertiajs/react";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/Components/ui/dialog";
|
||||
import { Button } from "@/Components/ui/button";
|
||||
import { Input } from "@/Components/ui/input";
|
||||
import { Label } from "@/Components/ui/label";
|
||||
import { Download, FileUp, Loader2, AlertCircle, FileSpreadsheet, Info } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
import { Alert, AlertDescription } from "@/Components/ui/alert";
|
||||
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/Components/ui/accordion";
|
||||
|
||||
interface Props {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
warehouseId: string;
|
||||
warehouseName: string;
|
||||
}
|
||||
|
||||
export default function InventoryImportDialog({ open, onOpenChange, warehouseId, warehouseName }: Props) {
|
||||
const { data, setData, post, processing, errors, reset, clearErrors } = useForm({
|
||||
file: null as File | null,
|
||||
inboundDate: new Date().toISOString().split('T')[0],
|
||||
notes: "Excel 匯入",
|
||||
});
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
|
||||
post(route("warehouses.inventory.import", warehouseId), {
|
||||
forceFormData: true,
|
||||
onSuccess: () => {
|
||||
toast.success("庫存匯入完成");
|
||||
onOpenChange(false);
|
||||
reset();
|
||||
},
|
||||
onError: (err) => {
|
||||
console.error("Import error:", err);
|
||||
toast.error("匯入失敗,請檢查檔案格式");
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const handleDownloadTemplate = () => {
|
||||
window.location.href = route("warehouses.inventory.template");
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={(val) => {
|
||||
onOpenChange(val);
|
||||
if (!val) {
|
||||
reset();
|
||||
clearErrors();
|
||||
}
|
||||
}}>
|
||||
<DialogContent className="sm:max-w-[550px]">
|
||||
<DialogHeader>
|
||||
<DialogTitle>匯入庫存資料</DialogTitle>
|
||||
<DialogDescription>
|
||||
請先下載範本,填寫完畢後上傳檔案進行批次入庫。
|
||||
<div className="mt-2 p-2 bg-primary-main/5 border border-primary-main/20 rounded text-primary-main font-medium">
|
||||
目標倉庫:{warehouseName}
|
||||
</div>
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-6">
|
||||
{/* 步驟 1: 下載範本 */}
|
||||
<div className="space-y-2 p-4 bg-gray-50 rounded-lg border border-gray-100">
|
||||
<Label className="font-medium flex items-center gap-2">
|
||||
<FileSpreadsheet className="w-4 h-4 text-green-600" />
|
||||
步驟 1:取得匯入範本
|
||||
</Label>
|
||||
<div className="text-sm text-gray-500 mb-2">
|
||||
下載標準範本以確保資料格式正確。請勿修改標題欄位。
|
||||
</div>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={handleDownloadTemplate}
|
||||
className="w-full sm:w-auto button-outlined-primary"
|
||||
>
|
||||
<Download className="w-4 h-4 mr-2" />
|
||||
下載庫存匯入範本 (.xlsx)
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* 步驟 2: 設定資訊 */}
|
||||
<div className="space-y-2">
|
||||
<Label className="font-medium flex items-center gap-2">
|
||||
<Info className="w-4 h-4 text-primary-main" />
|
||||
步驟 2:設定入庫資訊
|
||||
</Label>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="inboundDate" className="text-sm text-gray-700 flex items-center gap-1.5 align-middle">
|
||||
入庫日期 <span className="text-red-500">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id="inboundDate"
|
||||
type="date"
|
||||
value={data.inboundDate}
|
||||
onChange={e => setData('inboundDate', e.target.value)}
|
||||
required
|
||||
className="cursor-pointer"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="notes" className="text-sm text-gray-700 flex items-center gap-1.5">
|
||||
入庫備註
|
||||
</Label>
|
||||
<Input
|
||||
id="notes"
|
||||
placeholder="選填備註"
|
||||
value={data.notes}
|
||||
onChange={e => setData('notes', e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 步驟 3: 上傳檔案 */}
|
||||
<div className="space-y-2">
|
||||
<Label className="font-medium flex items-center gap-2">
|
||||
<FileUp className="w-4 h-4 text-blue-600" />
|
||||
步驟 3:上傳填寫後的檔案
|
||||
</Label>
|
||||
<div className="grid w-full max-w-sm items-center gap-1.5">
|
||||
<Input
|
||||
id="file"
|
||||
type="file"
|
||||
accept=".xlsx,.xls,.csv"
|
||||
onChange={e => setData('file', e.target.files ? e.target.files[0] : null)}
|
||||
required
|
||||
className="cursor-pointer"
|
||||
/>
|
||||
</div>
|
||||
{errors.file && (
|
||||
<Alert variant="destructive" className="mt-2">
|
||||
<AlertCircle className="h-4 w-4" />
|
||||
<AlertDescription className="whitespace-pre-wrap">
|
||||
{errors.file}
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* 欄位說明 */}
|
||||
<Accordion type="single" collapsible className="w-full border rounded-lg px-2">
|
||||
<AccordionItem value="rules" className="border-b-0">
|
||||
<AccordionTrigger className="text-sm text-gray-500 hover:no-underline py-3">
|
||||
<div className="flex items-center gap-2">
|
||||
<Info className="h-4 w-4" />
|
||||
庫存匯入規則與提示
|
||||
</div>
|
||||
</AccordionTrigger>
|
||||
<AccordionContent>
|
||||
<div className="text-sm text-gray-600 space-y-2 pb-2 pl-6">
|
||||
<ul className="list-disc space-y-1">
|
||||
<li><span className="font-medium text-gray-700">商品匹配</span>:優先使用「商品條碼」匹配,其次為「商品代號」。</li>
|
||||
<li><span className="font-medium text-gray-700">無批號模式</span>:若 Excel 中的「批號」欄位保持空白,系統將自動累加至該商品的「通用紀錄」。(對販賣機而言,此紀錄通常對應至預設的一般庫存)。</li>
|
||||
<li><span className="font-medium text-gray-700">效期設定</span>:若商品無效期概念可留空,或輸入格式如:2026/12/31。</li>
|
||||
<li><span className="font-medium text-gray-700">入庫單價</span>:未填寫時將預設使用商品的「採購成本價」。</li>
|
||||
</ul>
|
||||
</div>
|
||||
</AccordionContent>
|
||||
</AccordionItem>
|
||||
</Accordion>
|
||||
|
||||
<DialogFooter>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => onOpenChange(false)}
|
||||
disabled={processing}
|
||||
className="button-outlined-primary"
|
||||
>
|
||||
取消
|
||||
</Button>
|
||||
<Button
|
||||
type="submit"
|
||||
disabled={processing || !data.file}
|
||||
className="button-filled-primary"
|
||||
>
|
||||
{processing ? (
|
||||
<>
|
||||
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
|
||||
上傳中...
|
||||
</>
|
||||
) : (
|
||||
"開始匯入"
|
||||
)}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -42,7 +42,11 @@ export default function InventoryTable({
|
||||
onView,
|
||||
onDelete,
|
||||
onViewProduct,
|
||||
}: InventoryTableProps) {
|
||||
warehouse,
|
||||
}: InventoryTableProps & { warehouse: any }) {
|
||||
// 判斷是否為販賣機倉庫
|
||||
const isVending = warehouse?.type === "vending";
|
||||
|
||||
// 每個商品的展開/折疊狀態
|
||||
const [expandedProducts, setExpandedProducts] = useState<Set<string>>(new Set());
|
||||
|
||||
@@ -131,9 +135,19 @@ export default function InventoryTable({
|
||||
) : (
|
||||
<ChevronRight className="h-5 w-5 text-gray-600" />
|
||||
)}
|
||||
<h3 className="font-semibold text-gray-900">{group.productName}</h3>
|
||||
<h3 className="font-semibold text-gray-900">
|
||||
{group.productName}
|
||||
{isVending && group.batches.length > 0 && (() => {
|
||||
const locations = Array.from(new Set(group.batches.map(b => b.location).filter(Boolean)));
|
||||
return locations.length > 0 ? (
|
||||
<span className="ml-2 text-primary-main font-bold">
|
||||
{locations.map(loc => `[${loc}]`).join('')}
|
||||
</span>
|
||||
) : null;
|
||||
})()}
|
||||
</h3>
|
||||
<span className="text-sm text-gray-500">
|
||||
{hasInventory ? `${group.batches.length} 個批號` : '無庫存'}
|
||||
{isVending ? '' : (hasInventory ? `${group.batches.length} 個批號` : '無庫存')}
|
||||
</span>
|
||||
{group.batches.some(b => b.expiryDate && new Date(b.expiryDate) < new Date()) && (
|
||||
<Badge className="bg-red-50 text-red-600 border-red-200">
|
||||
@@ -197,7 +211,8 @@ export default function InventoryTable({
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead className="w-[5%]">#</TableHead>
|
||||
<TableHead className="w-[12%]">批號</TableHead>
|
||||
<TableHead className="w-[15%]">批號</TableHead>
|
||||
<TableHead className="w-[10%]">{isVending ? "貨道" : "儲位"}</TableHead>
|
||||
<TableHead className="w-[10%]">庫存數量</TableHead>
|
||||
<Can permission="inventory.view_cost">
|
||||
<TableHead className="w-[10%]">單位成本</TableHead>
|
||||
@@ -215,6 +230,7 @@ export default function InventoryTable({
|
||||
<TableRow key={batch.id}>
|
||||
<TableCell className="text-grey-2">{index + 1}</TableCell>
|
||||
<TableCell>{batch.batchNumber || "-"}</TableCell>
|
||||
<TableCell className="font-medium text-primary-main">{batch.location || "-"}</TableCell>
|
||||
<TableCell>
|
||||
<span>{batch.quantity} {batch.unit}</span>
|
||||
</TableCell>
|
||||
|
||||
@@ -53,7 +53,16 @@ export default function SafetyStockList({
|
||||
});
|
||||
|
||||
// 獲取狀態徽章 (與 InventoryTable 保持一致)
|
||||
const getStatusBadge = (quantity: number, safetyStock: number) => {
|
||||
const getStatusBadge = (quantity: number, safetyStock: number, isNew?: boolean) => {
|
||||
// 如果是自動帶入的品項且尚未存檔,顯示「未設定」
|
||||
if (isNew) {
|
||||
return (
|
||||
<Badge variant="outline" className="text-gray-400 border-gray-200 font-normal">
|
||||
未設定
|
||||
</Badge>
|
||||
);
|
||||
}
|
||||
|
||||
const status = getSafetyStockStatus(quantity, safetyStock);
|
||||
switch (status) {
|
||||
case "正常":
|
||||
@@ -122,7 +131,7 @@ export default function SafetyStockList({
|
||||
</span>
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
{getStatusBadge(currentStock, setting.safetyStock)}
|
||||
{getStatusBadge(currentStock, setting.safetyStock, setting.isNew)}
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<div className="flex justify-end gap-2">
|
||||
|
||||
@@ -11,6 +11,9 @@ import {
|
||||
Edit,
|
||||
Info,
|
||||
FileText,
|
||||
CupSoda,
|
||||
QrCode,
|
||||
Milk,
|
||||
} from "lucide-react";
|
||||
import { Warehouse, WarehouseStats } from "@/types/warehouse";
|
||||
import { Button } from "@/Components/ui/button";
|
||||
@@ -50,17 +53,28 @@ export default function WarehouseCard({
|
||||
onEdit,
|
||||
}: WarehouseCardProps) {
|
||||
const [showInfoDialog, setShowInfoDialog] = useState(false);
|
||||
const isVending = warehouse.type === 'vending';
|
||||
|
||||
return (
|
||||
<Card
|
||||
className={`relative overflow-hidden transition-all hover:shadow-lg flex flex-col ${hasWarning
|
||||
? "border-orange-400 border-2 bg-orange-50/50"
|
||||
: "border-gray-200"
|
||||
className={`relative overflow-hidden transition-all duration-300 hover:shadow-lg flex flex-col group ${isVending
|
||||
? "border-primary-400 border-2 bg-white min-h-[300px]"
|
||||
: hasWarning
|
||||
? "border-orange-400 border-2 bg-orange-50/50"
|
||||
: "border-gray-200"
|
||||
}`}
|
||||
>
|
||||
{/* 裝飾性背景元素 (僅限販賣機) */}
|
||||
{isVending && (
|
||||
<>
|
||||
{/* LED 裝飾線條 - 保持主色調 */}
|
||||
<div className="absolute top-0 bottom-0 left-0 w-1 bg-primary-500 shadow-[1px_0_5px_rgba(var(--primary-main-rgb),0.2)]" />
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* 警告橫幅 */}
|
||||
{hasWarning && (
|
||||
<div className="absolute top-0 left-0 right-0 bg-orange-500 text-white px-4 py-1 flex items-center justify-between text-sm">
|
||||
<div className="absolute top-0 left-0 right-0 bg-orange-500 text-white px-4 py-1 flex items-center justify-between text-sm z-10">
|
||||
<div className="flex items-center gap-2">
|
||||
<AlertTriangle className="h-4 w-4" />
|
||||
<span>低庫存警告</span>
|
||||
@@ -71,12 +85,14 @@ export default function WarehouseCard({
|
||||
|
||||
<CardContent className={`p-6 flex flex-col flex-1 ${hasWarning ? "pt-12" : "pt-6"}`}>
|
||||
{/* 上半部:資訊區域 */}
|
||||
<div className="flex-1">
|
||||
<div className="flex-1 relative z-10">
|
||||
{/* 標題區塊 */}
|
||||
<div className="flex items-start justify-between mb-4">
|
||||
<div className="flex items-start justify-between mb-2">
|
||||
<div>
|
||||
<div className="flex items-center gap-2 mb-1">
|
||||
<h3 className="text-2xl font-bold">{warehouse.name}</h3>
|
||||
<h3 className="text-2xl font-bold text-gray-900">
|
||||
{warehouse.name}
|
||||
</h3>
|
||||
<button
|
||||
onClick={() => setShowInfoDialog(true)}
|
||||
className="text-gray-400 hover:text-gray-600 transition-colors"
|
||||
@@ -92,30 +108,22 @@ export default function WarehouseCard({
|
||||
{WAREHOUSE_TYPE_LABELS[warehouse.type || 'standard'] || '標準倉'}
|
||||
{warehouse.type === 'quarantine' ? ' (不計入可用)' : ' (計入可用)'}
|
||||
</Badge>
|
||||
{warehouse.type === 'transit' && warehouse.license_plate && (
|
||||
<Badge variant="secondary" className="text-xs font-normal bg-yellow-100 text-yellow-800 border-yellow-200">
|
||||
{warehouse.license_plate} {warehouse.driver_name && `(${warehouse.driver_name})`}
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="text-sm text-gray-600 mb-4 line-clamp-2 min-h-[40px]">
|
||||
{warehouse.description || "無描述"}
|
||||
{warehouse.description || (isVending ? "管理此機台的商品配貨與補貨狀況" : "無描述")}
|
||||
</div>
|
||||
|
||||
|
||||
{/* 統計區塊 - 狀態標籤 */}
|
||||
{/* 統計區塊 */}
|
||||
<div className="space-y-3">
|
||||
|
||||
{/* 帳面庫存總計 (金額) - 瑕疵倉隱藏此項以減少重複 */}
|
||||
<Can permission="inventory.view_cost">
|
||||
{warehouse.type !== 'quarantine' && (
|
||||
<div className="flex items-center justify-between p-3 rounded-lg bg-primary-50/50 border border-primary-100">
|
||||
<div className="flex items-center gap-2 text-primary-700">
|
||||
<Package className="h-4 w-4" />
|
||||
<span className="text-sm font-medium">帳面庫存總計</span>
|
||||
<div className="flex items-center justify-between p-3 rounded-lg bg-primary-50/50 border border-primary-100 text-primary-700">
|
||||
<div className="flex items-center gap-2">
|
||||
<Package className="h-4 w-4 opacity-80" />
|
||||
<span className="text-sm font-medium">帳面庫存估值</span>
|
||||
</div>
|
||||
<div className="text-sm font-bold text-primary-main">
|
||||
${Number(stats.totalValue || 0).toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}
|
||||
@@ -124,7 +132,6 @@ export default function WarehouseCard({
|
||||
)}
|
||||
</Can>
|
||||
|
||||
{/* 過期統計 (金額) */}
|
||||
<Can permission="inventory.view_cost">
|
||||
{Number(stats.abnormalValue || 0) > 0 && (
|
||||
<div className="flex items-center justify-between p-3 rounded-lg bg-red-50/50 border border-red-100 mt-3">
|
||||
@@ -141,12 +148,31 @@ export default function WarehouseCard({
|
||||
)}
|
||||
</Can>
|
||||
|
||||
|
||||
{/* 販賣機特色視覺:投幣、取物口裝飾 (移動至帳面庫存下方,顏色更顯眼) */}
|
||||
{isVending && (
|
||||
<div className="flex gap-4 mt-6 items-end">
|
||||
<div className="flex-1 h-12 bg-gray-100 rounded-lg border-2 border-gray-300 shadow-inner flex items-center justify-center relative overflow-hidden">
|
||||
<div className="absolute inset-0 bg-gradient-to-b from-gray-200/50 to-transparent pointer-events-none" />
|
||||
<div className="flex gap-1 items-end opacity-30 pb-1">
|
||||
<CupSoda className="h-5 w-5 text-gray-400 rotate-12 -translate-x-1" />
|
||||
<Milk className="h-6 w-6 text-gray-500 -rotate-12 translate-y-1" />
|
||||
<CupSoda className="h-5 w-5 text-gray-400 rotate-3" />
|
||||
<Milk className="h-5 w-5 text-gray-400 -rotate-6 translate-x-1" />
|
||||
<CupSoda className="h-6 w-6 text-gray-500 rotate-12 translate-y-1" />
|
||||
</div>
|
||||
</div>
|
||||
<div className="w-10 h-10 bg-gray-200 rounded-sm border-2 border-gray-400 flex items-center justify-center p-1 shadow-sm self-center">
|
||||
<div className="text-gray-600 opacity-60">
|
||||
<QrCode className="h-6 w-6" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* 下半部:操作按鈕 */}
|
||||
<div className="mt-5 pt-3 border-t border-gray-200">
|
||||
<div className="mt-5 pt-4 border-t border-gray-200">
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
onClick={() => onViewInventory(warehouse.id)}
|
||||
|
||||
@@ -143,14 +143,15 @@ export default function WarehouseDialog({
|
||||
{/* 倉庫編號 */}
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="code">
|
||||
倉庫編號
|
||||
倉庫編號 <span className="text-red-500">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id="code"
|
||||
value={warehouse ? formData.code : ""}
|
||||
disabled={true}
|
||||
placeholder={warehouse ? "" : "系統自動產生"}
|
||||
className="bg-gray-100"
|
||||
value={formData.code}
|
||||
onChange={(e) => setFormData({ ...formData, code: e.target.value })}
|
||||
placeholder="請輸入倉庫編號"
|
||||
required
|
||||
className="h-9"
|
||||
/>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -452,7 +452,12 @@ export default function Show({ doc }: { auth: any, doc: AdjDoc }) {
|
||||
/>
|
||||
</TableCell>
|
||||
|
||||
<TableCell className="font-semibold text-grey-0">{inv.product_name}</TableCell>
|
||||
<TableCell className="py-3">
|
||||
<div className="flex flex-col">
|
||||
<span className="font-semibold text-grey-0">{inv.product_name}</span>
|
||||
<span className="text-xs text-grey-2 font-mono">{inv.product_code}</span>
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="text-sm font-mono text-grey-2">{inv.batch_number || '-'}</TableCell>
|
||||
<TableCell className="text-sm font-mono text-grey-2">{inv.expiry_date || '-'}</TableCell>
|
||||
<TableCell className="text-right font-bold text-primary-main pr-6">{inv.quantity} {inv.unit_name}</TableCell>
|
||||
|
||||
@@ -123,7 +123,7 @@ export default function GoodsReceiptCreate({ warehouses, pendingPurchaseOrders,
|
||||
product_id: item.product_id,
|
||||
purchase_order_item_id: item.id,
|
||||
product_name: item.product_name,
|
||||
sku: item.product_code,
|
||||
product_code: item.product_code,
|
||||
unit: item.unit,
|
||||
quantity_ordered: item.quantity,
|
||||
quantity_received_so_far: item.received_quantity,
|
||||
@@ -157,7 +157,7 @@ export default function GoodsReceiptCreate({ warehouses, pendingPurchaseOrders,
|
||||
const newItem = {
|
||||
product_id: product.id,
|
||||
product_name: product.name,
|
||||
sku: product.code,
|
||||
product_code: product.code,
|
||||
quantity_received: 0,
|
||||
unit_price: product.price || 0,
|
||||
batch_number: '',
|
||||
@@ -266,7 +266,7 @@ export default function GoodsReceiptCreate({ warehouses, pendingPurchaseOrders,
|
||||
// Note: fetch might not have returned yet, so seq might be default 001 until fetch updates nextSequences
|
||||
|
||||
const datePart = dateStr.replace(/-/g, '');
|
||||
const generatedBatch = `${item.sku}-${country}-${datePart}-${seq}`;
|
||||
const generatedBatch = `${item.product_code}-${country}-${datePart}-${seq}`;
|
||||
|
||||
if (item.batch_number !== generatedBatch) {
|
||||
// Update WITHOUT triggering re-render loop
|
||||
@@ -278,7 +278,7 @@ export default function GoodsReceiptCreate({ warehouses, pendingPurchaseOrders,
|
||||
}
|
||||
}
|
||||
});
|
||||
}, [nextSequences, JSON.stringify(data.items.map(i => ({ m: i.batchMode, c: i.originCountry, s: i.sku, p: i.product_id }))), data.received_date]);
|
||||
}, [nextSequences, JSON.stringify(data.items.map(i => ({ m: i.batchMode, c: i.originCountry, s: i.product_code, p: i.product_id }))), data.received_date]);
|
||||
|
||||
const submit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
@@ -610,7 +610,7 @@ export default function GoodsReceiptCreate({ warehouses, pendingPurchaseOrders,
|
||||
<TableCell>
|
||||
<div className="flex flex-col">
|
||||
<span className="font-medium text-gray-900">{item.product_name}</span>
|
||||
<span className="text-xs text-gray-500">{item.sku}</span>
|
||||
<span className="text-xs text-gray-500">{item.product_code}</span>
|
||||
</div>
|
||||
</TableCell>
|
||||
|
||||
@@ -654,7 +654,7 @@ export default function GoodsReceiptCreate({ warehouses, pendingPurchaseOrders,
|
||||
className="w-16 text-center px-1"
|
||||
/>
|
||||
<div className="flex-1 text-sm font-mono bg-gray-50 px-3 py-2 rounded text-gray-600 truncate">
|
||||
{getBatchPreview(item.product_id, item.sku, item.originCountry || 'TW', data.received_date)}
|
||||
{getBatchPreview(item.product_id, item.product_code, item.originCountry || 'TW', data.received_date)}
|
||||
</div>
|
||||
</div>
|
||||
</TableCell>
|
||||
|
||||
@@ -84,7 +84,8 @@ export default function Show({ order }: any) {
|
||||
const toggleSelectAll = () => {
|
||||
const filtered = availableInventory.filter(inv =>
|
||||
inv.product_name.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
||||
inv.product_code.toLowerCase().includes(searchQuery.toLowerCase())
|
||||
inv.product_code.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
||||
(inv.product_barcode && inv.product_barcode.toLowerCase().includes(searchQuery.toLowerCase()))
|
||||
);
|
||||
const filteredKeys = filtered.map(inv => `${inv.product_id}-${inv.batch_number}`);
|
||||
|
||||
@@ -338,10 +339,10 @@ export default function Show({ order }: any) {
|
||||
<DialogContent className="max-w-4xl max-h-[85vh] flex flex-col p-6">
|
||||
<DialogHeader className="flex flex-row items-center justify-between space-y-0 pb-4">
|
||||
<DialogTitle className="text-xl">選擇來源庫存 ({order.from_warehouse_name})</DialogTitle>
|
||||
<div className="relative w-64">
|
||||
<div className="relative w-72">
|
||||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-grey-3" />
|
||||
<Input
|
||||
placeholder="搜尋品名或代號..."
|
||||
placeholder="搜尋品名、代號或條碼..."
|
||||
className="pl-9 h-9 border-2 border-grey-3 focus:ring-primary-main"
|
||||
value={searchQuery}
|
||||
onChange={(e) => setSearchQuery(e.target.value)}
|
||||
@@ -364,7 +365,8 @@ export default function Show({ order }: any) {
|
||||
checked={availableInventory.length > 0 && (() => {
|
||||
const filtered = availableInventory.filter(inv =>
|
||||
inv.product_name.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
||||
inv.product_code.toLowerCase().includes(searchQuery.toLowerCase())
|
||||
inv.product_code.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
||||
(inv.product_barcode && inv.product_barcode.toLowerCase().includes(searchQuery.toLowerCase()))
|
||||
);
|
||||
const filteredKeys = filtered.map(inv => `${inv.product_id}-${inv.batch_number}`);
|
||||
return filteredKeys.length > 0 && filteredKeys.every(k => selectedInventory.includes(k));
|
||||
@@ -373,7 +375,7 @@ export default function Show({ order }: any) {
|
||||
/>
|
||||
</TableHead>
|
||||
|
||||
<TableHead className="font-medium text-grey-600">品名</TableHead>
|
||||
<TableHead className="font-medium text-grey-600">品名 / 代號</TableHead>
|
||||
<TableHead className="font-medium text-grey-600">批號</TableHead>
|
||||
<TableHead className="font-medium text-grey-600">效期</TableHead>
|
||||
<TableHead className="text-right font-medium text-grey-600 pr-6">現有庫存</TableHead>
|
||||
@@ -383,7 +385,8 @@ export default function Show({ order }: any) {
|
||||
{(() => {
|
||||
const filtered = availableInventory.filter(inv =>
|
||||
inv.product_name.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
||||
inv.product_code.toLowerCase().includes(searchQuery.toLowerCase())
|
||||
inv.product_code.toLowerCase().includes(searchQuery.toLowerCase()) ||
|
||||
(inv.product_barcode && inv.product_barcode.toLowerCase().includes(searchQuery.toLowerCase()))
|
||||
);
|
||||
|
||||
if (filtered.length === 0) {
|
||||
@@ -412,7 +415,12 @@ export default function Show({ order }: any) {
|
||||
/>
|
||||
</TableCell>
|
||||
|
||||
<TableCell className="font-semibold text-grey-0">{inv.product_name}</TableCell>
|
||||
<TableCell className="py-3">
|
||||
<div className="flex flex-col">
|
||||
<span className="font-semibold text-grey-0">{inv.product_name}</span>
|
||||
<span className="text-xs text-grey-2 font-mono">{inv.product_code}</span>
|
||||
</div>
|
||||
</TableCell>
|
||||
<TableCell className="text-sm font-mono text-grey-2">{inv.batch_number || '-'}</TableCell>
|
||||
<TableCell className="text-sm font-mono text-grey-2">{inv.expiry_date || '-'}</TableCell>
|
||||
<TableCell className="text-right font-bold text-primary-main pr-6">{inv.quantity} {inv.unit_name}</TableCell>
|
||||
|
||||
@@ -510,6 +510,9 @@ export default function AddInventoryPage({ warehouse, products }: Props) {
|
||||
數量 <span className="text-red-500">*</span>
|
||||
</TableHead>
|
||||
<TableHead className="w-[90px]">單位</TableHead>
|
||||
<TableHead className="w-[120px]">
|
||||
{warehouse.type === 'vending' ? '貨道' : '儲位'}
|
||||
</TableHead>
|
||||
<TableHead className="w-[50px]"></TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
@@ -548,7 +551,7 @@ export default function AddInventoryPage({ warehouse, products }: Props) {
|
||||
<TableCell>
|
||||
<div className="space-y-2">
|
||||
<SearchableSelect
|
||||
value={item.batchMode === 'new' ? 'new_batch' : (item.inventoryId || "")}
|
||||
value={item.batchMode === 'none' ? 'no_batch' : (item.batchMode === 'new' ? 'new_batch' : (item.inventoryId || ""))}
|
||||
onValueChange={(value) => {
|
||||
if (value === 'new_batch') {
|
||||
handleUpdateItem(item.tempId, {
|
||||
@@ -557,6 +560,15 @@ export default function AddInventoryPage({ warehouse, products }: Props) {
|
||||
originCountry: 'TW',
|
||||
expiryDate: undefined
|
||||
});
|
||||
} else if (value === 'no_batch') {
|
||||
// 嘗試匹配現有的 NO-BATCH 紀錄
|
||||
const existingNoBatch = (batchesCache[item.productId]?.batches || []).find(b => b.batchNumber === 'NO-BATCH');
|
||||
handleUpdateItem(item.tempId, {
|
||||
batchMode: 'none',
|
||||
inventoryId: existingNoBatch?.inventoryId || undefined,
|
||||
originCountry: 'TW',
|
||||
expiryDate: undefined
|
||||
});
|
||||
} else {
|
||||
const selectedBatch = (batchesCache[item.productId]?.batches || []).find(b => b.inventoryId === value);
|
||||
handleUpdateItem(item.tempId, {
|
||||
@@ -568,9 +580,10 @@ export default function AddInventoryPage({ warehouse, products }: Props) {
|
||||
}
|
||||
}}
|
||||
options={[
|
||||
{ label: "📦 不使用批號 (自動累加)", value: "no_batch" },
|
||||
{ label: "+ 建立新批號", value: "new_batch" },
|
||||
...(batchesCache[item.productId]?.batches || []).map(b => ({
|
||||
label: `${b.batchNumber} - 庫存: ${b.quantity}`,
|
||||
label: `${b.batchNumber === 'NO-BATCH' ? '(無批號紀錄)' : b.batchNumber} - 庫存: ${b.quantity}`,
|
||||
value: b.inventoryId
|
||||
}))
|
||||
]}
|
||||
@@ -582,50 +595,55 @@ export default function AddInventoryPage({ warehouse, products }: Props) {
|
||||
{errors[`item-${index}-batch`]}
|
||||
</p>
|
||||
)}
|
||||
|
||||
{item.batchMode === 'new' && (
|
||||
<>
|
||||
<div className="flex items-center gap-2 mt-2">
|
||||
<div className="flex-1">
|
||||
<Input
|
||||
value={item.originCountry || ""}
|
||||
onChange={(e) => {
|
||||
const val = e.target.value.toUpperCase().slice(0, 2);
|
||||
handleUpdateItem(item.tempId, { originCountry: val });
|
||||
}}
|
||||
maxLength={2}
|
||||
placeholder="產地"
|
||||
className="h-8 text-xs text-center border-gray-300"
|
||||
/>
|
||||
</div>
|
||||
<div className="flex-[3] text-xs bg-primary-50/50 text-primary-main px-2 py-1 rounded border border-primary-200/50 font-mono overflow-hidden whitespace-nowrap">
|
||||
{getBatchPreview(item.productId, product?.code, item.originCountry || 'TW', inboundDate)}
|
||||
</div>
|
||||
<div className="flex items-center gap-2 mt-2">
|
||||
<div className="flex-1">
|
||||
<Input
|
||||
value={item.originCountry || ""}
|
||||
onChange={(e) => {
|
||||
const val = e.target.value.toUpperCase().slice(0, 2);
|
||||
handleUpdateItem(item.tempId, { originCountry: val });
|
||||
}}
|
||||
maxLength={2}
|
||||
placeholder="產地"
|
||||
className="h-8 text-xs text-center border-gray-300"
|
||||
/>
|
||||
</div>
|
||||
{/* 新增效期輸入 (在新增批號模式下) */}
|
||||
<div className="mt-2 flex items-center gap-2">
|
||||
<span className="text-xs text-gray-500 whitespace-nowrap">效期:</span>
|
||||
<div className="relative flex-1">
|
||||
<Calendar className="absolute left-2.5 top-2.5 h-3.5 w-3.5 text-gray-400 pointer-events-none" />
|
||||
<Input
|
||||
type="date"
|
||||
value={item.expiryDate || ""}
|
||||
onChange={(e) =>
|
||||
handleUpdateItem(item.tempId, {
|
||||
expiryDate: e.target.value,
|
||||
})
|
||||
}
|
||||
className="h-8 pl-8 text-xs border-gray-300 w-full"
|
||||
/>
|
||||
</div>
|
||||
<div className="flex-[3] text-xs bg-primary-50/50 text-primary-main px-2 py-1 rounded border border-primary-200/50 font-mono overflow-hidden whitespace-nowrap">
|
||||
{getBatchPreview(item.productId, product?.code, item.originCountry || 'TW', inboundDate)}
|
||||
</div>
|
||||
</>
|
||||
</div>
|
||||
)}
|
||||
{/* 新增效期輸入 (僅在建立新批號模式下) */}
|
||||
{item.batchMode === 'new' && (
|
||||
<div className="mt-2 flex items-center gap-2">
|
||||
<span className="text-xs text-gray-500 whitespace-nowrap">效期:</span>
|
||||
<div className="relative flex-1">
|
||||
<Calendar className="absolute left-2.5 top-2.5 h-3.5 w-3.5 text-gray-400 pointer-events-none" />
|
||||
<Input
|
||||
type="date"
|
||||
value={item.expiryDate || ""}
|
||||
onChange={(e) =>
|
||||
handleUpdateItem(item.tempId, {
|
||||
expiryDate: e.target.value,
|
||||
})
|
||||
}
|
||||
className="h-8 pl-8 text-xs border-gray-300 w-full"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{item.batchMode === 'none' && (
|
||||
<div className="mt-1 px-2 py-1 bg-amber-50 text-amber-700 text-[10px] rounded border border-amber-100 flex items-center gap-1">
|
||||
<span className="shrink-0 font-bold">INFO</span>
|
||||
系統將自動累加至該商品的通用庫存紀錄
|
||||
</div>
|
||||
)}
|
||||
|
||||
{item.batchMode === 'existing' && item.inventoryId && (
|
||||
<div className="flex flax-col gap-1 mt-1">
|
||||
<div className="text-xs text-gray-500 font-mono">
|
||||
效期: {item.expiryDate || '未設定'}
|
||||
效期: {item.expiryDate || '無效期紀錄'}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
@@ -699,6 +717,16 @@ export default function AddInventoryPage({ warehouse, products }: Props) {
|
||||
)}
|
||||
</TableCell>
|
||||
|
||||
{/* 儲位/貨道 */}
|
||||
<TableCell>
|
||||
<Input
|
||||
value={item.location || ""}
|
||||
onChange={(e) => handleUpdateItem(item.tempId, { location: e.target.value })}
|
||||
className="border-gray-300"
|
||||
placeholder={warehouse.type === 'vending' ? "貨道 (如: A1)" : "儲位 (選填)"}
|
||||
/>
|
||||
</TableCell>
|
||||
|
||||
{/* 刪除按鈕 */}
|
||||
<TableCell>
|
||||
<Button
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useState, useMemo } from "react";
|
||||
import { ArrowLeft, PackagePlus, AlertTriangle, Shield, Boxes } from "lucide-react";
|
||||
import { ArrowLeft, PackagePlus, AlertTriangle, Shield, Boxes, FileUp } from "lucide-react";
|
||||
import { Button } from "@/Components/ui/button";
|
||||
import AuthenticatedLayout from "@/Layouts/AuthenticatedLayout";
|
||||
import { Head, Link, router } from "@inertiajs/react";
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
AlertDialogTitle,
|
||||
} from "@/Components/ui/alert-dialog";
|
||||
import { Can } from "@/Components/Permission/Can";
|
||||
import InventoryImportDialog from "@/Components/Warehouse/Inventory/InventoryImportDialog";
|
||||
|
||||
// 庫存頁面 Props
|
||||
interface Props {
|
||||
@@ -38,6 +39,7 @@ export default function WarehouseInventoryPage({
|
||||
const [searchTerm, setSearchTerm] = useState("");
|
||||
const [typeFilter, setTypeFilter] = useState<string>("all");
|
||||
const [deleteId, setDeleteId] = useState<string | null>(null);
|
||||
const [importDialogOpen, setImportDialogOpen] = useState(false);
|
||||
|
||||
// 篩選庫存列表
|
||||
const filteredInventories = useMemo(() => {
|
||||
@@ -131,7 +133,7 @@ export default function WarehouseInventoryPage({
|
||||
</div>
|
||||
|
||||
{/* 操作按鈕 (位於標題下方) */}
|
||||
<div className="flex items-center gap-3 mb-6">
|
||||
<div className="flex flex-wrap items-center gap-3 mb-6">
|
||||
{/* 安全庫存設定按鈕 */}
|
||||
<Can permission="inventory.safety_stock">
|
||||
<Link href={route('warehouses.safety-stock.index', warehouse.id)}>
|
||||
@@ -157,6 +159,18 @@ export default function WarehouseInventoryPage({
|
||||
庫存警告:{lowStockItems} 項
|
||||
</Button>
|
||||
|
||||
{/* 匯入入庫按鈕 */}
|
||||
<Can permission="inventory.adjust">
|
||||
<Button
|
||||
variant="outline"
|
||||
className="button-outlined-primary"
|
||||
onClick={() => setImportDialogOpen(true)}
|
||||
>
|
||||
<FileUp className="mr-2 h-4 w-4" />
|
||||
匯入入庫
|
||||
</Button>
|
||||
</Can>
|
||||
|
||||
{/* 新增庫存按鈕 */}
|
||||
<Can permission="inventory.adjust">
|
||||
<Link href={route('warehouses.inventory.create', warehouse.id)}>
|
||||
@@ -187,6 +201,7 @@ export default function WarehouseInventoryPage({
|
||||
onView={handleView}
|
||||
onDelete={confirmDelete}
|
||||
onViewProduct={handleViewProduct}
|
||||
warehouse={warehouse}
|
||||
/>
|
||||
</div>
|
||||
<AlertDialog open={!!deleteId} onOpenChange={(open) => !open && setDeleteId(null)}>
|
||||
@@ -210,6 +225,14 @@ export default function WarehouseInventoryPage({
|
||||
</AlertDialogFooter>
|
||||
</AlertDialogContent>
|
||||
</AlertDialog>
|
||||
|
||||
{/* 匯入對話框 */}
|
||||
<InventoryImportDialog
|
||||
open={importDialogOpen}
|
||||
onOpenChange={setImportDialogOpen}
|
||||
warehouseId={warehouse.id}
|
||||
warehouseName={warehouse.name}
|
||||
/>
|
||||
</div>
|
||||
</AuthenticatedLayout>
|
||||
);
|
||||
|
||||
@@ -69,18 +69,39 @@ export default function SafetyStockPage({
|
||||
};
|
||||
|
||||
const handleEdit = (updatedSetting: SafetyStockSetting) => {
|
||||
router.put(route('warehouses.safety-stock.update', [warehouse.id, updatedSetting.id]), {
|
||||
safetyStock: updatedSetting.safetyStock,
|
||||
}, {
|
||||
onSuccess: () => {
|
||||
setEditingSetting(null);
|
||||
toast.success(`成功更新 ${updatedSetting.productName} 的安全庫存`);
|
||||
},
|
||||
onError: (errors) => {
|
||||
const firstError = Object.values(errors)[0];
|
||||
toast.error(typeof firstError === 'string' ? firstError : "更新失敗");
|
||||
}
|
||||
});
|
||||
// 如果 ID 包含 temp_,表示這是一個「自動帶入但尚未存入資料庫」的建議項
|
||||
// 這種情況應該呼叫 POST (store) 而非 PUT (update),以避免被後端路由模型綁定報 404
|
||||
if (updatedSetting.id.includes('temp_')) {
|
||||
router.post(route('warehouses.safety-stock.store', warehouse.id), {
|
||||
settings: [{
|
||||
productId: updatedSetting.productId,
|
||||
quantity: updatedSetting.safetyStock
|
||||
}],
|
||||
}, {
|
||||
onSuccess: () => {
|
||||
setEditingSetting(null);
|
||||
toast.success(`成功設定 ${updatedSetting.productName} 的安全庫存`);
|
||||
},
|
||||
onError: (errors) => {
|
||||
const firstError = Object.values(errors)[0];
|
||||
toast.error(typeof firstError === 'string' ? firstError : "設定失敗");
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// 已存在的項目,正常執行 PUT 更新
|
||||
router.put(route('warehouses.safety-stock.update', [warehouse.id, updatedSetting.id]), {
|
||||
safetyStock: updatedSetting.safetyStock,
|
||||
}, {
|
||||
onSuccess: () => {
|
||||
setEditingSetting(null);
|
||||
toast.success(`成功更新 ${updatedSetting.productName} 的安全庫存`);
|
||||
},
|
||||
onError: (errors) => {
|
||||
const firstError = Object.values(errors)[0];
|
||||
toast.error(typeof firstError === 'string' ? firstError : "更新失敗");
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const handleDelete = () => {
|
||||
|
||||
@@ -47,6 +47,7 @@ export interface WarehouseInventory {
|
||||
safetyStock: number | null;
|
||||
status?: '正常' | '低於'; // 後端可能回傳的狀態
|
||||
batchNumber: string; // 批號 (Mock for now)
|
||||
location?: string; // 儲位/貨道
|
||||
expiryDate: string;
|
||||
lastInboundDate: string | null;
|
||||
lastOutboundDate: string | null;
|
||||
@@ -112,6 +113,7 @@ export interface SafetyStockSetting {
|
||||
unit?: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
isNew?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -174,9 +176,10 @@ export interface InboundItem {
|
||||
largeUnit?: string;
|
||||
conversionRate?: number;
|
||||
selectedUnit?: 'base' | 'large';
|
||||
batchMode?: 'existing' | 'new'; // 批號模式
|
||||
batchMode?: 'existing' | 'new' | 'none'; // 批號模式
|
||||
inventoryId?: string; // 選擇現有批號時的 ID
|
||||
batchNumber?: string;
|
||||
location?: string; // 儲位/貨道
|
||||
originCountry?: string; // 新增產地
|
||||
expiryDate?: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user