105 lines
3.2 KiB
Python
105 lines
3.2 KiB
Python
from datetime import date
|
|
from typing import Any, List, Optional
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import get_current_user
|
|
from app.crud import item, transaction
|
|
from app.db.utils import get_db
|
|
from app.models.transaction import TransactionType
|
|
from app.models.user import User
|
|
from app.schemas.transaction import Transaction, TransactionCreate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[Transaction])
|
|
def read_transactions(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
item_id: Optional[int] = None,
|
|
start_date: Optional[date] = None,
|
|
end_date: Optional[date] = None,
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Retrieve transactions.
|
|
|
|
- Optional filter by item_id
|
|
- Optional filter by date range
|
|
"""
|
|
if item_id:
|
|
return transaction.get_by_item_id(db, item_id=item_id, skip=skip, limit=limit)
|
|
elif start_date:
|
|
return transaction.get_by_date_range(
|
|
db, start_date=start_date, end_date=end_date, skip=skip, limit=limit
|
|
)
|
|
else:
|
|
return transaction.get_multi(db, skip=skip, limit=limit)
|
|
|
|
|
|
@router.post("/", response_model=Transaction)
|
|
def create_transaction(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
transaction_in: TransactionCreate,
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Create new transaction and update item quantity.
|
|
"""
|
|
# Check if item exists
|
|
db_item = item.get(db, id=transaction_in.item_id)
|
|
if not db_item:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Item not found",
|
|
)
|
|
|
|
# Create the transaction
|
|
db_transaction = transaction.create_with_item_and_user(
|
|
db, obj_in=transaction_in, recorded_by_id=current_user.id
|
|
)
|
|
|
|
# Update item quantity based on transaction type
|
|
if transaction_in.transaction_type == TransactionType.PURCHASE:
|
|
db_item.quantity += transaction_in.quantity
|
|
elif transaction_in.transaction_type == TransactionType.SALE:
|
|
if db_item.quantity < transaction_in.quantity:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Not enough items in stock",
|
|
)
|
|
db_item.quantity -= transaction_in.quantity
|
|
elif transaction_in.transaction_type == TransactionType.ADJUSTMENT:
|
|
db_item.quantity += transaction_in.quantity # Can be negative for decrease
|
|
elif transaction_in.transaction_type == TransactionType.RETURN:
|
|
db_item.quantity += transaction_in.quantity
|
|
|
|
# Save the updated item
|
|
db.add(db_item)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
|
|
return db_transaction
|
|
|
|
|
|
@router.get("/{id}", response_model=Transaction)
|
|
def read_transaction(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
id: int,
|
|
current_user: User = Depends(get_current_user),
|
|
) -> Any:
|
|
"""
|
|
Get transaction by ID.
|
|
"""
|
|
db_transaction = transaction.get(db, id=id)
|
|
if not db_transaction:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="Transaction not found",
|
|
)
|
|
return db_transaction |