Fix UI for inventory page

This commit is contained in:
Urtzi Alfaro
2025-09-15 15:31:27 +02:00
parent 36cfc88f93
commit 65a53c6d16
10 changed files with 953 additions and 378 deletions

View File

@@ -311,25 +311,12 @@ class StockRepository(BaseRepository[Stock, StockCreate, StockUpdate]):
async def get_stock_summary_by_tenant(self, tenant_id: UUID) -> Dict[str, Any]:
"""Get stock summary for tenant dashboard"""
try:
# Total stock value and counts
result = await self.session.execute(
# Basic stock summary
basic_result = await self.session.execute(
select(
func.count(Stock.id).label('total_stock_items'),
func.coalesce(func.sum(Stock.total_cost), 0).label('total_stock_value'),
func.count(func.distinct(Stock.ingredient_id)).label('unique_ingredients'),
func.sum(
func.case(
(Stock.expiration_date < datetime.now(), 1),
else_=0
)
).label('expired_items'),
func.sum(
func.case(
(and_(Stock.expiration_date.isnot(None),
Stock.expiration_date <= datetime.now() + timedelta(days=7)), 1),
else_=0
)
).label('expiring_soon_items')
func.count(func.distinct(Stock.ingredient_id)).label('unique_ingredients')
).where(
and_(
Stock.tenant_id == tenant_id,
@@ -337,17 +324,41 @@ class StockRepository(BaseRepository[Stock, StockCreate, StockUpdate]):
)
)
)
summary = result.first()
basic_summary = basic_result.first()
# Count expired items
expired_result = await self.session.execute(
select(func.count(Stock.id)).where(
and_(
Stock.tenant_id == tenant_id,
Stock.is_available == True,
Stock.expiration_date < datetime.now()
)
)
)
expired_count = expired_result.scalar() or 0
# Count expiring soon items
expiring_result = await self.session.execute(
select(func.count(Stock.id)).where(
and_(
Stock.tenant_id == tenant_id,
Stock.is_available == True,
Stock.expiration_date.isnot(None),
Stock.expiration_date <= datetime.now() + timedelta(days=7)
)
)
)
expiring_count = expiring_result.scalar() or 0
return {
'total_stock_items': summary.total_stock_items or 0,
'total_stock_value': float(summary.total_stock_value) if summary.total_stock_value else 0.0,
'unique_ingredients': summary.unique_ingredients or 0,
'expired_items': summary.expired_items or 0,
'expiring_soon_items': summary.expiring_soon_items or 0
'total_stock_items': basic_summary.total_stock_items or 0,
'total_stock_value': float(basic_summary.total_stock_value) if basic_summary.total_stock_value else 0.0,
'unique_ingredients': basic_summary.unique_ingredients or 0,
'expired_items': expired_count,
'expiring_soon_items': expiring_count
}
except Exception as e:
logger.error("Failed to get stock summary", error=str(e), tenant_id=tenant_id)
raise