Class: Spree::Promotion::Actions::CreateAdjustment

Inherits:
Spree::PromotionAction show all
Includes:
AdjustmentSource, CalculatedAdjustments
Defined in:
app/models/spree/promotion/actions/create_adjustment.rb

Instance Method Summary collapse

Methods included from AdjustmentSource

#deals_with_adjustments_for_deleted_source, #remove_adjustments_from_incomplete_orders

Methods included from CalculatedAdjustments

#calculator_type, #calculator_type=

Methods inherited from Spree::PromotionAction

#to_partial_path

Methods included from Spree::ParanoiaDeprecations

#paranoia_delete, #paranoia_destroy

Methods inherited from Base

display_includes, #initialize_preference_defaults, page, preference

Methods included from Spree::Preferences::Preferable

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

Instance Method Details

#compute_amount(calculable) ⇒ Object

Ensure a negative amount which does not exceed the sum of the order’s item_total and ship_total



40
41
42
43
44
45
46
47
48
# File 'app/models/spree/promotion/actions/create_adjustment.rb', line 40

def compute_amount(calculable)
  amount = calculator.compute(calculable)
  if !amount.is_a?(BigDecimal)
    Spree::Deprecation.warn "#{calculator.class.name}#compute returned #{amount.inspect}, it should return a BigDecimal"
  end
  amount ||= BigDecimal(0)
  amount = amount.abs
  [(calculable.item_total + calculable.ship_total), amount].min * -1
end

#perform(options = {}) ⇒ Object

Creates the adjustment related to a promotion for the order passed through options hash

Returns ‘true` if an adjustment is applied to an order, `false` if the promotion has already been applied.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/spree/promotion/actions/create_adjustment.rb', line 23

def perform(options = {})
  order = options[:order]
  return if promotion_credit_exists?(order)

  amount = compute_amount(order)
  order.adjustments.create!(
    amount: amount,
    order: order,
    source: self,
    promotion_code: options[:promotion_code],
    label: I18n.t('spree.adjustment_labels.order', promotion: Spree::Promotion.model_name.human, promotion_name: promotion.name)
  )
  true
end

#remove_from(order) ⇒ void

This method returns an undefined value.

Removes any adjustments generated by this action from the order.

Parameters:

  • order (Spree::Order)

    the order to remove the action from.



53
54
55
56
57
58
59
# File 'app/models/spree/promotion/actions/create_adjustment.rb', line 53

def remove_from(order)
  order.adjustments.each do |adjustment|
    if adjustment.source == self
      order.adjustments.destroy(adjustment)
    end
  end
end