Permalink
Browse files

Add summary reports for product categories.

  • Loading branch information...
1 parent 30448f8 commit eb6ed1f2ef2a5fbf9384ec4625c3c971f64b801e @lukesutton lukesutton committed Sep 12, 2012
@@ -376,7 +376,7 @@ span.indicator
border-right-width: 0
font-weight: bold
padding: 0.45em 1em 0.4em 1em
- margin: 0.6em 0
+ margin: 0.6em 0 !important
min-height: 0 !important
cursor: pointer
clear: none
@@ -398,10 +398,12 @@ span.indicator
p
width: 100%
float: left
+ margin: 0 !important
.total
font-size: 4.5em
font-family: $light-font-family
+ margin: 0 !important
color: $dark-grey
text-align: center
@@ -449,3 +451,7 @@ span.indicator
.amount
color: $mid-grey
float: right
+
+#product-listing .ancestors
+ color: $dark-grey
+ font-weight: bold
@@ -12,7 +12,8 @@ def orders
def products
@total_volume = ProductReport.total_volume
@products = ProductReport.product_summary
- @categories = ProductReport.category_summary
@skus = ProductReport.sku_summary
+
+ @categories, @all_categories = ProductReport.category_summary
end
end
@@ -7,16 +7,27 @@ def self.total_volume
Sku.count_by_sql(TOTAL_VOLUME)
end
+ # Summarizes SKUs with calculated volumes and revenues.
+ #
+ # @return Array<Hash>
def self.sku_summary
Sku.find_by_sql(SKU_SUMMARY)
end
+ # Summarizes products with calculated volumes and revenues.
+ #
+ # @return Array<Hash>
def self.product_summary
Product.find_by_sql(PRODUCT_SUMMARY)
end
+ # Creates a summary of categories, with calculated volumes and revenues,
+ # sorted by revenue.
+ #
+ # @return Array<Array<Hash>>
def self.category_summary
- ProductCategory.find_by_sql(CATEGORY_SUMMARY)
+ results = ProductCategory.find_by_sql(CATEGORY_SUMMARY)
+ [results.reject {|r| r.is_parent == 't'}, results]
end
TOTAL_VOLUME = %{
@@ -26,7 +37,46 @@ def self.category_summary
}.freeze
CATEGORY_SUMMARY = %{
- SELECT * FROM product_categories
+ WITH os AS (
+ SELECT
+ ps.product_category_id,
+ COALESCE(SUM(ois.quantity), 0) AS volume,
+ COALESCE(SUM(ois.total), 0) AS revenue
+ FROM orders AS os
+ JOIN order_items AS ois ON ois.order_id = os.id
+ JOIN skus ON skus.id = ois.sku_id
+ JOIN products AS ps ON ps.id = skus.product_id
+ GROUP BY ps.product_category_id, os.status HAVING is_revenue(os.status)
+ )
+
+ SELECT
+ pcs.id, pcs.slug, pcs.name,
+ CASE
+ WHEN ARRAY_LENGTH(child_ids, 1) > 0 THEN true
+ ELSE false
+ END AS is_parent,
+ CASE
+ WHEN ARRAY_LENGTH(child_ids, 1) > 0 THEN
+ (SELECT COALESCE(SUM(revenue), 0) FROM os WHERE product_category_id = ANY(pcs.child_ids))
+ ELSE
+ (SELECT revenue FROM os WHERE product_category_id = pcs.id)
+ END AS revenue,
+ CASE
+ WHEN ARRAY_LENGTH(child_ids, 1) > 0 THEN
+ (SELECT COALESCE(SUM(volume), 0) FROM os WHERE product_category_id = ANY(pcs.child_ids))
+ ELSE
+ (SELECT volume FROM os WHERE product_category_id = pcs.id)
+ END AS volume
+ FROM (
+ SELECT
+ id, name, slug, path,
+ ARRAY(
+ SELECT id FROM product_categories AS cpcs
+ WHERE cpcs.path <@ (pcs.path || text2ltree(pcs.id::text))
+ ) AS child_ids
+ FROM product_categories AS pcs
+ ) AS pcs
+ ORDER BY revenue DESC
}.freeze
PRODUCT_SUMMARY = %{
@@ -49,7 +49,7 @@
%td= sku.volume
%table
- %caption Categories
+ %caption Product Categories
%thead
%tr
@@ -61,5 +61,21 @@
- @categories.each do |category|
%tr
%td= link_to(category.name)
- %td --
- %td --
+ %td= format_money(category.revenue)
+ %td= category.volume
+
+ %table
+ %caption All Categories
+
+ %thead
+ %tr
+ %th Category
+ %th.sorted Revenue
+ %th Volume
+
+ %tbody
+ - @all_categories.each do |category|
+ %tr
+ %td= link_to(category.name)
+ %td= format_money(category.revenue)
+ %td= category.volume

0 comments on commit eb6ed1f

Please sign in to comment.