Class: Spree::StockTransfer

Inherits:
Object
  • Object
show all
Includes:
Metadata, Metafields, NumberAsParam, NumberIdentifier, Webhooks::HasWebhooks
Defined in:
app/models/spree/stock_transfer.rb

Instance Method Summary collapse

Instance Method Details

#destination_movementsObject



31
32
33
# File 'app/models/spree/stock_transfer.rb', line 31

def destination_movements
  find_stock_location_with_location_id(destination_location_id)
end

#receive(destination_location, variants) ⇒ Object

receive inventory from external vendor



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

def receive(destination_location, variants)
  transfer(nil, destination_location, variants)
end

#source_movementsObject



27
28
29
# File 'app/models/spree/stock_transfer.rb', line 27

def source_movements
  find_stock_location_with_location_id(source_location_id)
end

#transfer(source_location, destination_location, variants) ⇒ Object



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

def transfer(source_location, destination_location, variants)
  if variants.nil? || variants.empty?
    errors.add(:base, Spree.t('stock_transfer.errors.must_have_variant'))
    return false
  end

  unless variants_available_in_source_location?(source_location, variants)
    errors.add(:base, Spree.t('stock_transfer.errors.variants_unavailable'))
    return false
  end

  transaction do
    variants.each_pair do |variant, quantity|
      source_location&.unstock(variant, quantity, self, persist: false)
      destination_location.restock(variant, quantity, self, persist: false)

      self.source_location = source_location
      self.destination_location = destination_location
      save!
    end
  end

  true
end