2026-04-25 20:43:37 +12:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
2026-04-25 22:51:36 +12:00
|
|
|
from app.api.deps import AuthSession, require_client_session
|
2026-04-25 20:43:37 +12:00
|
|
|
from app.db.session import get_db
|
|
|
|
|
from app.models.mix import Mix
|
|
|
|
|
from app.models.product import Product
|
|
|
|
|
from app.schemas.product import ProductCostBreakdown, ProductCreate, ProductRead, ProductUpdate
|
|
|
|
|
from app.services.costing_engine import calculate_product_cost
|
|
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/api/products", tags=["products"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _serialize_product(product: Product) -> dict:
|
|
|
|
|
return {
|
|
|
|
|
"id": product.id,
|
|
|
|
|
"tenant_id": product.tenant_id,
|
|
|
|
|
"client_name": product.client_name,
|
|
|
|
|
"item_id": product.item_id,
|
|
|
|
|
"name": product.name,
|
|
|
|
|
"mix_id": product.mix_id,
|
|
|
|
|
"mix_name": product.mix.name if product.mix else "",
|
|
|
|
|
"sale_type": product.sale_type,
|
|
|
|
|
"own_bag": product.own_bag,
|
|
|
|
|
"unit_of_measure": product.unit_of_measure,
|
|
|
|
|
"items_per_pallet": product.items_per_pallet,
|
|
|
|
|
"bagging_process": product.bagging_process,
|
|
|
|
|
"distributor_margin": product.distributor_margin,
|
|
|
|
|
"wholesale_margin": product.wholesale_margin,
|
|
|
|
|
"notes": product.notes,
|
|
|
|
|
"created_at": product.created_at,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("", response_model=list[ProductRead])
|
2026-04-25 22:51:36 +12:00
|
|
|
def list_products(session: AuthSession = Depends(require_client_session), db: Session = Depends(get_db)):
|
|
|
|
|
products = db.scalars(select(Product).where(Product.tenant_id == session.tenant_id).order_by(Product.name)).all()
|
2026-04-25 20:43:37 +12:00
|
|
|
return [_serialize_product(product) for product in products]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("", response_model=ProductRead, status_code=status.HTTP_201_CREATED)
|
2026-04-25 22:51:36 +12:00
|
|
|
def create_product(payload: ProductCreate, session: AuthSession = Depends(require_client_session), db: Session = Depends(get_db)):
|
|
|
|
|
if db.scalar(select(Mix.id).where(Mix.id == payload.mix_id, Mix.tenant_id == session.tenant_id)) is None:
|
2026-04-25 20:43:37 +12:00
|
|
|
raise HTTPException(status_code=404, detail="Mix not found")
|
2026-04-25 22:51:36 +12:00
|
|
|
product = Product(tenant_id=session.tenant_id, **payload.model_dump())
|
2026-04-25 20:43:37 +12:00
|
|
|
db.add(product)
|
|
|
|
|
db.commit()
|
|
|
|
|
db.refresh(product)
|
|
|
|
|
return _serialize_product(product)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/{product_id}", response_model=ProductRead)
|
2026-04-25 22:51:36 +12:00
|
|
|
def get_product(product_id: int, session: AuthSession = Depends(require_client_session), db: Session = Depends(get_db)):
|
|
|
|
|
product = db.scalar(select(Product).where(Product.id == product_id, Product.tenant_id == session.tenant_id))
|
2026-04-25 20:43:37 +12:00
|
|
|
if product is None:
|
|
|
|
|
raise HTTPException(status_code=404, detail="Product not found")
|
|
|
|
|
return _serialize_product(product)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.patch("/{product_id}", response_model=ProductRead)
|
2026-04-25 22:51:36 +12:00
|
|
|
def update_product(product_id: int, payload: ProductUpdate, session: AuthSession = Depends(require_client_session), db: Session = Depends(get_db)):
|
|
|
|
|
product = db.scalar(select(Product).where(Product.id == product_id, Product.tenant_id == session.tenant_id))
|
2026-04-25 20:43:37 +12:00
|
|
|
if product is None:
|
|
|
|
|
raise HTTPException(status_code=404, detail="Product not found")
|
2026-04-25 22:51:36 +12:00
|
|
|
if payload.mix_id is not None and db.scalar(select(Mix.id).where(Mix.id == payload.mix_id, Mix.tenant_id == session.tenant_id)) is None:
|
2026-04-25 20:43:37 +12:00
|
|
|
raise HTTPException(status_code=404, detail="Mix not found")
|
|
|
|
|
for field, value in payload.model_dump(exclude_unset=True).items():
|
|
|
|
|
setattr(product, field, value)
|
|
|
|
|
db.commit()
|
|
|
|
|
db.refresh(product)
|
|
|
|
|
return _serialize_product(product)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/{product_id}/cost-breakdown", response_model=ProductCostBreakdown)
|
2026-04-25 22:51:36 +12:00
|
|
|
def get_product_cost_breakdown(product_id: int, session: AuthSession = Depends(require_client_session), db: Session = Depends(get_db)):
|
|
|
|
|
if db.scalar(select(Product.id).where(Product.id == product_id, Product.tenant_id == session.tenant_id)) is None:
|
|
|
|
|
raise HTTPException(status_code=404, detail="Product not found")
|
2026-04-25 20:43:37 +12:00
|
|
|
try:
|
|
|
|
|
return calculate_product_cost(db, product_id)
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
raise HTTPException(status_code=404, detail=str(exc)) from exc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/{product_id}/price-output", response_model=ProductCostBreakdown)
|
2026-04-25 22:51:36 +12:00
|
|
|
def get_product_price_output(product_id: int, session: AuthSession = Depends(require_client_session), db: Session = Depends(get_db)):
|
|
|
|
|
if db.scalar(select(Product.id).where(Product.id == product_id, Product.tenant_id == session.tenant_id)) is None:
|
|
|
|
|
raise HTTPException(status_code=404, detail="Product not found")
|
2026-04-25 20:43:37 +12:00
|
|
|
try:
|
|
|
|
|
return calculate_product_cost(db, product_id)
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
raise HTTPException(status_code=404, detail=str(exc)) from exc
|