60 lines
2.0 KiB
Python
60 lines
2.0 KiB
Python
from ..models.product import Product
|
|
from ..models.transaction import Transaction
|
|
import pandas as pd
|
|
import io
|
|
|
|
|
|
class ReportService:
|
|
@staticmethod
|
|
def generate_inventory_report():
|
|
"""生成庫存報表"""
|
|
products = Product.query.all()
|
|
|
|
data = []
|
|
for product in products:
|
|
data.append({
|
|
'商品代碼': product.code,
|
|
'商品名稱': product.name,
|
|
'現有庫存': product.quantity,
|
|
'最低庫存': product.min_quantity,
|
|
'單價': float(product.price),
|
|
'庫存金額': float(product.price) * product.quantity,
|
|
'庫存狀態': '低於安全庫存' if product.quantity <= product.min_quantity else '正常'
|
|
})
|
|
|
|
df = pd.DataFrame(data)
|
|
output = io.BytesIO()
|
|
df.to_excel(output, index=False, sheet_name='庫存報表')
|
|
output.seek(0)
|
|
return output
|
|
|
|
@staticmethod
|
|
def generate_transaction_report(start_date=None, end_date=None):
|
|
"""生成交易報表"""
|
|
query = Transaction.query
|
|
|
|
if start_date:
|
|
query = query.filter(Transaction.created_at >= start_date)
|
|
if end_date:
|
|
query = query.filter(Transaction.created_at <= end_date)
|
|
|
|
transactions = query.order_by(Transaction.created_at.desc()).all()
|
|
|
|
data = []
|
|
for trans in transactions:
|
|
data.append({
|
|
'日期': trans.created_at.strftime('%Y-%m-%d %H:%M:%S'),
|
|
'商品代碼': trans.product.code,
|
|
'商品名稱': trans.product.name,
|
|
'類型': '入庫' if trans.type == 'in' else '出庫',
|
|
'數量': trans.quantity,
|
|
'單價': float(trans.product.price),
|
|
'金額': float(trans.product.price) * trans.quantity
|
|
})
|
|
|
|
df = pd.DataFrame(data)
|
|
output = io.BytesIO()
|
|
df.to_excel(output, index=False, sheet_name='交易報表')
|
|
output.seek(0)
|
|
return output
|