Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add summary reports for product categories.

  • Loading branch information...
commit eb6ed1f2ef2a5fbf9384ec4625c3c971f64b801e 1 parent 30448f8
Luke Matthew Sutton authored September 12, 2012
8  app/assets/stylesheets/islay_shop/admin/islay_shop.css.sass
@@ -376,7 +376,7 @@ span.indicator
376 376
       border-right-width: 0
377 377
       font-weight: bold
378 378
       padding: 0.45em 1em 0.4em 1em
379  
-      margin: 0.6em 0
  379
+      margin: 0.6em 0 !important
380 380
       min-height: 0 !important
381 381
       cursor: pointer
382 382
       clear: none
@@ -398,10 +398,12 @@ span.indicator
398 398
   p
399 399
     width: 100%
400 400
     float: left
  401
+    margin: 0 !important
401 402
 
402 403
   .total
403 404
     font-size: 4.5em
404 405
     font-family: $light-font-family
  406
+    margin: 0 !important
405 407
     color: $dark-grey
406 408
     text-align: center
407 409
 
@@ -449,3 +451,7 @@ span.indicator
449 451
   .amount
450 452
     color: $mid-grey
451 453
     float: right
  454
+
  455
+#product-listing .ancestors
  456
+  color: $dark-grey
  457
+  font-weight: bold
3  app/controllers/islay_shop/admin/reports_controller.rb
@@ -12,7 +12,8 @@ def orders
12 12
   def products
13 13
     @total_volume = ProductReport.total_volume
14 14
     @products     = ProductReport.product_summary
15  
-    @categories   = ProductReport.category_summary
16 15
     @skus         = ProductReport.sku_summary
  16
+
  17
+    @categories, @all_categories   = ProductReport.category_summary
17 18
   end
18 19
 end
54  app/models/product_report.rb
@@ -7,16 +7,27 @@ def self.total_volume
7 7
     Sku.count_by_sql(TOTAL_VOLUME)
8 8
   end
9 9
 
  10
+  # Summarizes SKUs with calculated volumes and revenues.
  11
+  #
  12
+  # @return Array<Hash>
10 13
   def self.sku_summary
11 14
     Sku.find_by_sql(SKU_SUMMARY)
12 15
   end
13 16
 
  17
+  # Summarizes products with calculated volumes and revenues.
  18
+  #
  19
+  # @return Array<Hash>
14 20
   def self.product_summary
15 21
     Product.find_by_sql(PRODUCT_SUMMARY)
16 22
   end
17 23
 
  24
+  # Creates a summary of categories, with calculated volumes and revenues,
  25
+  # sorted by revenue.
  26
+  #
  27
+  # @return Array<Array<Hash>>
18 28
   def self.category_summary
19  
-    ProductCategory.find_by_sql(CATEGORY_SUMMARY)
  29
+    results = ProductCategory.find_by_sql(CATEGORY_SUMMARY)
  30
+    [results.reject {|r| r.is_parent == 't'}, results]
20 31
   end
21 32
 
22 33
   TOTAL_VOLUME = %{
@@ -26,7 +37,46 @@ def self.category_summary
26 37
   }.freeze
27 38
 
28 39
   CATEGORY_SUMMARY = %{
29  
-    SELECT * FROM product_categories
  40
+    WITH os AS (
  41
+      SELECT
  42
+        ps.product_category_id,
  43
+        COALESCE(SUM(ois.quantity), 0) AS volume,
  44
+        COALESCE(SUM(ois.total), 0) AS revenue
  45
+      FROM orders AS os
  46
+      JOIN order_items AS ois ON ois.order_id = os.id
  47
+      JOIN skus ON skus.id = ois.sku_id
  48
+      JOIN products AS ps ON ps.id = skus.product_id
  49
+      GROUP BY ps.product_category_id, os.status HAVING is_revenue(os.status)
  50
+    )
  51
+
  52
+    SELECT
  53
+      pcs.id, pcs.slug, pcs.name,
  54
+      CASE
  55
+        WHEN ARRAY_LENGTH(child_ids, 1) > 0 THEN true
  56
+        ELSE false
  57
+      END AS is_parent,
  58
+      CASE
  59
+        WHEN ARRAY_LENGTH(child_ids, 1) > 0 THEN
  60
+          (SELECT COALESCE(SUM(revenue), 0) FROM os WHERE product_category_id = ANY(pcs.child_ids))
  61
+        ELSE
  62
+          (SELECT revenue FROM os WHERE product_category_id = pcs.id)
  63
+      END AS revenue,
  64
+      CASE
  65
+        WHEN ARRAY_LENGTH(child_ids, 1) > 0 THEN
  66
+          (SELECT COALESCE(SUM(volume), 0) FROM os WHERE product_category_id = ANY(pcs.child_ids))
  67
+        ELSE
  68
+          (SELECT volume FROM os WHERE product_category_id = pcs.id)
  69
+      END AS volume
  70
+    FROM (
  71
+      SELECT
  72
+        id, name, slug, path,
  73
+        ARRAY(
  74
+          SELECT id FROM product_categories AS cpcs
  75
+          WHERE cpcs.path <@ (pcs.path || text2ltree(pcs.id::text))
  76
+       ) AS child_ids
  77
+      FROM product_categories AS pcs
  78
+    ) AS pcs
  79
+    ORDER BY revenue DESC
30 80
   }.freeze
31 81
 
32 82
   PRODUCT_SUMMARY = %{
22  app/views/islay_shop/admin/reports/products.html.haml
@@ -49,7 +49,7 @@
49 49
             %td= sku.volume
50 50
 
51 51
     %table
52  
-      %caption Categories
  52
+      %caption Product Categories
53 53
 
54 54
       %thead
55 55
         %tr
@@ -61,5 +61,21 @@
61 61
         - @categories.each do |category|
62 62
           %tr
63 63
             %td= link_to(category.name)
64  
-            %td --
65  
-            %td --
  64
+            %td= format_money(category.revenue)
  65
+            %td= category.volume
  66
+
  67
+    %table
  68
+      %caption All Categories
  69
+
  70
+      %thead
  71
+        %tr
  72
+          %th Category
  73
+          %th.sorted Revenue
  74
+          %th Volume
  75
+
  76
+      %tbody
  77
+        - @all_categories.each do |category|
  78
+          %tr
  79
+            %td= link_to(category.name)
  80
+            %td= format_money(category.revenue)
  81
+            %td= category.volume

0 notes on commit eb6ed1f

Please sign in to comment.
Something went wrong with that request. Please try again.