spree_promo 0.60.4 → 0.60.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -47,8 +47,7 @@ class Promotion < ActiveRecord::Base
47
47
 
48
48
  def create_discount(order)
49
49
  return if order.promotion_credit_exists?(self)
50
- if eligible?(order) and amount = calculator.compute(order)
51
- amount = order.item_total if amount > order.item_total
50
+ if eligible?(order) and amount = compute(order)
52
51
  order.promotion_credits.reload.clear unless combine? and order.promotion_credits.all? { |credit| credit.source.combine? }
53
52
  order.update!
54
53
  PromotionCredit.create!({
@@ -60,7 +59,11 @@ class Promotion < ActiveRecord::Base
60
59
  end
61
60
  end
62
61
 
63
-
62
+ def compute(order)
63
+ amount = calculator.compute(order)
64
+ amount = order.item_total if amount > order.item_total
65
+ -amount
66
+ end
64
67
 
65
68
  # Products assigned to all product rules
66
69
  def products
@@ -1,29 +1,9 @@
1
1
  class PromotionCredit < ::Adjustment
2
2
  scope :with_order, :conditions => "order_id IS NOT NULL"
3
3
 
4
- def calculate_adjustment
5
- adjustment_source && calculate_coupon_credit
6
- end
7
-
8
4
  # Checks if credit is still applicable to order
9
5
  # If source of adjustment is credit, it checks if it's still valid
10
6
  def applicable?
11
- adjustment_source && adjustment_source.eligible?(order) && super
12
- end
13
-
14
- # Calculates credit for the coupon.
15
- #
16
- # If coupon amount exceeds the order item_total, credit is adjusted.
17
- #
18
- # Always returns negative non positive.
19
- def calculate_coupon_credit
20
- return 0 if order.line_items.empty?
21
- amount = adjustment_source.calculator.compute(order.line_items).abs
22
- amount = order.item_total if amount > order.item_total
23
- -1 * amount
24
- end
25
-
26
- def total
27
- map(&:amount).sum
7
+ source && source.eligible?(order) && super
28
8
  end
29
- end
9
+ end
data/lib/spree_promo.rb CHANGED
@@ -27,6 +27,14 @@ module SpreePromo
27
27
  attr_accessible :coupon_code
28
28
  attr_accessor :coupon_code
29
29
  before_save :process_coupon_code, :if => "@coupon_code.present?"
30
+
31
+ def finalized?
32
+ self.class.finalized_states.include?(state)
33
+ end
34
+
35
+ def self.finalized_states
36
+ ["complete", "awaiting_return", "returned"]
37
+ end
30
38
 
31
39
  def promotion_credit_exists?(credit)
32
40
  promotion_credits.reload.detect { |c| c.source_id == credit.id }
@@ -47,7 +55,7 @@ module SpreePromo
47
55
  self.payment_total = payments.completed.map(&:amount).sum
48
56
  self.item_total = line_items.map(&:amount).sum
49
57
 
50
- process_automatic_promotions
58
+ process_automatic_promotions unless finalized?
51
59
 
52
60
  if force_adjustment_recalculation
53
61
  applicable_adjustments, adjustments_to_destroy = adjustments.partition{|a| a.applicable?}
@@ -64,7 +72,7 @@ module SpreePromo
64
72
  # recalculate amount
65
73
  self.promotion_credits.each do |credit|
66
74
  if credit.source.eligible?(self)
67
- amount = -credit.source.calculator.compute(self).abs
75
+ amount = credit.source.compute(self)
68
76
  if credit.amount != amount
69
77
  # avoid infinite callbacks
70
78
  PromotionCredit.update_all("amount = #{amount}", { :id => credit.id })
@@ -81,12 +89,11 @@ module SpreePromo
81
89
  new_promotions = eligible_automatic_promotions - current_promotions
82
90
  new_promotions.each do |promotion|
83
91
  next if current_promotions.present? && !promotion.combine?
84
- amount = promotion.calculator.compute(self).abs
85
- amount = item_total if amount > item_total
92
+ amount = credit.source.compute(self)
86
93
  if amount > 0
87
94
  self.promotion_credits.create(
88
95
  :source => promotion,
89
- :amount => -amount,
96
+ :amount => amount,
90
97
  :label => promotion.name
91
98
  )
92
99
  end
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: 231
4
+ hash: 229
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 60
9
- - 4
10
- version: 0.60.4
9
+ - 5
10
+ version: 0.60.5
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-10-21 00:00:00 Z
18
+ date: 2012-03-04 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  version_requirements: &id001 !ruby/object:Gem::Requirement
@@ -23,12 +23,12 @@ dependencies:
23
23
  requirements:
24
24
  - - "="
25
25
  - !ruby/object:Gem::Version
26
- hash: 231
26
+ hash: 229
27
27
  segments:
28
28
  - 0
29
29
  - 60
30
- - 4
31
- version: 0.60.4
30
+ - 5
31
+ version: 0.60.5
32
32
  requirement: *id001
33
33
  type: :runtime
34
34
  prerelease: false