ERP_Preprocessor / utils /template_utils.py
hariqueen's picture
Upload 5 files
afd0a81 verified
"""
ERP 양식 및 ν…œν”Œλ¦Ώ κ΄€λ ¨ μœ ν‹Έλ¦¬ν‹°
"""
import pandas as pd
from typing import Dict, Any, Optional
from core import config as cfg
def load_erp_form_template(erp_form_file: str) -> Optional[pd.DataFrame]:
"""
ERP 양식 파일 λ‘œλ“œ
Args:
erp_form_file: ERP 양식 파일 경둜
Returns:
ERP 양식 λ°μ΄ν„°ν”„λ ˆμž„ λ˜λŠ” None
"""
try:
erp_form = pd.read_csv(erp_form_file, encoding=cfg.DEFAULT_ENCODING)
print(f"ERP 양식 파일 '{erp_form_file}'을 μ„±κ³΅μ μœΌλ‘œ λ‘œλ“œν–ˆμŠ΅λ‹ˆλ‹€.")
return erp_form
except Exception as e:
print(f"ERP 양식 파일 λ‘œλ“œ μ‹€νŒ¨: {e}")
print("κΈ°λ³Έ 양식 없이 μ§„ν–‰ν•©λ‹ˆλ‹€.")
return None
def prepare_file_with_template(erp_df: pd.DataFrame, erp_form: Optional[pd.DataFrame]) -> pd.DataFrame:
"""
ERP 양식을 μ μš©ν•˜μ—¬ 파일 μ€€λΉ„
Args:
erp_df: ERP λ°μ΄ν„°ν”„λ ˆμž„
erp_form: ERP 양식 λ°μ΄ν„°ν”„λ ˆμž„
Returns:
κ²°κ³Ό λ°μ΄ν„°ν”„λ ˆμž„
"""
if erp_form is not None:
# 양식 파일의 컬럼 μˆœμ„œ μ‚¬μš©
form_columns = erp_form.columns.tolist()
# κ²°κ³Ό λ°μ΄ν„°ν”„λ ˆμž„μ„ 양식 컬럼 μˆœμ„œμ— 맞게 μž¬μ •λ ¬
for col in form_columns:
if col not in erp_df.columns:
erp_df[col] = ""
erp_df = erp_df[form_columns]
# erp_form 볡사 (양식 파일의 처음 nν–‰λ§Œ μ‚¬μš©)
result_df = erp_form.copy()
# 양식 파일이 μ§€μ •λœ μ‹œμž‘ν–‰λ³΄λ‹€ 많으면 ν•„μš”ν•œ 만큼만 μœ μ§€
if len(result_df) > cfg.ERP_DATA_ROW_START - 1:
result_df = result_df.iloc[:(cfg.ERP_DATA_ROW_START - 1)]
# 빈 ν–‰ μΆ”κ°€ (ν•„μš”ν•œ 경우)
current_rows = len(result_df)
target_rows = cfg.ERP_DATA_ROW_START - 1 # μ‹œμž‘ν–‰ - 1 (μΈλ±μŠ€λŠ” 0λΆ€ν„° μ‹œμž‘ν•˜λ―€λ‘œ)
# ν˜„μž¬ ν–‰ μˆ˜κ°€ νƒ€κ²Ÿ ν–‰ μˆ˜λ³΄λ‹€ 적으면 빈 ν–‰ μΆ”κ°€
if current_rows < target_rows:
empty_rows_needed = target_rows - current_rows
empty_df = pd.DataFrame([[""] * len(form_columns) for _ in range(empty_rows_needed)], columns=form_columns)
result_df = pd.concat([result_df, empty_df], ignore_index=True)
# 처리된 데이터 μΆ”κ°€ (ERP_DATA_ROW_STARTν–‰λΆ€ν„° μ‹œμž‘)
result_df = pd.concat([result_df, erp_df], ignore_index=True)
return result_df
else:
# 양식 파일이 μ—†λŠ” 경우 빈 λ°μ΄ν„°ν”„λ ˆμž„ 생성 ν›„ 데이터 μΆ”κ°€
# ν•„μš”ν•œ 빈 ν–‰ 생성 (ERP_DATA_ROW_START-1개의 빈 ν–‰)
empty_rows = cfg.ERP_DATA_ROW_START - 1
empty_df = pd.DataFrame([[""] * len(erp_df.columns) for _ in range(empty_rows)], columns=erp_df.columns)
result_df = pd.concat([empty_df, erp_df], ignore_index=True)
return result_df