forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport StoreCredit#amount_remaining Solidus PR
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
Showing
7 changed files
with
78 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
core/db/migrate/20161006104057_add_amount_remaining_to_store_credit_events.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters