fix: BOM 자재 input_qty 합산 매핑 키 수정 (item_code 기준)

process_work_result.detail_content에는 item_code(R_PLAST_024)가 저장되는데
기존 코드는 child_item_id(UUID)로 매칭하려 해서 항상 0 반환되던 버그.

매칭 키를 child_item_code로 변경 → 정상 합산 확인 (5+30=35)
This commit is contained in:
SeongHyun Kim
2026-04-07 12:45:39 +09:00
parent 2fee120007
commit 630b135254

View File

@@ -2813,35 +2813,36 @@ export const getBomMaterials = async (req: AuthenticatedRequest, res: Response)
);
const baseQty = parseFloat(bomBase.rows[0]?.base_qty || "1") || 1;
// 기존 투입량 조회 (child_item_id별 합산)
// 기존 투입량 조회 (item_code별 합산 — detail_content에 item_code 저장됨)
const inputResult = await pool.query(
`SELECT detail_content as child_item_id, SUM(CAST(NULLIF(result_value, '') AS numeric)) as total_input
`SELECT detail_content as item_code, 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'
AND result_value IS NOT NULL AND result_value != ''
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);
inputMap.set(String(row.item_code), 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 || "");
const childItemCode = String(bd.child_item_code || "");
return {
id: bd.id,
child_item_id: childItemId,
child_item_code: bd.child_item_code || "",
child_item_id: bd.child_item_id,
child_item_code: childItemCode,
child_item_name: bd.child_item_name || "",
bom_qty: bomQty,
unit: bd.unit || bd.item_unit || "",
process_type: bd.process_type || "",
loss_rate: lossRate,
required_qty: requiredQty,
input_qty: inputMap.get(childItemId) || 0,
input_qty: inputMap.get(childItemCode) || 0,
};
});