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

#remove_adjustments_from_incomplete_orders

Methods included from CalculatedAdjustments

#calculator_type, #calculator_type=

Methods inherited from Spree::PromotionAction

#to_partial_path

Methods inherited from Base

display_includes

Methods included from Core::Permalinks

#generate_permalink, #save_permalink

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



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

def compute_amount(calculable)
  amount = calculator.compute(calculable)
  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.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/spree/promotion/actions/create_adjustment.rb', line 27

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

#preload_relationsObject



18
19
20
# File 'app/models/spree/promotion/actions/create_adjustment.rb', line 18

def preload_relations
  [:calculator]
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.



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

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