feat: 견적 품목 추가 시 단가 자동 조회 기능

- fetchItemPrices 클라이언트 API 함수 추가 (items.ts)
- 품목 선택 시 /api/proxy/quotes/items/prices를 통해 단가 조회
- 조회된 단가로 견적금액요약에 즉시 반영
This commit is contained in:
2026-01-29 08:05:08 +09:00
parent 15b4350051
commit 32a1ed2de7
2 changed files with 77 additions and 5 deletions

View File

@@ -12,6 +12,7 @@
import { useState, useMemo, useEffect } from "react";
import { Package, Settings, Plus, Trash2, Loader2, Calculator, Save } from "lucide-react";
import { getItemCategoryTree, type ItemCategoryNode } from "./actions";
import { fetchItemPrices } from "@/lib/api/items";
import { Badge } from "../ui/badge";
import { Button } from "../ui/button";
@@ -724,21 +725,32 @@ export function LocationDetailPanel({
<ItemSearchModal
open={itemSearchOpen}
onOpenChange={setItemSearchOpen}
onSelectItem={(item) => {
onSelectItem={async (item) => {
if (!location) return;
const currentTab = detailTabs.find((t) => t.value === activeTab);
const categoryCode = activeTab;
const categoryLabel = currentTab?.label || activeTab;
// 단가 조회 (클라이언트 API 호출)
let unitPrice = 0;
try {
const priceResult = await fetchItemPrices([item.code]);
unitPrice = priceResult[item.code]?.unit_price || 0;
} catch (error) {
console.error('[품목 추가] 단가 조회 실패:', error);
}
const totalPrice = unitPrice * 1; // quantity = 1
const newItem: BomCalculationResultItem & { category_code?: string; is_manual?: boolean } = {
item_code: item.code,
item_name: item.name,
specification: item.specification || "",
unit: "EA",
quantity: 1,
unit_price: 0,
total_price: 0,
unit_price: unitPrice,
total_price: totalPrice,
process_group: categoryLabel,
category_code: categoryCode,
is_manual: true,
@@ -789,8 +801,13 @@ export function LocationDetailPanel({
grand_total: updatedGrandTotal,
};
onUpdateLocation(location.id, { bomResult: updatedBomResult });
console.log(`[품목 추가] ${item.code} - ${item.name}${categoryLabel} (${categoryCode})`);
// unitPrice, totalPrice도 함께 업데이트
onUpdateLocation(location.id, {
bomResult: updatedBomResult,
unitPrice: updatedGrandTotal,
totalPrice: updatedGrandTotal * location.quantity,
});
console.log(`[품목 추가] ${item.code} - ${item.name}${categoryLabel} (${categoryCode}), 단가: ${unitPrice}`);
}}
tabLabel={detailTabs.find((t) => t.value === activeTab)?.label}
/>

View File

@@ -759,4 +759,59 @@ export async function revalidateItems(): Promise<void> {
const { revalidatePath } = await import('next/cache');
revalidatePath('/items');
}
}
// ===== 품목 단가 조회 =====
/** 품목 단가 조회 결과 */
export interface ItemPriceResult {
item_code: string;
unit_price: number;
}
/**
* 품목 단가 조회 (견적 품목 추가 시 사용)
*
* @param itemCodes - 조회할 품목 코드 배열
* @returns 품목 코드별 단가 정보
*
* @example
* const prices = await fetchItemPrices(['N71807', 'N71808']);
* console.log(prices['N71807']?.unit_price); // 1320000
*/
export async function fetchItemPrices(
itemCodes: string[]
): Promise<Record<string, ItemPriceResult>> {
if (!itemCodes.length) {
return {};
}
try {
// 프록시 경유: /api/proxy/quotes/items/prices → /api/v1/quotes/items/prices
const response = await fetch('/api/proxy/quotes/items/prices', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ item_codes: itemCodes }),
credentials: 'include',
});
if (!response.ok) {
console.error('[fetchItemPrices] API 에러:', response.status);
return {};
}
const result = await response.json();
console.log('[fetchItemPrices] API 응답:', result);
if (result.success && result.data) {
return result.data as Record<string, ItemPriceResult>;
}
return {};
} catch (error) {
console.error('[fetchItemPrices] 에러:', error);
return {};
}
}