feat(inventory): 販賣機視覺優化、修復匯入日期缺失與倉庫刪除權限錯誤

This commit is contained in:
2026-02-09 10:19:46 +08:00
parent f22df90e01
commit 5e542752ba
14 changed files with 255 additions and 71 deletions

View File

@@ -53,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');

View File

@@ -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,

View File

@@ -156,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', '無法刪除:該倉庫有相關聯的採購單,請先處理採購單。');
}

View File

@@ -34,7 +34,6 @@ class InventoryDataSheet implements FromArray, WithHeadings, WithTitle, ShouldAu
'商品代號',
'商品名稱',
'數量',
'單位',
'入庫單價',
'儲位/貨道',
'批號',
@@ -58,7 +57,6 @@ class InventoryInstructionSheet implements FromArray, WithHeadings, WithTitle, S
['商品代號', '擇一輸入', '若條碼未填寫,系統會依據代號匹配商品'],
['商品名稱', '選填', '僅供對照參考,匯入時系統會自動忽略此欄位內容'],
['數量', '必填', '入庫的商品數量,須為大於 0 的數字'],
['單位', '必填', '單位 (如:個、件)'],
['入庫單價', '選填', '未填寫時將預設使用商品的「採購成本價」'],
['儲位/貨道', '選填', '一般倉庫請填寫「儲位(位址)」,販賣機倉庫請填寫「貨道編號」(如: A1)'],
['批號', '選填', '如需批次控管請填寫,若留空系統會自動標記為 "NO-BATCH"'],

View File

@@ -9,10 +9,11 @@ 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
class InventoryImport implements ToModel, WithHeadingRow, WithValidation, WithMapping, SkipsEmptyRows
{
private $warehouse;
private $inboundDate;
@@ -35,6 +36,9 @@ class InventoryImport implements ToModel, WithHeadingRow, WithValidation, WithMa
if (isset($row['商品代號'])) {
$row['商品代號'] = (string) $row['商品代號'];
}
if (isset($row['儲位/貨道'])) {
$row['儲位/貨道'] = (string) $row['儲位/貨道'];
}
return $row;
}
@@ -100,8 +104,11 @@ class InventoryImport implements ToModel, WithHeadingRow, WithValidation, WithMa
'batch_number' => $inventory->batch_number,
'quantity' => $quantity,
'unit_cost' => $unitCost,
'transaction_type' => '手動入庫',
'type' => '手動入庫',
'reason' => 'Excel 匯入入庫',
'balance_before' => $oldQty,
'balance_after' => $inventory->quantity,
'actual_time' => $this->inboundDate,
'notes' => $this->notes,
'expiry_date' => $inventory->expiry_date,
]);
@@ -115,8 +122,11 @@ class InventoryImport implements ToModel, WithHeadingRow, WithValidation, WithMa
return [
'商品條碼' => ['nullable', 'string'],
'商品代號' => ['nullable', 'string'],
'數量' => ['required', 'numeric', 'min:0.01'],
'單位' => ['required', 'string'],
'數量' => [
'required_with:商品條碼,商品代號', // 只有在有商品資訊時,數量才是必填
'numeric',
'min:0' // 允許數量為 0
],
'入庫單價' => ['nullable', 'numeric', 'min:0'],
'儲位/貨道' => ['nullable', 'string', 'max:50'],
'批號' => ['nullable', 'string'],