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

- (true, false) calculate_eligibility

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Calculates based on attached promotion (if this is a promotion adjustment) whether this promotion is still eligible.

Returns:

  • (true, false)

    Whether this adjustment is eligible



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

def calculate_eligibility
  if !finalized? && source && promotion?
    source.promotion.eligible?(adjustable, promotion_code: promotion_code)
  else
    eligible?
  end
end

- (Boolean) cancellation?

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)



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

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

- (Object) currency



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

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

- (Object) finalize



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

def finalize
  update_attributes(finalized: true)
end

- (Object) finalize!



57
58
59
# File 'app/models/spree/adjustment.rb', line 57

def finalize!
  update_attributes!(finalized: true)
end

- (Boolean) promotion?

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)



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

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

- (Boolean) tax?

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)



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

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

- (Object) unfinalize



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

def unfinalize
  update_attributes(finalized: false)
end

- (Object) unfinalize!



61
62
63
# File 'app/models/spree/adjustment.rb', line 61

def unfinalize!
  update_attributes!(finalized: false)
end

- (BigDecimal) update!

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.

Returns:

  • (BigDecimal)

    New amount of this adjustment



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/models/spree/adjustment.rb', line 99

def update!
  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(adjustable)

    if promotion?
      self.eligible = calculate_eligibility
    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