200 lines
5.4 KiB
Python
200 lines
5.4 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_db, get_current_user
|
|
from app.models.user import User
|
|
from app.crud import item, transaction
|
|
from app.schemas.item import (
|
|
Item, ItemCreate, ItemUpdate,
|
|
Transaction, TransactionCreate,
|
|
InventoryAdjustment
|
|
)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[Item])
|
|
def read_items(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
category_id: int = None,
|
|
supplier_id: int = None,
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve items with optional filtering by category or supplier.
|
|
"""
|
|
if category_id:
|
|
items = item.get_by_category(db, category_id=category_id, skip=skip, limit=limit)
|
|
elif supplier_id:
|
|
items = item.get_by_supplier(db, supplier_id=supplier_id, skip=skip, limit=limit)
|
|
else:
|
|
items = item.get_multi(db, skip=skip, limit=limit)
|
|
return items
|
|
|
|
|
|
@router.get("/low-stock", response_model=List[Item])
|
|
def read_low_stock_items(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve items with stock level at or below reorder level.
|
|
"""
|
|
items = item.get_low_stock_items(db, skip=skip, limit=limit)
|
|
return items
|
|
|
|
|
|
@router.post("/", response_model=Item)
|
|
def create_item(
|
|
*,
|
|
item_in: ItemCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Create new item.
|
|
"""
|
|
db_item = item.get_by_sku(db, sku=item_in.sku)
|
|
if db_item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="An item with this SKU already exists",
|
|
)
|
|
return item.create(db, obj_in=item_in)
|
|
|
|
|
|
@router.get("/{item_id}", response_model=Item)
|
|
def read_item(
|
|
*,
|
|
item_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Get item by ID.
|
|
"""
|
|
db_item = item.get(db, id=item_id)
|
|
if not db_item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
return db_item
|
|
|
|
|
|
@router.put("/{item_id}", response_model=Item)
|
|
def update_item(
|
|
*,
|
|
item_id: int,
|
|
item_in: ItemUpdate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Update an item.
|
|
"""
|
|
db_item = item.get(db, id=item_id)
|
|
if not db_item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
return item.update(db, db_obj=db_item, obj_in=item_in)
|
|
|
|
|
|
@router.delete("/{item_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None)
|
|
def delete_item(
|
|
*,
|
|
item_id: int,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
) -> None:
|
|
"""
|
|
Delete an item.
|
|
"""
|
|
db_item = item.get(db, id=item_id)
|
|
if not db_item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
item.remove(db, id=item_id)
|
|
return None
|
|
|
|
|
|
@router.post("/{item_id}/adjust", response_model=Item)
|
|
def adjust_item_inventory(
|
|
*,
|
|
item_id: int,
|
|
adjustment: InventoryAdjustment,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Adjust the inventory quantity of an item.
|
|
"""
|
|
db_item = item.get(db, id=item_id)
|
|
if not db_item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
|
|
# Ensure the adjustment doesn't result in negative stock
|
|
if db_item.quantity + adjustment.quantity < 0:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Adjustment would result in negative stock",
|
|
)
|
|
|
|
return item.adjust_inventory(db, item_id=item_id, adjustment=adjustment)
|
|
|
|
|
|
@router.get("/{item_id}/transactions", response_model=List[Transaction])
|
|
def read_item_transactions(
|
|
*,
|
|
item_id: int,
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Get all transactions for a specific item.
|
|
"""
|
|
db_item = item.get(db, id=item_id)
|
|
if not db_item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
|
|
return transaction.get_transactions_by_item(db, item_id=item_id, skip=skip, limit=limit)
|
|
|
|
|
|
@router.post("/transactions", response_model=Transaction)
|
|
def create_transaction(
|
|
*,
|
|
transaction_in: TransactionCreate,
|
|
db: Session = Depends(get_db),
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Create a new transaction (purchase, sale, etc.).
|
|
"""
|
|
# Validate that all item IDs exist
|
|
for item_data in transaction_in.items:
|
|
db_item = item.get(db, id=item_data.item_id)
|
|
if not db_item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail=f"Item with ID {item_data.item_id} not found",
|
|
)
|
|
|
|
return transaction.create(db, obj_in=transaction_in) |