Improve the frontend modals

This commit is contained in:
Urtzi Alfaro
2025-10-27 16:33:26 +01:00
parent 61376b7a9f
commit 858d985c92
143 changed files with 9289 additions and 2306 deletions

View File

@@ -156,7 +156,8 @@ class BaseRepository(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):
db: AsyncSession,
*,
obj_in: CreateSchemaType,
created_by: Optional[UUID] = None
created_by: Optional[UUID] = None,
tenant_id: Optional[UUID] = None
) -> ModelType:
"""Create a new record"""
try:
@@ -166,6 +167,10 @@ class BaseRepository(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):
else:
obj_data = obj_in
# Add tenant_id if the model supports it and it's provided
if tenant_id and hasattr(self.model, 'tenant_id'):
obj_data['tenant_id'] = tenant_id
# Add created_by if the model supports it
if created_by and hasattr(self.model, 'created_by'):
obj_data['created_by'] = created_by
@@ -281,4 +286,4 @@ class BaseRepository(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):
model=self.model.__name__,
id=str(id),
error=str(e))
raise
raise

View File

@@ -402,6 +402,87 @@ class OrderRepository(BaseRepository[CustomerOrder, OrderCreate, OrderUpdate]):
)
)
# Calculate repeat customers rate
# Count customers who have made more than one order
repeat_customers_query = await db.execute(
select(func.count()).select_from(
select(CustomerOrder.customer_id)
.where(CustomerOrder.tenant_id == tenant_id)
.group_by(CustomerOrder.customer_id)
.having(func.count(CustomerOrder.id) > 1)
.subquery()
)
)
total_customers_query = await db.execute(
select(func.count(func.distinct(CustomerOrder.customer_id))).where(
CustomerOrder.tenant_id == tenant_id
)
)
repeat_customers_count = repeat_customers_query.scalar() or 0
total_customers_count = total_customers_query.scalar() or 0
repeat_customers_rate = Decimal("0.0")
if total_customers_count > 0:
repeat_customers_rate = Decimal(str(repeat_customers_count)) / Decimal(str(total_customers_count))
repeat_customers_rate = repeat_customers_rate * Decimal("100.0") # Convert to percentage
# Calculate order fulfillment rate
total_orders_query = await db.execute(
select(func.count()).where(
and_(
CustomerOrder.tenant_id == tenant_id,
CustomerOrder.status != "cancelled"
)
)
)
fulfilled_orders_query = await db.execute(
select(func.count()).where(
and_(
CustomerOrder.tenant_id == tenant_id,
CustomerOrder.status.in_(["delivered", "completed"])
)
)
)
total_orders_count = total_orders_query.scalar() or 0
fulfilled_orders_count = fulfilled_orders_query.scalar() or 0
fulfillment_rate = Decimal("0.0")
if total_orders_count > 0:
fulfillment_rate = Decimal(str(fulfilled_orders_count)) / Decimal(str(total_orders_count))
fulfillment_rate = fulfillment_rate * Decimal("100.0") # Convert to percentage
# Calculate on-time delivery rate
on_time_delivered_query = await db.execute(
select(func.count()).where(
and_(
CustomerOrder.tenant_id == tenant_id,
CustomerOrder.status == "delivered",
CustomerOrder.actual_delivery_date <= CustomerOrder.requested_delivery_date
)
)
)
total_delivered_query = await db.execute(
select(func.count()).where(
and_(
CustomerOrder.tenant_id == tenant_id,
CustomerOrder.status == "delivered"
)
)
)
on_time_delivered_count = on_time_delivered_query.scalar() or 0
total_delivered_count = total_delivered_query.scalar() or 0
on_time_delivery_rate = Decimal("0.0")
if total_delivered_count > 0:
on_time_delivery_rate = Decimal(str(on_time_delivered_count)) / Decimal(str(total_delivered_count))
on_time_delivery_rate = on_time_delivery_rate * Decimal("100.0") # Convert to percentage
return {
"total_orders_today": orders_today.scalar(),
"total_orders_this_week": orders_week.scalar(),
@@ -410,7 +491,16 @@ class OrderRepository(BaseRepository[CustomerOrder, OrderCreate, OrderUpdate]):
"revenue_this_week": revenue_week.scalar(),
"revenue_this_month": revenue_month.scalar(),
"status_breakdown": status_breakdown,
"average_order_value": avg_order_value.scalar()
"average_order_value": avg_order_value.scalar(),
"repeat_customers_rate": repeat_customers_rate,
"fulfillment_rate": fulfillment_rate,
"on_time_delivery_rate": on_time_delivery_rate,
"repeat_customers_count": repeat_customers_count,
"total_customers_count": total_customers_count,
"total_orders_count": total_orders_count,
"fulfilled_orders_count": fulfilled_orders_count,
"on_time_delivered_count": on_time_delivered_count,
"total_delivered_count": total_delivered_count
}
except Exception as e:
logger.error("Error getting dashboard metrics", error=str(e))