diff --git a/src/modules/orders/providers/orders.service.ts b/src/modules/orders/providers/orders.service.ts index 026fcfb..62b4489 100644 --- a/src/modules/orders/providers/orders.service.ts +++ b/src/modules/orders/providers/orders.service.ts @@ -511,12 +511,46 @@ export class OrdersService { async getFoodSalesPieChart(restId: string) { return this.em.transactional(async em => { - // Calculate last month date range + // Use last 30 days instead of just last month to be more inclusive const now = new Date(); + const endDate = new Date(now); + endDate.setHours(23, 59, 59, 999); + const startDate = new Date(now); + startDate.setDate(startDate.getDate() - 30); + startDate.setHours(0, 0, 0, 0); + + // Calculate actual last month for the period display const firstDayOfLastMonth = new Date(now.getFullYear(), now.getMonth() - 1, 1); + firstDayOfLastMonth.setHours(0, 0, 0, 0); const lastDayOfLastMonth = new Date(now.getFullYear(), now.getMonth(), 0, 23, 59, 59, 999); - // Query order items from orders in the last month + this.logger.debug( + `Food sales pie chart query params: restId=${restId}, startDate=${startDate.toISOString()}, endDate=${endDate.toISOString()}`, + ); + + // Diagnostic query to check what orders exist + const diagnosticResult = await em.execute( + ` + SELECT + o.id, + o.status, + o.created_at, + COUNT(oi.id) as item_count + FROM orders o + LEFT JOIN order_items oi ON oi.order_id = o.id + WHERE o.restaurant_id = ? + GROUP BY o.id, o.status, o.created_at + ORDER BY o.created_at DESC + LIMIT 10 + `, + [restId], + ); + this.logger.debug(`Diagnostic: Found ${diagnosticResult.length} recent orders for restaurant ${restId}`); + if (diagnosticResult.length > 0) { + this.logger.debug(`Sample order: status=${diagnosticResult[0].status}, created_at=${diagnosticResult[0].created_at}`); + } + + // Query order items from orders in the date range // Only include completed orders (excluding canceled and pending) const result = await em.execute( ` @@ -535,9 +569,11 @@ export class OrdersService { GROUP BY f.id, f.title ORDER BY total_revenue DESC `, - [restId, firstDayOfLastMonth, lastDayOfLastMonth], + [restId, startDate, endDate], ); + this.logger.debug(`Food sales pie chart query returned ${result.length} rows`); + // Calculate total revenue for percentage calculation const totalRevenue = result.reduce((sum: number, item: any) => { return sum + Number(item.total_revenue || 0);