管理採購單的商品金額修正
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 29s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped

This commit is contained in:
2026-01-08 17:51:06 +08:00
parent cbd8d11848
commit 3088959c7c
4 changed files with 53 additions and 40 deletions

View File

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