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

- (Object) close(closed_by)



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, Spree.t(:stock_transfer_must_be_receivable))
    false
  end
end

- (Boolean) closed?

Returns:

  • (Boolean)


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

def closed?
  closed_at.present?
end

- (Object) destination_movements



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

- (Object) expected_item_count



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

def expected_item_count
  transfer_items.sum(:expected_quantity)
end

- (Boolean) finalizable?

Returns:

  • (Boolean)


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

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

- (Object) finalize(finalized_by)



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, Spree.t(:stock_transfer_cannot_be_finalized))
    false
  end
end

- (Boolean) finalized?

Returns:

  • (Boolean)


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

def finalized?
  finalized_at.present?
end

- (Boolean) receivable?

Returns:

  • (Boolean)


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

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

- (Object) received_item_count



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

def received_item_count
  transfer_items.sum(:received_quantity)
end

- (Object) ship(tracking_number: self.tracking_number, shipped_at: nil)



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

- (Boolean) shipped?

Returns:

  • (Boolean)


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

def shipped?
  shipped_at.present?
end

- (Object) source_movements



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

- (Object) to_param



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

def to_param
  number
end

- (Object) transfer



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, Spree.t(:not_enough_stock))
  false
end