init: mini-erp-fabric-demo 迷你 ERP + 治理演示底座(FastAPI+MySQL+Docker,含业务/架构/数据字典文档与稽核脚本)
This commit is contained in:
commit
509fd32518
38 files changed
+3920
No files matched your search
@@ -0,0 +1,58 @@
|
||||
from datetime import date
|
||||
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models import Customer, PurchaseOrder, SalesOrder, Sku, Stock, Supplier
|
||||
from ..serializers import order_out
|
||||
from .inventory import low_stock_list
|
||||
from .orders import PO_CFG, SO_CFG
|
||||
|
||||
|
||||
def summary(db: Session) -> dict:
|
||||
today = date.today()
|
||||
month_start = today.replace(day=1)
|
||||
|
||||
def count(model, *conds):
|
||||
return db.scalar(select(func.count()).select_from(model).where(*conds)) or 0
|
||||
|
||||
def amount(model, status, since=None):
|
||||
stmt = select(func.coalesce(func.sum(model.total_amount), 0)).where(model.status == status)
|
||||
if since is not None:
|
||||
stmt = stmt.where(model.order_date >= since)
|
||||
return float(db.scalar(stmt))
|
||||
|
||||
recent_po = db.scalars(
|
||||
select(PurchaseOrder).order_by(PurchaseOrder.id.desc()).limit(5)
|
||||
).all()
|
||||
recent_so = db.scalars(
|
||||
select(SalesOrder).order_by(SalesOrder.id.desc()).limit(5)
|
||||
).all()
|
||||
|
||||
return {
|
||||
"master": {
|
||||
"supplier_active": count(Supplier, Supplier.status == "ACTIVE"),
|
||||
"customer_active": count(Customer, Customer.status == "ACTIVE"),
|
||||
"sku_active": count(Sku, Sku.status == "ACTIVE"),
|
||||
"stock_sku": count(Stock, Stock.qty_on_hand > 0),
|
||||
},
|
||||
"purchase": {
|
||||
"draft": count(PurchaseOrder, PurchaseOrder.status == "DRAFT"),
|
||||
"confirmed": count(PurchaseOrder, PurchaseOrder.status == "CONFIRMED"),
|
||||
"received": count(PurchaseOrder, PurchaseOrder.status == "RECEIVED"),
|
||||
"cancelled": count(PurchaseOrder, PurchaseOrder.status == "CANCELLED"),
|
||||
"received_amount_total": amount(PurchaseOrder, "RECEIVED"),
|
||||
"received_amount_month": amount(PurchaseOrder, "RECEIVED", month_start),
|
||||
},
|
||||
"sales": {
|
||||
"draft": count(SalesOrder, SalesOrder.status == "DRAFT"),
|
||||
"confirmed": count(SalesOrder, SalesOrder.status == "CONFIRMED"),
|
||||
"shipped": count(SalesOrder, SalesOrder.status == "SHIPPED"),
|
||||
"cancelled": count(SalesOrder, SalesOrder.status == "CANCELLED"),
|
||||
"shipped_amount_total": amount(SalesOrder, "SHIPPED"),
|
||||
"shipped_amount_month": amount(SalesOrder, "SHIPPED", month_start),
|
||||
},
|
||||
"low_stock": low_stock_list(db),
|
||||
"recent_purchase": [order_out(o, with_items=False) for o in recent_po],
|
||||
"recent_sales": [order_out(o, with_items=False) for o in recent_so],
|
||||
}
|
||||
Reference in new issue
Block a user