+ ) || {}
+
+ const rows: JSX.Element[] = []
+ let globalIndex = 0
+
+ // Sort categories alphabetically
+ Object.keys(groupedProducts)
+ .sort()
+ .forEach(categoryName => {
+ const categoryProducts = groupedProducts[categoryName]
+
+ // Category header row
+ rows.push(
+
+ |
+ {categoryName.toUpperCase()}
+ |
+ |
+ |
+ |
+ |
+
+ )
+
+ // Product rows for this category
+ categoryProducts.forEach((item, index) => {
+ globalIndex++
+ rows.push(
+
+ |
+ {item.product_name}
+ |
+
+ {item.quantity_sold}
+ |
+
+ {item.order_count ?? 0}
+ |
+
+ {formatCurrency(item.revenue)}
+ |
+
+ {formatCurrency(item.average_price)}
+ |
+
+ )
+ })
+
+ // Category subtotal row
+ const categoryTotalQty = categoryProducts.reduce(
+ (sum, item) => sum + (item.quantity_sold || 0),
+ 0
+ )
+ const categoryTotalOrders = categoryProducts.reduce(
+ (sum, item) => sum + (item.order_count || 0),
+ 0
+ )
+ const categoryTotalRevenue = categoryProducts.reduce((sum, item) => sum + (item.revenue || 0), 0)
+
+ rows.push(
+
+ |
+ Subtotal {categoryName}
+ |
+
+ {categoryTotalQty}
+ |
+
+ {categoryTotalOrders}
+ |
+
+ {formatCurrency(categoryTotalRevenue)}
+ |
+ |
+
+ )
+ })
+
+ return rows
+ })()}
+