File size: 3,042 Bytes
afd0a81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

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