h-1.flet.3/app_master_management.py

156 lines
5.2 KiB
Python

"""
マスタ管理アプリケーション
統合的なマスタ管理機能を提供
"""
import flet as ft
import sqlite3
import signal
import sys
import logging
from components.master_editor import (
CustomerMasterEditor, ProductMasterEditor, SalesSlipMasterEditor,
create_customer_master, create_product_master, create_sales_slip_master
)
class MasterManagementApp:
"""マスタ管理アプリケーション"""
def __init__(self, page: ft.Page):
self.page = page
ErrorHandler.current_page = page
# ログ設定
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('app.log'),
logging.StreamHandler()
]
)
# シグナルハンドラ設定
def signal_handler(signum, frame):
print(f"\nシグナル {signum} を受信しました")
print("✅ 正常終了処理完了")
logging.info("アプリケーション正常終了")
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
# データベース初期化
self._init_database()
# ウィンドウ設定
page.title = "マスタ管理システム"
page.window_width = 1000
page.window_height = 700
page.theme_mode = ft.ThemeMode.LIGHT
# ウィンドウクローズイベント
page.on_window_close = lambda _: signal_handler(0, None)
# マスタエディタ作成
self.customer_editor = create_customer_master(page)
self.product_editor = create_product_master(page)
self.sales_slip_editor = create_sales_slip_master(page)
# 現在のエディタ
current_editor = [0]
# タブインターフェース
tabs = ft.Tabs(
selected_index=0,
tabs=[
ft.Tab(
text="顧客マスタ",
content=self.customer_editor.build()
),
ft.Tab(
text="商品マスタ",
content=self.product_editor.build()
),
ft.Tab(
text="伝票マスタ",
content=self.sales_slip_editor.build()
)
],
expand=True
)
# ページ構築
page.add(
ft.Column([
ft.Text("マスタ管理システム", size=24, weight=ft.FontWeight.BOLD, color=ft.Colors.BLUE_900),
ft.Divider(),
ft.Text("各マスタデータの編集・管理が可能です", size=16),
ft.Divider(),
tabs
], expand=True, spacing=15)
)
logging.info("マスタ管理システム起動完了")
print("🚀 マスタ管理システム起動完了")
def _init_database(self):
"""データベース初期化"""
try:
conn = sqlite3.connect('sales.db')
cursor = conn.cursor()
# 各マスタテーブル作成
cursor.execute('''
CREATE TABLE IF NOT EXISTS customers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
phone TEXT,
email TEXT,
address TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
category TEXT,
price REAL NOT NULL,
stock INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS sales_slips (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
customer_name TEXT NOT NULL,
items TEXT NOT NULL,
total_amount REAL NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
conn.commit()
conn.close()
logging.info("マスタデータベース初期化完了")
except Exception as e:
logging.error(f"データベース初期化エラー: {e}")
def main(page: ft.Page):
"""メイン関数"""
try:
app = MasterManagementApp(page)
except Exception as e:
logging.error(f"アプリケーション起動エラー: {e}")
if __name__ == "__main__":
ft.run(main)