Class: Spree::Adjustment

Inherits:
Base
  • Object
show all
Extended by:
DisplayMoney
Defined in:
app/models/spree/adjustment.rb

Overview

Adjustments represent a change to the item_total of an Order. Each adjustment has an amount that can be either positive or negative.

Adjustments can be “opened” or “closed”. Once an adjustment is closed, it will not be automatically updated.

Boolean attributes

  1. mandatory

    If this flag is set to true then it means the the charge is required and will not be removed from the order, even if the amount is zero. In other words a record will be created even if the amount is zero. This is useful for representing things such as shipping and tax charges where you may want to make it explicitly clear that no charge was made for such things.

  2. eligible?

    This boolean attributes stores whether this adjustment is currently eligible for its order. Only eligible adjustments count towards the order’s adjustment total. This allows an adjustment to be preserved if it becomes ineligible so it might be reinstated.

Instance Method Summary collapse

Methods included from DisplayMoney

money_methods

Methods inherited from Base

page

Methods included from Preferences::Preferable

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

Instance Method Details

#cancellation?Boolean

Returns true when this is a cancellation adjustment (Cancellation adjustments have a UnitCancel source).

Returns:

  • (Boolean)

    true when this is a cancellation adjustment (Cancellation adjustments have a UnitCancel source)



94
95
96
# File 'app/models/spree/adjustment.rb', line 94

def cancellation?
  source_type == 'Spree::UnitCancel'
end

#closed?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'app/models/spree/adjustment.rb', line 75

def closed?
  state == "closed"
end

#currencyObject



79
80
81
# File 'app/models/spree/adjustment.rb', line 79

def currency
  adjustable ? adjustable.currency : Spree::Config[:currency]
end

#promotion?Boolean

Returns true when this is a promotion adjustment (Promotion adjustments have a PromotionAction source).

Returns:

  • (Boolean)

    true when this is a promotion adjustment (Promotion adjustments have a PromotionAction source)



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

def promotion?
  source_type == 'Spree::PromotionAction'
end

#tax?Boolean

Returns true when this is a tax adjustment (Tax adjustments have a TaxRate source).

Returns:

  • (Boolean)

    true when this is a tax adjustment (Tax adjustments have a TaxRate source)



89
90
91
# File 'app/models/spree/adjustment.rb', line 89

def tax?
  source_type == 'Spree::TaxRate'
end

#update!(target = nil) ⇒ BigDecimal

Recalculate and persist the amount from this adjustment’s source based on the adjustable (Order, Shipment, or LineItem)

If the adjustment has no source (such as when created manually from the admin) or is closed, this is a noop.

Parameters:

Returns:

  • (BigDecimal)

    New amount of this adjustment



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/spree/adjustment.rb', line 106

def update!(target = nil)
  if target
    ActiveSupport::Deprecation.warn("Passing a target to Adjustment#update! is deprecated. The adjustment will use the correct target from it's adjustable association.", caller)
  end
  return amount if closed?

  # If the adjustment has no source, do not attempt to re-calculate the amount.
  # Chances are likely that this was a manually created adjustment in the admin backend.
  if source.present?
    self.amount = source.compute_amount(target || adjustable)

    if promotion?
      self.eligible = source.promotion.eligible?(adjustable, promotion_code: promotion_code)
    end

    # Persist only if changed
    # This is only not a save! to avoid the extra queries to load the order
    # (for validations) and to touch the adjustment.
    update_columns(eligible: eligible, amount: amount, updated_at: Time.now) if changed?
  end
  amount
end