-
Notifications
You must be signed in to change notification settings - Fork 27
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
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 %> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) %> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we translate this note, please? |
||
</div> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
collection do | ||
get :sales_total | ||
post :sales_total | ||
get :sales_total_by_product | ||
post :sales_total_by_product | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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