File size: 4,422 Bytes
bd6d9a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
파일 μž…μΆœλ ₯ κ΄€λ ¨ λͺ¨λ“ˆ
"""
import os
import pandas as pd
from typing import Dict, Any
import config as cfg


def load_erp_form_template(erp_form_file: str) -> pd.DataFrame:
    """
    ERP 양식 파일 λ‘œλ“œ
    
    Args:
        erp_form_file: ERP 양식 파일 경둜
        
    Returns:
        ERP 양식 λ°μ΄ν„°ν”„λ ˆμž„
    """
    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 save_to_files(result_df: pd.DataFrame, output_csv: str, output_excel: str, erp_data_count: int) -> None:
    """
    κ²°κ³Όλ₯Ό CSV 및 Excel 파일둜 μ €μž₯
    
    Args:
        result_df: κ²°κ³Ό λ°μ΄ν„°ν”„λ ˆμž„
        output_csv: CSV 좜λ ₯ 파일 경둜
        output_excel: Excel 좜λ ₯ 파일 경둜
        erp_data_count: ERP 데이터 ν–‰ 수
        
    Returns:
        None
    """
    # CSV 파일 μ €μž₯
    print(f"'{output_csv}'둜 CSV μ €μž₯ 쀑...")
    try:
        result_df.to_csv(output_csv, index=False, encoding=cfg.CSV_OUTPUT_ENCODING)
        print(f"처리 μ™„λ£Œ: {erp_data_count}개 행이 '{output_csv}'에 μ €μž₯됨 ({cfg.CSV_OUTPUT_ENCODING} 인코딩)")
        print(f"λ°μ΄ν„°λŠ” {cfg.ERP_DATA_ROW_START}ν–‰λΆ€ν„° μ‹œμž‘ν•©λ‹ˆλ‹€.")
    except Exception as e:
        print(f"CSV 파일 μ €μž₯ 쀑 였λ₯˜ λ°œμƒ: {e}")
    
    # Excel 파일 μ €μž₯
    print(f"\n'{output_excel}'둜 μ—‘μ…€ 파일 μ €μž₯ 쀑...")
    try:
        result_df.to_excel(output_excel, index=False, engine='openpyxl')
        print(f"처리 μ™„λ£Œ: {erp_data_count}개 행이 '{output_excel}'에 μ €μž₯됨")
        print(f"μ—‘μ…€ 파일이 μ„±κ³΅μ μœΌλ‘œ μƒμ„±λ˜μ—ˆμŠ΅λ‹ˆλ‹€: {os.path.abspath(output_excel)}")
    except Exception as e:
        print(f"μ—‘μ…€ 파일 μ €μž₯ 쀑 였λ₯˜ λ°œμƒ: {e}")
        print("CSV νŒŒμΌμ€ μ •μƒμ μœΌλ‘œ μ €μž₯λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
        print("CSV νŒŒμΌμ„ μ—΄ λ•ŒλŠ” Excel의 '데이터' νƒ­μ—μ„œ 'ν…μŠ€νŠΈ/CSVμ—μ„œ' κΈ°λŠ₯을 μ‚¬μš©ν•˜μ‹œκΈ° λ°”λžλ‹ˆλ‹€.")


def prepare_file_with_template(erp_df: pd.DataFrame, erp_form: 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 볡사 (양식 파일의 처음 4ν–‰λ§Œ μ‚¬μš©)
        result_df = erp_form.copy()
        
        # 양식 파일이 4행보닀 많으면 4ν–‰λ§Œ μœ μ§€
        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