原料编辑功能改为独立窗口

- 新增 RawMaterialEditDialog 独立编辑对话框类
- 编辑原料时弹出新窗口,不再复用"新增原料"标签页
- 编辑窗口中型号字段设为只读,防止误修改
- 新增原料功能改为纯新增模式,检查重复型号
- 移除 current_edit_model 状态变量,简化代码逻辑

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-27 18:34:28 +08:00
parent c45912cb9e
commit 14e25ad03b

View File

@@ -14,12 +14,200 @@ from database import get_db_connection
from stock_dialog import StockInDialog
class RawMaterialEditDialog(QDialog):
"""原料编辑对话框 - 独立窗口"""
def __init__(self, db_path, model, is_admin=False, parent=None):
super().__init__(parent)
self.db_path = db_path
self.model = model
self.is_admin = is_admin
self.setWindowTitle(f"编辑原料 - {model}")
self.resize(600, 500)
self.setup_ui()
self.load_material_data()
def setup_ui(self):
"""设置用户界面"""
layout = QGridLayout(self)
# 类目选择
layout.addWidget(QLabel("类目:"), 0, 0, Qt.AlignRight)
self.edit_major_category = QComboBox()
self.edit_major_category.setEditable(True)
layout.addWidget(self.edit_major_category, 0, 1)
layout.addWidget(QLabel("类型:"), 0, 2, Qt.AlignRight)
self.edit_sub_category = QLineEdit()
layout.addWidget(self.edit_sub_category, 0, 3)
# 基本信息
layout.addWidget(QLabel("型号:"), 1, 0, Qt.AlignRight)
self.edit_model = QLineEdit()
self.edit_model.setReadOnly(True) # 型号不可修改
self.edit_model.setStyleSheet("background-color: #f0f0f0;")
layout.addWidget(self.edit_model, 1, 1, 1, 3)
layout.addWidget(QLabel("供应商:"), 2, 0, Qt.AlignRight)
self.edit_supplier = QComboBox()
self.edit_supplier.setEditable(True)
layout.addWidget(self.edit_supplier, 2, 1, 1, 3)
layout.addWidget(QLabel("颜色:"), 3, 0, Qt.AlignRight)
self.edit_color = QLineEdit()
layout.addWidget(self.edit_color, 3, 1, 1, 3)
# 规格信息
layout.addWidget(QLabel("幅宽 (cm):"), 4, 0, Qt.AlignRight)
self.edit_width = QDoubleSpinBox()
self.edit_width.setRange(0, 300)
self.edit_width.setValue(0)
layout.addWidget(self.edit_width, 4, 1)
layout.addWidget(QLabel("克重 (g/m²):"), 5, 0, Qt.AlignRight)
self.edit_gsm = QDoubleSpinBox()
self.edit_gsm.setRange(0, 1000)
self.edit_gsm.setValue(0)
layout.addWidget(self.edit_gsm, 5, 1)
layout.addWidget(QLabel("单位:"), 6, 0, Qt.AlignRight)
self.edit_unit = QComboBox()
self.edit_unit.setEditable(True)
self.edit_unit.addItems(["", "", "公斤", "一对", "", ""])
layout.addWidget(self.edit_unit, 6, 1)
# 价格信息
layout.addWidget(QLabel("散剪价 (元/单位):"), 7, 0, Qt.AlignRight)
self.edit_retail = QDoubleSpinBox()
self.edit_retail.setRange(0, 10000)
self.edit_retail.setDecimals(2)
layout.addWidget(self.edit_retail, 7, 1)
layout.addWidget(QLabel("大货价 (元/单位):"), 8, 0, Qt.AlignRight)
self.edit_bulk = QDoubleSpinBox()
self.edit_bulk.setRange(0, 10000)
self.edit_bulk.setDecimals(2)
layout.addWidget(self.edit_bulk, 8, 1)
# 按钮
button_layout = QHBoxLayout()
save_btn = QPushButton("保存修改")
save_btn.clicked.connect(self.save_changes)
save_btn.setStyleSheet("background-color: #4CAF50; color: white; padding: 8px; font-weight: bold;")
button_layout.addWidget(save_btn)
cancel_btn = QPushButton("取消")
cancel_btn.clicked.connect(self.reject)
button_layout.addWidget(cancel_btn)
layout.addLayout(button_layout, 9, 0, 1, 4)
# 加载类目和供应商列表
self.load_categories_and_suppliers()
def get_conn(self):
"""获取数据库连接"""
return get_db_connection(self.db_path)
def load_categories_and_suppliers(self):
"""加载类目和供应商列表"""
try:
with self.get_conn() as conn:
# 加载类目
cursor = conn.execute("SELECT DISTINCT category FROM fabrics WHERE category IS NOT NULL AND category != ''")
majors = set(row[0] for row in cursor.fetchall() if row[0])
majors.update({"布料", "辅料", "其他"})
self.edit_major_category.addItems(sorted(majors))
# 加载供应商
cursor = conn.execute("SELECT DISTINCT supplier FROM fabrics WHERE supplier IS NOT NULL AND supplier != '' ORDER BY supplier")
suppliers = [row[0] for row in cursor.fetchall()]
self.edit_supplier.addItems(suppliers)
except:
pass
def load_material_data(self):
"""加载原料数据"""
try:
with self.get_conn() as conn:
cursor = conn.execute(
"SELECT category, fabric_type, supplier, color, width, gsm, unit, retail_price, bulk_price FROM fabrics WHERE model = ?",
(self.model,)
)
row = cursor.fetchone()
if not row:
QMessageBox.warning(self, "提示", "原料不存在或已被删除")
self.reject()
return
category, fabric_type, supplier, color, width, gsm, unit, retail, bulk = row
# 填充表单
self.edit_model.setText(self.model)
self.edit_major_category.setCurrentText(category or "")
self.edit_sub_category.setText(fabric_type or "")
self.edit_supplier.setCurrentText(supplier or "")
self.edit_color.setText(color or "")
self.edit_width.setValue(width or 0)
self.edit_gsm.setValue(gsm or 0)
self.edit_unit.setCurrentText(unit or "")
self.edit_retail.setValue(retail or 0)
self.edit_bulk.setValue(bulk or 0)
# 特殊处理胸杯类型
if fabric_type and "胸杯" in fabric_type:
self.edit_unit.setEnabled(False)
except Exception as e:
QMessageBox.critical(self, "错误", f"加载原料信息失败: {str(e)}")
self.reject()
def save_changes(self):
"""保存修改"""
major = self.edit_major_category.currentText().strip()
sub = self.edit_sub_category.text().strip()
# 特殊处理胸杯类型
if "胸杯" in sub:
major = "辅料"
category = major or ""
fabric_type = sub or ""
supplier = self.edit_supplier.currentText().strip()
color = self.edit_color.text().strip()
unit = self.edit_unit.currentText().strip() or ""
try:
with self.get_conn() as conn:
conn.execute('''
UPDATE fabrics
SET category=?, fabric_type=?, supplier=?, color=?, width=?, gsm=?,
retail_price=?, bulk_price=?, unit=?, timestamp=?
WHERE model=?
''', (
category, fabric_type, supplier, color,
self.edit_width.value() or None,
self.edit_gsm.value() or None,
self.edit_retail.value() or None,
self.edit_bulk.value() or None,
unit,
datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
self.model
))
conn.commit()
QMessageBox.information(self, "成功", f"已更新 '{self.model}'")
self.accept()
except Exception as e:
QMessageBox.critical(self, "错误", f"保存失败: {str(e)}")
class RawMaterialLibraryDialog(QDialog):
def __init__(self, db_path, is_admin=False):
super().__init__()
self.db_path = db_path
self.is_admin = is_admin
self.current_edit_model = None
self.setWindowTitle("原料库管理")
self.resize(1400, 700)
@@ -51,7 +239,7 @@ class RawMaterialLibraryDialog(QDialog):
# 新增/编辑标签页
add_tab = self.create_add_tab()
tabs.addTab(add_tab, "新增/编辑原料")
tabs.addTab(add_tab, "新增原料")
# 库存跟踪标签页
stock_tab = self.create_stock_tab()
@@ -83,7 +271,7 @@ class RawMaterialLibraryDialog(QDialog):
self.supplier_combo.currentIndexChanged.connect(self.load_table)
filter_layout.addWidget(self.supplier_combo)
filter_layout.addWidget(QLabel("搜索型号/名称:"))
filter_layout.addWidget(QLabel("搜索型号:"))
self.search_input = QLineEdit()
self.search_input.textChanged.connect(self.load_table)
filter_layout.addWidget(self.search_input)
@@ -120,18 +308,12 @@ class RawMaterialLibraryDialog(QDialog):
add_layout.addWidget(QLabel("类目:"), 0, 0, Qt.AlignRight)
self.add_major_category = QComboBox()
self.add_major_category.setEditable(True)
self.add_major_category.currentTextChanged.connect(self.on_major_changed)
add_layout.addWidget(self.add_major_category, 0, 1)
add_layout.addWidget(QLabel("类型:"), 0, 2, Qt.AlignRight)
self.add_sub_category = QLineEdit()
self.add_sub_category.textChanged.connect(self.on_sub_changed)
add_layout.addWidget(self.add_sub_category, 0, 3)
add_layout.addWidget(QLabel("完整分类(显示用):"), 1, 0, Qt.AlignRight)
self.full_category_label = QLabel("布料-")
add_layout.addWidget(self.full_category_label, 1, 1, 1, 3)
# 基本信息
add_layout.addWidget(QLabel("型号:"), 2, 0, Qt.AlignRight)
self.add_model = QLineEdit()
@@ -213,30 +395,6 @@ class RawMaterialLibraryDialog(QDialog):
dialog.exec_()
self.load_stock_table()
def on_major_changed(self, text):
"""主类目改变事件"""
self.update_full_category()
def on_sub_changed(self, text):
"""子类型改变事件"""
if "胸杯" in text:
self.add_major_category.setCurrentText("辅料")
self.add_unit.setCurrentText("一对")
self.add_unit.setEnabled(False)
else:
self.add_unit.setEnabled(True)
self.update_full_category()
def update_full_category(self):
"""更新完整类目显示"""
major = self.add_major_category.currentText().strip()
sub = self.add_sub_category.text().strip()
if major == "布料" and sub:
full = "布料-" + sub
else:
full = sub or major
self.full_category_label.setText(full)
def refresh_filters_and_table(self):
"""刷新过滤器和表格"""
self.load_major_categories()
@@ -427,43 +585,11 @@ class RawMaterialLibraryDialog(QDialog):
QMessageBox.critical(self, "加载失败", str(e))
def edit_raw_material(self, model):
"""编辑原料"""
try:
with self.get_conn() as conn:
cursor = conn.execute("SELECT category, fabric_type, supplier, color, width, gsm, unit, retail_price, bulk_price FROM fabrics WHERE model = ?", (model,))
row = cursor.fetchone()
if not row:
QMessageBox.warning(self, "提示", "原料不存在或已被删除")
return
category, fabric_type, supplier, color, width, gsm, unit, retail, bulk = row
major = category or ""
sub = fabric_type or ""
self.add_major_category.setCurrentText(major)
self.add_sub_category.setText(sub)
self.update_full_category()
self.add_supplier.setCurrentText(supplier or "")
self.add_color.setText(color or "")
self.add_model.setText(model)
self.add_width.setValue(width or 0)
self.add_gsm.setValue(gsm or 0)
self.add_unit.setCurrentText(unit or "")
self.add_retail.setValue(retail or 0)
self.add_bulk.setValue(bulk or 0)
if "胸杯" in sub:
self.add_unit.setEnabled(False)
self.current_edit_model = model
# 切换到编辑标签页
tabs = self.findChild(QTabWidget)
tabs.setCurrentIndex(1)
QMessageBox.information(self, "提示", f"已加载 '{model}' 的信息,可修改后点击'保存原料'")
except Exception as e:
QMessageBox.critical(self, "错误", "加载原料信息失败: " + str(e))
"""编辑原料 - 打开独立编辑窗口"""
dialog = RawMaterialEditDialog(self.db_path, model, self.is_admin, self)
if dialog.exec_() == QDialog.Accepted:
# 编辑成功后刷新列表
self.refresh_filters_and_table()
def delete_raw(self, model):
"""删除原料"""
@@ -479,7 +605,7 @@ class RawMaterialLibraryDialog(QDialog):
QMessageBox.critical(self, "错误", "删除失败: " + str(e))
def save_raw_material(self):
"""保存原料"""
"""保存新增原料"""
model = self.add_model.text().strip()
if not model:
QMessageBox.warning(self, "错误", "请输入型号/名称")
@@ -499,8 +625,14 @@ class RawMaterialLibraryDialog(QDialog):
try:
with self.get_conn() as conn:
# 检查是否已存在
cursor = conn.execute("SELECT model FROM fabrics WHERE model = ?", (model,))
if cursor.fetchone():
QMessageBox.warning(self, "错误", f"型号 '{model}' 已存在,请使用编辑功能修改")
return
conn.execute('''
INSERT OR REPLACE INTO fabrics
INSERT INTO fabrics
(model, category, fabric_type, supplier, color, width, gsm, retail_price, bulk_price, unit, timestamp)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (model, category, fabric_type, supplier, color,
@@ -509,9 +641,7 @@ class RawMaterialLibraryDialog(QDialog):
unit, datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
conn.commit()
action = "更新" if self.current_edit_model else "保存"
QMessageBox.information(self, "成功", f"{action} '{model}'")
self.current_edit_model = None
QMessageBox.information(self, "成功", f"已保存 '{model}'")
# 清空表单
self.add_model.clear()
@@ -522,7 +652,6 @@ class RawMaterialLibraryDialog(QDialog):
self.add_bulk.setValue(0)
self.add_sub_category.clear()
self.add_unit.setEnabled(True)
self.update_full_category()
self.refresh_filters_and_table()
self.load_add_major_categories()