This commit is contained in:
2026-03-15 22:30:41 +08:00
parent a86fb9803a
commit 4e9f66ccd6
2 changed files with 81 additions and 12 deletions
+13 -4
View File
@@ -22,6 +22,15 @@ from .utils import generate_order_no
router = APIRouter(prefix="/stock-movements", tags=["库存变动"])
INBOUND_TYPES = {
"in", "purchase_in", "return_from_production", "outsource_return", "finish_in"
}
OUTBOUND_TYPES = {
"out", "issue_to_production", "outsource_send", "shipment_out", "scrap_out"
}
ADJUST_TYPES = {"adjust"}
SUPPORTED_MOVEMENT_TYPES = INBOUND_TYPES | OUTBOUND_TYPES | ADJUST_TYPES
@router.post("", response_model=StockMovementResponse, status_code=201)
async def create_stock_movement(
@@ -29,7 +38,7 @@ async def create_stock_movement(
db_session: AsyncSession = Depends(get_db_session),
current_user: User = Depends(get_current_active_user)
):
if movement_data.movement_type not in ["in", "out", "adjust"]:
if movement_data.movement_type not in SUPPORTED_MOVEMENT_TYPES:
raise HTTPException(status_code=400, detail="无效的变动类型")
if movement_data.quantity <= 0:
raise HTTPException(status_code=400, detail="数量必须大于0")
@@ -70,7 +79,7 @@ async def create_stock_movement(
inventory = result.scalar_one_or_none()
if not inventory:
if movement_data.movement_type == "out":
if movement_data.movement_type in OUTBOUND_TYPES:
raise HTTPException(status_code=400, detail="库存不足")
inventory = Inventory(
product_id=resolved_product_id,
@@ -82,9 +91,9 @@ async def create_stock_movement(
before_qty = inventory.quantity
if movement_data.movement_type == "in":
if movement_data.movement_type in INBOUND_TYPES:
inventory.quantity += movement_data.quantity
elif movement_data.movement_type == "out":
elif movement_data.movement_type in OUTBOUND_TYPES:
if inventory.quantity < movement_data.quantity:
raise HTTPException(status_code=400, detail="库存不足")
inventory.quantity -= movement_data.quantity