From 9343780e790d0d2f4e443dc28828b35c5c2b2094 Mon Sep 17 00:00:00 2001 From: Rasmus Styrk Date: Thu, 4 Aug 2022 13:37:08 +0200 Subject: [PATCH 1/4] Added sales total by products --- .../spree/admin/reports_controller.rb | 35 ++++++++++++ .../reports/sales_total_by_product.html.erb | 56 +++++++++++++++++++ config/locales/en.yml | 3 + config/routes.rb | 2 + 4 files changed, 96 insertions(+) create mode 100644 app/views/spree/admin/reports/sales_total_by_product.html.erb diff --git a/app/controllers/spree/admin/reports_controller.rb b/app/controllers/spree/admin/reports_controller.rb index 650a4bc..f2286ab 100644 --- a/app/controllers/spree/admin/reports_controller.rb +++ b/app/controllers/spree/admin/reports_controller.rb @@ -25,12 +25,47 @@ def add_available_report!(report_key, report_description_key = nil) def initialize super ReportsController.add_available_report!(:sales_total) + ReportsController.add_available_report!(:sales_total_by_product) end def index @reports = ReportsController.available_reports end + def sales_total_by_product + params[:q] = search_params + + @search = Order.complete.not_canceled.ransack(params[:q]) + @orders = @search.result + + @totals = {} + @orders.each do |order| + unless @totals[order.currency] + @totals[order.currency] = { + data: {}, + quantity_total: 0, + sales_total: ::Money.new(0, order.currency) + } + end + + order.line_items.each do |line_item| + unless @totals[order.currency][:data][line_item.variant_id] + @totals[order.currency][:data][line_item.variant_id] = { + quantity: 0, + item_price: line_item.display_price, + sales_total: ::Money.new(0, order.currency) + } + end + + @totals[order.currency][:data][line_item.variant_id][:quantity] += line_item.quantity + @totals[order.currency][:data][line_item.variant_id][:sales_total] += line_item.display_amount.money + + @totals[order.currency][:quantity_total] += line_item.quantity + @totals[order.currency][:sales_total] += line_item.display_amount.money + end + end + end + def sales_total params[:q] = search_params diff --git a/app/views/spree/admin/reports/sales_total_by_product.html.erb b/app/views/spree/admin/reports/sales_total_by_product.html.erb new file mode 100644 index 0000000..62f52ed --- /dev/null +++ b/app/views/spree/admin/reports/sales_total_by_product.html.erb @@ -0,0 +1,56 @@ +<% admin_breadcrumb(link_to t('spree.reports'), spree.admin_reports_path) %> +<% admin_breadcrumb(t('spree.sales_total_by_product')) %> + +<% content_for :page_actions do %> +<% end %> + + +<% content_for :table_filter_title do %> + <%= t('spree.date_range') %> +<% end %> + +<% content_for :table_filter do %> + <%= render partial: 'spree/admin/shared/report_order_criteria', locals: { action: :sales_total_by_product } %> +<% end %> + + + + + + + + + + + + + <% @totals.each do |currency, report| %> + <% report[:data].each do |variant_id, row| %> + + + + + + + + <% end %> + + + + + + + + <% end %> + +
<%= t('spree.currency') %><%= t('spree.product') %><%= t('spree.quantity') %><%= t('spree.item_price') %><%= t('spree.sales_total') %>
<%= currency %> + <% if (variant = ::Spree::Variant.where(id: variant_id).first) %> + <%= link_to variant.name, variant.product, target: '_blank' %> + <% else %> + Unknown + <% end %> + <%= row[:quantity] %><%= row[:item_price].format %><%= row[:sales_total].format %>
<%=t('spree.total') %> <%= currency %> + <%= report[:quantity_total] %><%= report[:sales_total].format %>
+
+ Not including tax, shipping, and adjustments +
diff --git a/config/locales/en.yml b/config/locales/en.yml index 04e2e97..a473d01 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -6,4 +6,7 @@ en: admin: tab: reports: Reports + item_price: Item Price + sales_total_by_product: Sales Total by Product + sales_total_by_product_description: Sales Totals by Products reports: Reports diff --git a/config/routes.rb b/config/routes.rb index d0e7b5f..39117a3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,6 +6,8 @@ collection do get :sales_total post :sales_total + get :sales_total_by_product + post :sales_total_by_product end end end From 536d8b39aad5db704ac64eb1aba41d48a7cd3a33 Mon Sep 17 00:00:00 2001 From: Rasmus Styrk Date: Mon, 22 Aug 2022 10:35:39 +0200 Subject: [PATCH 2/4] Only include paid orders in reports --- app/controllers/spree/admin/reports_controller.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/controllers/spree/admin/reports_controller.rb b/app/controllers/spree/admin/reports_controller.rb index f2286ab..32b70f1 100644 --- a/app/controllers/spree/admin/reports_controller.rb +++ b/app/controllers/spree/admin/reports_controller.rb @@ -35,7 +35,7 @@ def index def sales_total_by_product params[:q] = search_params - @search = Order.complete.not_canceled.ransack(params[:q]) + @search = Order.complete.not_canceled.where(payment_state: "paid").ransack(params[:q]) @orders = @search.result @totals = {} @@ -69,9 +69,11 @@ def sales_total_by_product def sales_total params[:q] = search_params - @search = Order.complete.not_canceled.ransack(params[:q]) + @search = Order.complete.not_canceled.where(payment_state: "paid").ransack(params[:q]) @orders = @search.result + Rails.logger.info("Orders Total: #{@orders.count}") + @totals = {} @orders.each do |order| unless @totals[order.currency] From eceb139458cf87d814c900d4e75481a9b36d668f Mon Sep 17 00:00:00 2001 From: Rasmus Styrk Date: Mon, 12 Jun 2023 12:16:16 +0200 Subject: [PATCH 3/4] Adjusted totals --- app/controllers/spree/admin/reports_controller.rb | 8 ++++++-- .../spree/admin/reports/sales_total_by_product.html.erb | 3 +++ config/locales/en.yml | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/controllers/spree/admin/reports_controller.rb b/app/controllers/spree/admin/reports_controller.rb index 32b70f1..4b2cb43 100644 --- a/app/controllers/spree/admin/reports_controller.rb +++ b/app/controllers/spree/admin/reports_controller.rb @@ -44,7 +44,8 @@ def sales_total_by_product @totals[order.currency] = { data: {}, quantity_total: 0, - sales_total: ::Money.new(0, order.currency) + sales_total: ::Money.new(0, order.currency), + adjusted_total: ::Money.new(0, order.currency) } end @@ -53,15 +54,18 @@ def sales_total_by_product @totals[order.currency][:data][line_item.variant_id] = { quantity: 0, item_price: line_item.display_price, - sales_total: ::Money.new(0, order.currency) + sales_total: ::Money.new(0, order.currency), + adjusted_total: ::Money.new(0, order.currency) } end @totals[order.currency][:data][line_item.variant_id][:quantity] += line_item.quantity @totals[order.currency][:data][line_item.variant_id][:sales_total] += line_item.display_amount.money + @totals[order.currency][:data][line_item.variant_id][:adjusted_total] += line_item.display_total.money @totals[order.currency][:quantity_total] += line_item.quantity @totals[order.currency][:sales_total] += line_item.display_amount.money + @totals[order.currency][:adjusted_total] += line_item.display_total.money end end end diff --git a/app/views/spree/admin/reports/sales_total_by_product.html.erb b/app/views/spree/admin/reports/sales_total_by_product.html.erb index 62f52ed..f6bd67b 100644 --- a/app/views/spree/admin/reports/sales_total_by_product.html.erb +++ b/app/views/spree/admin/reports/sales_total_by_product.html.erb @@ -21,6 +21,7 @@ <%= t('spree.quantity') %> <%= t('spree.item_price') %> <%= t('spree.sales_total') %> + <%= t('spree.adjusted_total') %> @@ -38,6 +39,7 @@ <%= row[:quantity] %> <%= row[:item_price].format %> <%= row[:sales_total].format %> + <%= row[:adjusted_total].format %> <% end %> @@ -47,6 +49,7 @@ <%= report[:quantity_total] %> <%= report[:sales_total].format %> + <%= report[:adjusted_total].format %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index a473d01..21868d0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -9,4 +9,5 @@ en: item_price: Item Price sales_total_by_product: Sales Total by Product sales_total_by_product_description: Sales Totals by Products + adjusted_total: Adjusted Totals reports: Reports From 5c71b883d4b36523eeaf01d1e23c478e8677ee45 Mon Sep 17 00:00:00 2001 From: Michael Modvig Date: Wed, 9 Aug 2023 07:44:54 +0200 Subject: [PATCH 4/4] Updates to totals in reports --- .../spree/admin/reports_controller.rb | 20 +++++++++++-------- .../reports/sales_total_by_product.html.erb | 8 ++++---- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/app/controllers/spree/admin/reports_controller.rb b/app/controllers/spree/admin/reports_controller.rb index 4b2cb43..3aa7eca 100644 --- a/app/controllers/spree/admin/reports_controller.rb +++ b/app/controllers/spree/admin/reports_controller.rb @@ -44,8 +44,8 @@ def sales_total_by_product @totals[order.currency] = { data: {}, quantity_total: 0, - sales_total: ::Money.new(0, order.currency), - adjusted_total: ::Money.new(0, order.currency) + sales_total: 0, + adjusted_total: 0 } end @@ -54,18 +54,22 @@ def sales_total_by_product @totals[order.currency][:data][line_item.variant_id] = { quantity: 0, item_price: line_item.display_price, - sales_total: ::Money.new(0, order.currency), - adjusted_total: ::Money.new(0, order.currency) + sales_total: 0, + adjusted_total: 0 } end @totals[order.currency][:data][line_item.variant_id][:quantity] += line_item.quantity @totals[order.currency][:data][line_item.variant_id][:sales_total] += line_item.display_amount.money - @totals[order.currency][:data][line_item.variant_id][:adjusted_total] += line_item.display_total.money + @totals[order.currency][:data][line_item.variant_id][:adjusted_total] += line_item.price + order.adjustment_total + end + end - @totals[order.currency][:quantity_total] += line_item.quantity - @totals[order.currency][:sales_total] += line_item.display_amount.money - @totals[order.currency][:adjusted_total] += line_item.display_total.money + @totals.each do |currency, data| + @totals[currency][:data].each do |variant_id, data| + @totals[currency][:quantity_total] += data[:quantity] + @totals[currency][:sales_total] += data[:sales_total] + @totals[currency][:adjusted_total] += data[:adjusted_total] end end end diff --git a/app/views/spree/admin/reports/sales_total_by_product.html.erb b/app/views/spree/admin/reports/sales_total_by_product.html.erb index f6bd67b..ac43cff 100644 --- a/app/views/spree/admin/reports/sales_total_by_product.html.erb +++ b/app/views/spree/admin/reports/sales_total_by_product.html.erb @@ -38,8 +38,8 @@ <%= row[:quantity] %> <%= row[:item_price].format %> - <%= row[:sales_total].format %> - <%= row[:adjusted_total].format %> + <%= ::Money.new(row[:sales_total], currency).format %> + <%= ::Money.new(row[:adjusted_total] * 100, currency).format %> <% end %> @@ -48,8 +48,8 @@ <%= report[:quantity_total] %> - <%= report[:sales_total].format %> - <%= report[:adjusted_total].format %> + <%= ::Money.new(report[:sales_total], currency).format %> + <%= ::Money.new(report[:adjusted_total] * 100, currency).format %> <% end %>