Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

[SD-1266] Fix Customer lifetime stats in Admin plus add lifetime stats to Platform API Users endpoint (#11208)

Szymon Iwacz95fcebc24c88
diff --git a/api/app/serializers/spree/api/v2/platform/user_serializer.rb b/api/app/serializers/spree/api/v2/platform/user_serializer.rb
index 291b468f4f..20d0c2830c 100644
--- a/api/app/serializers/spree/api/v2/platform/user_serializer.rb
+++ b/api/app/serializers/spree/api/v2/platform/user_serializer.rb
@@ -7,6 +7,18 @@ module Spree
attributes :email, :created_at, :updated_at
+ attribute :average_order_value do |user, params|
+ price_stats(user.report_values_for(:average_order_value, params[:store]))
+ end
+
+ attribute :lifetime_value do |user, params|
+ price_stats(user.report_values_for(:lifetime_value, params[:store]))
+ end
+
+ attribute :store_credits do |user, params|
+ price_stats(user.available_store_credits(params[:store]))
+ end
+
has_one :bill_address,
record_type: :address,
serializer: :address
@@ -14,6 +26,10 @@ module Spree
has_one :ship_address,
record_type: :address,
serializer: :address
+
+ def self.price_stats(stats)
+ stats.map { |value| { currency: value.currency.to_s, amount: value.money.to_s } }
+ end
end
end
end
diff --git a/api/docs/v2/platform/index.yaml b/api/docs/v2/platform/index.yaml
index 62a1842b98..f4c03de280 100644
--- a/api/docs/v2/platform/index.yaml
+++ b/api/docs/v2/platform/index.yaml
@@ -57,8 +57,8 @@ paths:
state_name:
alternative_phone: 555-555-0199
company: Company
- created_at: '2021-08-18T07:08:27.889Z'
- updated_at: '2021-08-18T07:08:27.889Z'
+ created_at: '2021-08-18T13:45:53.182Z'
+ updated_at: '2021-08-18T13:45:53.182Z'
deleted_at:
label:
relationships:
@@ -85,8 +85,8 @@ paths:
state_name:
alternative_phone: 555-555-0199
company: Company
- created_at: '2021-08-18T07:08:27.894Z'
- updated_at: '2021-08-18T07:08:27.894Z'
+ created_at: '2021-08-18T13:45:53.188Z'
+ updated_at: '2021-08-18T13:45:53.188Z'
deleted_at:
label:
relationships:
@@ -154,8 +154,8 @@ paths:
state_name:
alternative_phone: 555-555-0199
company: Company
- created_at: '2021-08-18T07:08:28.218Z'
- updated_at: '2021-08-18T07:08:28.218Z'
+ created_at: '2021-08-18T13:45:53.540Z'
+ updated_at: '2021-08-18T13:45:53.540Z'
deleted_at:
label:
relationships:
@@ -243,8 +243,8 @@ paths:
state_name:
alternative_phone: 555-555-0199
company: Company
- created_at: '2021-08-18T07:08:28.277Z'
- updated_at: '2021-08-18T07:08:28.277Z'
+ created_at: '2021-08-18T13:45:53.599Z'
+ updated_at: '2021-08-18T13:45:53.599Z'
deleted_at:
label:
relationships:
@@ -315,8 +315,8 @@ paths:
state_name:
alternative_phone: 555-555-0199
company: Company
- created_at: '2021-08-18T07:08:28.357Z'
- updated_at: '2021-08-18T07:08:28.369Z'
+ created_at: '2021-08-18T13:45:53.677Z'
+ updated_at: '2021-08-18T13:45:53.688Z'
deleted_at:
label:
relationships:
@@ -439,8 +439,8 @@ paths:
type: classification
attributes:
position: 1
- created_at: '2021-08-18T07:08:28.791Z'
- updated_at: '2021-08-18T07:08:28.791Z'
+ created_at: '2021-08-18T13:45:54.112Z'
+ updated_at: '2021-08-18T13:45:54.112Z'
relationships:
product:
data:
@@ -454,8 +454,8 @@ paths:
type: classification
attributes:
position: 1
- created_at: '2021-08-18T07:08:28.872Z'
- updated_at: '2021-08-18T07:08:28.872Z'
+ created_at: '2021-08-18T13:45:54.187Z'
+ updated_at: '2021-08-18T13:45:54.187Z'
relationships:
product:
data:
@@ -510,8 +510,8 @@ paths:
type: classification
attributes:
position: 1
- created_at: '2021-08-18T07:08:29.237Z'
- updated_at: '2021-08-18T07:08:29.237Z'
+ created_at: '2021-08-18T13:45:54.545Z'
+ updated_at: '2021-08-18T13:45:54.545Z'
relationships:
product:
data:
@@ -572,8 +572,8 @@ paths:
type: classification
attributes:
position: 1
- created_at: '2021-08-18T07:08:29.378Z'
- updated_at: '2021-08-18T07:08:29.378Z'
+ created_at: '2021-08-18T13:45:54.675Z'
+ updated_at: '2021-08-18T13:45:54.675Z'
relationships:
product:
data:
@@ -631,8 +631,8 @@ paths:
type: classification
attributes:
position: 1
- created_at: '2021-08-18T07:08:29.657Z'
- updated_at: '2021-08-18T07:08:29.657Z'
+ created_at: '2021-08-18T13:45:54.951Z'
+ updated_at: '2021-08-18T13:45:54.951Z'
relationships:
product:
data:
@@ -738,8 +738,8 @@ paths:
type: classification
attributes:
position: 2
- created_at: '2021-08-18T07:08:30.317Z'
- updated_at: '2021-08-18T07:08:30.336Z'
+ created_at: '2021-08-18T13:45:55.595Z'
+ updated_at: '2021-08-18T13:45:55.614Z'
relationships:
product:
data:
@@ -804,9 +804,9 @@ paths:
name: United States of America
numcode: 840
states_required: true
- updated_at: '2021-08-18T07:08:30.636Z'
+ updated_at: '2021-08-18T13:45:55.899Z'
zipcode_required: true
- created_at: '2021-08-18T07:08:30.636Z'
+ created_at: '2021-08-18T13:45:55.899Z'
relationships:
states:
data: []
@@ -819,9 +819,9 @@ paths:
name: NAME_2
numcode: 840
states_required: false
- updated_at: '2021-08-18T07:08:30.645Z'
+ updated_at: '2021-08-18T13:45:55.908Z'
zipcode_required: true
- created_at: '2021-08-18T07:08:30.645Z'
+ created_at: '2021-08-18T13:45:55.908Z'
relationships:
states:
data: []
@@ -834,9 +834,9 @@ paths:
name: NAME_3
numcode: 840
states_required: false
- updated_at: '2021-08-18T07:08:30.647Z'
+ updated_at: '2021-08-18T13:45:55.910Z'
zipcode_required: true
- created_at: '2021-08-18T07:08:30.647Z'
+ created_at: '2021-08-18T13:45:55.910Z'
relationships:
states:
data: []
@@ -889,9 +889,9 @@ paths:
name: NAME_6
numcode: 840
states_required: false
- updated_at: '2021-08-18T07:08:30.713Z'
+ updated_at: '2021-08-18T13:45:55.975Z'
zipcode_required: true
- created_at: '2021-08-18T07:08:30.713Z'
+ created_at: '2021-08-18T13:45:55.975Z'
relationships:
states:
data: []
@@ -957,8 +957,8 @@ paths:
name: foo-size-1
presentation: Size
position: 1
- created_at: '2021-08-18T07:08:30.787Z'
- updated_at: '2021-08-18T07:08:30.787Z'
+ created_at: '2021-08-18T13:45:56.048Z'
+ updated_at: '2021-08-18T13:45:56.048Z'
filterable: true
relationships:
option_values:
@@ -969,8 +969,8 @@ paths:
name: foo-size-2
presentation: Size
position: 2
- created_at: '2021-08-18T07:08:30.790Z'
- updated_at: '2021-08-18T07:08:30.790Z'
+ created_at: '2021-08-18T13:45:56.050Z'
+ updated_at: '2021-08-18T13:45:56.050Z'
filterable: true
relationships:
option_values:
@@ -1022,8 +1022,8 @@ paths:
name: foo-size-5
presentation: Size
position: 1
- created_at: '2021-08-18T07:08:30.866Z'
- updated_at: '2021-08-18T07:08:30.866Z'
+ created_at: '2021-08-18T13:45:56.126Z'
+ updated_at: '2021-08-18T13:45:56.126Z'
filterable: true
relationships:
option_values:
@@ -1081,8 +1081,8 @@ paths:
name: foo-size-6
presentation: Size
position: 1
- created_at: '2021-08-18T07:08:30.920Z'
- updated_at: '2021-08-18T07:08:30.920Z'
+ created_at: '2021-08-18T13:45:56.169Z'
+ updated_at: '2021-08-18T13:45:56.169Z'
filterable: true
relationships:
option_values:
@@ -1137,8 +1137,8 @@ paths:
name: Size-X
presentation: Size
position: 1
- created_at: '2021-08-18T07:08:31.000Z'
- updated_at: '2021-08-18T07:08:31.009Z'
+ created_at: '2021-08-18T13:45:56.247Z'
+ updated_at: '2021-08-18T13:45:56.257Z'
filterable: true
relationships:
option_values:
@@ -1252,8 +1252,8 @@ paths:
position: 1
name: Size-1
presentation: S
- created_at: '2021-08-18T07:08:31.178Z'
- updated_at: '2021-08-18T07:08:31.178Z'
+ created_at: '2021-08-18T13:45:56.421Z'
+ updated_at: '2021-08-18T13:45:56.421Z'
relationships:
option_type:
data:
@@ -1265,8 +1265,8 @@ paths:
position: 1
name: Size-2
presentation: S
- created_at: '2021-08-18T07:08:31.184Z'
- updated_at: '2021-08-18T07:08:31.184Z'
+ created_at: '2021-08-18T13:45:56.426Z'
+ updated_at: '2021-08-18T13:45:56.426Z'
relationships:
option_type:
data:
@@ -1319,8 +1319,8 @@ paths:
position: 1
name: Size-5
presentation: S
- created_at: '2021-08-18T07:08:31.265Z'
- updated_at: '2021-08-18T07:08:31.265Z'
+ created_at: '2021-08-18T13:45:56.507Z'
+ updated_at: '2021-08-18T13:45:56.507Z'
relationships:
option_type:
data:
@@ -1377,8 +1377,8 @@ paths:
position: 1
name: Size-6
presentation: S
- created_at: '2021-08-18T07:08:31.313Z'
- updated_at: '2021-08-18T07:08:31.313Z'
+ created_at: '2021-08-18T13:45:56.554Z'
+ updated_at: '2021-08-18T13:45:56.554Z'
relationships:
option_type:
data:
@@ -1434,8 +1434,8 @@ paths:
position: 1
name: M
presentation: S
- created_at: '2021-08-18T07:08:31.392Z'
- updated_at: '2021-08-18T07:08:31.403Z'
+ created_at: '2021-08-18T13:45:56.630Z'
+ updated_at: '2021-08-18T13:45:56.640Z'
relationships:
option_type:
data:
@@ -1553,8 +1553,8 @@ paths:
lft: 1
rgt: 6
description:
- created_at: '2021-08-18T07:08:31.598Z'
- updated_at: '2021-08-18T07:08:31.660Z'
+ created_at: '2021-08-18T13:45:56.837Z'
+ updated_at: '2021-08-18T13:45:56.897Z'
meta_title:
meta_description:
meta_keywords:
@@ -1588,8 +1588,8 @@ paths:
lft: 2
rgt: 3
description:
- created_at: '2021-08-18T07:08:31.623Z'
- updated_at: '2021-08-18T07:08:31.626Z'
+ created_at: '2021-08-18T13:45:56.860Z'
+ updated_at: '2021-08-18T13:45:56.863Z'
meta_title:
meta_description:
meta_keywords:
@@ -1623,8 +1623,8 @@ paths:
lft: 4
rgt: 5
description:
- created_at: '2021-08-18T07:08:31.647Z'
- updated_at: '2021-08-18T07:08:31.651Z'
+ created_at: '2021-08-18T13:45:56.884Z'
+ updated_at: '2021-08-18T13:45:56.888Z'
meta_title:
meta_description:
meta_keywords:
@@ -1699,8 +1699,8 @@ paths:
lft: 2
rgt: 3
description:
- created_at: '2021-08-18T07:08:31.823Z'
- updated_at: '2021-08-18T07:08:31.827Z'
+ created_at: '2021-08-18T13:45:57.054Z'
+ updated_at: '2021-08-18T13:45:57.058Z'
meta_title:
meta_description:
meta_keywords:
@@ -1779,8 +1779,8 @@ paths:
lft: 2
rgt: 3
description:
- created_at: '2021-08-18T07:08:31.918Z'
- updated_at: '2021-08-18T07:08:31.921Z'
+ created_at: '2021-08-18T13:45:57.148Z'
+ updated_at: '2021-08-18T13:45:57.151Z'
meta_title:
meta_description:
meta_keywords:
@@ -1860,8 +1860,8 @@ paths:
lft: 2
rgt: 3
description:
- created_at: '2021-08-18T07:08:32.084Z'
- updated_at: '2021-08-18T07:08:32.108Z'
+ created_at: '2021-08-18T13:45:57.300Z'
+ updated_at: '2021-08-18T13:45:57.324Z'
meta_title:
meta_description:
meta_keywords:
@@ -1992,9 +1992,12 @@ paths:
- id: '1'
type: user
attributes:
- created_at: '2021-08-18T07:08:32.466Z'
- updated_at: '2021-08-18T07:08:32.466Z'
+ created_at: '2021-08-18T13:45:57.670Z'
+ updated_at: '2021-08-18T13:45:57.670Z'
+ average_order_value: []
+ lifetime_value: []
+ store_credits: []
relationships:
bill_address:
data:
@@ -2003,9 +2006,12 @@ paths:
- id: '2'
type: user
attributes:
- created_at: '2021-08-18T07:08:32.475Z'
- updated_at: '2021-08-18T07:08:32.475Z'
+ created_at: '2021-08-18T13:45:57.680Z'
+ updated_at: '2021-08-18T13:45:57.680Z'
+ average_order_value: []
+ lifetime_value: []
+ store_credits: []
relationships:
bill_address:
data:
@@ -2014,9 +2020,12 @@ paths:
- id: '3'
type: user
attributes:
- created_at: '2021-08-18T07:08:32.477Z'
- updated_at: '2021-08-18T07:08:32.477Z'
+ created_at: '2021-08-18T13:45:57.682Z'
+ updated_at: '2021-08-18T13:45:57.682Z'
+ average_order_value: []
+ lifetime_value: []
+ store_credits: []
relationships:
bill_address:
data:
@@ -2066,9 +2075,12 @@ paths:
id: '2'
type: user
attributes:
- created_at: '2021-08-18T07:08:32.569Z'
- updated_at: '2021-08-18T07:08:32.569Z'
+ created_at: '2021-08-18T13:45:57.800Z'
+ updated_at: '2021-08-18T13:45:57.800Z'
+ average_order_value: []
+ lifetime_value: []
+ store_credits: []
relationships:
bill_address:
data:
@@ -2122,9 +2134,12 @@ paths:
id: '2'
type: user
attributes:
- created_at: '2021-08-18T07:08:32.634Z'
- updated_at: '2021-08-18T07:08:32.634Z'
+ created_at: '2021-08-18T13:45:57.881Z'
+ updated_at: '2021-08-18T13:45:57.881Z'
+ average_order_value: []
+ lifetime_value: []
+ store_credits: []
relationships:
bill_address:
data:
@@ -2178,8 +2193,11 @@ paths:
type: user
attributes:
- created_at: '2021-08-18T07:08:32.732Z'
- updated_at: '2021-08-18T07:08:32.742Z'
+ created_at: '2021-08-18T13:45:57.992Z'
+ updated_at: '2021-08-18T13:45:58.002Z'
+ average_order_value: []
+ lifetime_value: []
+ store_credits: []
relationships:
bill_address:
data:
diff --git a/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb
index 953d4a9e29..ab492f30dd 100644
--- a/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/user_serializer_spec.rb
@@ -1,7 +1,9 @@
require 'spec_helper'
describe Spree::Api::V2::Platform::UserSerializer do
- subject { described_class.new(user) }
+ include_context 'API v2 serializers params'
+
+ subject { described_class.new(user, params: serializer_params) }
let(:user) { create(:user_with_addresses) }
@@ -15,6 +17,9 @@ describe Spree::Api::V2::Platform::UserSerializer do
type: :user,
attributes: {
email: user.email,
+ average_order_value: [],
+ lifetime_value: [],
+ store_credits: [],
created_at: user.created_at,
updated_at: user.updated_at
},
@@ -36,4 +41,24 @@ describe Spree::Api::V2::Platform::UserSerializer do
}
)
end
+
+ context 'when user has orders' do
+ before do
+ create(:completed_order_with_totals, user: user, currency: 'USD')
+ create(:completed_order_with_totals, user: user, currency: 'EUR')
+ create(:store_credit, amount: '100', store: store, user: user, currency: 'USD')
+ create(:store_credit, amount: '90', store: store, user: user, currency: 'EUR')
+ end
+
+ it do
+ expect(subject.serializable_hash[:data][:attributes]).to eq({
+ email: user.email,
+ average_order_value: [{ amount: '110.00', currency: 'USD' }, { amount: '110.00', currency: 'EUR' }],
+ lifetime_value: [{ amount: '110.00', currency: 'USD' }, { amount: '110.00', currency: 'EUR' }],
+ store_credits: [{ amount: '100.00', currency: 'USD' }, { amount: '90.00', currency: 'EUR' }],
+ created_at: user.created_at,
+ updated_at: user.updated_at
+ })
+ end
+ end
end
diff --git a/backend/app/views/spree/admin/users/_lifetime_stats.html.erb b/backend/app/views/spree/admin/users/_lifetime_stats.html.erb
index b7ba15750d..57dd34875c 100644
--- a/backend/app/views/spree/admin/users/_lifetime_stats.html.erb
+++ b/backend/app/views/spree/admin/users/_lifetime_stats.html.erb
@@ -8,19 +8,31 @@
<table class="table" id="user-lifetime-stats" data-hook>
<tr>
<td width="30%"><%= Spree.t(:total_sales) %>:</td>
- <td><%= @user.display_lifetime_value.to_html %></td>
+ <td>
+ <% @user.report_values_for(:lifetime_value, current_store).each do |value| %>
+ <div><%= value %></div>
+ <% end %>
+ </td>
</tr>
<tr>
<td><%= Spree.t(:num_orders) %>:</td>
- <td><%= @user.order_count %></td>
+ <td><%= @user.order_count(current_store) %></td>
</tr>
<tr>
<td><%= Spree.t(:average_order_value) %>:</td>
- <td><%= @user.display_average_order_value.to_html %></td>
+ <td>
+ <% @user.report_values_for(:average_order_value, current_store).each do |value| %>
+ <div><%= value %></div>
+ <% end %>
+ </td>
</tr>
<tr>
<td><%= Spree.t('admin.user.store_credits') %>:</td>
- <td id="store_credit"><%= Spree::Money.new(@user.total_available_store_credit) %></td>
+ <td id="store_credit">
+ <% @user.available_store_credits(current_store).each do |value| %>
+ <div><%= value %></div>
+ <% end %>
+ </td>
</tr>
<tr>
<td><%= Spree.t(:member_since) %>:</td>
diff --git a/backend/spec/features/admin/users_spec.rb b/backend/spec/features/admin/users_spec.rb
index 1a848b5b43..23d72d6a7c 100644
--- a/backend/spec/features/admin/users_spec.rb
+++ b/backend/spec/features/admin/users_spec.rb
@@ -5,32 +5,55 @@ describe 'Users', type: :feature do
stub_authorization!
include Spree::Admin::BaseHelper
+ let(:store) { Spree::Store.default }
let!(:user_a) { create(:user_with_addresses, email: '[email protected]') }
let!(:user_b) { create(:user_with_addresses, email: '[email protected]') }
- let(:order) { create(:completed_order_with_totals, user: user_a, number: 'R123') }
-
- let(:order_2) do
- create(:completed_order_with_totals, user: user_a, number: 'R456').tap do |o|
+ let!(:order) { create(:completed_order_with_totals, store: store, user: user_a, number: 'R123') }
+ let!(:order_2) do
+ create(:completed_order_with_totals, store: store, user: user_a, number: 'R456').tap do |o|
li = o.line_items.last
li.update_column(:price, li.price + 10)
end
end
+ let!(:order_eur) { create(:completed_order_with_totals, store: store, user: user_a, currency: 'EUR') }
+ let!(:order_gbp) { create(:completed_order_with_totals, store: store, user: user_a, currency: 'GBP') }
+ let!(:orders) { Spree::Order.where(id: [order.id, order_2.id, order_eur.id, order_gbp.id]) }
- let(:orders) { [order, order_2] }
+ let!(:store_credit_usd) { create(:store_credit, amount: '100', store: store, user: user_a, currency: 'USD') }
+ let!(:store_credit_eur) { create(:store_credit, amount: '90', store: store, user: user_a, currency: 'EUR') }
+ let!(:store_credit_gbp) { create(:store_credit, amount: '80', store: store, user: user_a, currency: 'GBP') }
shared_examples_for 'a user page' do
- it 'has lifetime stats' do
- orders
- refresh # need to refresh after creating the orders for specs that did not require orders
- within('#user-lifetime-stats') do
- [:total_sales, :num_orders, :average_order_value, :member_since].each do |stat_name|
- expect(page).to have_content Spree.t(stat_name)
+ context 'lifetime stats' do
+ shared_examples_for 'has lifetime stats' do
+ it 'has lifetime stats' do
+ within('#user-lifetime-stats') do
+ [:total_sales, :num_orders, :average_order_value, :member_since].each do |stat_name|
+ expect(page).to have_content Spree.t(stat_name)
+ end
+
+ total_sales = "$#{order.total.to_i + order_2.total.to_i}.00 €#{order_eur.total.to_i}.00 £#{order_gbp.total.to_i}.00"
+ total_average = "$#{(order.total.to_i + order_2.total.to_i)/2}.00 €#{order_eur.total.to_i}.00 £#{order_gbp.total.to_i}.00"
+ total_credits = "$#{store_credit_usd.amount.to_i}.00 €#{store_credit_eur.amount.to_i}.00 £#{store_credit_gbp.amount.to_i}.00"
+ expect(page).to have_content("Total Sales: #{total_sales}", normalize_ws: true)
+ expect(page).to have_content("Average Order Value: #{total_average}", normalize_ws: true)
+ expect(page).to have_content("Store Credits: #{total_credits}", normalize_ws: true)
+ expect(page).to have_content("# Orders: #{orders.count}", normalize_ws: true)
+ expect(page).to have_content("Member Since: #{pretty_time(user_a.created_at).gsub(/[[:space:]]+/, ' ')}", normalize_ws: true)
+ end
end
- expect(page).to have_content (order.total + order_2.total)
- expect(page).to have_content orders.count
- expect(page).to have_content (orders.sum(&:total) / orders.count)
- expect(page).to have_content pretty_time(user_a.created_at).gsub(/[[:space:]]+/, ' ')
+ end
+
+ it_behaves_like 'has lifetime stats'
+
+ context 'when other store order exits' do
+ let(:new_store) { create(:store) }
+ let!(:new_order) { create(:completed_order_with_totals, store: new_store, number: 'K999') }
+
+ it { expect(Spree::Order.count).not_to eq(store.orders.count) }
+
+ it_behaves_like 'has lifetime stats'
end
end
diff --git a/core/app/models/concerns/spree/display_money.rb b/core/app/models/concerns/spree/display_money.rb
index 71791dfc12..2741fc79de 100644
--- a/core/app/models/concerns/spree/display_money.rb
+++ b/core/app/models/concerns/spree/display_money.rb
@@ -17,13 +17,17 @@ module Spree
# Decorate a method, but with some additional options
# extend Spree::DisplayMoney
# money_methods tax_amount: { currency: "CAD", no_cents: true }, :price
+ # Use generated method with the same arguments as in wrapped method
+ # def tax_amount(order, country)
+ # display_tax_amount(order, country)
def money_methods(*args)
args.each do |money_method|
money_method = { money_method => {} } unless money_method.is_a? Hash
money_method.each do |method_name, opts|
- define_method("display_#{method_name}") do
+ define_method("display_#{method_name}") do |**args_list|
default_opts = respond_to?(:currency) ? { currency: currency } : {}
- Spree::Money.new(send(method_name), default_opts.merge(opts))
+
+ Spree::Money.new(send(method_name, **args_list), default_opts.merge(opts).merge(args_list.slice(:currency)))
end
end
end
diff --git a/core/app/models/concerns/spree/user_methods.rb b/core/app/models/concerns/spree/user_methods.rb
index 126f3295b1..58796b1646 100644
--- a/core/app/models/concerns/spree/user_methods.rb
+++ b/core/app/models/concerns/spree/user_methods.rb
@@ -58,6 +58,14 @@ module Spree
store_credits.for_store(store).where(currency: currency).reload.to_a.sum(&:amount_remaining)
end
+ def available_store_credits(store)
+ store ||= Store.default
+
+ store_credits.for_store(store).pluck(:currency).uniq.each_with_object([]) do |currency, arr|
+ arr << Spree::Money.new(total_available_store_credit(currency, store), currency: currency)
+ end
+ end
+
private
def check_completed_orders
diff --git a/core/app/models/concerns/spree/user_reporting.rb b/core/app/models/concerns/spree/user_reporting.rb
index 7ad0148e15..a57d6aa681 100644
--- a/core/app/models/concerns/spree/user_reporting.rb
+++ b/core/app/models/concerns/spree/user_reporting.rb
@@ -3,20 +3,45 @@ module Spree
extend DisplayMoney
money_methods :lifetime_value, :average_order_value
- def lifetime_value
- orders.complete.sum(:total)
+ def report_values_for(report_name, store)
+ store ||= Store.default
+
+ completed_orders(store).pluck(:currency).uniq.each_with_object([]) do |currency, arr|
+ arr << send("display_#{report_name}", store: store, currency: currency)
+ end
end
- def order_count
- orders.complete.size
+ def lifetime_value(**args)
+ order_calculate(operation: :sum,
+ column: :total,
+ **args)
end
- def average_order_value
- if order_count > 0
- lifetime_value / order_count
- else
- BigDecimal('0.00')
- end
+ def average_order_value(**args)
+ order_calculate(operation: :average,
+ column: :total,
+ **args)
+ end
+
+ def order_count(store = nil)
+ store ||= Store.default
+ order_calculate(store: store,
+ currency: store.supported_currencies.split(','),
+ operation: :count,
+ column: :all)
+ end
+
+ private
+
+ def order_calculate(operation:, column:, store: nil, currency: nil)
+ store ||= Store.default
+ currency ||= store.default_currency
+
+ completed_orders(store).where(currency: currency).calculate(operation, column) || BigDecimal('0.00')
+ end
+
+ def completed_orders(store)
+ orders.for_store(store).complete
end
end
end
diff --git a/core/spec/models/spree/concerns/display_money_spec.rb b/core/spec/models/spree/concerns/display_money_spec.rb
index 3ba191e2db..08fea74d3c 100644
--- a/core/spec/models/spree/concerns/display_money_spec.rb
+++ b/core/spec/models/spree/concerns/display_money_spec.rb
@@ -2,6 +2,7 @@ require 'spec_helper'
module Spree
describe DisplayMoney do
+ let(:test_instance) { test_class.new }
let(:test_class) do
Class.new do
extend DisplayMoney
@@ -16,7 +17,37 @@ module Spree
context 'currency is not defined' do
it 'generates a display_method that builds a Spree::Money without options' do
- expect(test_class.new.display_total).to eq Spree::Money.new(10.0)
+ expect(test_instance.display_total).to eq Spree::Money.new(10.0)
+ end
+
+ context 'wrapped method accepts `:currency` keyword argument' do
+ let(:test_class) do
+ Class.new do
+ extend DisplayMoney
+ def total(currency:)
+ 10.0
+ end
+ end
+ end
+ let(:currency) { 'GBP' }
+
+ it 'defined method passes the received keyword argument to the wrapped method' do
+ expect(test_class.new.display_total(currency: currency)).to eq Spree::Money.new(10.0, currency: currency)
+ end
+ end
+ context 'wrapped method accepts `currency` argument' do
+ let(:test_class) do
+ Class.new do
+ extend DisplayMoney
+ def total(currency)
+ 10.0
+ end
+ end
+ end
+ let(:currency) { 'GBP' }
+ it 'defined method passes the received keyword argument to the wrapped method' do
+ expect(test_class.new.display_total(currency: currency)).to eq Spree::Money.new(10.0, currency: currency)
+ end
end
end
@@ -30,7 +61,7 @@ module Spree
end
it 'generates a display_* method that builds a Spree::Money with currency' do
- expect(test_class.new.display_total).to eq Spree::Money.new(10.0, currency: 'USD')
+ expect(test_instance.display_total).to eq Spree::Money.new(10.0, currency: 'USD')
end
end
@@ -45,8 +76,8 @@ module Spree
end
it 'generates a display_* method that builds a Spree::Money with the specified options' do
- expect(test_class.new.display_total).to eq Spree::Money.new(10.0)
- expect(test_class.new.display_amount).to eq Spree::Money.new(20.0, no_cents: true)
+ expect(test_instance.display_total).to eq Spree::Money.new(10.0)
+ expect(test_instance.display_amount).to eq Spree::Money.new(20.0, no_cents: true)
end
end
end
diff --git a/core/spec/models/spree/user_spec.rb b/core/spec/models/spree/user_spec.rb
index dad8198d39..bd9e0e0640 100644
--- a/core/spec/models/spree/user_spec.rb
+++ b/core/spec/models/spree/user_spec.rb
@@ -5,11 +5,11 @@ describe Spree::LegacyUser, type: :model do # rubocop:disable RSpec/MultipleDesc
context '#last_incomplete_order' do
let!(:user) { create(:user) }
let!(:order) { create(:order, bill_address: create(:address), ship_address: create(:address)) }
- let(:current_store) { Spree::Store.default }
+ let(:store) { create :store }
- let(:order_1) { create(:order, created_at: 1.day.ago, user: user, created_by: user, store: current_store) }
- let(:order_2) { create(:order, user: user, created_by: user, store: current_store) }
- let(:order_3) { create(:order, user: user, created_by: create(:user), store: current_store) }
+ let(:order_1) { create(:order, created_at: 1.day.ago, user: user, created_by: user, store: store) }
+ let(:order_2) { create(:order, user: user, created_by: user, store: store) }
+ let(:order_3) { create(:order, user: user, created_by: create(:user), store: store) }
it 'returns correct order' do
Timecop.scale(3600) do
@@ -17,7 +17,7 @@ describe Spree::LegacyUser, type: :model do # rubocop:disable RSpec/MultipleDesc
order_2
order_3
- expect(user.last_incomplete_spree_order(current_store)).to eq order_3
+ expect(user.last_incomplete_spree_order(store)).to eq order_3
end
end
@@ -69,74 +69,89 @@ end
describe Spree.user_class, type: :model do
context 'reporting' do
+ let!(:orders) { create_list(:order, order_count, user: subject, store: store, total: order_value, completed_at: Date.today, currency: currency) }
+ let(:currency) { 'USD' }
+ let(:store) { create :store }
let(:order_value) { BigDecimal('80.94') }
let(:order_count) { 4 }
- let(:orders) { Array.new(order_count, double(total: order_value)) }
-
- before do
- allow(orders).to receive(:sum).with(:total).and_return(orders.sum(&:total))
- allow(orders).to receive(:count).and_return(orders.length)
- end
-
- def load_orders
- allow(subject).to receive(:orders).and_return(double(complete: orders))
- end
describe '#lifetime_value' do
context 'with orders' do
- before { load_orders }
-
it 'returns the total of completed orders for the user' do
- expect(subject.lifetime_value).to eq (order_count * order_value)
+ expect(subject.lifetime_value(store: store, currency: currency)).to eq(order_count * order_value)
end
end
context 'without orders' do
+ let(:orders) {}
+
it 'returns 0.00' do
- expect(subject.lifetime_value).to eq BigDecimal('0.00')
+ expect(subject.lifetime_value(store: store, currency: currency)).to eq BigDecimal('0.00')
end
end
end
describe '#display_lifetime_value' do
it 'returns a Spree::Money version of lifetime_value' do
- value = BigDecimal('500.05')
- allow(subject).to receive(:lifetime_value).and_return(value)
- expect(subject.display_lifetime_value).to eq Spree::Money.new(value)
+ expect(subject.display_lifetime_value(store: store, currency: currency).money.fractional).to eq(order_count * order_value * 100)
end
end
describe '#order_count' do
- before { load_orders }
-
it 'returns the count of completed orders for the user' do
- expect(subject.order_count).to eq order_count
+ expect(subject.order_count(store)).to eq order_count
end
end
describe '#average_order_value' do
context 'with orders' do
- before { load_orders }
-
it 'returns the average completed order price for the user' do
- expect(subject.average_order_value).to eq order_value
+ expect(subject.average_order_value(store: store, currency: currency)).to eq order_value
end
end
context 'without orders' do
+ let(:orders) {}
+
it 'returns 0.00' do
- expect(subject.average_order_value).to eq BigDecimal('0.00')
+ expect(subject.average_order_value(store: store, currency: currency)).to eq BigDecimal('0.00')
end
end
end
describe '#display_average_order_value' do
- before { load_orders }
-
it 'returns a Spree::Money version of average_order_value' do
value = BigDecimal('500.05')
allow(subject).to receive(:average_order_value).and_return(value)
- expect(subject.display_average_order_value).to eq Spree::Money.new(value)
+ expect(subject.display_average_order_value(store: store, currency: currency).money.fractional).to eq(value * 100)
+ end
+ end
+
+ describe '#report_values_for' do
+ context 'when order purchases in other currencies exist' do
+ let(:eur_currency) { 'EUR' }
+ let(:eur_order_value) { BigDecimal('12.34') }
+ let(:eur_order_count) { 2 }
+
+ before do
+ create_list(:order, eur_order_count, user: subject, store: store, total: eur_order_value, completed_at: Date.today, currency: eur_currency)
+ end
+
+ context 'lifetime_value' do
+ it 'returns a list of store lifetime values' do
+ expect(subject.report_values_for(:lifetime_value, store)).to eq([Spree::Money.new((order_count * order_value), currency: currency),
+ Spree::Money.new((eur_order_count * eur_order_value), currency: eur_currency)])
+ end
+ end
+
+ context 'average_order_value' do
+ context 'with orders' do
+ it 'returns a list of average completed order prices for the user' do
+ expect(subject.report_values_for(:average_order_value, store)).to eq([Spree::Money.new((order_value), currency: currency),
+ Spree::Money.new((eur_order_value), currency: eur_currency)])
+ end
+ end
+ end
end
end
end
@@ -208,6 +223,49 @@ describe Spree.user_class, type: :model do
end
end
+ describe '#available_store_credits' do
+ let(:store) { create(:store) }
+
+ context 'user does not have any associated store credits' do
+ subject { create(:user) }
+
+ it 'returns empty array' do
+ expect(subject.available_store_credits(store)).to be_empty
+ end
+ end
+
+ context 'user has several associated store credits' do
+ subject { store_credit.user }
+
+ let(:user) { create(:user) }
+ let(:usd_amount) { 120.25 }
+ let(:additional_amount) { 55.75 }
+ let(:store_credit) { create(:store_credit, user: user, amount: usd_amount, amount_used: 0.0, store: store) }
+
+ context 'store credits have never been used' do
+ it 'returns store credit amount' do
+ expect(subject.available_store_credits(store)).to eq([Spree::Money.new(usd_amount, currency: 'USD')])
+ end
+ end
+
+ context 'store credits in different currencies exits' do
+ let(:gbp_amount) { '123.12' }
+ let(:eur_amount) { '321.31' }
+
+ before do
+ create(:store_credit, user: user, amount: gbp_amount, amount_used: 0.0, store: store, currency: 'GBP')
+ create(:store_credit, user: user, amount: eur_amount, amount_used: 0.0, store: store, currency: 'EUR')
+ end
+
+ it 'returns sum of amounts' do
+ expect(subject.available_store_credits(store)).to match_array([Spree::Money.new(usd_amount, currency: 'USD'),
+ Spree::Money.new(gbp_amount, currency: 'GBP'),
+ Spree::Money.new(eur_amount, currency: 'EUR')])
+ end
+ end
+ end
+ end
+
context 'address book' do
let(:address) { create(:address) }
let(:address2) { create(:address) }
@@ -362,7 +420,7 @@ describe Spree.user_class, type: :model do
end
context 'when default ship address is associated to uncompleted order' do
- let!(:uncompleted_order) { create(:order, user: user, ship_address: address, ship_address: address) }
+ let!(:uncompleted_order) { create(:order, user: user, ship_address: address) }
it_should_behave_like 'valid'
end
diff --git a/frontend/app/views/spree/users/show.html.erb b/frontend/app/views/spree/users/show.html.erb
index b121a54140..9fec0545a2 100755
--- a/frontend/app/views/spree/users/show.html.erb
+++ b/frontend/app/views/spree/users/show.html.erb
@@ -13,7 +13,10 @@
<div class="d-lg-inline-block account-page-user-info-item-store-credits">
<p class="my-0 text-uppercase text-center">
- <%= Spree.t(:store_credit_name) %>: <b class="account-page-user-info-item-store-credits-amount"><%= Spree::Money.new(@user.total_available_store_credit(current_currency, current_store), currency: current_currency).to_s.gsub('.00', '') %></b>
+ <%= Spree.t(:store_credit_name) %>
+ <% @user.available_store_credits(current_store).each do |value| %>
+ <br><b class="account-page-user-info-item-store-credits-amount"><%= value %></b>
+ <% end %>
</p>
</div>
</div>