""" 采购单生成模块 - 处理采购单的生成和导出 """ from datetime import datetime from PyQt5.QtWidgets import ( QDialog, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QTextEdit, QMessageBox, QFileDialog, QApplication ) from PyQt5.QtGui import QFont from database import get_db_connection class PurchaseOrderDialog(QDialog): """采购单生成对话框""" def __init__(self, db_path, style_number, quantity, loss_rate): super().__init__() self.db_path = db_path self.style_number = style_number self.quantity = quantity self.loss_rate = loss_rate self.setWindowTitle(f"生成采购单 - {style_number}") self.resize(900, 700) self.setup_ui() self.generate_po_text() def setup_ui(self): """设置用户界面""" layout = QVBoxLayout(self) # 信息标签 info_label = QLabel( f"款号:{self.style_number}
" f"生产件数:{self.quantity} 件
" f"损耗率:{self.loss_rate*100:.1f}%" ) info_label.setStyleSheet("font-size: 14px; padding: 10px; background-color: #e8f5e9; border-radius: 8px;") layout.addWidget(info_label) # 采购单内容显示 self.po_text = QTextEdit() self.po_text.setReadOnly(True) self.po_text.setFont(QFont("Microsoft YaHei", 12)) layout.addWidget(self.po_text) # 按钮区域 btn_layout = QHBoxLayout() copy_btn = QPushButton("复制到剪贴板") copy_btn.clicked.connect(self.copy_to_clipboard) copy_btn.setStyleSheet("background-color: #2196f3; color: white; padding: 10px; font-weight: bold;") btn_layout.addWidget(copy_btn) save_btn = QPushButton("保存为TXT文件") save_btn.clicked.connect(self.save_to_file) save_btn.setStyleSheet("background-color: #ff9800; color: white; padding: 10px; font-weight: bold;") btn_layout.addWidget(save_btn) layout.addLayout(btn_layout) def get_conn(self): """获取数据库连接""" return get_db_connection(self.db_path) def generate_po_text(self): """生成采购单文本""" text = f"【采购单】\n" text += f"款号:{self.style_number}\n" text += f"生产数量:{self.quantity} 件\n" text += f"损耗率:{self.loss_rate*100:.1f}%\n" text += f"生成日期:{datetime.now().strftime('%Y-%m-%d %H:%M')}\n" text += "="*50 + "\n\n" try: with self.get_conn() as conn: cursor = conn.execute(''' SELECT category, fabric_type, model, usage_per_piece, unit FROM garment_materials WHERE style_number = ? AND usage_per_piece > 0 ORDER BY id ''', (self.style_number,)) rows = cursor.fetchall() for category, fabric_type, model, usage_per_piece, unit in rows: total_usage = usage_per_piece * self.quantity * (1 + self.loss_rate) # 显示材料名称:优先显示型号,否则显示类目-类型,最后只显示类目 if model: material_name = model elif fabric_type: material_name = f"{category}-{fabric_type}" if category else fabric_type else: material_name = category or "未命名材料" text += f"材料:{material_name}\n" text += f" 单件用量:{usage_per_piece:.3f} {unit}\n" text += f" 总需采购:{total_usage:.3f} {unit}\n\n" if not rows: text += "该款号暂无材料用量记录。\n" except Exception as e: text += f"加载失败:{str(e)}" self.po_text.setPlainText(text) def copy_to_clipboard(self): """复制到剪贴板""" QApplication.clipboard().setText(self.po_text.toPlainText()) QMessageBox.information(self, "成功", "采购单内容已复制到剪贴板!") def save_to_file(self): """保存为文件""" default_name = f"采购单_{self.style_number}_{self.quantity}件_{datetime.now().strftime('%Y%m%d')}.txt" file_path, _ = QFileDialog.getSaveFileName(self, "保存采购单", default_name, "Text Files (*.txt)") if file_path: try: with open(file_path, "w", encoding="utf-8") as f: f.write(self.po_text.toPlainText()) QMessageBox.information(self, "成功", f"采购单已保存至:\n{file_path}") except Exception as e: QMessageBox.critical(self, "错误", "保存失败: " + str(e))