Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sales total by products #38

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion app/controllers/spree/admin/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would like to see a spec for this


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])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change seems to break the existing specs.

@orders = @search.result

Rails.logger.info("Orders Total: #{@orders.count}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can probably be removed?


@totals = {}
@orders.each do |order|
unless @totals[order.currency]
Expand Down
59 changes: 59 additions & 0 deletions app/views/spree/admin/reports/sales_total_by_product.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This empty block can be omitted

<% 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 %>

<table class="admin-report" data-hook="sales_total">
<thead>
<tr>
<th><%= t('spree.currency') %></th>
<th><%= t('spree.product') %></th>
<th><%= t('spree.quantity') %></th>
<th><%= t('spree.item_price') %></th>
<th><%= t('spree.sales_total') %></th>
<th><%= t('spree.adjusted_total') %></th>
</tr>
</thead>
<tbody>
<% @totals.each do |currency, report| %>
<% report[:data].each do |variant_id, row| %>
<tr>
<td><%= currency %></td>
<td>
<% if (variant = ::Spree::Variant.where(id: variant_id).first) %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we eager load in the controller this to prevent a N+1 query here, please?

<%= link_to variant.name, variant.product, target: '_blank' %>
<% else %>
Unknown
<% end %>
</td>
<td><%= row[:quantity] %></td>
<td><%= row[:item_price].format %></td>
<td><%= ::Money.new(row[:sales_total], currency).format %></td>
<td><%= ::Money.new(row[:adjusted_total] * 100, currency).format %></td>
</tr>
<% end %>
<tr>
<td><strong><%=t('spree.total') %> <%= currency %></strong></td>
<td>
</td>
<td><strong><%= report[:quantity_total] %></strong></td>
<td></td>
<td><strong><%= ::Money.new(report[:sales_total], currency).format %></strong></td>
<td><strong><%= ::Money.new(report[:adjusted_total] * 100, currency).format %></strong></td>
</tr>
<% end %>
</tbody>
</table>
<div style="text-align: center">
<i>Not including tax, shipping, and adjustments</i>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we translate this note, please?

</div>
4 changes: 4 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
collection do
get :sales_total
post :sales_total
get :sales_total_by_product
post :sales_total_by_product
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why we would need the post route here.

end
end
end
Expand Down