172 lines
5.1 KiB
Python
172 lines
5.1 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.deps import get_current_active_user, get_current_superuser
|
|
from app.db.session import get_db
|
|
from app.models.user import User
|
|
from app.schemas.inventory import (
|
|
Inventory,
|
|
InventoryCreate,
|
|
InventoryUpdate,
|
|
InventoryWithDetails,
|
|
)
|
|
from app.services import inventory as inventory_service
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[Inventory])
|
|
def read_inventory(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
warehouse_id: int = Query(None, description="Filter by warehouse ID"),
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve inventory items.
|
|
"""
|
|
inventory = inventory_service.get_multi(
|
|
db, skip=skip, limit=limit, warehouse_id=warehouse_id
|
|
)
|
|
return inventory
|
|
|
|
|
|
@router.get("/with-details", response_model=List[InventoryWithDetails])
|
|
def read_inventory_with_details(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
warehouse_id: int = Query(None, description="Filter by warehouse ID"),
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve inventory items with product and warehouse details.
|
|
"""
|
|
results = inventory_service.get_multi_with_details(
|
|
db, skip=skip, limit=limit, warehouse_id=warehouse_id
|
|
)
|
|
|
|
inventory_with_details = []
|
|
for inventory_item, product_name, warehouse_name in results:
|
|
inventory_dict = {**inventory_item.__dict__}
|
|
inventory_dict["product_name"] = product_name
|
|
inventory_dict["warehouse_name"] = warehouse_name
|
|
inventory_with_details.append(inventory_dict)
|
|
|
|
return inventory_with_details
|
|
|
|
|
|
@router.get("/low-stock", response_model=List[Inventory])
|
|
def read_low_stock_inventory(
|
|
db: Session = Depends(get_db),
|
|
limit: int = 100,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve inventory items that are below minimum stock level.
|
|
"""
|
|
inventory = inventory_service.get_low_stock_items(db, limit=limit)
|
|
return inventory
|
|
|
|
|
|
@router.post("/", response_model=Inventory)
|
|
def create_inventory(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
inventory_in: InventoryCreate,
|
|
current_user: User = Depends(get_current_superuser),
|
|
) -> Any:
|
|
"""
|
|
Create new inventory item or update quantity if it already exists.
|
|
Requires superuser access.
|
|
"""
|
|
inventory = inventory_service.create(db, obj_in=inventory_in)
|
|
return inventory
|
|
|
|
|
|
@router.get("/{inventory_id}", response_model=Inventory)
|
|
def read_inventory_item(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
inventory_id: int,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get inventory item by ID.
|
|
"""
|
|
inventory = inventory_service.get(db, inventory_id=inventory_id)
|
|
if not inventory:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Inventory item not found",
|
|
)
|
|
return inventory
|
|
|
|
|
|
@router.get("/{inventory_id}/details", response_model=InventoryWithDetails)
|
|
def read_inventory_item_with_details(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
inventory_id: int,
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get inventory item by ID with product and warehouse details.
|
|
"""
|
|
result = inventory_service.get_with_details(db, inventory_id=inventory_id)
|
|
if not result:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Inventory item not found",
|
|
)
|
|
|
|
inventory, product_name, warehouse_name = result
|
|
inventory_dict = {**inventory.__dict__}
|
|
inventory_dict["product_name"] = product_name
|
|
inventory_dict["warehouse_name"] = warehouse_name
|
|
|
|
return inventory_dict
|
|
|
|
|
|
@router.put("/{inventory_id}", response_model=Inventory)
|
|
def update_inventory(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
inventory_id: int,
|
|
inventory_in: InventoryUpdate,
|
|
current_user: User = Depends(get_current_superuser),
|
|
) -> Any:
|
|
"""
|
|
Update an inventory item. Requires superuser access.
|
|
"""
|
|
inventory = inventory_service.get(db, inventory_id=inventory_id)
|
|
if not inventory:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Inventory item not found",
|
|
)
|
|
inventory = inventory_service.update(db, db_obj=inventory, obj_in=inventory_in)
|
|
return inventory
|
|
|
|
|
|
@router.delete("/{inventory_id}", response_model=Inventory)
|
|
def delete_inventory(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
inventory_id: int,
|
|
current_user: User = Depends(get_current_superuser),
|
|
) -> Any:
|
|
"""
|
|
Delete an inventory item. Requires superuser access.
|
|
"""
|
|
inventory = inventory_service.get(db, inventory_id=inventory_id)
|
|
if not inventory:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Inventory item not found",
|
|
)
|
|
inventory = inventory_service.remove(db, inventory_id=inventory_id)
|
|
return inventory |