feat: add inventory CSV import with warehouse/bin locations
- Add warehouse and bin_location columns to Inventory model - Create inventory_import_service for bulk TSV/CSV import - Add POST /api/v1/admin/inventory/import endpoint - Add Import button and modal to inventory admin page - Support both single-unit rows and explicit QUANTITY column File format: BIN, EAN, PRODUCT (optional), QUANTITY (optional) Products matched by GTIN/EAN, unmatched items reported. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -14,11 +14,13 @@ Vendor selection is passed as a request parameter.
|
||||
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
from fastapi import APIRouter, Depends, File, Form, Query, UploadFile
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_admin_api
|
||||
from app.core.database import get_db
|
||||
from app.services.inventory_import_service import inventory_import_service
|
||||
from app.services.inventory_service import inventory_service
|
||||
from models.database.user import User
|
||||
from models.schema.inventory import (
|
||||
@@ -284,3 +286,95 @@ def delete_inventory(
|
||||
|
||||
db.commit()
|
||||
return InventoryMessageResponse(message="Inventory deleted successfully")
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Import Endpoints
|
||||
# ============================================================================
|
||||
|
||||
|
||||
class UnmatchedGtin(BaseModel):
|
||||
"""GTIN that couldn't be matched to a product."""
|
||||
|
||||
gtin: str
|
||||
quantity: int
|
||||
product_name: str
|
||||
|
||||
|
||||
class InventoryImportResponse(BaseModel):
|
||||
"""Response from inventory import."""
|
||||
|
||||
success: bool
|
||||
total_rows: int
|
||||
entries_created: int
|
||||
entries_updated: int
|
||||
quantity_imported: int
|
||||
unmatched_gtins: list[UnmatchedGtin]
|
||||
errors: list[str]
|
||||
|
||||
|
||||
@router.post("/import", response_model=InventoryImportResponse)
|
||||
async def import_inventory(
|
||||
file: UploadFile = File(..., description="TSV/CSV file with BIN, EAN, PRODUCT, QUANTITY columns"),
|
||||
vendor_id: int = Form(..., description="Vendor ID"),
|
||||
warehouse: str = Form("strassen", description="Warehouse name"),
|
||||
clear_existing: bool = Form(False, description="Clear existing inventory before import"),
|
||||
db: Session = Depends(get_db),
|
||||
current_admin: User = Depends(get_current_admin_api),
|
||||
):
|
||||
"""
|
||||
Import inventory from a TSV/CSV file.
|
||||
|
||||
File format (TSV recommended):
|
||||
- Required columns: BIN, EAN
|
||||
- Optional columns: PRODUCT (for display), QUANTITY (defaults to 1 per row)
|
||||
|
||||
If QUANTITY column is present, each row represents the quantity specified.
|
||||
If QUANTITY is absent, each row counts as 1 unit (rows with same EAN+BIN are summed).
|
||||
|
||||
Products are matched by GTIN/EAN. Unmatched GTINs are reported in the response.
|
||||
"""
|
||||
# Verify vendor exists
|
||||
inventory_service.verify_vendor_exists(db, vendor_id)
|
||||
|
||||
# Read file content
|
||||
content = await file.read()
|
||||
try:
|
||||
content_str = content.decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
content_str = content.decode("latin-1")
|
||||
|
||||
# Detect delimiter
|
||||
first_line = content_str.split("\n")[0] if content_str else ""
|
||||
delimiter = "\t" if "\t" in first_line else ","
|
||||
|
||||
# Run import
|
||||
result = inventory_import_service.import_from_text(
|
||||
db=db,
|
||||
content=content_str,
|
||||
vendor_id=vendor_id,
|
||||
warehouse=warehouse,
|
||||
delimiter=delimiter,
|
||||
clear_existing=clear_existing,
|
||||
)
|
||||
|
||||
if result.success:
|
||||
db.commit()
|
||||
logger.info(
|
||||
f"Admin {current_admin.email} imported inventory: "
|
||||
f"{result.entries_created} created, {result.entries_updated} updated, "
|
||||
f"{result.quantity_imported} total units"
|
||||
)
|
||||
else:
|
||||
db.rollback()
|
||||
logger.error(f"Inventory import failed: {result.errors}")
|
||||
|
||||
return InventoryImportResponse(
|
||||
success=result.success,
|
||||
total_rows=result.total_rows,
|
||||
entries_created=result.entries_created,
|
||||
entries_updated=result.entries_updated,
|
||||
quantity_imported=result.quantity_imported,
|
||||
unmatched_gtins=[UnmatchedGtin(**g) for g in result.unmatched_gtins],
|
||||
errors=result.errors,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user