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. 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

display_includes, #initialize_preference_defaults, page, preference

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)



145
146
147
# File 'app/models/spree/adjustment.rb', line 145

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

#closeObject



119
120
121
122
# File 'app/models/spree/adjustment.rb', line 119

def close
  Spree::Deprecation.warn "Adjustment#close is deprecated. Instead use Adjustment#finalize", caller
  finalize
end

#close!Object



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

def close!
  Spree::Deprecation.warn "Adjustment#close! is deprecated. Instead use Adjustment#finalize!", caller
  finalize!
end

#closed?Boolean

Returns:

  • (Boolean)


104
105
106
107
# File 'app/models/spree/adjustment.rb', line 104

def closed?
  Spree::Deprecation.warn "Adjustment#closed? is deprecated. Instead use Adjustment#finalized?", caller
  finalized?
end

#currencyObject

End deprecated methods



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

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

#finalizeObject



73
74
75
# File 'app/models/spree/adjustment.rb', line 73

def finalize
  update_attributes(finalized: true)
end

#finalize!Object



65
66
67
# File 'app/models/spree/adjustment.rb', line 65

def finalize!
  update_attributes!(finalized: true)
end

#openObject



109
110
111
112
# File 'app/models/spree/adjustment.rb', line 109

def open
  Spree::Deprecation.warn "Adjustment#open is deprecated. Instead use Adjustment#unfinalize", caller
  unfinalize
end

#open!Object



114
115
116
117
# File 'app/models/spree/adjustment.rb', line 114

def open!
  Spree::Deprecation.warn "Adjustment#open! is deprecated. Instead use Adjustment#unfinalize!", caller
  unfinalize!
end

#open?Boolean

Returns:

  • (Boolean)


99
100
101
102
# File 'app/models/spree/adjustment.rb', line 99

def open?
  Spree::Deprecation.warn "Adjustment#open? is deprecated. Instead use Adjustment#finalized?", caller
  !closed?
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)



135
136
137
# File 'app/models/spree/adjustment.rb', line 135

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

#stateObject

Deprecated methods



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

def state
  Spree::Deprecation.warn "Adjustment#state is deprecated. Instead use Adjustment#finalized?", caller
  finalized? ? "closed" : "open"
end

#state=(new_state) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'app/models/spree/adjustment.rb', line 87

def state=(new_state)
  Spree::Deprecation.warn "Adjustment#state= is deprecated. Instead use Adjustment#finalized=", caller
  case new_state
  when "open"
    self.finalized = false
  when "closed"
    self.finalized = true
  else
    raise "invaliid adjustment state #{new_state}"
  end
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)



140
141
142
# File 'app/models/spree/adjustment.rb', line 140

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

#unfinalizeObject



77
78
79
# File 'app/models/spree/adjustment.rb', line 77

def unfinalize
  update_attributes(finalized: false)
end

#unfinalize!Object



69
70
71
# File 'app/models/spree/adjustment.rb', line 69

def unfinalize!
  update_attributes!(finalized: false)
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



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

def update!(target = nil)
  if target
    Spree::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 finalized?

  # 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.current) if changed?
  end
  amount
end