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

Fix access of Spree namespaces #13

Open
wants to merge 5 commits into
base: rails-6-support
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def current_supplier_ids
end

def get_suppliers
@suppliers = Spree::Supplier.order(:name)
@suppliers = ::Spree::Supplier.order(:name)
end

# Scopes the collection to what the user should have access to, based on the user's role
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'csv'
require "csv"
Copy link
Member

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

Copy link
Author

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


module SolidusMarketplace
module Spree
Expand All @@ -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
Expand All @@ -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])
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Author

Choose a reason for hiding this comment

The 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
Expand All @@ -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
Copy link

Choose a reason for hiding this comment

The 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:

supplier_earnings_map.flatten.group_by { |s| s[:name] }&.values

Copy link
Author

Choose a reason for hiding this comment

The 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.prepended(base)

def load_supplier_stock_location
if try_spree_current_user.supplier
@stock_locations = Spree::StockLocation.by_supplier(try_spree_current_user.supplier).accessible_by(current_ability, :read)
@stock_locations = ::Spree::StockLocation.by_supplier(try_spree_current_user.supplier).accessible_by(current_ability, :read)
@stock_item_stock_locations = params[:stock_location_id].present? ? @stock_locations.where(id: params[:stock_location_id]) : @stock_locations
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.prepended(base)
end

def index
@stock_locations = Spree::StockLocation.accessible_by(current_ability, :read)
@stock_locations = ::Spree::StockLocation.accessible_by(current_ability, :read)
.order('name ASC')
.ransack(params[:q])
.result
Expand Down
24 changes: 20 additions & 4 deletions app/decorators/models/solidus_marketplace/spree/order_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link

Choose a reason for hiding this comment

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

optional:
you can avoid using self

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),
Copy link

Choose a reason for hiding this comment

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

optional:
same here

}
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)
Expand All @@ -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
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.prepended(base)
end

def display_final_price_with_items
Spree::Money.new final_price_with_items
::Spree::Money.new final_price_with_items
end

def final_price_with_items
Expand Down
4 changes: 2 additions & 2 deletions app/overrides/spree/admin/add_supplier_to_admin_orders.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
insert_before: '[data-hook="admin_orders_index_row_actions"]',
text: "<td class='align-center'>
<% 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 %>
</td>",
disabled: false
disabled: false,

)
33 changes: 33 additions & 0 deletions app/views/spree/admin/reports/supplier_total_sales.html.erb
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 %>
10 changes: 6 additions & 4 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ en:
attributes:
url:
invalid: is invalid. Please be sure to include include 'http://'
not_responding: 'is invalid, not responding or redirecting to another location'
not_responding: "is invalid, not responding or redirecting to another location"
spree:
admin:
user:
Expand Down Expand Up @@ -83,7 +83,7 @@ en:
inactive: Inactive
individual: Individual
manage: Manage
must_be_logged_in: 'Must Be Logged In'
must_be_logged_in: "Must Be Logged In"
name: Name
new_supplier: New Supplier
or: or
Expand All @@ -102,7 +102,7 @@ en:
unauthorized: Unauthorized
show_only_incomplete_orders: Show Only Incomplete Orders
signup: Sign Up
signup_to_become_a_supplier: 'Sign Up To Become A Supplier'
signup_to_become_a_supplier: "Sign Up To Become A Supplier"
supplier: Supplier
supplier_details: Supplier Details
supplier_earnings: Supplier Earnings
Expand All @@ -113,7 +113,7 @@ en:
hello: "Hello %{name},"
logging_into_your_account: logging into your account.
manage_your_account: You may now manage your profile and inventory by
subject: 'Thank you for signing up. Please verify your information.'
subject: "Thank you for signing up. Please verify your information."
thank_you_again: "Thank you again for your business, <b>%{name}</b>"
thank_you_for_signing_up: Thank you for signing up to be a supplier.
supplier_paypal_email: PayPal Email
Expand All @@ -124,6 +124,8 @@ en:
invalid_password: Invalid password please sign in or sign up.
success: Thank you for signing up!
supplier_signup: Supplier Sign Up
supplier_total_sales: Supplier Total Sales
supplier_total_sales_description: Total sales for Suppliers over the specified time period
suppliers: Suppliers
user_admin: Admin User
remove_payment_method: Remove payment method
Expand Down
2 changes: 2 additions & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ es:
invalid_password: Contraseña no válida, por favor inicie sesión o regístrese
success: Gracias por registrarse!
supplier_signup: Registro de proveedor
supplier_total_sales: Ventas totales del proveedor
supplier_total_sales_description: Ventas totales para proveedores durante el período de tiempo especificado
suppliers: Proveedores
user_admin: Usuario administrador
remove_payment_method: Remover método de pago
Expand Down
8 changes: 6 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
resources :suppliers
resources :reports, only: [:index] do
collection do
get :earnings
post :earnings
get :earnings
post :earnings
Copy link
Member

Choose a reason for hiding this comment

The 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 GET so it can be bookmarkable

end
collection do
get :supplier_total_sales
post :supplier_total_sales
end
end

Expand Down
22 changes: 12 additions & 10 deletions spec/models/spree/order_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The 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
Expand Down