Class: Stock::Entity

Inherits:
Ohm::Model
  • Object
show all
Defined in:
lib/porp/stock/entity.rb

Overview

The StockEntity class represents physical stock. Quantities of StockEntities are represented by StockHoldings.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) const_missing(name)



90
91
92
93
94
95
96
# File 'lib/porp/stock/entity.rb', line 90

def self.const_missing(name)
  begin 
    Stock.const_get(name)
  rescue NameError
    Object.const_get(name)
  end
end

+ (Object) find_by_plu(plu_value)

Find an entity by PLU



23
24
25
26
27
28
29
30
# File 'lib/porp/stock/entity.rb', line 23

def self.find_by_plu(plu_value)
  plu = PLU.lookup(plu_value)
  if !plu.nil?
    self[plu.stock_entity_id]
  else
    nil
  end
end

Instance Method Details

- (Object) holding(attrs)

Look up a holding corresponding to the passed attributes



36
37
38
39
40
41
42
# File 'lib/porp/stock/entity.rb', line 36

def holding(attrs)
  if attrs.kind_of?(Hash)
    holdings.find(name: attrs[:holder].to_s + "_" + attrs[:status].to_s).first
  else
    nil
  end
end

- (Object) lookup_target(target)

Check the supplied target is a MovementTarget. If a hash is passed, attempt to find a corresponding holding on this entity

Parameters:

  • target:

    The target to check / hash to look up



48
49
50
51
52
53
54
55
56
57
# File 'lib/porp/stock/entity.rb', line 48

def lookup_target(target)
  if target.kind_of?(Hash)
    target = holding(target)
  end
  
  unless target.kind_of?(MovementTarget)
    raise Orp::MovementInvalidTarget, "#{target} is not a valid MovementTarget"
  end
  target
end

- (Object) move(source_target, dest_target, qty, ucost)

Move stock represented by this entity

Parameters:

  • source_target: (MovementTarget, Hash)

    The source of the stock to be moved, represented by a MovementTarget. Alternatively, if a Hash is passed, a Holding corresponsing to to the passed attributes will be used as the target.

  • dest_target: (MovementTarget, Hash)

    The destination of stock to be mover, either as a MovementTarget or Hash specifying Holding as above.

  • qty: (Integer)

    The number of units of stock being moved.

  • ucost: (Rational)

    The cost price per unit of stock. This can be overrided by the source_target if the value of stock is already known.

Returns:

  • The completion status of the Movement



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/porp/stock/entity.rb', line 72

def move(source_target, dest_target, qty, ucost)
  # Make sure the supplied target information is valid, looking up holdings
  # as appropriate
  source_target = lookup_target(source_target)
  dest_target = lookup_target(dest_target)

  # Create and conduct movement
  movement = Movement.move_no_cleanup(source_target:    source_target,
                                      source_entity_id: id,
                                      source_amount:    Amount.new(qty, ucost),
                                      dest_target:      dest_target,
                                      dest_entity_id:   id)

  # Return the completion status of the movement
  movements << movement
  movement.completed
end