Fix new Frontend 15
This commit is contained in:
@@ -226,3 +226,53 @@ class SalesService:
|
|||||||
await db.rollback()
|
await db.rollback()
|
||||||
logger.error("Failed to delete sales record", error=str(e))
|
logger.error("Failed to delete sales record", error=str(e))
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
async def get_products_list(tenant_id: str, db: AsyncSession) -> List[Dict[str, Any]]:
|
||||||
|
"""Get list of all products with sales data for tenant"""
|
||||||
|
try:
|
||||||
|
# Query to get unique products with aggregated sales data
|
||||||
|
query = (
|
||||||
|
select(
|
||||||
|
SalesData.product_name,
|
||||||
|
func.count(SalesData.id).label('total_sales'),
|
||||||
|
func.sum(SalesData.quantity_sold).label('total_quantity'),
|
||||||
|
func.sum(SalesData.revenue).label('total_revenue'),
|
||||||
|
func.min(SalesData.date).label('first_sale_date'),
|
||||||
|
func.max(SalesData.date).label('last_sale_date'),
|
||||||
|
func.avg(SalesData.quantity_sold).label('avg_quantity'),
|
||||||
|
func.avg(SalesData.revenue).label('avg_revenue')
|
||||||
|
)
|
||||||
|
.where(SalesData.tenant_id == tenant_id)
|
||||||
|
.group_by(SalesData.product_name)
|
||||||
|
.order_by(desc(func.sum(SalesData.revenue)))
|
||||||
|
)
|
||||||
|
|
||||||
|
result = await db.execute(query)
|
||||||
|
products_data = result.all()
|
||||||
|
|
||||||
|
# Format the response
|
||||||
|
products = []
|
||||||
|
for row in products_data:
|
||||||
|
products.append({
|
||||||
|
'product_name': row.product_name,
|
||||||
|
'total_sales': row.total_sales,
|
||||||
|
'total_quantity': int(row.total_quantity) if row.total_quantity else 0,
|
||||||
|
'total_revenue': float(row.total_revenue) if row.total_revenue else 0.0,
|
||||||
|
'first_sale_date': row.first_sale_date.isoformat() if row.first_sale_date else None,
|
||||||
|
'last_sale_date': row.last_sale_date.isoformat() if row.last_sale_date else None,
|
||||||
|
'avg_quantity': float(row.avg_quantity) if row.avg_quantity else 0.0,
|
||||||
|
'avg_revenue': float(row.avg_revenue) if row.avg_revenue else 0.0
|
||||||
|
})
|
||||||
|
|
||||||
|
logger.debug("Products list retrieved successfully",
|
||||||
|
tenant_id=tenant_id,
|
||||||
|
product_count=len(products))
|
||||||
|
|
||||||
|
return products
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("Failed to get products list from database",
|
||||||
|
error=str(e),
|
||||||
|
tenant_id=tenant_id)
|
||||||
|
raise
|
||||||
Reference in New Issue
Block a user