管理採購單的商品金額修正
This commit is contained in:
@@ -97,8 +97,8 @@ class PurchaseOrderController extends Controller
|
||||
'items' => 'required|array|min:1',
|
||||
'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
|
||||
'items.*.subtotal' => 'required|numeric|min:0', // 總金額
|
||||
'items.*.unitId' => 'nullable|exists:units,id',
|
||||
]);
|
||||
|
||||
try {
|
||||
@@ -122,7 +122,7 @@ class PurchaseOrderController extends Controller
|
||||
|
||||
$totalAmount = 0;
|
||||
foreach ($validated['items'] as $item) {
|
||||
$totalAmount += $item['quantity'] * $item['unitPrice'];
|
||||
$totalAmount += $item['subtotal'];
|
||||
}
|
||||
|
||||
// Simple tax calculation (e.g., 5%)
|
||||
@@ -157,12 +157,15 @@ class PurchaseOrderController extends Controller
|
||||
]);
|
||||
|
||||
foreach ($validated['items'] as $item) {
|
||||
// 反算單價
|
||||
$unitPrice = $item['quantity'] > 0 ? $item['subtotal'] / $item['quantity'] : 0;
|
||||
|
||||
$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'],
|
||||
'unit_id' => $item['unitId'] ?? null,
|
||||
'unit_price' => $unitPrice,
|
||||
'subtotal' => $item['subtotal'],
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -310,8 +313,8 @@ class PurchaseOrderController extends Controller
|
||||
'items' => 'required|array|min:1',
|
||||
'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
|
||||
'items.*.subtotal' => 'required|numeric|min:0', // 總金額
|
||||
'items.*.unitId' => 'nullable|exists:units,id',
|
||||
]);
|
||||
|
||||
try {
|
||||
@@ -319,7 +322,7 @@ class PurchaseOrderController extends Controller
|
||||
|
||||
$totalAmount = 0;
|
||||
foreach ($validated['items'] as $item) {
|
||||
$totalAmount += $item['quantity'] * $item['unitPrice'];
|
||||
$totalAmount += $item['subtotal'];
|
||||
}
|
||||
|
||||
// Simple tax calculation (e.g., 5%)
|
||||
@@ -340,12 +343,15 @@ class PurchaseOrderController extends Controller
|
||||
// Sync items
|
||||
$order->items()->delete();
|
||||
foreach ($validated['items'] as $item) {
|
||||
// 反算單價
|
||||
$unitPrice = $item['quantity'] > 0 ? $item['subtotal'] / $item['quantity'] : 0;
|
||||
|
||||
$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'],
|
||||
'unit_id' => $item['unitId'] ?? null,
|
||||
'unit_price' => $unitPrice,
|
||||
'subtotal' => $item['subtotal'],
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user