Class: Spree::Promotion

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/promotion.rb,
app/models/spree/promotion/rules/user.rb,
app/models/spree/promotion/rules/taxon.rb,
app/models/spree/promotion/rules/product.rb,
app/models/spree/promotion/rules/item_total.rb,
app/models/spree/promotion/rules/first_order.rb,
app/models/spree/promotion/rules/option_value.rb,
app/models/spree/promotion/rules/user_logged_in.rb,
app/models/spree/promotion/actions/free_shipping.rb,
app/models/spree/promotion/rules/one_use_per_user.rb,
app/models/spree/promotion/actions/create_adjustment.rb,
app/models/spree/promotion/actions/create_line_items.rb,
app/models/spree/promotion/actions/create_item_adjustments.rb

Defined Under Namespace

Modules: Actions, Rules

Constant Summary collapse

MATCH_POLICIES =
%w(all any)
UNACTIVATABLE_ORDER_STATES =
["complete", "awaiting_return", "returned"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

page

Methods included from Spree::Preferences::Preferable

#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Instance Attribute Details

#eligibility_errorsObject (readonly)

Returns the value of attribute eligibility_errors.



6
7
8
# File 'app/models/spree/promotion.rb', line 6

def eligibility_errors
  @eligibility_errors
end

Class Method Details

.activeObject



45
46
47
48
# File 'app/models/spree/promotion.rb', line 45

def self.active
  where('spree_promotions.starts_at IS NULL OR spree_promotions.starts_at < ?', Time.now).
    where('spree_promotions.expires_at IS NULL OR spree_promotions.expires_at > ?', Time.now)
end

.advertisedObject



37
38
39
# File 'app/models/spree/promotion.rb', line 37

def self.advertised
  where(advertise: true)
end

.order_activatable?(order) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/models/spree/promotion.rb', line 50

def self.order_activatable?(order)
  order && !UNACTIVATABLE_ORDER_STATES.include?(order.state)
end

.with_coupon_code(coupon_code) ⇒ Object



41
42
43
# File 'app/models/spree/promotion.rb', line 41

def self.with_coupon_code(coupon_code)
  where("lower(#{self.table_name}.code) = ?", coupon_code.strip.downcase).first
end

Instance Method Details

#activate(payload) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/spree/promotion.rb', line 58

def activate(payload)
  order = payload[:order]
  return unless self.class.order_activatable?(order)

  payload[:promotion] = self

  # Track results from actions to see if any action has been taken.
  # Actions should return nil/false if no action has been taken.
  # If an action returns true, then an action has been taken.
  results = actions.map do |action|
    action.perform(payload)
  end
  # If an action has been taken, report back to whatever activated this promotion.
  action_taken = results.include?(true)

  if action_taken
  # connect to the order
  # create the join_table entry.
    self.orders << order
    self.save
  end

  return action_taken
end

#adjusted_credits_count(promotable) ⇒ Object



129
130
131
132
# File 'app/models/spree/promotion.rb', line 129

def adjusted_credits_count(promotable)
  adjustments = promotable.is_a?(Order) ? promotable.all_adjustments : promotable.adjustments
  credits_count - adjustments.promotion.where(:source_id => actions.pluck(:id)).count
end

#creditsObject



134
135
136
# File 'app/models/spree/promotion.rb', line 134

def credits
  Adjustment.eligible.promotion.where(source_id: actions.map(&:id))
end

#credits_countObject



138
139
140
# File 'app/models/spree/promotion.rb', line 138

def credits_count
  credits.count
end

#eligible?(promotable) ⇒ Boolean

called anytime order.update! happens

Returns:

  • (Boolean)


84
85
86
87
# File 'app/models/spree/promotion.rb', line 84

def eligible?(promotable)
  return false if expired? || usage_limit_exceeded?(promotable) || blacklisted?(promotable)
  !!eligible_rules(promotable, {})
end

#eligible_rules(promotable, options = {}) ⇒ Object

eligible_rules returns an array of promotion rules where eligible? is true for the promotable if there are no such rules, an empty array is returned if the rules make this promotable ineligible, then nil is returned (i.e. this promotable is not eligible)



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/spree/promotion.rb', line 92

def eligible_rules(promotable, options = {})
  # Promotions without rules are eligible by default.
  return [] if rules.none?
  eligible = lambda { |r| r.eligible?(promotable, options) }
  specific_rules = rules.select { |rule| rule.applicable?(promotable) }
  return [] if specific_rules.none?

  rule_eligibility = Hash[specific_rules.map do |rule|
    [rule, rule.eligible?(promotable, options)]
  end]

  if match_all?
    # If there are rules for this promotion, but no rules for this
    # particular promotable, then the promotion is ineligible by default.
    unless rule_eligibility.values.all?
      @eligibility_errors = specific_rules.map(&:eligibility_errors).detect(&:present?)
      return nil
    end
    specific_rules
  else
    unless rule_eligibility.values.any?
      @eligibility_errors = specific_rules.map(&:eligibility_errors).detect(&:present?)
      return nil
    end

    [rule_eligibility.detect { |_, eligibility| eligibility }.first]
  end
end

#expired?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/models/spree/promotion.rb', line 54

def expired?
  !!(starts_at && Time.now < starts_at || expires_at && Time.now > expires_at)
end

#line_item_actionable?(order, line_item) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/models/spree/promotion.rb', line 142

def line_item_actionable?(order, line_item)
  if eligible? order
    rules = eligible_rules(order)
    if rules.blank?
      true
    else
      rules.send(match_all? ? :all? : :any?) do |rule|
        rule.actionable? line_item
      end
    end
  else
    false
  end
end

#productsObject



121
122
123
# File 'app/models/spree/promotion.rb', line 121

def products
  rules.where(type: "Spree::Promotion::Rules::Product").map(&:products).flatten.uniq
end

#usage_limit_exceeded?(promotable) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'app/models/spree/promotion.rb', line 125

def usage_limit_exceeded?(promotable)
  usage_limit.present? && usage_limit > 0 && adjusted_credits_count(promotable) >= usage_limit
end

#used_by?(user, excluded_orders = []) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/models/spree/promotion.rb', line 157

def used_by?(user, excluded_orders = [])
  [
    :adjustments,
    :line_item_adjustments,
    :shipment_adjustments
  ].any? do |adjustment_type|
    user.orders.complete.joins(adjustment_type).where(
      spree_adjustments: {
        source_type: 'Spree::PromotionAction',
        source_id: actions.map(&:id),
        eligible: true
      }
    ).where.not(
      id: excluded_orders.map(&:id)
    ).any?
  end
end