Class: Spree::ReturnItem

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

Defined Under Namespace

Modules: ExchangeVariantEligibility

Constant Summary

INTERMEDIATE_RECEPTION_STATUSES =
given_to_customer lost_in_transit shipped_wrong_item short_shipped in_transit
COMPLETED_RECEPTION_STATUSES =
INTERMEDIATE_RECEPTION_STATUSES + [:received]

Class Attribute Summary (collapse)

Class Method Summary (collapse)

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

Class Attribute Details

+ (Class) exchange_variant_engine

Configurable engine for determining which variants can be exchanged for a given variant.

Returns:

  • (Class)


19
# File 'app/models/spree/return_item.rb', line 19

class_attribute :exchange_variant_engine

+ (Class) refund_amount_calculator

Configurable calculator for determining the amount ro refund when refunding.

Returns:

  • (Class)


27
# File 'app/models/spree/return_item.rb', line 27

class_attribute :refund_amount_calculator

+ (Class) return_eligibility_validator

Configurable validator for determining whether given return item is eligible for return.

Returns:

  • (Class)


11
# File 'app/models/spree/return_item.rb', line 11

class_attribute :return_eligibility_validator

Class Method Details

+ (Spree::ReturnItem) from_inventory_unit(inventory_unit)

Returns a valid return item for the given inventory unit if one exists, or a new one if one does not

Parameters:

Returns:

  • (Spree::ReturnItem)

    a valid return item for the given inventory unit if one exists, or a new one if one does not



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

def self.from_inventory_unit(inventory_unit)
  valid.find_by(inventory_unit: inventory_unit) ||
    new(inventory_unit: inventory_unit).tap(&:set_default_amount)
end

Instance Method Details

- (Object) build_exchange_inventory_unit

Builds the exchange inventory unit for this return item, only if an exchange is required, correctly associating the variant, line item and order.



178
179
180
181
182
183
184
185
# File 'app/models/spree/return_item.rb', line 178

def build_exchange_inventory_unit
  # The inventory unit needs to have the new variant
  # but it also needs to know the original line item
  # for pricing information for if the inventory unit is
  # ever returned. This means that the inventory unit's line_item
  # will have a different variant than the inventory unit itself
  super(variant: exchange_variant, line_item: inventory_unit.line_item, order: inventory_unit.order) if exchange_required?
end

- (ActiveRecord::Relation<Spree::Variant>) eligible_exchange_variants(stock_locations = nil)

Note:

This uses the exchange_variant_engine configured on the class.

Returns the variants eligible for exchange for this return item

Parameters:

Returns:

  • (ActiveRecord::Relation<Spree::Variant>)

    the variants eligible for exchange for this return item



171
172
173
# File 'app/models/spree/return_item.rb', line 171

def eligible_exchange_variants(stock_locations = nil)
  exchange_variant_engine.eligible_variants(variant, stock_locations: stock_locations)
end

- (Boolean) exchange_processed?

True when an exchange has been processed for this return item

Returns:

  • (Boolean)

    true when an exchange has been processed for this return item



147
148
149
# File 'app/models/spree/return_item.rb', line 147

def exchange_processed?
  exchange_inventory_unit.present?
end

- (Boolean) exchange_requested?

Returns true when an exchange has been requested on this return item

Returns:

  • (Boolean)

    true when an exchange has been requested on this return item



141
142
143
# File 'app/models/spree/return_item.rb', line 141

def exchange_requested?
  exchange_variant.present?
end

- (Boolean) exchange_required?

Returns true when an exchange has been requested but has yet to be processed

Returns:

  • (Boolean)

    true when an exchange has been requested but has yet to be processed



153
154
155
# File 'app/models/spree/return_item.rb', line 153

def exchange_required?
  exchange_requested? && !exchange_processed?
end

- (Spree::Shipment?) exchange_shipment

Returns the exchange inventory unit's shipment if it exists

Returns:

  • (Spree::Shipment, nil)

    the exchange inventory unit's shipment if it exists



188
189
190
# File 'app/models/spree/return_item.rb', line 188

def exchange_shipment
  exchange_inventory_unit.try(:shipment)
end

- (Boolean) part_of_exchange?

Returns:

  • (Boolean)


213
214
215
216
217
# File 'app/models/spree/return_item.rb', line 213

def part_of_exchange?
  # test whether this ReturnItem was either a) one for which an exchange was sent or
  #   b) the exchanged item itself being returned in lieu of the original item
  exchange_requested? || sibling_intended_for_exchange('unexchanged')
end

- (Object) potential_reception_transitions



200
201
202
203
204
205
206
207
208
209
210
211
# File 'app/models/spree/return_item.rb', line 200

def potential_reception_transitions
  status_paths = reception_status_paths.to_states
  event_paths = reception_status_paths.events
  status_paths.delete(:cancelled)
  status_paths.delete(:expired)
  status_paths.delete(:unexchanged)
  event_paths.delete(:cancel)
  event_paths.delete(:expired)
  event_paths.delete(:unexchange)

  status_paths.map{ |s| s.to_s.humanize }.zip(event_paths)
end

- (BigDecimal) pre_tax_amount

Returns the cost of the item before tax

Returns:

  • (BigDecimal)

    the cost of the item before tax



163
164
165
# File 'app/models/spree/return_item.rb', line 163

def pre_tax_amount
  amount - included_tax_total
end

- (Boolean) reception_completed?

Returns true when this retur item is in a complete reception state

Returns:

  • (Boolean)

    true when this retur item is in a complete reception state



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

def reception_completed?
  COMPLETED_RECEPTION_STATUSES.map(&:to_s).include?(reception_status.to_s)
end

- (Object) set_default_amount

Note:

This uses the configured refund_amount_calculator configured on the class.

Calculates and sets the default amount to be refunded.



196
197
198
# File 'app/models/spree/return_item.rb', line 196

def set_default_amount
  self.amount = refund_amount_calculator.new.compute(self)
end

- (BigDecimal) total

Returns the cost of the item after tax

Returns:

  • (BigDecimal)

    the cost of the item after tax



158
159
160
# File 'app/models/spree/return_item.rb', line 158

def total
  amount + additional_tax_total
end