#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 입고 관련 데이터 전체 초기화 스크립트 - 입고장, 재고, 로트, 조제, 재고 조정 등 모두 초기화 - herb_items는 기본 31개만 유지 """ import sqlite3 import sys def reset_purchase_data(): """입고 및 관련 데이터 전체 초기화""" conn = sqlite3.connect('database/kdrug.db') cursor = conn.cursor() try: print("=== 입고 및 관련 데이터 초기화 시작 ===\n") # 1. 조제 관련 초기화 (재고 소비 기록) cursor.execute("DELETE FROM compound_consumptions") print(f"✓ compound_consumptions 초기화: {cursor.rowcount}개 삭제") cursor.execute("DELETE FROM compound_ingredients") print(f"✓ compound_ingredients 초기화: {cursor.rowcount}개 삭제") cursor.execute("DELETE FROM compounds") print(f"✓ compounds 초기화: {cursor.rowcount}개 삭제") # 2. 재고 원장 전체 초기화 cursor.execute("DELETE FROM stock_ledger") print(f"✓ stock_ledger 전체 초기화: {cursor.rowcount}개 삭제") # 3. 재고 로트 초기화 cursor.execute("DELETE FROM inventory_lots") print(f"✓ inventory_lots 초기화: {cursor.rowcount}개 삭제") # 4. 입고장 라인 초기화 cursor.execute("DELETE FROM purchase_receipt_lines") print(f"✓ purchase_receipt_lines 초기화: {cursor.rowcount}개 삭제") # 5. 입고장 헤더 초기화 cursor.execute("DELETE FROM purchase_receipts") print(f"✓ purchase_receipts 초기화: {cursor.rowcount}개 삭제") # 6. 재고 조정 초기화 cursor.execute("DELETE FROM stock_adjustment_details") print(f"✓ stock_adjustment_details 초기화: {cursor.rowcount}개 삭제") cursor.execute("DELETE FROM stock_adjustments") print(f"✓ stock_adjustments 초기화: {cursor.rowcount}개 삭제") # 7. herb_items 중 보험코드가 8자리인 잘못된 데이터 삭제 cursor.execute(""" DELETE FROM herb_items WHERE LENGTH(insurance_code) = 8 AND insurance_code NOT LIKE 'A%' """) print(f"✓ 잘못된 herb_items 삭제 (8자리 보험코드): {cursor.rowcount}개") # 8. 테스트용으로 추가된 herb_items 삭제 (ID 32 이후) cursor.execute(""" DELETE FROM herb_items WHERE herb_item_id > 31 """) print(f"✓ 테스트 herb_items 삭제 (ID > 31): {cursor.rowcount}개") # 9. 기존 herb_items의 ingredient_code, specification 초기화 cursor.execute(""" UPDATE herb_items SET ingredient_code = NULL, specification = NULL WHERE herb_item_id <= 31 """) print(f"✓ herb_items ingredient_code/specification 초기화: {cursor.rowcount}개") # 커밋 conn.commit() print("\n=== 현재 데이터 상태 ===") # 현재 상태 확인 cursor.execute("SELECT COUNT(*) FROM herb_items") herb_count = cursor.fetchone()[0] print(f"herb_items: {herb_count}개") cursor.execute("SELECT COUNT(*) FROM purchase_receipts") receipt_count = cursor.fetchone()[0] print(f"purchase_receipts: {receipt_count}개") cursor.execute("SELECT COUNT(*) FROM inventory_lots") lot_count = cursor.fetchone()[0] print(f"inventory_lots: {lot_count}개") print("\n✓ 입고 데이터 초기화 완료!") except Exception as e: conn.rollback() print(f"✗ 오류 발생: {str(e)}", file=sys.stderr) return False finally: conn.close() return True if __name__ == "__main__": # 확인 print("입고 관련 데이터를 초기화합니다.") print("계속하시겠습니까? (y/n): ", end="") confirm = input().strip().lower() if confirm == 'y': if reset_purchase_data(): print("\n초기화가 완료되었습니다.") else: print("\n초기화 실패!") else: print("취소되었습니다.")