生產工單BOM以及批號完善
This commit is contained in:
@@ -3,8 +3,9 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Warehouse;
|
||||
use App\Models\Inventory;
|
||||
use App\Models\WarehouseProductSafetyStock;
|
||||
use App\Models\Product;
|
||||
use App\Models\Inventory;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -16,8 +17,6 @@ class SafetyStockController extends Controller
|
||||
*/
|
||||
public function index(Warehouse $warehouse)
|
||||
{
|
||||
$warehouse->load(['inventories.product.category']);
|
||||
|
||||
$allProducts = Product::with(['category', 'baseUnit'])->get();
|
||||
|
||||
// 準備可選商品列表
|
||||
@@ -30,32 +29,34 @@ class SafetyStockController extends Controller
|
||||
];
|
||||
});
|
||||
|
||||
// 準備現有庫存列表 (用於狀態計算)
|
||||
$inventories = $warehouse->inventories->map(function ($inv) {
|
||||
return [
|
||||
'id' => (string) $inv->id,
|
||||
'productId' => (string) $inv->product_id,
|
||||
'quantity' => (float) $inv->quantity,
|
||||
'safetyStock' => (float) $inv->safety_stock,
|
||||
];
|
||||
});
|
||||
|
||||
// 準備安全庫存設定列表
|
||||
$safetyStockSettings = $warehouse->inventories->filter(function($inv) {
|
||||
return !is_null($inv->safety_stock);
|
||||
})->map(function ($inv) {
|
||||
return [
|
||||
'id' => (string) $inv->id,
|
||||
'warehouseId' => (string) $inv->warehouse_id,
|
||||
'productId' => (string) $inv->product_id,
|
||||
'productName' => $inv->product->name,
|
||||
'productType' => $inv->product->category ? $inv->product->category->name : '其他',
|
||||
'safetyStock' => (float) $inv->safety_stock,
|
||||
'unit' => $inv->product->baseUnit?->name ?? '個',
|
||||
'updatedAt' => $inv->updated_at->toIso8601String(),
|
||||
];
|
||||
})->values();
|
||||
// 準備現有庫存列表 (用於庫存量對比)
|
||||
$inventories = Inventory::where('warehouse_id', $warehouse->id)
|
||||
->select('product_id', DB::raw('SUM(quantity) as total_quantity'))
|
||||
->groupBy('product_id')
|
||||
->get()
|
||||
->map(function ($inv) {
|
||||
return [
|
||||
'productId' => (string) $inv->product_id,
|
||||
'quantity' => (float) $inv->total_quantity,
|
||||
];
|
||||
});
|
||||
|
||||
// 準備安全庫存設定列表 (從新表格讀取)
|
||||
$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,
|
||||
@@ -78,7 +79,7 @@ class SafetyStockController extends Controller
|
||||
|
||||
DB::transaction(function () use ($validated, $warehouse) {
|
||||
foreach ($validated['settings'] as $item) {
|
||||
Inventory::updateOrCreate(
|
||||
WarehouseProductSafetyStock::updateOrCreate(
|
||||
[
|
||||
'warehouse_id' => $warehouse->id,
|
||||
'product_id' => $item['productId'],
|
||||
@@ -96,13 +97,13 @@ class SafetyStockController extends Controller
|
||||
/**
|
||||
* 更新單筆安全庫存設定
|
||||
*/
|
||||
public function update(Request $request, Warehouse $warehouse, Inventory $inventory)
|
||||
public function update(Request $request, Warehouse $warehouse, WarehouseProductSafetyStock $safetyStock)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'safetyStock' => 'required|numeric|min:0',
|
||||
]);
|
||||
|
||||
$inventory->update([
|
||||
$safetyStock->update([
|
||||
'safety_stock' => $validated['safetyStock'],
|
||||
]);
|
||||
|
||||
@@ -110,13 +111,11 @@ class SafetyStockController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* 刪除 (歸零) 安全庫存設定
|
||||
* 刪除安全庫存設定
|
||||
*/
|
||||
public function destroy(Warehouse $warehouse, Inventory $inventory)
|
||||
public function destroy(Warehouse $warehouse, WarehouseProductSafetyStock $safetyStock)
|
||||
{
|
||||
$inventory->update([
|
||||
'safety_stock' => null,
|
||||
]);
|
||||
$safetyStock->delete();
|
||||
|
||||
return redirect()->back()->with('success', '安全庫存設定已移除');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user