from typing import Any, List from fastapi import APIRouter, Depends, HTTPException from sqlalchemy.orm import Session from app import crud, models, schemas from app.api import deps from app.models.inventory_transaction import TransactionType router = APIRouter() @router.post("/", response_model=schemas.InventoryTransaction) def create_inventory_transaction( *, db: Session = Depends(deps.get_db), transaction_in: schemas.InventoryTransactionCreate, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Create new inventory transaction. """ # Verify product exists and belongs to the current user product = crud.product.get(db, id=transaction_in.product_id) if not product: raise HTTPException(status_code=404, detail="Product not found") if product.owner_id != current_user.id and not current_user.is_superuser: raise HTTPException(status_code=403, detail="Not enough permissions") try: transaction = crud.inventory_transaction.create_with_product_update( db, obj_in=transaction_in ) except ValueError as e: raise HTTPException(status_code=400, detail=str(e)) return transaction @router.get("/", response_model=List[schemas.InventoryTransaction]) def read_inventory_transactions( db: Session = Depends(deps.get_db), skip: int = 0, limit: int = 100, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Retrieve inventory transactions. """ # Get all products belonging to the user products = crud.product.get_multi_by_owner(db, owner_id=current_user.id) product_ids = [p.id for p in products] # Get transactions for those products transactions = db.query(models.InventoryTransaction).filter( models.InventoryTransaction.product_id.in_(product_ids) ).order_by( models.InventoryTransaction.transaction_date.desc() ).offset(skip).limit(limit).all() return transactions @router.get("/by-product/{product_id}", response_model=List[schemas.InventoryTransaction]) def read_inventory_transactions_by_product( *, db: Session = Depends(deps.get_db), product_id: str, skip: int = 0, limit: int = 100, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Retrieve inventory transactions by product. """ # Verify product exists and belongs to the current user product = crud.product.get(db, id=product_id) if not product: raise HTTPException(status_code=404, detail="Product not found") if product.owner_id != current_user.id and not current_user.is_superuser: raise HTTPException(status_code=403, detail="Not enough permissions") transactions = crud.inventory_transaction.get_by_product( db, product_id=product_id, skip=skip, limit=limit ) return transactions @router.get("/by-type/{transaction_type}", response_model=List[schemas.InventoryTransaction]) def read_inventory_transactions_by_type( *, db: Session = Depends(deps.get_db), transaction_type: TransactionType, skip: int = 0, limit: int = 100, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Retrieve inventory transactions by transaction type. """ # Get all products belonging to the user products = crud.product.get_multi_by_owner(db, owner_id=current_user.id) product_ids = [p.id for p in products] # Get transactions for those products with the specified type transactions = db.query(models.InventoryTransaction).filter( models.InventoryTransaction.product_id.in_(product_ids), models.InventoryTransaction.transaction_type == transaction_type ).order_by( models.InventoryTransaction.transaction_date.desc() ).offset(skip).limit(limit).all() return transactions @router.get("/{id}", response_model=schemas.InventoryTransaction) def read_inventory_transaction( *, db: Session = Depends(deps.get_db), id: str, current_user: models.User = Depends(deps.get_current_active_user), ) -> Any: """ Get inventory transaction by ID. """ transaction = crud.inventory_transaction.get(db, id=id) if not transaction: raise HTTPException(status_code=404, detail="Inventory transaction not found") # Verify product belongs to the current user product = crud.product.get(db, id=transaction.product_id) if product.owner_id != current_user.id and not current_user.is_superuser: raise HTTPException(status_code=403, detail="Not enough permissions") return transaction