chore: 完善模組化架構遷移與修復前端顯示錯誤
- 修正所有模組 Controller 的 Model 引用路徑 (App\Modules\...) - 更新 ProductionOrder 與 ProductionOrderItem 模型結構以符合新版邏輯 - 修復 resources/js/utils/format.ts 在處理空值時導致 toLocaleString 崩潰的問題 - 清除全域路徑與 Controller 遷移殘留檔案
This commit is contained in:
124
app/Modules/Inventory/Controllers/SafetyStockController.php
Normal file
124
app/Modules/Inventory/Controllers/SafetyStockController.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Inventory\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
|
||||
use App\Modules\Inventory\Models\Warehouse;
|
||||
use App\Modules\Inventory\Models\WarehouseProductSafetyStock;
|
||||
use App\Modules\Inventory\Models\Product;
|
||||
use App\Modules\Inventory\Models\Inventory;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class SafetyStockController extends Controller
|
||||
{
|
||||
/**
|
||||
* 顯示安全庫存設定頁面
|
||||
*/
|
||||
public function index(Warehouse $warehouse)
|
||||
{
|
||||
$allProducts = Product::with(['category', 'baseUnit'])->get();
|
||||
|
||||
// 準備可選商品列表
|
||||
$availableProducts = $allProducts->map(function ($product) {
|
||||
return [
|
||||
'id' => (string) $product->id,
|
||||
'name' => $product->name,
|
||||
'type' => $product->category ? $product->category->name : '其他',
|
||||
'unit' => $product->baseUnit?->name ?? '個',
|
||||
];
|
||||
});
|
||||
|
||||
// 準備現有庫存列表 (用於庫存量對比)
|
||||
$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,
|
||||
'safetyStockSettings' => $safetyStockSettings,
|
||||
'inventories' => $inventories,
|
||||
'availableProducts' => $availableProducts,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量儲存安全庫存設定
|
||||
*/
|
||||
public function store(Request $request, Warehouse $warehouse)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'settings' => 'required|array|min:1',
|
||||
'settings.*.productId' => 'required|exists:products,id',
|
||||
'settings.*.quantity' => 'required|numeric|min:0',
|
||||
]);
|
||||
|
||||
DB::transaction(function () use ($validated, $warehouse) {
|
||||
foreach ($validated['settings'] as $item) {
|
||||
WarehouseProductSafetyStock::updateOrCreate(
|
||||
[
|
||||
'warehouse_id' => $warehouse->id,
|
||||
'product_id' => $item['productId'],
|
||||
],
|
||||
[
|
||||
'safety_stock' => $item['quantity'],
|
||||
]
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
return redirect()->back()->with('success', '安全庫存設定已更新');
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新單筆安全庫存設定
|
||||
*/
|
||||
public function update(Request $request, Warehouse $warehouse, WarehouseProductSafetyStock $safetyStock)
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'safetyStock' => 'required|numeric|min:0',
|
||||
]);
|
||||
|
||||
$safetyStock->update([
|
||||
'safety_stock' => $validated['safetyStock'],
|
||||
]);
|
||||
|
||||
return redirect()->back()->with('success', '安全庫存已更新');
|
||||
}
|
||||
|
||||
/**
|
||||
* 刪除安全庫存設定
|
||||
*/
|
||||
public function destroy(Warehouse $warehouse, WarehouseProductSafetyStock $safetyStock)
|
||||
{
|
||||
$safetyStock->delete();
|
||||
|
||||
return redirect()->back()->with('success', '安全庫存設定已移除');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user