Mecanismo sort para tablas
This commit is contained in:
@@ -278,6 +278,8 @@ class ItemService:
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
filters: Optional[dict] = None,
|
||||
sort_by: Optional[str] = None,
|
||||
sort_order: Optional[str] = "asc",
|
||||
) -> Tuple[List[LineItem], int]:
|
||||
"""Get all items for a tenant/company with pagination and optional filters"""
|
||||
query = (
|
||||
@@ -316,6 +318,25 @@ class ItemService:
|
||||
LineItem.guide_number.ilike(search_term),
|
||||
)
|
||||
)
|
||||
if filters.get("invoice_number"):
|
||||
# Join with InvoiceHeader to search by invoice_number
|
||||
query = query.join(InvoiceHeader).filter(
|
||||
InvoiceHeader.invoice_number.ilike(f"%{filters['invoice_number']}%")
|
||||
)
|
||||
|
||||
# Apply sorting
|
||||
if sort_by:
|
||||
# Map sort_by to actual model column if possible
|
||||
# Note: Some columns might require joins if they are in related models
|
||||
column = getattr(LineItem, sort_by, None)
|
||||
if column:
|
||||
if sort_order == "desc":
|
||||
query = query.order_by(column.desc())
|
||||
else:
|
||||
query = query.order_by(column.asc())
|
||||
else:
|
||||
# Default sorting
|
||||
query = query.order_by(LineItem.line_number.asc())
|
||||
|
||||
total = query.count()
|
||||
items = query.offset(skip).limit(limit).all()
|
||||
@@ -332,6 +353,8 @@ class ItemService:
|
||||
company_id: int,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
sort_by: Optional[str] = None,
|
||||
sort_order: Optional[str] = "asc",
|
||||
) -> Tuple[List[LineItem], int]:
|
||||
"""Get all items for a specific invoice"""
|
||||
query = (
|
||||
@@ -351,6 +374,18 @@ class ItemService:
|
||||
)
|
||||
)
|
||||
|
||||
# Apply sorting
|
||||
if sort_by:
|
||||
column = getattr(LineItem, sort_by, None)
|
||||
if column:
|
||||
if sort_order == "desc":
|
||||
query = query.order_by(column.desc())
|
||||
else:
|
||||
query = query.order_by(column.asc())
|
||||
else:
|
||||
# Default sorting
|
||||
query = query.order_by(LineItem.line_number.asc())
|
||||
|
||||
total = query.count()
|
||||
items = query.offset(skip).limit(limit).all()
|
||||
for item in items:
|
||||
@@ -494,8 +529,13 @@ class ItemService:
|
||||
)
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
logger.error(f"Unexpected error creating LineItem: {e}")
|
||||
raise HTTPException(status_code=500, detail="Error creating LineItem")
|
||||
import traceback
|
||||
error_msg = f"Unexpected error creating LineItem: {str(e)}"
|
||||
logger.error(f"{error_msg}\n{traceback.format_exc()}")
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail=f"Error interno al crear la partida: {type(e).__name__}: {str(e)}"
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def update(
|
||||
|
||||
Reference in New Issue
Block a user