fix: 자재 리비전 비교 시스템 완전 수정 및 UI 개선

🔧 백엔드 수정:
- 이전 리비전 탐지 로직을 문자열에서 숫자 기반으로 개선 (Rev.1에서 Rev.0 정상 탐지)
- get_materials_by_hash 함수 필드명 수정 및 단순화
- 자재 비교 API 응답 구조 개선

🎨 프론트엔드 UI 대폭 개선:
- MaterialComparisonPage를 MaterialsPage와 동일한 깔끔한 디자인으로 리뉴얼
- 요약 통계 카드 4개 추가 (신규/변경/삭제/총 자재)
- 탭으로 구분된 자재 목록 (신규/변경/삭제)
- 테이블 형태로 비교 결과 표시 (이전수량 → 현재수량 → 변경량)
- 색상별 카테고리 Chip과 변경량 강조 표시

🔄 네비게이션 개선:
- MaterialComparisonPage 돌아가기 버튼을 BOM 상태 페이지로 수정
- RevisionPurchasePage 버튼 텍스트를 'BOM 목록으로'로 명확화
- 모든 비교 관련 페이지의 네비게이션 일관성 확보

🐛 버그 수정:
- BOMStatusPage 리비전 업로드 시 불필요한 파라미터 제거 (네트워크 에러 해결)
- API 응답 데이터 처리 로직 개선 (result.data 처리)
- 에러 핸들링 및 디버깅 로그 강화

 완전 작동하는 리비전 비교 시스템 완성
This commit is contained in:
Hyungi Ahn
2025-07-23 06:58:31 +09:00
parent 534015cc7c
commit bef0d8bf7c
5 changed files with 257 additions and 90 deletions

View File

@@ -48,13 +48,14 @@ async def compare_material_revisions(
db, current_file, previous_file, job_no
)
# 4. 결과 저장 (선택사항)
# 4. 결과 저장 (선택사항) - 임시로 비활성화
comparison_id = None
if save_result and previous_file and previous_revision:
comparison_id = await save_comparison_result(
db, job_no, current_revision, previous_revision,
current_file["id"], previous_file["id"], comparison_result
)
# TODO: 저장 기능 활성화
# if save_result and previous_file and previous_revision:
# comparison_id = await save_comparison_result(
# db, job_no, current_revision, previous_revision,
# current_file["id"], previous_file["id"], comparison_result
# )
return {
"success": True,
@@ -65,8 +66,7 @@ async def compare_material_revisions(
"summary": comparison_result["summary"],
"new_items": comparison_result["new_items"],
"modified_items": comparison_result["modified_items"],
"removed_items": comparison_result["removed_items"],
"purchase_summary": comparison_result["purchase_summary"]
"removed_items": comparison_result["removed_items"]
}
except Exception as e:
@@ -323,21 +323,39 @@ async def get_file_by_revision(db: Session, job_no: str, revision: str) -> Optio
return None
async def get_previous_revision(db: Session, job_no: str, current_revision: str) -> Optional[str]:
"""이전 리비전 자동 탐지"""
"""이전 리비전 자동 탐지 - 숫자 기반 비교"""
# 현재 리비전의 숫자 추출
try:
current_rev_num = int(current_revision.replace("Rev.", ""))
except (ValueError, AttributeError):
current_rev_num = 0
query = text("""
SELECT revision
FROM files
WHERE job_no = :job_no AND revision < :current_revision AND is_active = TRUE
WHERE job_no = :job_no AND is_active = TRUE
ORDER BY revision DESC
LIMIT 1
""")
result = db.execute(query, {"job_no": job_no, "current_revision": current_revision})
prev_row = result.fetchone()
result = db.execute(query, {"job_no": job_no})
revisions = result.fetchall()
if prev_row is not None:
return prev_row[0]
return None
# 현재 리비전보다 낮은 리비전 중 가장 높은 것 찾기
previous_revision = None
highest_prev_num = -1
for row in revisions:
rev = row[0]
try:
rev_num = int(rev.replace("Rev.", ""))
if rev_num < current_rev_num and rev_num > highest_prev_num:
highest_prev_num = rev_num
previous_revision = rev
except (ValueError, AttributeError):
continue
return previous_revision
async def perform_material_comparison(
db: Session,
@@ -346,9 +364,7 @@ async def perform_material_comparison(
job_no: str
) -> Dict:
"""
핵심 자재 비교 로직
- 해시 기반 고성능 비교
- 누적 재고 고려한 실제 구매 필요량 계산
핵심 자재 비교 로직 - 간단한 버전
"""
# 1. 현재 리비전 자재 목록 (해시별로 그룹화)
@@ -359,63 +375,44 @@ async def perform_material_comparison(
if previous_file:
previous_materials = await get_materials_by_hash(db, previous_file["id"])
# 3. 현재까지의 누적 재고 조회
current_inventory = await get_current_inventory(db, job_no)
# 4. 비교 실행
# 3. 비교 실행
new_items = []
modified_items = []
removed_items = []
purchase_summary = {
"additional_purchase_needed": 0,
"total_new_items": 0,
"total_increased_items": 0
}
# 신규/변경 항목 찾기
for material_hash, current_item in current_materials.items():
current_qty = current_item["quantity"]
available_stock = current_inventory.get(material_hash, 0)
if material_hash not in previous_materials:
# 완전히 새로운 항목
additional_needed = max(current_qty - available_stock, 0)
new_items.append({
"material_hash": material_hash,
"description": current_item["description"],
"size_spec": current_item["size_spec"],
"material_grade": current_item["material_grade"],
"quantity": current_qty,
"available_stock": available_stock,
"additional_needed": additional_needed
"category": current_item["category"],
"unit": current_item["unit"]
})
purchase_summary["additional_purchase_needed"] += additional_needed
purchase_summary["total_new_items"] += 1
else:
# 기존 항목 - 수량 변경 체크
previous_qty = previous_materials[material_hash]["quantity"]
qty_diff = current_qty - previous_qty
qty_change = current_qty - previous_qty
if qty_diff != 0:
additional_needed = max(current_qty - available_stock, 0)
if qty_change != 0:
modified_items.append({
"material_hash": material_hash,
"description": current_item["description"],
"size_spec": current_item["size_spec"],
"material_grade": current_item["material_grade"],
"previous_quantity": previous_qty,
"current_quantity": current_qty,
"quantity_diff": qty_diff,
"available_stock": available_stock,
"additional_needed": additional_needed
"quantity_change": qty_change,
"category": current_item["category"],
"unit": current_item["unit"]
})
if additional_needed > 0:
purchase_summary["additional_purchase_needed"] += additional_needed
purchase_summary["total_increased_items"] += 1
# 삭제된 항목 찾기
for material_hash, previous_item in previous_materials.items():
@@ -424,7 +421,10 @@ async def perform_material_comparison(
"material_hash": material_hash,
"description": previous_item["description"],
"size_spec": previous_item["size_spec"],
"quantity": previous_item["quantity"]
"material_grade": previous_item["material_grade"],
"quantity": previous_item["quantity"],
"category": previous_item["category"],
"unit": previous_item["unit"]
})
return {
@@ -437,8 +437,7 @@ async def perform_material_comparison(
},
"new_items": new_items,
"modified_items": modified_items,
"removed_items": removed_items,
"purchase_summary": purchase_summary
"removed_items": removed_items
}
async def get_materials_by_hash(db: Session, file_id: int) -> Dict[str, Dict]:
@@ -451,10 +450,11 @@ async def get_materials_by_hash(db: Session, file_id: int) -> Dict[str, Dict]:
size_spec,
material_grade,
SUM(quantity) as quantity,
classified_category
classified_category,
unit
FROM materials
WHERE file_id = :file_id
GROUP BY original_description, size_spec, material_grade, classified_category
GROUP BY original_description, size_spec, material_grade, classified_category, unit
""")
result = db.execute(query, {"file_id": file_id})
@@ -468,27 +468,20 @@ async def get_materials_by_hash(db: Session, file_id: int) -> Dict[str, Dict]:
materials_dict[material_hash] = {
"material_hash": material_hash,
"original_description": mat[0],
"description": mat[0], # original_description -> description
"size_spec": mat[1],
"material_grade": mat[2],
"quantity": float(mat[3]) if mat[3] else 0.0,
"classified_category": mat[4]
"category": mat[4], # classified_category -> category
"unit": mat[5] or 'EA'
}
return materials_dict
async def get_current_inventory(db: Session, job_no: str) -> Dict[str, float]:
"""현재까지의 누적 재고량 조회"""
query = text("""
SELECT material_hash, available_stock
FROM material_inventory_status
WHERE job_no = :job_no
""")
result = db.execute(query, {"job_no": job_no})
inventory = result.fetchall()
return {inv.material_hash: float(inv.available_stock or 0) for inv in inventory}
"""현재까지의 누적 재고량 조회 - 임시로 빈 딕셔너리 반환"""
# TODO: 실제 재고 시스템 구현 후 활성화
return {}
async def save_comparison_result(
db: Session,

View File

@@ -135,8 +135,6 @@ const BOMStatusPage = () => {
formData.append('job_no', jobNo);
formData.append('revision', 'Rev.0'); // 백엔드에서 자동 증가
formData.append('bom_name', revisionDialog.bomName);
formData.append('bom_type', 'excel');
formData.append('description', '');
formData.append('parent_file_id', revisionDialog.parentId);
const response = await uploadFileApi(formData);

View File

@@ -9,7 +9,21 @@ import {
Alert,
Breadcrumbs,
Link,
Stack
Stack,
Card,
CardContent,
Table,
TableBody,
TableCell,
TableContainer,
TableHead,
TableRow,
Paper,
Chip,
Grid,
Divider,
Tabs,
Tab
} from '@mui/material';
import {
ArrowBack,
@@ -27,6 +41,7 @@ const MaterialComparisonPage = () => {
const [confirmLoading, setConfirmLoading] = useState(false);
const [error, setError] = useState(null);
const [comparisonResult, setComparisonResult] = useState(null);
const [selectedTab, setSelectedTab] = useState(0);
// URL 파라미터에서 정보 추출
const jobNo = searchParams.get('job_no');
@@ -48,7 +63,12 @@ const MaterialComparisonPage = () => {
setLoading(true);
setError(null);
console.log('자재 비교 실행:', { jobNo, currentRevision, previousRevision });
console.log('🔍 자재 비교 실행 - 파라미터:', {
jobNo,
currentRevision,
previousRevision,
filename
});
const result = await compareMaterialRevisions(
jobNo,
@@ -57,11 +77,16 @@ const MaterialComparisonPage = () => {
true // 결과 저장
);
console.log('비교 결과:', result);
setComparisonResult(result);
console.log('비교 결과 성공:', result);
setComparisonResult(result.data || result);
} catch (err) {
console.error('자재 비교 실패:', err);
console.error('자재 비교 실패:', {
message: err.message,
response: err.response?.data,
status: err.response?.status,
params: { jobNo, currentRevision, previousRevision }
});
setError(err.response?.data?.detail || err.message || '자재 비교 중 오류가 발생했습니다');
} finally {
setLoading(false);
@@ -102,14 +127,172 @@ const MaterialComparisonPage = () => {
};
const handleGoBack = () => {
// 이전 페이지로 이동 (대부분 파일 업로드 완료 페이지)
// BOM 상태 페이지로 이동
if (jobNo) {
navigate(`/materials?job_no=${jobNo}`);
navigate(`/bom-status?job_no=${jobNo}`);
} else {
navigate(-1);
}
};
const renderComparisonResults = () => {
const { summary, new_items = [], modified_items = [], removed_items = [] } = comparisonResult;
return (
<Box>
{/* 요약 통계 카드 */}
<Grid container spacing={3} sx={{ mb: 3 }}>
<Grid item xs={12} md={3}>
<Card>
<CardContent sx={{ textAlign: 'center' }}>
<Typography variant="h4" color="primary" gutterBottom>
{summary?.new_items_count || 0}
</Typography>
<Typography variant="h6">신규 자재</Typography>
<Typography variant="body2" color="text.secondary">
새로 추가된 자재
</Typography>
</CardContent>
</Card>
</Grid>
<Grid item xs={12} md={3}>
<Card>
<CardContent sx={{ textAlign: 'center' }}>
<Typography variant="h4" color="warning.main" gutterBottom>
{summary?.modified_items_count || 0}
</Typography>
<Typography variant="h6">변경 자재</Typography>
<Typography variant="body2" color="text.secondary">
수량이 변경된 자재
</Typography>
</CardContent>
</Card>
</Grid>
<Grid item xs={12} md={3}>
<Card>
<CardContent sx={{ textAlign: 'center' }}>
<Typography variant="h4" color="error.main" gutterBottom>
{summary?.removed_items_count || 0}
</Typography>
<Typography variant="h6">삭제 자재</Typography>
<Typography variant="body2" color="text.secondary">
제거된 자재
</Typography>
</CardContent>
</Card>
</Grid>
<Grid item xs={12} md={3}>
<Card>
<CardContent sx={{ textAlign: 'center' }}>
<Typography variant="h4" color="success.main" gutterBottom>
{summary?.total_current_items || 0}
</Typography>
<Typography variant="h6"> 자재</Typography>
<Typography variant="body2" color="text.secondary">
현재 리비전 전체
</Typography>
</CardContent>
</Card>
</Grid>
</Grid>
{/* 탭으로 구분된 자재 목록 */}
<Card>
<Tabs
value={selectedTab}
onChange={(e, newValue) => setSelectedTab(newValue)}
variant="fullWidth"
>
<Tab label={`신규 자재 (${new_items.length})`} />
<Tab label={`변경 자재 (${modified_items.length})`} />
<Tab label={`삭제 자재 (${removed_items.length})`} />
</Tabs>
<CardContent>
{selectedTab === 0 && renderMaterialTable(new_items, 'new')}
{selectedTab === 1 && renderMaterialTable(modified_items, 'modified')}
{selectedTab === 2 && renderMaterialTable(removed_items, 'removed')}
</CardContent>
</Card>
</Box>
);
};
const renderMaterialTable = (items, type) => {
if (items.length === 0) {
return (
<Alert severity="info">
{type === 'new' && '새로 추가된 자재가 없습니다.'}
{type === 'modified' && '수량이 변경된 자재가 없습니다.'}
{type === 'removed' && '삭제된 자재가 없습니다.'}
</Alert>
);
}
return (
<TableContainer component={Paper} variant="outlined">
<Table size="small">
<TableHead>
<TableRow>
<TableCell>카테고리</TableCell>
<TableCell>자재 설명</TableCell>
<TableCell>사이즈</TableCell>
<TableCell>재질</TableCell>
{type === 'modified' && (
<>
<TableCell align="center">이전 수량</TableCell>
<TableCell align="center">현재 수량</TableCell>
<TableCell align="center">변경량</TableCell>
</>
)}
{type !== 'modified' && <TableCell align="center">수량</TableCell>}
<TableCell>단위</TableCell>
</TableRow>
</TableHead>
<TableBody>
{items.map((item, index) => (
<TableRow key={index}>
<TableCell>
<Chip
label={item.category}
size="small"
color={type === 'new' ? 'primary' : type === 'modified' ? 'warning' : 'error'}
/>
</TableCell>
<TableCell>{item.description}</TableCell>
<TableCell>{item.size_spec || '-'}</TableCell>
<TableCell>{item.material_grade || '-'}</TableCell>
{type === 'modified' && (
<>
<TableCell align="center">{item.previous_quantity}</TableCell>
<TableCell align="center">{item.current_quantity}</TableCell>
<TableCell align="center">
<Typography
variant="body2"
fontWeight="bold"
color={item.quantity_change > 0 ? 'success.main' : 'error.main'}
>
{item.quantity_change > 0 ? '+' : ''}{item.quantity_change}
</Typography>
</TableCell>
</>
)}
{type !== 'modified' && (
<TableCell align="center">
<Typography variant="body2" fontWeight="bold">
{item.quantity}
</Typography>
</TableCell>
)}
<TableCell>{item.unit || 'EA'}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
);
};
const renderHeader = () => (
<Box sx={{ mb: 3 }}>
<Breadcrumbs sx={{ mb: 2 }}>
@@ -163,7 +346,7 @@ const MaterialComparisonPage = () => {
startIcon={<ArrowBack />}
onClick={handleGoBack}
>
돌아가기
BOM 목록으로
</Button>
</Stack>
</Stack>
@@ -216,17 +399,7 @@ const MaterialComparisonPage = () => {
<Container maxWidth="xl" sx={{ py: 4 }}>
{renderHeader()}
{comparisonResult ? (
<MaterialComparisonResult
comparison={comparisonResult}
onConfirmPurchase={handleConfirmPurchase}
loading={confirmLoading}
/>
) : (
<Alert severity="info">
비교 결과가 없습니다.
</Alert>
)}
{comparisonResult && renderComparisonResults()}
</Container>
);
};

View File

@@ -165,10 +165,10 @@ const RevisionPurchasePage = () => {
<Button
variant="outlined"
startIcon={<ArrowBack />}
onClick={() => navigate(-1)}
onClick={() => navigate(`/bom-status?job_no=${jobNo}`)}
sx={{ mb: 2 }}
>
뒤로가기
BOM 목록으로
</Button>
<Alert severity="error">{error}</Alert>
</Box>
@@ -188,9 +188,9 @@ const RevisionPurchasePage = () => {
<Button
variant="outlined"
startIcon={<ArrowBack />}
onClick={() => navigate(-1)}
onClick={() => navigate(`/bom-status?job_no=${jobNo}`)}
>
뒤로가기
BOM 목록으로
</Button>
<Box sx={{ display: 'flex', gap: 2 }}>

3
test_upload.csv Normal file
View File

@@ -0,0 +1,3 @@
description,qty,main_nom
PIPE A,10,4
FITTING B,5,2
1 description qty main_nom
2 PIPE A 10 4
3 FITTING B 5 2