32 lines
1022 B
Python
32 lines
1022 B
Python
from . import db, BaseModel
|
|
|
|
|
|
class Transaction(BaseModel):
|
|
"""交易記錄模型"""
|
|
__tablename__ = 'transactions'
|
|
|
|
product_id = db.Column(db.Integer, db.ForeignKey('products.id'), nullable=False, comment='商品ID')
|
|
type = db.Column(db.String(20), nullable=False, comment='交易類型: in=入庫, out=出庫')
|
|
quantity = db.Column(db.Integer, nullable=False, comment='交易數量')
|
|
notes = db.Column(db.Text, comment='備註')
|
|
|
|
# 關聯關係
|
|
product = db.relationship(
|
|
'Product',
|
|
backref=db.backref('transactions', lazy='dynamic'),
|
|
lazy='joined'
|
|
)
|
|
|
|
def __repr__(self):
|
|
return f'<Transaction {self.type}: {self.quantity} of {self.product.code}>'
|
|
|
|
@property
|
|
def transaction_type_display(self):
|
|
"""顯示友好的交易類型名稱"""
|
|
return '入庫' if self.type == 'in' else '出庫'
|
|
|
|
@property
|
|
def transaction_value(self):
|
|
"""計算交易金額"""
|
|
return float(self.product.price * self.quantity)
|