From 1927ad91e6b4831ddb8d241325efce849c42b7fd Mon Sep 17 00:00:00 2001 From: KWON Date: Mon, 28 Jul 2025 16:26:41 +0900 Subject: [PATCH] =?UTF-8?q?GUI=20=EA=B8=B0=EB=8A=A5=EC=9D=80=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EC=A7=80=20=EC=95=8A=EC=9C=BC=EB=AF=80?= =?UTF-8?q?=EB=A1=9C=20=EC=A0=9C=EA=B1=B0=ED=95=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/pos_update_daily_product.py | 66 +++++++++------------------------ 1 file changed, 18 insertions(+), 48 deletions(-) diff --git a/lib/pos_update_daily_product.py b/lib/pos_update_daily_product.py index 6df705c..9ddd326 100644 --- a/lib/pos_update_daily_product.py +++ b/lib/pos_update_daily_product.py @@ -1,22 +1,16 @@ # POS Update ''' -포스 데이터를 추출한 엑셀파일을 업데이트 OK포스 > 매출관리 > 일자별 > 상품별 > 날짜 지정 > 조회줄수 5000으로 변경 > 엑셀 추출파일을 ./data에 복사 본 파일 실행하면 자동으로 mariadb의 DB에 삽입함. - ''' - import sys, os -sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) - -import tkinter as tk import pandas as pd -from tkinter import filedialog, messagebox from sqlalchemy.dialects.mysql import insert as mysql_insert from sqlalchemy.exc import IntegrityError +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from conf import db, db_schema from lib.common import load_config @@ -29,14 +23,12 @@ def update_pos_table(engine, table, df): data = row.to_dict() stmt = mysql_insert(table).values(**data) - # insert ... on duplicate key update (복합 unique key 기준) update_data = { 'qty': data['qty'], 'tot_amount': data['tot_amount'], 'tot_discount': data['tot_discount'], 'actual_amount': data['actual_amount'] } - stmt = stmt.on_duplicate_key_update(**update_data) try: @@ -50,7 +42,6 @@ def process_file(filepath, table, engine): print(f"[INFO] 처리 시작: {filepath}") try: ext = os.path.splitext(filepath)[-1].lower() - if ext == ".xls": df = pd.read_excel(filepath, header=5, engine="xlrd") elif ext == ".xlsx": @@ -73,8 +64,7 @@ def process_file(filepath, table, engine): '실매출액': 'actual_amount' }, inplace=True) - if 'idx' in df.columns: - df = df.drop(columns=['idx']) + df.drop(columns=[col for col in ['idx'] if col in df.columns], inplace=True) df['date'] = pd.to_datetime(df['date']).dt.date df['barcode'] = df['barcode'].astype(int) @@ -98,37 +88,29 @@ def process_file(filepath, table, engine): def batch_process_files(table, engine): files = [f for f in os.listdir(DATA_DIR) if f.startswith("일자별 (상품별)") and f.endswith(('.xlsx', '.xls'))] - if not files: print("[INFO] 처리할 파일이 없습니다.") return False print(f"[INFO] {len(files)}개의 파일을 찾았습니다.") total_rows = 0 + deleted_files = 0 - print(f"[INFO] 처리된 전체 데이터 건수: {total_rows}") - return True - -def run_pos_update(): - filepath = filedialog.askopenfilename( - filetypes=[("Excel Files", "*.xlsx *.xls")], - title="파일을 선택하세요" - ) - if not filepath: - return - - engine = db.engine - try: - table = db_schema.pos - except AttributeError: - messagebox.showerror("DB 오류", "'pos' 테이블이 db_schema에 정의되어 있지 않습니다.") - return - - if messagebox.askyesno("확인", f"'{os.path.basename(filepath)}' 파일을 'pos' 테이블에 업로드 하시겠습니까?"): - success, count = process_file(filepath, table, engine) + for fname in files: + full_path = os.path.join(DATA_DIR, fname) + success, count = process_file(full_path, table, engine) if success: - print(f"[INFO] 수동 선택된 파일 처리 완료: {count}건") - messagebox.showinfo("완료", f"DB 업데이트가 완료되었습니다.\n총 {count}건 처리됨.") + total_rows += count + try: + os.remove(full_path) + print(f"[INFO] 파일 삭제 완료: {fname}") + deleted_files += 1 + except Exception as e: + print(f"[WARN] 파일 삭제 실패: {fname} / {e}") + + print(f"[INFO] 총 처리 데이터 건수: {total_rows}") + print(f"[INFO] 삭제된 파일 수: {deleted_files}") + return True def main(): engine = db.engine @@ -140,19 +122,7 @@ def main(): batch_done = batch_process_files(table, engine) if not batch_done: - print("[ERROR] batch_process_files 실행 실패") -# # GUI 시작 -# root = tk.Tk() -# root.title("POS 데이터 업데이트") -# root.geometry("300x150") -# -# lbl = tk.Label(root, text="POS 데이터 업데이트") -# lbl.pack(pady=20) -# -# btn = tk.Button(root, text="데이터 선택 및 업데이트", command=run_pos_update) -# btn.pack() -# -# root.mainloop() + print("[INFO] 처리할 데이터가 없습니다.") if __name__ == "__main__": main()