spree_promo 0.50.0 → 0.50.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  class Promotion::Rules::FirstOrder < PromotionRule
2
2
 
3
3
  def eligible?(order)
4
- order.user && order.user.orders.checkout_complete.count == 0
4
+ order.user && order.user.orders.complete.count == 0
5
5
  end
6
6
 
7
7
  end
@@ -7,7 +7,7 @@ class Promotion < ActiveRecord::Base
7
7
  accepts_nested_attributes_for :promotion_rules
8
8
  alias_method :rules, :promotion_rules
9
9
 
10
- validates :name, :code, :presence => true
10
+ validates :name, :presence => true
11
11
 
12
12
  # TODO: Remove that after fix for https://rails.lighthouseapp.com/projects/8994/tickets/4329-has_many-through-association-does-not-link-models-on-association-save
13
13
  # is provided
@@ -20,6 +20,7 @@ class Promotion < ActiveRecord::Base
20
20
  MATCH_POLICIES = %w(all any)
21
21
 
22
22
  scope :automatic, where("code IS NULL OR code = ''")
23
+ scope :manual, where("code IS NOT NULL AND code <> ''")
23
24
 
24
25
  def eligible?(order)
25
26
  !expired? && rules_are_eligible?(order)
@@ -1,5 +1,4 @@
1
1
  class PromotionCredit < ::Adjustment
2
- belongs_to :order
3
2
  scope :with_order, :conditions => "order_id IS NOT NULL"
4
3
 
5
4
  def calculate_adjustment
@@ -4,7 +4,7 @@
4
4
  </p>
5
5
  <p class="field products_rule_product_group">
6
6
  <label><%= t('product_group') %><br />
7
- <%= select_tag "#{param_prefix}[product_group_id]", '<option></option>' + options_from_collection_for_select(ProductGroup.all, :id, :name, promotion_rule.product_group_id) %></label>
7
+ <%= select_tag "#{param_prefix}[product_group_id]", options_from_collection_for_select(ProductGroup.all, :id, :name, promotion_rule.product_group_id) %></label>
8
8
  </p>
9
9
  <p class="field products_rule_products">
10
10
  <label>
@@ -1,4 +1,6 @@
1
+ <% if Promotion.manual.count > 0 %>
1
2
  <p>
2
3
  <%= form.label :coupon_code %><br />
3
4
  <%= form.text_field :coupon_code %>
4
5
  </p>
6
+ <% end %>
@@ -1,5 +1,14 @@
1
1
  ---
2
2
  en:
3
+ activerecord:
4
+ attributes:
5
+ promotion:
6
+ name: "Name"
7
+ description: "Description"
8
+ code: "Code"
9
+ usage_limit: "Usage limit"
10
+ starts_at: "Starts at"
11
+ expires_at: "Expires at"
3
12
  add_rule_of_type: Add rule of type
4
13
  coupon: Coupon
5
14
  coupon_code: Coupon code
data/lib/spree_promo.rb CHANGED
@@ -22,11 +22,11 @@ module SpreePromo
22
22
 
23
23
  Order.class_eval do
24
24
 
25
- has_many :promotion_credits, :conditions => "source_type='Promotion'"
25
+ has_many :promotion_credits, :conditions => "source_type='Promotion'", :dependent => :destroy
26
26
 
27
27
  attr_accessible :coupon_code
28
28
  attr_accessor :coupon_code
29
- before_save :process_coupon_code, :if => "@coupon_code"
29
+ before_save :process_coupon_code, :if => "@coupon_code.present?"
30
30
 
31
31
  def promotion_credit_exists?(credit)
32
32
  promotion_credits.reload.detect { |c| c.source_id == credit.id }
@@ -61,19 +61,36 @@ module SpreePromo
61
61
 
62
62
 
63
63
  def process_automatic_promotions
64
- #promotion_credits.reload.clear
65
- eligible_automatic_promotions.each do |coupon|
66
- # can't use coupon.create_discount as it re-saves the order causing an infinite loop
67
- if amount = coupon.calculator.compute(line_items)
68
- amount = item_total if amount > item_total
69
- promotion_credits.reload.clear unless coupon.combine? and promotion_credits.all? { |credit| credit.adjustment_source.combine? }
70
- promotion_credits.create!({
71
- :source => coupon,
72
- :amount => -amount.abs,
73
- :label => coupon.description
74
- })
64
+ # recalculate amount
65
+ self.promotion_credits.each do |credit|
66
+ if credit.source.eligible?(self)
67
+ amount = -credit.source.calculator.compute(self).abs
68
+ if credit.amount != amount
69
+ # avoid infinite callbacks
70
+ PromotionCredit.update_all("amount = #{amount}", { :id => credit.id })
71
+ end
72
+ else
73
+ credit.destroy
75
74
  end
76
- end.compact
75
+ end
76
+
77
+ current_promotions = self.promotion_credits.map(&:source)
78
+ # return if current promotions can not be combined
79
+ return if current_promotions.any? { |promotion| !promotion.combine? }
80
+
81
+ new_promotions = eligible_automatic_promotions - current_promotions
82
+ new_promotions.each do |promotion|
83
+ next if current_promotions.present? && !promotion.combine?
84
+ amount = promotion.calculator.compute(self).abs
85
+ amount = item_total if amount > item_total
86
+ if amount > 0
87
+ self.promotion_credits.create(
88
+ :source => promotion,
89
+ :amount => -amount,
90
+ :label => promotion.name
91
+ )
92
+ end
93
+ end
77
94
  end
78
95
 
79
96
  def eligible_automatic_promotions
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spree_promo
3
3
  version: !ruby/object:Gem::Version
4
- hash: 215
5
- prerelease: false
4
+ hash: 213
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 50
9
- - 0
10
- version: 0.50.0
9
+ - 1
10
+ version: 0.50.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - David North
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-23 00:00:00 -04:00
18
+ date: 2011-04-19 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -26,12 +26,12 @@ dependencies:
26
26
  requirements:
27
27
  - - "="
28
28
  - !ruby/object:Gem::Version
29
- hash: 215
29
+ hash: 213
30
30
  segments:
31
31
  - 0
32
32
  - 50
33
- - 0
34
- version: 0.50.0
33
+ - 1
34
+ version: 0.50.1
35
35
  type: :runtime
36
36
  version_requirements: *id001
37
37
  description: Required dependancy for Spree
@@ -116,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  requirements:
117
117
  - none
118
118
  rubyforge_project: spree_promo
119
- rubygems_version: 1.3.7
119
+ rubygems_version: 1.4.2
120
120
  signing_key:
121
121
  specification_version: 3
122
122
  summary: Promotion functionality for use with Spree.