Class: Stock::SourceDest

Inherits:
MovementTarget show all
Includes:
Ohm::Locking
Defined in:
lib/porp/stock/sourcedest.rb

Overview

The StockSourceDest class represents movement targets which create or destroy stock. Class implements a default source/destination issue and receipt behaviour

Direct Known Subclasses

MiscTarget

Instance Method Summary (collapse)

Methods inherited from MovementTarget

inherited, #reload_attributes

Constructor Details

- (SourceDest) initialize(attrs = {})

Set defaults for attributes if not supplied



22
23
24
25
26
# File 'lib/porp/stock/sourcedest.rb', line 22

def initialize(attrs = {})
  super(attrs)
  self.net_stock_qty ||= 0
  self.net_stock_value ||= 0
end

Instance Method Details

- (Object) issue(movement)

Issue stock from this target



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/porp/stock/sourcedest.rb', line 29

def issue(movement)
  # Lock while we update the attributes
  mutex(0.01) do
    self.net_stock_qty = Integer(self.net_stock_qty) - Integer(movement.source_amount.qty)
    self.net_stock_value = Rational(self.net_stock_value) - Rational(movement.source_amount.value)
  end
  self.save

  # Return the quantity issued
  movement.source_amount.qty
end

- (Object) receive(movement)

Receive stock to this target



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/porp/stock/sourcedest.rb', line 55

def receive(movement)
  # Lock while we update the attributes
  # The stock value change is calculated on source qty and source unit
  # to ensure conservation of value. The destination unit cost can be
  # calculated by dividing out the dest qty
  mutex(0.01) do
    self.net_stock_qty = Integer(self.net_stock_qty) + Integer(movement.dest_amount.qty)
    self.net_stock_value = Rational(self.net_stock_value) + Rational(movement.source_amount.value)
  end
  self.save
  
  # Return the quantity received
  movement.dest_amount.qty
end

- (Object) reverse_issue(movement)

Reverse an issue



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/porp/stock/sourcedest.rb', line 42

def reverse_issue(movement)
  # Lock while we update the attributes
  mutex(0.01) do
    self.net_stock_qty = Integer(self.net_stock_qty) + Integer(movement.source_amount.qty)
    self.net_stock_value = Rational(self.net_stock_value) + Rational(movement.source_amount.value)
  end
  self.save

  # Return the quantity issued (negated as we're reversing)
  -movement.source_amount.qty
end