Skip to content

Commit

Permalink
Backport StoreCredit#amount_remaining Solidus PR
Browse files Browse the repository at this point in the history
This commit backports work from an open [Solidus PR][1]

[1]: solidusio#1512

This migration adds the `amount_remaining` column to the
`spree_store_credit_events` table, and iterates over existing Store
Credit Events to calculate historical data for this column. This value
is also displayed in the admin, instead of the User's total remaining
store credit.
  • Loading branch information
stewart committed Oct 26, 2016
1 parent d1cfed4 commit 66f340b
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 7 deletions.
2 changes: 1 addition & 1 deletion api/app/helpers/spree/api/api_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def required_fields_for(model)

@@store_credit_history_attributes = [
:display_amount, :display_user_total_amount, :display_action,
:display_event_date
:display_event_date, :display_remaining_amount
]

@@stock_transfer_attributes = [:id, :number]
Expand Down
2 changes: 1 addition & 1 deletion backend/app/views/spree/admin/store_credits/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
<td><%= store_credit_event_admin_action_name(event) %></td>
<td class='align-right'><%= event.display_amount %></td>
<td><%= store_credit_event_originator_link(event) %></td>
<td class='align-right'><%= event.display_user_total_amount %></td>
<td class='align-right'><%= event.display_remaining_amount %></td>
<td><%= event.update_reason.try!(:name) %></td>
</tr>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions core/app/models/spree/store_credit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ def store_event
event.update_attributes!({
amount: action_amount || amount,
authorization_code: action_authorization_code || event.authorization_code || generate_authorization_code,
amount_remaining: amount_remaining,
user_total_amount: user.total_available_store_credit,
originator: action_originator,
update_reason: update_reason,
Expand Down
4 changes: 4 additions & 0 deletions core/app/models/spree/store_credit_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def display_user_total_amount
Spree::Money.new(user_total_amount, { currency: currency })
end

def display_remaining_amount
Spree::Money.new(amount_remaining, { currency: currency })
end

def display_event_date
I18n.l(created_at.to_date, format: :long)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class AddAmountRemainingToStoreCreditEvents < ActiveRecord::Migration
def up
add_column :spree_store_credit_events, :amount_remaining, :decimal, precision: 8, scale: 2, default: nil, null: true

Spree::StoreCredit.all.each do |credit|
credit_amount = credit.amount

credit.store_credit_events.chronological.each do |event|
case event.action
when Spree::StoreCredit::ALLOCATION_ACTION,
Spree::StoreCredit::ELIGIBLE_ACTION,
Spree::StoreCredit::CAPTURE_ACTION
# These actions do not change the amount_remaining so the previous
# amount available is used (either the credit's amount or the
# amount_remaining coming from the event right before this one).
when Spree::StoreCredit::AUTHORIZE_ACTION,
Spree::StoreCredit::INVALIDATE_ACTION
# These actions remove the amount from the available credit amount.
credit_amount -= event.amount
when Spree::StoreCredit::ADJUSTMENT_ACTION,
Spree::StoreCredit::CREDIT_ACTION,
Spree::StoreCredit::VOID_ACTION
# These actions add the amount to the available credit amount. For
# ADJUSTMENT_ACTION the event's amount could be negative (so it could
# end up subtracting the amount).
credit_amount += event.amount
end

event.update_attribute(:amount_remaining, credit_amount)
end
end
end

def down
remove_column :spree_store_credit_events, :amount_remaining
end
end
14 changes: 14 additions & 0 deletions core/spec/models/spree/store_credit_event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,20 @@
end
end

describe "#display_remaining_amount" do
let(:amount_remaining) { 300.0 }

subject { create(:store_credit_auth_event, amount_remaining: amount_remaining) }

it "returns a Spree::Money instance" do
expect(subject.display_remaining_amount).to be_instance_of(Spree::Money)
end

it "uses the events amount_remaining attribute" do
expect(subject.display_remaining_amount).to eq Spree::Money.new(amount_remaining, { currency: subject.currency })
end
end

describe "#display_event_date" do
let(:date) { DateTime.new(2014, 06, 1) }

Expand Down
25 changes: 20 additions & 5 deletions core/spec/models/spree/store_credit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@
end
end

describe "#store_events" do
describe "#store_event" do
context "create" do
context "user has one store credit" do
let(:store_credit_amount) { 100.0 }
Expand All @@ -754,19 +754,34 @@
it "saves the user's total store credit in the event" do
expect(subject.store_credit_events.first.user_total_amount).to eq store_credit_amount
end

it "saves the user's unused store credit in the event" do
expect(subject.store_credit_events.first.amount_remaining).to eq store_credit_amount
end
end

context "user has multiple store credits" do
let(:store_credit_amount) { 100.0 }
let(:additional_store_credit_amount) { 200.0 }

let(:user) { create(:user) }
let!(:store_credit) { create(:store_credit, user: user, amount: store_credit_amount) }

subject { create(:store_credit, user: user, amount: additional_store_credit_amount) }
let!(:store_credits) do
[
create(:store_credit, user: user, amount: store_credit_amount),
create(:store_credit, user: user, amount: additional_store_credit_amount)
]
end

subject { store_credits.flat_map(&:store_credit_events) }

it "saves the user's total store credit in the event" do
expect(subject.store_credit_events.first.user_total_amount).to eq (store_credit_amount + additional_store_credit_amount)
expect(subject.first.user_total_amount).to eq store_credit_amount
expect(subject.last.user_total_amount).to eq(store_credit_amount + additional_store_credit_amount)
end

it "saves the user's unused store credit in the event" do
expect(subject.first.amount_remaining).to eq store_credit_amount
expect(subject.last.amount_remaining).to eq additional_store_credit_amount
end
end

Expand Down

0 comments on commit 66f340b

Please sign in to comment.