Skip to content
Browse files

enable expiration of store credits

  • Loading branch information...
1 parent a2d2dad commit a1f5b561e6092f6385c2eb7f1f7225a6a53bc017 kinesbea committed Dec 6, 2011
Showing with 27 additions and 1 deletion.
  1. +18 −1 app/models/user_decorator.rb
  2. +1 −0 lib/spree_store_credits.rb
  3. +8 −0 lib/spree_store_credits_hooks.rb
View
19 app/models/user_decorator.rb
@@ -6,6 +6,23 @@ def has_store_credit?
end
def store_credits_total
- store_credits.sum(:remaining_amount)
+ if store_credits.any? and !Spree::Config[:store_credit_expire_days].blank?
+ result = store_credits.find(:all, :select => 'max(created_at) recent', :conditions => 'remaining_amount > 0')
+ max_date = result[0]['recent']
+ if max_date and (Spree::Config[:store_credit_expire_days].to_i).days.ago.beginning_of_day > max_date
+ 0
+ else
+ store_credits.sum(:remaining_amount)
+ end
+ else
+ store_credits.sum(:remaining_amount)
+ end
+ end
+
+ def store_credits_expiration
+ if store_credits.any? and !Spree::Config[:store_credit_expire_days].blank?
+ result = store_credits.find(:all, :select => 'max(created_at) recent', :conditions => 'remaining_amount > 0')
+ result[0]['recent'] + Spree::Config[:store_credit_expire_days].days
+ end
end
end
View
1 lib/spree_store_credits.rb
@@ -10,6 +10,7 @@ def self.activate
end
AppConfiguration.class_eval do
preference :use_store_credit_minimum, :float, :default => 30.0
+ preference :store_credit_expire_days, :number, :default => 0
end
end
View
8 lib/spree_store_credits_hooks.rb
@@ -8,6 +8,10 @@ class SpreeStoreCreditsHooks < Spree::ThemeSupport::HookListener
<th scope=\"row\"><%= t(\"minimum_order_amount_for_store_credit_use\") %>:</th>
<td><%= Spree::Config[:use_store_credit_minimum] %></td>
</tr>
+<tr>
+ <th scope=\"row\"><%= t(\"number_of_days_store_credit_expires\") %>:</th>
+ <td><%= Spree::Config[:store_credit_expire_days] %></td>
+</tr>
",
:disabled => false)
@@ -19,6 +23,10 @@ class SpreeStoreCreditsHooks < Spree::ThemeSupport::HookListener
<label><%= t(\"minimum_order_amount_for_store_credit_use\") %></label>
<%= text_field_tag('app_configuration[preferred_use_store_credit_minimum]', Spree::Config[:use_store_credit_minimum]) %>
</p>
+ <p>
+ <label><%= t(\"number_of_days_store_credit_expires\") %></label>
+ <%= text_field_tag('app_configuration[preferred_store_credit_expire_days]', Spree::Config[:store_credit_expire_days]) %>
+ </p>
",
:disabled => false)

1 comment on commit a1f5b56

@davidnorth

This is an easier way to get that expiry date:

max_date = store_credits.where('remaining_amount > 0').maximum(:created_at)
Please sign in to comment.
Something went wrong with that request. Please try again.