🐛 Fix: Project.name → project_name 속성명 수정 및 보고서 시스템 안정화
- backend/routers/reports.py: project.name을 project.project_name으로 수정 (3곳) - 일일보고서 엑셀 내보내기 오류 해결 - 배포 가이드 업데이트 (DEPLOYMENT_GUIDE_20251028.md) - 프로젝트 속성명 불일치로 인한 500 에러 해결 Fixes: 'Project' object has no attribute 'name' 오류
This commit is contained in:
@@ -1,11 +1,16 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import func
|
||||
from datetime import datetime
|
||||
from sqlalchemy import func, and_, or_
|
||||
from datetime import datetime, date
|
||||
from typing import List
|
||||
import io
|
||||
from openpyxl import Workbook
|
||||
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
|
||||
from openpyxl.utils import get_column_letter
|
||||
|
||||
from database.database import get_db
|
||||
from database.models import Issue, DailyWork, IssueStatus, IssueCategory, User, UserRole
|
||||
from database.models import Issue, DailyWork, IssueStatus, IssueCategory, User, UserRole, Project, ReviewStatus
|
||||
from database import schemas
|
||||
from routers.auth import get_current_user
|
||||
|
||||
@@ -128,3 +133,222 @@ async def get_report_daily_works(
|
||||
"overtime_total": work.overtime_total,
|
||||
"total_hours": work.total_hours
|
||||
} for work in works]
|
||||
|
||||
@router.post("/daily-export")
|
||||
async def export_daily_report(
|
||||
request: schemas.DailyReportRequest,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""품질팀용 일일보고서 엑셀 내보내기"""
|
||||
|
||||
# 권한 확인 (품질팀만 접근 가능)
|
||||
if current_user.role != UserRole.admin:
|
||||
raise HTTPException(status_code=403, detail="품질팀만 접근 가능합니다")
|
||||
|
||||
# 프로젝트 확인
|
||||
project = db.query(Project).filter(Project.id == request.project_id).first()
|
||||
if not project:
|
||||
raise HTTPException(status_code=404, detail="프로젝트를 찾을 수 없습니다")
|
||||
|
||||
# 관리함 데이터 조회 (진행 중 + 완료됨)
|
||||
issues_query = db.query(Issue).filter(
|
||||
Issue.project_id == request.project_id,
|
||||
Issue.review_status.in_([ReviewStatus.in_progress, ReviewStatus.completed])
|
||||
).order_by(Issue.report_date.desc())
|
||||
|
||||
issues = issues_query.all()
|
||||
|
||||
# 통계 계산
|
||||
stats = calculate_project_stats(issues)
|
||||
|
||||
# 엑셀 파일 생성
|
||||
wb = Workbook()
|
||||
ws = wb.active
|
||||
ws.title = "일일보고서"
|
||||
|
||||
# 스타일 정의
|
||||
header_font = Font(bold=True, color="FFFFFF")
|
||||
header_fill = PatternFill(start_color="4472C4", end_color="4472C4", fill_type="solid")
|
||||
stats_font = Font(bold=True, size=12)
|
||||
stats_fill = PatternFill(start_color="E7E6E6", end_color="E7E6E6", fill_type="solid")
|
||||
border = Border(
|
||||
left=Side(style='thin'),
|
||||
right=Side(style='thin'),
|
||||
top=Side(style='thin'),
|
||||
bottom=Side(style='thin')
|
||||
)
|
||||
center_alignment = Alignment(horizontal='center', vertical='center')
|
||||
|
||||
# 제목 및 기본 정보
|
||||
ws.merge_cells('A1:L1')
|
||||
ws['A1'] = f"{project.project_name} - 일일보고서"
|
||||
ws['A1'].font = Font(bold=True, size=16)
|
||||
ws['A1'].alignment = center_alignment
|
||||
|
||||
ws.merge_cells('A2:L2')
|
||||
ws['A2'] = f"생성일: {datetime.now().strftime('%Y년 %m월 %d일')}"
|
||||
ws['A2'].alignment = center_alignment
|
||||
|
||||
# 프로젝트 통계 (4행부터)
|
||||
ws.merge_cells('A4:L4')
|
||||
ws['A4'] = "프로젝트 현황"
|
||||
ws['A4'].font = stats_font
|
||||
ws['A4'].fill = stats_fill
|
||||
ws['A4'].alignment = center_alignment
|
||||
|
||||
# 통계 데이터
|
||||
stats_row = 5
|
||||
ws[f'A{stats_row}'] = "총 신고 수량"
|
||||
ws[f'B{stats_row}'] = stats.total_count
|
||||
ws[f'D{stats_row}'] = "관리처리 현황"
|
||||
ws[f'E{stats_row}'] = stats.management_count
|
||||
ws[f'G{stats_row}'] = "완료 현황"
|
||||
ws[f'H{stats_row}'] = stats.completed_count
|
||||
ws[f'J{stats_row}'] = "지연 중"
|
||||
ws[f'K{stats_row}'] = stats.delayed_count
|
||||
|
||||
# 통계 스타일 적용
|
||||
for col in ['A', 'D', 'G', 'J']:
|
||||
ws[f'{col}{stats_row}'].font = Font(bold=True)
|
||||
ws[f'{col}{stats_row}'].fill = PatternFill(start_color="FFF2CC", end_color="FFF2CC", fill_type="solid")
|
||||
|
||||
# 데이터 테이블 헤더 (7행부터)
|
||||
headers = [
|
||||
"번호", "프로젝트", "부적합명", "상세내용", "원인분류",
|
||||
"해결방안", "담당부서", "담당자", "마감일", "상태",
|
||||
"신고일", "완료일"
|
||||
]
|
||||
|
||||
header_row = 7
|
||||
for col, header in enumerate(headers, 1):
|
||||
cell = ws.cell(row=header_row, column=col, value=header)
|
||||
cell.font = header_font
|
||||
cell.fill = header_fill
|
||||
cell.alignment = center_alignment
|
||||
cell.border = border
|
||||
|
||||
# 데이터 입력
|
||||
current_row = header_row + 1
|
||||
|
||||
for issue in issues:
|
||||
# 완료됨 항목의 첫 내보내기 여부 확인 (실제로는 DB에 플래그를 저장해야 함)
|
||||
# 지금은 모든 완료됨 항목을 포함
|
||||
|
||||
ws.cell(row=current_row, column=1, value=issue.id)
|
||||
ws.cell(row=current_row, column=2, value=project.project_name)
|
||||
ws.cell(row=current_row, column=3, value=issue.nonconformity_name or "")
|
||||
ws.cell(row=current_row, column=4, value=issue.detail_notes or "")
|
||||
ws.cell(row=current_row, column=5, value=get_category_text(issue.category))
|
||||
ws.cell(row=current_row, column=6, value=issue.solution or "")
|
||||
ws.cell(row=current_row, column=7, value=get_department_text(issue.responsible_department))
|
||||
ws.cell(row=current_row, column=8, value=issue.responsible_person or "")
|
||||
ws.cell(row=current_row, column=9, value=issue.expected_completion_date.strftime('%Y-%m-%d') if issue.expected_completion_date else "")
|
||||
ws.cell(row=current_row, column=10, value=get_status_text(issue.review_status))
|
||||
ws.cell(row=current_row, column=11, value=issue.report_date.strftime('%Y-%m-%d') if issue.report_date else "")
|
||||
ws.cell(row=current_row, column=12, value=issue.completion_date.strftime('%Y-%m-%d') if issue.completion_date else "")
|
||||
|
||||
# 상태별 색상 적용
|
||||
status_color = get_status_color(issue.review_status)
|
||||
if status_color:
|
||||
for col in range(1, len(headers) + 1):
|
||||
ws.cell(row=current_row, column=col).fill = PatternFill(
|
||||
start_color=status_color, end_color=status_color, fill_type="solid"
|
||||
)
|
||||
|
||||
# 테두리 적용
|
||||
for col in range(1, len(headers) + 1):
|
||||
ws.cell(row=current_row, column=col).border = border
|
||||
ws.cell(row=current_row, column=col).alignment = Alignment(vertical='center')
|
||||
|
||||
current_row += 1
|
||||
|
||||
# 열 너비 자동 조정
|
||||
for col in range(1, len(headers) + 1):
|
||||
column_letter = get_column_letter(col)
|
||||
ws.column_dimensions[column_letter].width = 15
|
||||
|
||||
# 특정 열 너비 조정
|
||||
ws.column_dimensions['C'].width = 20 # 부적합명
|
||||
ws.column_dimensions['D'].width = 30 # 상세내용
|
||||
ws.column_dimensions['F'].width = 25 # 해결방안
|
||||
|
||||
# 엑셀 파일을 메모리에 저장
|
||||
excel_buffer = io.BytesIO()
|
||||
wb.save(excel_buffer)
|
||||
excel_buffer.seek(0)
|
||||
|
||||
# 파일명 생성
|
||||
today = date.today().strftime('%Y%m%d')
|
||||
filename = f"{project.project_name}_일일보고서_{today}.xlsx"
|
||||
|
||||
return StreamingResponse(
|
||||
io.BytesIO(excel_buffer.read()),
|
||||
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
headers={"Content-Disposition": f"attachment; filename={filename}"}
|
||||
)
|
||||
|
||||
def calculate_project_stats(issues: List[Issue]) -> schemas.DailyReportStats:
|
||||
"""프로젝트 통계 계산"""
|
||||
stats = schemas.DailyReportStats()
|
||||
|
||||
today = date.today()
|
||||
|
||||
for issue in issues:
|
||||
stats.total_count += 1
|
||||
|
||||
if issue.review_status == ReviewStatus.in_progress:
|
||||
stats.management_count += 1
|
||||
|
||||
# 지연 여부 확인
|
||||
if issue.expected_completion_date and issue.expected_completion_date < today:
|
||||
stats.delayed_count += 1
|
||||
|
||||
elif issue.review_status == ReviewStatus.completed:
|
||||
stats.completed_count += 1
|
||||
|
||||
return stats
|
||||
|
||||
def get_category_text(category: IssueCategory) -> str:
|
||||
"""카테고리 한글 변환"""
|
||||
category_map = {
|
||||
IssueCategory.material_missing: "자재 누락",
|
||||
IssueCategory.design_error: "설계 미스",
|
||||
IssueCategory.incoming_defect: "입고 불량",
|
||||
IssueCategory.inspection_miss: "검사 미스",
|
||||
IssueCategory.etc: "기타"
|
||||
}
|
||||
return category_map.get(category, str(category))
|
||||
|
||||
def get_department_text(department) -> str:
|
||||
"""부서 한글 변환"""
|
||||
if not department:
|
||||
return ""
|
||||
|
||||
department_map = {
|
||||
"production": "생산",
|
||||
"quality": "품질",
|
||||
"purchasing": "구매",
|
||||
"design": "설계",
|
||||
"sales": "영업"
|
||||
}
|
||||
return department_map.get(department, str(department))
|
||||
|
||||
def get_status_text(status: ReviewStatus) -> str:
|
||||
"""상태 한글 변환"""
|
||||
status_map = {
|
||||
ReviewStatus.pending_review: "검토 대기",
|
||||
ReviewStatus.in_progress: "진행 중",
|
||||
ReviewStatus.completed: "완료됨",
|
||||
ReviewStatus.disposed: "폐기됨"
|
||||
}
|
||||
return status_map.get(status, str(status))
|
||||
|
||||
def get_status_color(status: ReviewStatus) -> str:
|
||||
"""상태별 색상 반환"""
|
||||
color_map = {
|
||||
ReviewStatus.in_progress: "FFF2CC", # 연한 노랑
|
||||
ReviewStatus.completed: "E2EFDA", # 연한 초록
|
||||
ReviewStatus.disposed: "F2F2F2" # 연한 회색
|
||||
}
|
||||
return color_map.get(status, None)
|
||||
|
||||
Reference in New Issue
Block a user