大更新
This commit is contained in:
@@ -54,7 +54,7 @@ class PurchaseOrderController extends Controller
|
||||
|
||||
public function create()
|
||||
{
|
||||
$vendors = Vendor::with('products')->get()->map(function ($vendor) {
|
||||
$vendors = Vendor::with(['products.baseUnit', 'products.largeUnit', 'products.purchaseUnit'])->get()->map(function ($vendor) {
|
||||
return [
|
||||
'id' => (string) $vendor->id,
|
||||
'name' => $vendor->name,
|
||||
@@ -62,9 +62,11 @@ class PurchaseOrderController extends Controller
|
||||
return [
|
||||
'productId' => (string) $product->id,
|
||||
'productName' => $product->name,
|
||||
'unit' => $product->purchase_unit ?: ($product->large_unit ?: $product->base_unit), // 優先使用採購單位 > 大單位 > 基本單位
|
||||
'base_unit' => $product->base_unit,
|
||||
'purchase_unit' => $product->purchase_unit ?: $product->large_unit, // 若無採購單位,預設為大單位
|
||||
'base_unit_id' => $product->base_unit_id,
|
||||
'base_unit_name' => $product->baseUnit?->name,
|
||||
'large_unit_id' => $product->large_unit_id,
|
||||
'large_unit_name' => $product->largeUnit?->name,
|
||||
'purchase_unit_id' => $product->purchase_unit_id,
|
||||
'conversion_rate' => (float) $product->conversion_rate,
|
||||
'lastPrice' => (float) ($product->pivot->last_price ?? 0),
|
||||
];
|
||||
@@ -96,6 +98,7 @@ class PurchaseOrderController extends Controller
|
||||
'items.*.productId' => 'required|exists:products,id',
|
||||
'items.*.quantity' => 'required|numeric|min:0.01',
|
||||
'items.*.unitPrice' => 'required|numeric|min:0',
|
||||
'items.*.unitId' => 'nullable|exists:units,id', // 驗證單位ID
|
||||
]);
|
||||
|
||||
try {
|
||||
@@ -157,6 +160,7 @@ class PurchaseOrderController extends Controller
|
||||
$order->items()->create([
|
||||
'product_id' => $item['productId'],
|
||||
'quantity' => $item['quantity'],
|
||||
'unit_id' => $item['unitId'] ?? null, // 儲存單位ID
|
||||
'unit_price' => $item['unitPrice'],
|
||||
'subtotal' => $item['quantity'] * $item['unitPrice'],
|
||||
]);
|
||||
@@ -174,20 +178,39 @@ class PurchaseOrderController extends Controller
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$order = PurchaseOrder::with(['vendor', 'warehouse', 'user', 'items.product'])->findOrFail($id);
|
||||
$order = PurchaseOrder::with(['vendor', 'warehouse', 'user', 'items.product.baseUnit', 'items.product.largeUnit'])->findOrFail($id);
|
||||
|
||||
// Transform items to include product details needed for frontend calculation
|
||||
$order->items->transform(function ($item) {
|
||||
$order->items->transform(function ($item) use ($order) {
|
||||
$product = $item->product;
|
||||
if ($product) {
|
||||
// 手動附加 productName 和 unit (因為已從 $appends 移除)
|
||||
// 手動附加所有必要的屬性
|
||||
$item->productId = (string) $product->id;
|
||||
$item->productName = $product->name;
|
||||
$item->productId = $product->id;
|
||||
$item->base_unit = $product->base_unit;
|
||||
$item->purchase_unit = $product->purchase_unit ?: $product->large_unit; // Fallback logic same as Create
|
||||
$item->base_unit_id = $product->base_unit_id;
|
||||
$item->base_unit_name = $product->baseUnit?->name;
|
||||
$item->large_unit_id = $product->large_unit_id;
|
||||
$item->large_unit_name = $product->largeUnit?->name;
|
||||
$item->purchase_unit_id = $product->purchase_unit_id;
|
||||
|
||||
$item->conversion_rate = (float) $product->conversion_rate;
|
||||
// 優先使用採購單位 > 大單位 > 基本單位
|
||||
$item->unit = $product->purchase_unit ?: ($product->large_unit ?: $product->base_unit);
|
||||
|
||||
// Fetch last price
|
||||
$lastPrice = DB::table('product_vendor')
|
||||
->where('vendor_id', $order->vendor_id)
|
||||
->where('product_id', $product->id)
|
||||
->value('last_price');
|
||||
$item->previousPrice = (float) ($lastPrice ?? 0);
|
||||
|
||||
// 設定當前選中的單位 ID (from saved item)
|
||||
$item->unitId = $item->unit_id;
|
||||
|
||||
// 決定 selectedUnit (用於 UI 顯示)
|
||||
if ($item->unitId && $item->large_unit_id && $item->unitId == $item->large_unit_id) {
|
||||
$item->selectedUnit = 'large';
|
||||
} else {
|
||||
$item->selectedUnit = 'base';
|
||||
}
|
||||
|
||||
$item->unitPrice = (float) $item->unit_price;
|
||||
}
|
||||
return $item;
|
||||
@@ -202,7 +225,7 @@ class PurchaseOrderController extends Controller
|
||||
{
|
||||
$order = PurchaseOrder::with(['items.product'])->findOrFail($id);
|
||||
|
||||
$vendors = Vendor::with('products')->get()->map(function ($vendor) {
|
||||
$vendors = Vendor::with(['products.baseUnit', 'products.largeUnit', 'products.purchaseUnit'])->get()->map(function ($vendor) {
|
||||
return [
|
||||
'id' => (string) $vendor->id,
|
||||
'name' => $vendor->name,
|
||||
@@ -210,9 +233,11 @@ class PurchaseOrderController extends Controller
|
||||
return [
|
||||
'productId' => (string) $product->id,
|
||||
'productName' => $product->name,
|
||||
'unit' => $product->purchase_unit ?: ($product->large_unit ?: $product->base_unit),
|
||||
'base_unit' => $product->base_unit,
|
||||
'purchase_unit' => $product->purchase_unit ?: $product->large_unit,
|
||||
'base_unit_id' => $product->base_unit_id,
|
||||
'base_unit_name' => $product->baseUnit?->name,
|
||||
'large_unit_id' => $product->large_unit_id,
|
||||
'large_unit_name' => $product->largeUnit?->name,
|
||||
'purchase_unit_id' => $product->purchase_unit_id,
|
||||
'conversion_rate' => (float) $product->conversion_rate,
|
||||
'lastPrice' => (float) ($product->pivot->last_price ?? 0),
|
||||
];
|
||||
@@ -228,17 +253,38 @@ class PurchaseOrderController extends Controller
|
||||
});
|
||||
|
||||
// Transform items for frontend form
|
||||
$order->items->transform(function ($item) {
|
||||
// Transform items for frontend form
|
||||
$vendorId = $order->vendor_id;
|
||||
$order->items->transform(function ($item) use ($vendorId) {
|
||||
$product = $item->product;
|
||||
if ($product) {
|
||||
// 手動附加所有必要的屬性 (因為已從 $appends 移除)
|
||||
$item->productId = (string) $product->id; // Ensure consistent ID type
|
||||
// 手動附加所有必要的屬性
|
||||
$item->productId = (string) $product->id;
|
||||
$item->productName = $product->name;
|
||||
$item->base_unit = $product->base_unit;
|
||||
$item->purchase_unit = $product->purchase_unit ?: $product->large_unit;
|
||||
$item->base_unit_id = $product->base_unit_id;
|
||||
$item->base_unit_name = $product->baseUnit?->name;
|
||||
$item->large_unit_id = $product->large_unit_id;
|
||||
$item->large_unit_name = $product->largeUnit?->name;
|
||||
|
||||
$item->conversion_rate = (float) $product->conversion_rate;
|
||||
// 優先使用採購單位 > 大單位 > 基本單位
|
||||
$item->unit = $product->purchase_unit ?: ($product->large_unit ?: $product->base_unit);
|
||||
|
||||
// Fetch last price
|
||||
$lastPrice = DB::table('product_vendor')
|
||||
->where('vendor_id', $vendorId)
|
||||
->where('product_id', $product->id)
|
||||
->value('last_price');
|
||||
$item->previousPrice = (float) ($lastPrice ?? 0);
|
||||
|
||||
// 設定當前選中的單位 ID
|
||||
$item->unitId = $item->unit_id; // 資料庫中的 unit_id
|
||||
|
||||
// 決定 selectedUnit (用於 UI 狀態)
|
||||
if ($item->unitId && $item->large_unit_id && $item->unitId == $item->large_unit_id) {
|
||||
$item->selectedUnit = 'large';
|
||||
} else {
|
||||
$item->selectedUnit = 'base';
|
||||
}
|
||||
|
||||
$item->unitPrice = (float) $item->unit_price;
|
||||
}
|
||||
return $item;
|
||||
@@ -265,6 +311,7 @@ class PurchaseOrderController extends Controller
|
||||
'items.*.productId' => 'required|exists:products,id',
|
||||
'items.*.quantity' => 'required|numeric|min:0.01',
|
||||
'items.*.unitPrice' => 'required|numeric|min:0',
|
||||
'items.*.unitId' => 'nullable|exists:units,id', // 驗證單位ID
|
||||
]);
|
||||
|
||||
try {
|
||||
@@ -296,6 +343,7 @@ class PurchaseOrderController extends Controller
|
||||
$order->items()->create([
|
||||
'product_id' => $item['productId'],
|
||||
'quantity' => $item['quantity'],
|
||||
'unit_id' => $item['unitId'] ?? null, // 儲存單位ID
|
||||
'unit_price' => $item['unitPrice'],
|
||||
'subtotal' => $item['quantity'] * $item['unitPrice'],
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user