[FIX] 修正採購單大單位換算問題並建立 Git 開發規範
All checks were successful
ERP-Deploy-Production / deploy-production (push) Successful in 1m19s
All checks were successful
ERP-Deploy-Production / deploy-production (push) Successful in 1m19s
This commit is contained in:
@@ -133,6 +133,7 @@ class GoodsReceiptController extends Controller
|
||||
'id' => $po->id,
|
||||
'code' => $po->code,
|
||||
'status' => $po->status,
|
||||
'supplierId' => $po->vendor_id, // Alias for frontend
|
||||
'vendor_id' => $po->vendor_id,
|
||||
'vendor_name' => $po->vendor?->name ?? '',
|
||||
'warehouse_id' => $po->warehouse_id,
|
||||
@@ -140,15 +141,35 @@ class GoodsReceiptController extends Controller
|
||||
'items' => $po->items->map(function ($item) use ($products) {
|
||||
$product = $products->get($item->product_id);
|
||||
$remaining = max(0, $item->quantity - ($item->received_quantity ?? 0));
|
||||
|
||||
// 獲取單位名稱
|
||||
$baseUnitName = $product?->baseUnit?->name ?? '個';
|
||||
$largeUnitName = $product?->largeUnit?->name ?? '';
|
||||
|
||||
// 判斷當前採購使用的單位 (這需要從 PurchaseOrderItem 獲取 unit_id 並與產品的 large_unit_id 比較)
|
||||
$selectedUnit = 'base';
|
||||
if ($item->unit_id && $product && $item->unit_id == $product->large_unit_id) {
|
||||
$selectedUnit = 'large';
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $item->id,
|
||||
'productId' => $item->product_id, // Alias for frontend
|
||||
'product_id' => $item->product_id,
|
||||
'productName' => $product?->name ?? '', // Alias for frontend
|
||||
'product_name' => $product?->name ?? '',
|
||||
'product_code' => $product?->code ?? '',
|
||||
'unit' => $product?->baseUnit?->name ?? '個',
|
||||
'unit' => $product?->baseUnit?->name ?? '個', // 預設顯示文字
|
||||
'selectedUnit' => $selectedUnit,
|
||||
'base_unit_id' => $product?->base_unit_id,
|
||||
'base_unit_name' => $baseUnitName,
|
||||
'large_unit_id' => $product?->large_unit_id,
|
||||
'large_unit_name' => $largeUnitName,
|
||||
'conversion_rate' => $product?->conversion_rate ?? 1,
|
||||
'quantity' => $item->quantity,
|
||||
'received_quantity' => $item->received_quantity ?? 0,
|
||||
'remaining' => $remaining,
|
||||
'unitPrice' => $item->unit_price, // Alias for frontend
|
||||
'unit_price' => $item->unit_price,
|
||||
];
|
||||
})->filter(fn($item) => $item['remaining'] > 0)->values(),
|
||||
@@ -209,12 +230,24 @@ class GoodsReceiptController extends Controller
|
||||
$product = $products->get($item->product_id);
|
||||
$poItem = $poItems->get($item->purchase_order_item_id);
|
||||
|
||||
// 判斷單位
|
||||
$selectedUnit = 'base';
|
||||
if ($poItem && $product && $poItem->unit_id && $poItem->unit_id == $product->large_unit_id) {
|
||||
$selectedUnit = 'large';
|
||||
}
|
||||
|
||||
return [
|
||||
'product_id' => $item->product_id,
|
||||
'purchase_order_item_id' => $item->purchase_order_item_id,
|
||||
'product_name' => $product?->name ?? '',
|
||||
'product_code' => $product?->code ?? '',
|
||||
'unit' => $product?->baseUnit?->name ?? '個',
|
||||
'unit' => $poItem && $selectedUnit === 'large' ? ($product?->largeUnit?->name ?? '') : ($product?->baseUnit?->name ?? '個'),
|
||||
'selectedUnit' => $selectedUnit,
|
||||
'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 ?? '',
|
||||
'conversion_rate' => $product?->conversion_rate ?? 1,
|
||||
'quantity_ordered' => $poItem ? $poItem->quantity : null,
|
||||
'quantity_received_so_far' => $poItem ? ($poItem->received_quantity ?? 0) : null,
|
||||
'quantity_received' => (float) $item->quantity_received,
|
||||
@@ -237,6 +270,7 @@ class GoodsReceiptController extends Controller
|
||||
'id' => $po->id,
|
||||
'code' => $po->code,
|
||||
'status' => $po->status,
|
||||
'supplierId' => $po->vendor_id,
|
||||
'vendor_id' => $po->vendor_id,
|
||||
'vendor_name' => $po->vendor?->name ?? '',
|
||||
'warehouse_id' => $po->warehouse_id,
|
||||
@@ -244,15 +278,30 @@ class GoodsReceiptController extends Controller
|
||||
'items' => $po->items->map(function ($item) use ($productsForPOs) {
|
||||
$product = $productsForPOs->get($item->product_id);
|
||||
$remaining = max(0, $item->quantity - ($item->received_quantity ?? 0));
|
||||
|
||||
$selectedUnit = 'base';
|
||||
if ($item->unit_id && $product && $item->unit_id == $product->large_unit_id) {
|
||||
$selectedUnit = 'large';
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $item->id,
|
||||
'productId' => $item->product_id,
|
||||
'product_id' => $item->product_id,
|
||||
'productName' => $product?->name ?? '',
|
||||
'product_name' => $product?->name ?? '',
|
||||
'product_code' => $product?->code ?? '',
|
||||
'unit' => $product?->baseUnit?->name ?? '個',
|
||||
'unit' => $item->unit_id && $product && $item->unit_id == $product->large_unit_id ? ($product?->largeUnit?->name ?? '') : ($product?->baseUnit?->name ?? '個'),
|
||||
'selectedUnit' => $selectedUnit,
|
||||
'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 ?? '',
|
||||
'conversion_rate' => $product?->conversion_rate ?? 1,
|
||||
'quantity' => $item->quantity,
|
||||
'received_quantity' => $item->received_quantity ?? 0,
|
||||
'remaining' => $remaining,
|
||||
'unitPrice' => $item->unit_price,
|
||||
'unit_price' => $item->unit_price,
|
||||
];
|
||||
})->filter(fn($item) => $item['remaining'] > 0)->values(),
|
||||
|
||||
@@ -266,10 +266,24 @@ class GoodsReceiptService implements \App\Modules\Inventory\Contracts\GoodsRecei
|
||||
default => '進貨入庫',
|
||||
};
|
||||
|
||||
$quantityToRecord = $grItem->quantity_received;
|
||||
|
||||
// 單位換算邏輯:僅針對標準採購且有連結 PO Item 時
|
||||
if ($goodsReceipt->type === 'standard' && $grItem->purchase_order_item_id) {
|
||||
$poItem = \App\Modules\Procurement\Models\PurchaseOrderItem::find($grItem->purchase_order_item_id);
|
||||
$product = $this->inventoryService->getProduct($grItem->product_id);
|
||||
|
||||
if ($poItem && $product && $poItem->unit_id && $product->large_unit_id && $poItem->unit_id == $product->large_unit_id) {
|
||||
// 如果使用的是大單位,則換算為基本單位數量
|
||||
$quantityToRecord = $grItem->quantity_received * ($product->conversion_rate ?: 1);
|
||||
Log::info("Goods Receipt [{$goodsReceipt->code}] converted quantity for product [{$product->id}]: {$grItem->quantity_received} large unit -> {$quantityToRecord} base unit.");
|
||||
}
|
||||
}
|
||||
|
||||
$this->inventoryService->createInventoryRecord([
|
||||
'warehouse_id' => $goodsReceipt->warehouse_id,
|
||||
'product_id' => $grItem->product_id,
|
||||
'quantity' => $grItem->quantity_received,
|
||||
'quantity' => $quantityToRecord,
|
||||
'unit_cost' => $grItem->unit_price,
|
||||
'batch_number' => $grItem->batch_number,
|
||||
'expiry_date' => $grItem->expiry_date,
|
||||
@@ -282,6 +296,7 @@ class GoodsReceiptService implements \App\Modules\Inventory\Contracts\GoodsRecei
|
||||
|
||||
// 2. Update PO if linked and type is standard
|
||||
if ($goodsReceipt->type === 'standard' && $goodsReceipt->purchase_order_id && $grItem->purchase_order_item_id) {
|
||||
// 更新採購單的實收數量 (維持原始單位數量,以便與採購數量比較)
|
||||
$this->procurementService->updateReceivedQuantity(
|
||||
$grItem->purchase_order_item_id,
|
||||
$grItem->quantity_received
|
||||
|
||||
@@ -15,6 +15,7 @@ class PurchaseOrderItem extends Model
|
||||
'purchase_order_id',
|
||||
'product_id',
|
||||
'quantity',
|
||||
'unit_id',
|
||||
'unit_price',
|
||||
'subtotal',
|
||||
// 驗收欄位
|
||||
|
||||
Reference in New Issue
Block a user