Class: Spree::ReturnAuthorization

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/spree/return_authorization.rb

Instance Method Summary collapse

Instance Method Details

#add_variant(variant_id, quantity) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/spree/return_authorization.rb', line 33

def add_variant(variant_id, quantity)
  order_units = returnable_inventory.group_by(&:variant_id)
  returned_units = inventory_units.group_by(&:variant_id)
  return false if order_units.empty?

  count = 0

  if returned_units[variant_id].nil? || returned_units[variant_id].size < quantity
    count = returned_units[variant_id].nil? ? 0 : returned_units[variant_id].size

    order_units[variant_id].each do |inventory_unit|
      next unless inventory_unit.return_authorization.nil? && count < quantity

      inventory_unit.return_authorization = self
      inventory_unit.save!

      count += 1
    end
  elsif returned_units[variant_id].size > quantity
    (returned_units[variant_id].size - quantity).times do |i|
      returned_units[variant_id][i].return_authorization_id = nil
      returned_units[variant_id][i].save!
    end
  end

  order.authorize_return! if inventory_units.reload.size > 0 && !order.awaiting_return?
end

#compute_amount(*args) ⇒ Object

Used when Adjustment#update! wants to update the related adjustment



66
67
68
# File 'app/models/spree/return_authorization.rb', line 66

def compute_amount(*args)
  amount.abs * -1
end

#currencyObject



25
26
27
# File 'app/models/spree/return_authorization.rb', line 25

def currency
  order.nil? ? Spree::Config[:currency] : order.currency
end

#display_amountObject



29
30
31
# File 'app/models/spree/return_authorization.rb', line 29

def display_amount
  Spree::Money.new(amount, { currency: currency })
end

#returnable_inventoryObject



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

def returnable_inventory
  order.shipped_shipments.collect{|s| s.inventory_units.to_a}.flatten
end