fix: BOM 자재 조회 시 기존 투입량 합산 반영

- getBomMaterials에서 input_qty: 0 하드코딩 → process_work_result에서 실제 투입량 SUM
- child_item_id별 투입 합산으로 정확한 잔여량 표시
This commit is contained in:
SeongHyun Kim
2026-04-07 11:07:19 +09:00
parent 495a5f034b
commit 3929ec17f2

View File

@@ -2813,13 +2813,27 @@ export const getBomMaterials = async (req: AuthenticatedRequest, res: Response)
);
const baseQty = parseFloat(bomBase.rows[0]?.base_qty || "1") || 1;
// 기존 투입량 조회 (child_item_id별 합산)
const inputResult = await pool.query(
`SELECT detail_content as child_item_id, SUM(CAST(NULLIF(result_value, '') AS numeric)) as total_input
FROM process_work_result
WHERE work_order_process_id = $1 AND company_code = $2 AND detail_type = 'material_input'
GROUP BY detail_content`,
[processId, companyCode]
);
const inputMap = new Map<string, number>();
for (const row of inputResult.rows) {
inputMap.set(String(row.child_item_id), parseFloat(row.total_input) || 0);
}
const materials = bomResult.rows.map((bd: Record<string, unknown>) => {
const bomQty = parseFloat(String(bd.quantity || "0")) || 0;
const lossRate = parseFloat(String(bd.loss_rate || "0")) || 0;
const requiredQty = Math.ceil((processQty / baseQty) * bomQty * (1 + lossRate / 100));
const childItemId = String(bd.child_item_id || "");
return {
id: bd.id,
child_item_id: bd.child_item_id,
child_item_id: childItemId,
child_item_code: bd.child_item_code || "",
child_item_name: bd.child_item_name || "",
bom_qty: bomQty,
@@ -2827,7 +2841,7 @@ export const getBomMaterials = async (req: AuthenticatedRequest, res: Response)
process_type: bd.process_type || "",
loss_rate: lossRate,
required_qty: requiredQty,
input_qty: 0,
input_qty: inputMap.get(childItemId) || 0,
};
});