能够帮我以此为基础改写一篇用于公司销售管理的代码实例吗
时间: 2024-02-13 20:06:32 浏览: 29
当然可以,以下是一个基于该博客的代码实例,用于公司销售管理:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QMessageBox, QComboBox, QTableWidget, QTableWidgetItem
from PyQt5.QtCore import Qt, QDate, QTime, QDateTime
from PyQt5.QtGui import QFont, QIntValidator
from sqlalchemy import create_engine, Column, String, Integer, ForeignKey
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy.ext.declarative import declarative_base
import pandas as pd
import random
# 创建连接和ORM基类
engine = create_engine('mssql+pymssql://username:password@server/database')
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()
# 定义ORM模型
class Customer(Base):
__tablename__ = 'customers'
id = Column(Integer, primary_key=True)
name = Column(String(20))
phone = Column(String(11))
address = Column(String(50))
orders = relationship('Order', back_populates='customer')
class Product(Base):
__tablename__ = 'products'
id = Column(Integer, primary_key=True)
name = Column(String(20))
price = Column(Integer)
orders = relationship('OrderItem', back_populates='product')
class Order(Base):
__tablename__ = 'orders'
id = Column(Integer, primary_key=True)
order_no = Column(String(20), unique=True)
customer_id = Column(Integer, ForeignKey('customers.id'))
customer = relationship('Customer', back_populates='orders')
order_date = Column(String(20))
total_price = Column(Integer)
order_items = relationship('OrderItem', back_populates='order')
class OrderItem(Base):
__tablename__ = 'order_items'
id = Column(Integer, primary_key=True)
order_id = Column(Integer, ForeignKey('orders.id'))
order = relationship('Order', back_populates='order_items')
product_id = Column(Integer, ForeignKey('products.id'))
product = relationship('Product', back_populates='orders')
quantity = Column(Integer)
# 创建表
Base.metadata.create_all(engine)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 设置主窗口属性
self.setWindowTitle('销售管理系统')
self.resize(800, 600)
# 创建主窗口中的控件
self.central_widget = QWidget(self)
self.setCentralWidget(self.central_widget)
self.customer_label = QLabel('客户:', self.central_widget)
self.customer_combo = QComboBox(self.central_widget)
self.customer_combo.addItem('')
self.load_customers()
self.product_label = QLabel('产品:', self.central_widget)
self.product_combo = QComboBox(self.central_widget)
self.product_combo.addItem('')
self.load_products()
self.quantity_label = QLabel('数量:', self.central_widget)
self.quantity_edit = QLineEdit(self.central_widget)
self.quantity_edit.setValidator(QIntValidator())
self.add_button = QPushButton('添加', self.central_widget)
self.add_button.clicked.connect(self.add_order_item)
self.clear_button = QPushButton('清空', self.central_widget)
self.clear_button.clicked.connect(self.clear_inputs)
self.submit_button = QPushButton('提交订单', self.central_widget)
self.submit_button.clicked.connect(self.submit_order)
self.order_table = QTableWidget(self.central_widget)
self.order_table.setColumnCount(4)
self.order_table.setHorizontalHeaderLabels(['产品', '单价', '数量', '金额'])
self.order_table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.total_price_label = QLabel('总价:', self.central_widget)
total_price_font = QFont()
total_price_font.setPointSize(16)
self.total_price_label.setFont(total_price_font)
self.total_price_value_label = QLabel(self.central_widget)
self.total_price_value_label.setFont(total_price_font)
# 创建主窗口中的布局
self.v_layout = QVBoxLayout(self.central_widget)
self.h_layout = QHBoxLayout()
self.h_layout.addWidget(self.customer_label)
self.h_layout.addWidget(self.customer_combo)
self.h_layout.addWidget(self.product_label)
self.h_layout.addWidget(self.product_combo)
self.h_layout.addWidget(self.quantity_label)
self.h_layout.addWidget(self.quantity_edit)
self.h_layout.addWidget(self.add_button)
self.h_layout.addWidget(self.clear_button)
self.v_layout.addLayout(self.h_layout)
self.v_layout.addWidget(self.order_table)
self.v_layout.addWidget(self.total_price_label)
self.v_layout.addWidget(self.total_price_value_label)
self.v_layout.addWidget(self.submit_button)
# 初始化订单数据
self.order_items = []
# 加载客户列表
def load_customers(self):
self.customer_combo.clear()
customers = session.query(Customer).all()
for customer in customers:
self.customer_combo.addItem(customer.name)
# 加载产品列表
def load_products(self):
self.product_combo.clear()
products = session.query(Product).all()
for product in products:
self.product_combo.addItem(product.name)
# 添加订单明细
def add_order_item(self):
customer_name = self.customer_combo.currentText()
product_name = self.product_combo.currentText()
quantity = self.quantity_edit.text()
if not customer_name or not product_name or not quantity:
QMessageBox.warning(self, '警告', '请填写完整')
return
customer = session.query(Customer).filter_by(name=customer_name).first()
product = session.query(Product).filter_by(name=product_name).first()
price = product.price
amount = price * int(quantity)
order_item = {'product': product_name, 'price': price, 'quantity': quantity, 'amount': amount}
self.order_items.append(order_item)
self.update_order_table()
# 更新订单表格
def update_order_table(self):
self.order_table.setRowCount(len(self.order_items))
total_price = 0
for i, order_item in enumerate(self.order_items):
product_item = QTableWidgetItem(order_item['product'])
price_item = QTableWidgetItem(str(order_item['price']))
quantity_item = QTableWidgetItem(order_item['quantity'])
amount_item = QTableWidgetItem(str(order_item['amount']))
self.order_table.setItem(i, 0, product_item)
self.order_table.setItem(i, 1, price_item)
self.order_table.setItem(i, 2, quantity_item)
self.order_table.setItem(i, 3, amount_item)
total_price += order_item['amount']
self.total_price_value_label.setText(str(total_price))
# 清空输入框
def clear_inputs(self):
self.customer_combo.setCurrentIndex(0)
self.product_combo.setCurrentIndex(0)
self.quantity_edit.clear()
# 提交订单
def submit_order(self):
customer_name = self.customer_combo.currentText()
if not customer_name or not self.order_items:
QMessageBox.warning(self, '警告', '请填写完整')
return
customer = session.query(Customer).filter_by(name=customer_name).first()
order_no = self.generate_order_no()
order_date = QDateTime.currentDateTime().toString('yyyy-MM-dd hh:mm:ss')
total_price = int(self.total_price_value_label.text())
order = Order(order_no=order_no, customer=customer, order_date=order_date, total_price=total_price)
session.add(order)
for order_item in self.order_items:
product_name = order_item['product']
quantity = order_item['quantity']
product = session.query(Product).filter_by(name=product_name).first()
order_item = OrderItem(order=order, product=product, quantity=quantity)
session.add(order_item)
session.commit()
QMessageBox.information(self, '提示', '订单提交成功')
self.order_items = []
self.update_order_table()
self.load_customers()
self.clear_inputs()
# 生成订单号
def generate_order_no(self):
prefix = 'SO'
date = QDate.currentDate().toString('yyyyMMdd')
suffix = str(random.randint(1000, 9999))
order_no = prefix + date + suffix
return order_no
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
该代码实现了一个简单的销售管理系统,包括客户管理、产品管理、订单管理等功能,使用了PyQt5和SQLAlchemy等库,并且采用了ORM模型,使得代码具有较好的可扩展性和可维护性。具体使用方法请参考代码注释。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)