-
Notifications
You must be signed in to change notification settings - Fork 9
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
Fix access of Spree namespaces #13
base: rails-6-support
Are you sure you want to change the base?
Changes from all commits
345c61f
bb64443
62fdbb2
591dd03
f81edbf
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'csv' | ||
require "csv" | ||
|
||
module SolidusMarketplace | ||
module Spree | ||
|
@@ -20,8 +20,8 @@ def earnings | |
end | ||
|
||
def earnings_csv | ||
header1 = ['Supplier Earnings'] | ||
header2 = ['Supplier', 'Earnings', 'Paypal Email'] | ||
header1 = ["Supplier Earnings"] | ||
header2 = ["Supplier", "Earnings", "Paypal Email"] | ||
|
||
CSV.generate do |csv| | ||
csv << header1 | ||
|
@@ -34,21 +34,67 @@ def earnings_csv | |
end | ||
end | ||
|
||
def supplier_total_sales | ||
params[:q] = search_params | ||
|
||
@search = ::Spree::Order.complete.not_canceled.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. is there any default params? like only last 30 days, I can see some performance degradation over time with more and more data being computed 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. Makes sense to add a range, will update that |
||
@orders = @search.result | ||
|
||
@totals = {} | ||
supplier_sales_total_map = @orders.map(&:supplier_total_sales_map) | ||
grouped_suppliers_map = supplier_sales_total_map.flatten.group_by { |s| s[:name] }.values | ||
@grouped_sales = grouped_suppliers_map.map do |gs| | ||
h = {} | ||
h[:name] = nil | ||
h[:total_sales] = [] | ||
gs.each do |s| | ||
h[:name] = s[:name] if h[:name].nil? | ||
h[:total_sales] << s[:total_sales] | ||
end | ||
h | ||
end | ||
|
||
@grouped_sales.each do |gs| | ||
gs[:total_sales] = gs[:total_sales].inject(::Spree::Money.new(0)) do |e, c| | ||
c + e | ||
end | ||
end | ||
|
||
respond_to do |format| | ||
format.html | ||
format.csv { send_data(supplier_total_sales_csv) } | ||
end | ||
end | ||
|
||
def supplier_total_sales_csv | ||
header1 = ["Supplier Total Sales"] | ||
header2 = ["Supplier", "Total Sales"] | ||
|
||
CSV.generate do |csv| | ||
csv << header1 | ||
csv << header2 | ||
@grouped_sales.each do |se| | ||
csv << ["#{se[:name]}", | ||
"#{se[:total_sales].to_html}"] | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def add_marketplace_reports | ||
marketplace_reports.each do |report| | ||
Spree::Admin::ReportsController.add_available_report!(report) | ||
::Spree::Admin::ReportsController.add_available_report!(report) | ||
end | ||
end | ||
|
||
def marketplace_reports | ||
[:earnings] | ||
[:earnings, :supplier_total_sales] | ||
end | ||
|
||
def get_supplier_earnings | ||
grouped_supplier_earnings.each do |se| | ||
se[:earnings] = se[:earnings].inject(Spree::Money.new(0)) do |e, c| | ||
se[:earnings] = se[:earnings].inject(::Spree::Money.new(0)) do |e, c| | ||
c + e | ||
end | ||
end | ||
|
@@ -57,11 +103,11 @@ def get_supplier_earnings | |
def grouped_supplier_earnings | ||
params[:q] = search_params | ||
|
||
@search = Spree::Order.complete.not_canceled.ransack(params[:q]) | ||
@search = ::Spree::Order.complete.not_canceled.ransack(params[:q]) | ||
@orders = @search.result | ||
|
||
supplier_earnings_map = @orders.map(&:supplier_earnings_map) | ||
grouped_suppliers_map = supplier_earnings_map.flatten.group_by(&:name).values | ||
grouped_suppliers_map = supplier_earnings_map.flatten.group_by { |s| s[:name] }.values | ||
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. here in case that the suppliers aren't present, maybe we should use:
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. Let me test that first and then change it |
||
grouped_earnings = grouped_suppliers_map.map do |gs| | ||
h = {} | ||
h[:name] = nil | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,23 @@ def self.prepended(base) | |
base.has_many :suppliers, through: :stock_locations | ||
end | ||
|
||
def supplier_total(user_or_supplier) | ||
def supplier_total_sales(user_or_supplier) | ||
supplier = user_or_supplier.is_a?(::Spree::Supplier) ? user_or_supplier : user_or_supplier.supplier | ||
shipments = self.shipments.by_supplier(supplier) | ||
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. optional: |
||
total = shipments.map(&:display_final_price_with_items) | ||
::Spree::Money.new(total.sum) | ||
end | ||
|
||
def supplier_total_sales_map | ||
suppliers.map do |s| | ||
{ | ||
name: s.name, | ||
total_sales: self.supplier_total_sales(s), | ||
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. optional: |
||
} | ||
end | ||
end | ||
|
||
def supplier_total_commissions(user_or_supplier) | ||
supplier = user_or_supplier.is_a?(::Spree::Supplier) ? user_or_supplier : user_or_supplier.supplier | ||
shipments = self.shipments.by_supplier(supplier) | ||
commissions = shipments.map(&:supplier_commission_total) | ||
|
@@ -19,8 +35,8 @@ def supplier_earnings_map | |
suppliers.map do |s| | ||
{ | ||
name: s.name, | ||
earnings: self.supplier_total(s), | ||
paypal_email: s.paypal_email | ||
earnings: self.supplier_total_commissions(s), | ||
paypal_email: s.paypal_email, | ||
} | ||
end | ||
end | ||
|
@@ -33,7 +49,7 @@ def finalize_with_supplier! | |
shipments.each do |shipment| | ||
if SolidusMarketplace::Config.send_supplier_email && shipment.supplier.present? | ||
begin | ||
Spree::MarketplaceOrderMailer.supplier_order(shipment.id).deliver! | ||
::Spree::MarketplaceOrderMailer.supplier_order(shipment.id).deliver! | ||
rescue => ex #Errno::ECONNREFUSED => ex | ||
puts ex.message | ||
puts ex.backtrace.join("\n") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<% admin_breadcrumb(link_to t('spree.reports'), spree.admin_reports_path) %> | ||
<% admin_breadcrumb(t('spree.supplier_total_sales')) %> | ||
|
||
<% content_for :page_actions do %> | ||
<li id='export_supplier_csv'> | ||
<%= link_to t('spree.export_supplier_csv'), spree.supplier_total_sales_admin_reports_url(format: 'csv'), class: 'btn btn-primary' %> | ||
</li> | ||
<% 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: :supplier_total_sales } %> | ||
<% end %> | ||
|
||
<table class="admin-report" data-hook="supplier_total_sales"> | ||
<thead> | ||
<tr> | ||
<th><%= t('spree.supplier') %></th> | ||
<th><%= t('spree.supplier_total_sales') %></th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<% @grouped_sales.each do |grouped_sale| %> | ||
<tr> | ||
<td><%= grouped_sale[:name] %></td> | ||
<td><%= grouped_sale[:total_sales].to_html %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<% if try_spree_current_user.supplier? %> | ||
<%= order.supplier_total(try_spree_current_user).to_html %> | ||
<%= order.supplier_total_commissions(try_spree_current_user).to_html %> | ||
<% else %> | ||
<%= order.display_total.to_html %> | ||
<% end %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,12 @@ | |
resources :suppliers | ||
resources :reports, only: [:index] do | ||
collection do | ||
get :earnings | ||
post :earnings | ||
get :earnings | ||
post :earnings | ||
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. Interesting, is the current report sending the form as a post request? this should be |
||
end | ||
collection do | ||
get :supplier_total_sales | ||
post :supplier_total_sales | ||
end | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,25 +51,27 @@ | |
end | ||
end | ||
|
||
xdescribe '#supplier_total' do | ||
let!(:order) { create(:completed_order_from_supplier_with_totals, | ||
ship_address: create(:address)) } | ||
xdescribe "#supplier_total_commissions" do | ||
let!(:order) { | ||
create(:completed_order_from_supplier_with_totals, | ||
ship_address: create(:address)) | ||
} | ||
let(:supplier) { order.suppliers.first } | ||
let(:expected_supplier_total) { Spree::Money.new(15.00) } | ||
let(:expected_supplier_total_commissions) { Spree::Money.new(15.00) } | ||
|
||
context 'when passed a supplier' do | ||
it 'returns the total commission earned for the order for a given supplier' do | ||
context "when passed a supplier" 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. Same here as @softr8 has recommended, I would suggest to keep the single quotes |
||
it "returns the total commission earned for the order for a given supplier" do | ||
expect(order.total).to eq(150.0) | ||
expect(order.suppliers.count).to eq(1) | ||
expect(order.supplier_total(supplier).to_s).to eq(expected_supplier_total.to_s) | ||
expect(order.supplier_total_commissions(supplier).to_s).to eq(expected_supplier_total_commissions.to_s) | ||
end | ||
end | ||
|
||
context 'when passed a user associated with a supplier' do | ||
it 'returns the total commission earned for the order for a given supplier' do | ||
context "when passed a user associated with a supplier" do | ||
it "returns the total commission earned for the order for a given supplier" do | ||
expect(order.total).to eq(150.0) | ||
expect(order.suppliers.count).to eq(1) | ||
expect(order.supplier_total(supplier)).to eq(expected_supplier_total) | ||
expect(order.supplier_total_commissions(supplier)).to eq(expected_supplier_total_commissions) | ||
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.
we try to use double quotes only whenever we need to interpolate variables, leaving the rest as single quotes
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.
That is my editor playing with me