diff --git a/app/controllers/spree/admin/reports_controller.rb b/app/controllers/spree/admin/reports_controller.rb index 650a4bc..3aa7eca 100644 --- a/app/controllers/spree/admin/reports_controller.rb +++ b/app/controllers/spree/admin/reports_controller.rb @@ -25,18 +25,63 @@ 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.where(payment_state: "paid").ransack(params[:q]) + @orders = @search.result + + @totals = {} + @orders.each do |order| + unless @totals[order.currency] + @totals[order.currency] = { + data: {}, + quantity_total: 0, + sales_total: 0, + adjusted_total: 0 + } + 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: 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.price + order.adjustment_total + end + end + + @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 + 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] 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..ac43cff --- /dev/null +++ b/app/views/spree/admin/reports/sales_total_by_product.html.erb @@ -0,0 +1,59 @@ +<% 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') %><%= t('spree.adjusted_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 %><%= ::Money.new(row[:sales_total], currency).format %><%= ::Money.new(row[:adjusted_total] * 100, currency).format %>
<%=t('spree.total') %> <%= currency %> + <%= report[:quantity_total] %><%= ::Money.new(report[:sales_total], currency).format %><%= ::Money.new(report[:adjusted_total] * 100, currency).format %>
+
+ Not including tax, shipping, and adjustments +
diff --git a/config/locales/en.yml b/config/locales/en.yml index 04e2e97..21868d0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -6,4 +6,8 @@ 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 + adjusted_total: Adjusted Totals 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