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

Add amount_remaining for Spree::StoreCreditEvent #1512

Merged
Merged
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
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 @@ -173,7 +173,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
]

def variant_attributes
Expand Down
2 changes: 2 additions & 0 deletions backend/app/views/spree/admin/store_credits/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
<th><%= t('spree.admin.store_credits.amount_credited') %></th>
<th><%= t('spree.admin.store_credits.created_by') %></th>
<th><%= Spree::StoreCreditEvent.human_attribute_name(:user_total_amount) %></th>
<th><%= Spree::StoreCreditEvent.human_attribute_name(:amount_remaining) %></th>
<th><%= Spree::StoreCreditUpdateReason.human_attribute_name(:name) %></th>
</tr>
</thead>
Expand All @@ -96,6 +97,7 @@
<td><%= event.display_amount %></td>
<td><%= store_credit_event_originator_link(event) %></td>
<td><%= event.display_user_total_amount %></td>
<td><%= 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 @@ -246,6 +246,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.available_store_credit_total(currency: currency),
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 @@ -46,6 +46,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
3 changes: 2 additions & 1 deletion core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ en:
memo: Memo
spree/store_credit_event:
action: Action
user_total_amount: Total Unused
amount_remaining: Total Unused
user_total_amount: Total Amount
spree/store_credit_update_reason:
name: Reason For Updating
spree/stock_item:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

class AddAmountRemainingToStoreCreditEvents < ActiveRecord::Migration[5.0]
class StoreCredit < ActiveRecord::Base
self.table_name = 'spree_store_credits'
has_many :store_credit_events

VOID_ACTION = 'void'
CREDIT_ACTION = 'credit'
CAPTURE_ACTION = 'capture'
ELIGIBLE_ACTION = 'eligible'
AUTHORIZE_ACTION = 'authorize'
ALLOCATION_ACTION = 'allocation'
ADJUSTMENT_ACTION = 'adjustment'
INVALIDATE_ACTION = 'invalidate'
end

class StoreCreditEvent < ActiveRecord::Base
self.table_name = "spree_store_credit_events"
belongs_to :store_credit

scope :chronological, -> { order(:created_at) }
end

def up
add_column :spree_store_credit_events, :amount_remaining, :decimal, precision: 8, scale: 2, default: nil, null: true

StoreCredit.includes(:store_credit_events).find_each do |credit|
credit_amount = credit.amount

credit.store_credit_events.chronological.each do |event|
case event.action
when StoreCredit::ALLOCATION_ACTION,
StoreCredit::ELIGIBLE_ACTION,
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).
credit_amount
when StoreCredit::AUTHORIZE_ACTION,
StoreCredit::INVALIDATE_ACTION
# These actions remove the amount from the available credit amount.
credit_amount -= event.amount
when StoreCredit::ADJUSTMENT_ACTION,
StoreCredit::CREDIT_ACTION,
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) { Time.parse("2014-06-01") }

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 @@ -745,7 +745,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 @@ -763,19 +763,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