Class: Spree::StockTransfer

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

Defined Under Namespace

Classes: InvalidTransferMovement

Instance Method Summary collapse

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

#close(closed_by) ⇒ Object



92
93
94
95
96
97
98
99
# File 'app/models/spree/stock_transfer.rb', line 92

def close(closed_by)
  if receivable?
    update_attributes({ closed_at: Time.current, closed_by: closed_by })
  else
    errors.add(:base, I18n.t('spree.stock_transfer_must_be_receivable'))
    false
  end
end

#closed?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/models/spree/stock_transfer.rb', line 33

def closed?
  closed_at.present?
end

#destination_movementsObject



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

def destination_movements
  stock_movements.joins(:stock_item)
    .where('spree_stock_items.stock_location_id' => destination_location_id)
end

#expected_item_countObject



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

def expected_item_count
  transfer_items.sum(:expected_quantity)
end

#finalizable?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/models/spree/stock_transfer.rb', line 41

def finalizable?
  !finalized? && !shipped? && !closed?
end

#finalize(finalized_by) ⇒ Object



71
72
73
74
75
76
77
78
# File 'app/models/spree/stock_transfer.rb', line 71

def finalize(finalized_by)
  if finalizable?
    update_attributes({ finalized_at: Time.current, finalized_by: finalized_by })
  else
    errors.add(:base, I18n.t('spree.stock_transfer_cannot_be_finalized'))
    false
  end
end

#finalized?Boolean

Returns:

  • (Boolean)


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

def finalized?
  finalized_at.present?
end

#receivable?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/models/spree/stock_transfer.rb', line 45

def receivable?
  finalized? && shipped? && !closed?
end

#received_item_countObject



53
54
55
# File 'app/models/spree/stock_transfer.rb', line 53

def received_item_count
  transfer_items.sum(:received_quantity)
end

#ship(tracking_number: self.tracking_number, shipped_at: nil) ⇒ Object



49
50
51
# File 'app/models/spree/stock_transfer.rb', line 49

def ship(tracking_number: self.tracking_number, shipped_at: nil)
  update_attributes!(tracking_number: tracking_number, shipped_at: shipped_at)
end

#shipped?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/models/spree/stock_transfer.rb', line 37

def shipped?
  shipped_at.present?
end

#source_movementsObject



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

def source_movements
  stock_movements.joins(:stock_item)
    .where('spree_stock_items.stock_location_id' => source_location_id)
end

#to_paramObject



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

def to_param
  number
end

#transferObject



80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/spree/stock_transfer.rb', line 80

def transfer
  transaction do
    transfer_items.each do |item|
      raise InvalidTransferMovement unless item.valid?
      source_location.unstock(item.variant, item.expected_quantity, self)
    end
  end
rescue InvalidTransferMovement
  errors.add(:base, I18n.t('spree.not_enough_stock'))
  false
end