Files
KCGL/inventory-backend/app/models/stock.py

56 lines
2.4 KiB
Python

from app.extensions import db
from datetime import datetime
class StockBuy(db.Model):
__tablename__ = 'stock_buy'
id = db.Column(db.Integer, primary_key=True)
# 外键:必须关联一个 MaterialBase 的 ID
material_id = db.Column(db.Integer, db.ForeignKey('material_base.id'), nullable=False)
# 业务数据
inbound_date = db.Column(db.DateTime, default=datetime.utcnow) # 入库时间
batch_no = db.Column(db.String(100)) # 批次号
warehouse_loc = db.Column(db.String(100)) # 库位
supplier_name = db.Column(db.String(255)) # 供应商
# 数量与状态
qty_inbound = db.Column(db.Numeric(19, 4), default=0) # 初始入库量
qty_current = db.Column(db.Numeric(19, 4), default=0) # 当前剩余量
qty_available = db.Column(db.Numeric(19, 4), default=0) # 当前可用量
status = db.Column(db.String(50), default='NORMAL')
# 财务数据
price_unit = db.Column(db.Numeric(19, 4), default=0) # 单价
price_total = db.Column(db.Numeric(19, 4), default=0) # 总价
# 建立与 MaterialBase 的双向关系
material = db.relationship('MaterialBase', back_populates='stock_buys')
def to_dict(self):
"""
序列化方法:
这里做了一个扁平化处理,把关联的 material 里的 name/sku 直接拿出来,
方便前端表格直接显示,不用前端再去拼凑。
"""
return {
'id': self.id,
'material_id': self.material_id,
# 从关联对象获取基础信息
'sku_code': self.material.sku_code if self.material else None,
'material_name': self.material.name if self.material else None,
'spec_model': self.material.spec_model if self.material else None,
'unit': self.material.unit if self.material else None,
'category': self.material.category if self.material else None,
# 本表信息
'inbound_date': self.inbound_date.strftime('%Y-%m-%d %H:%M:%S') if self.inbound_date else None,
'batch_no': self.batch_no,
'warehouse_loc': self.warehouse_loc,
'supplier_name': self.supplier_name,
'qty_inbound': float(self.qty_inbound) if self.qty_inbound else 0,
'price_unit': float(self.price_unit) if self.price_unit else 0,
'price_total': float(self.price_total) if self.price_total else 0,
}