class Skr::SkuTran

In Stockor, inventory related transactions are not performed directly on the model(s)

Instead a SkuTran is created, and it is responsible for adjusting either the cost or qty of the inventory. By doing so, all inventory changes is logged and can be referred to in order to audit changes.

Attributes

allocate_after_save[RW]
credit_gl_account[RW]
debit_gl_account[RW]
gl_tran_description_text[RW]

Public Instance Methods

ea_qty() click to toggle source

@return [Fixnum] {#qty} expressed in terms of single UOM

# File lib/skr/sku_tran.rb, line 46
def ea_qty
    self.qty * ( self.uom_size || 1 )
end
sku_loc=(sl) click to toggle source

@param sl [SkuLoc] set's the sku loc and also sets {#prior_qty} and {#prior_mac}

Calls superclass method
# File lib/skr/sku_tran.rb, line 39
def sku_loc=(sl)
    super
    self.prior_qty = sl.qty
    self.prior_mac = sl.mac
end

Private Instance Methods

adjust_sku_loc_values() click to toggle source

Adjusts {SkuLoc#qty} by {#ea_qty}

# File lib/skr/sku_tran.rb, line 84
def adjust_sku_loc_values
    sl = self.sku_loc
    Skr::Core.logger.debug "Adj +#{ea_qty} Sku #{sl.sku.code} location #{location.code} " +
                           "from MAC: #{sl.mac} to #{self.mac}, qty: #{sl.qty} += #{ea_qty} #{combined_uom}"
    sl.unlock_fields( :qty, :mac ) do
        sl.mac = self.mac unless self.mac.nan? or self.mac.zero?
        sl.adjust_qty( ea_qty )
        sl.save!
    end
    sl.reload
    sl.allocate_available_qty! if self.allocate_after_save

    Skr::Core.logger.debug "After Adj Qty #{sl.qty}"
end
calculate_mac() click to toggle source

sets {#mac} to the correct amount for the {SkuLoc}. To calculate the MAC, the {SkuLoc#onhand_mac_value} is added to {#cost} and then divided by #{SkuLoc#qty} + {#ea_qty}

# File lib/skr/sku_tran.rb, line 60
def calculate_mac
    new_qty = sku_loc.qty + self.ea_qty
    return true if self.mac.present?
    if new_qty.zero?
        self.mac = BigDecimal.new(0)
    elsif cost
        self.mac = ( sku_loc.onhand_mac_value + cost ) / new_qty
    else
        self.mac = sku_loc.onhand_mac_value
    end
    true
end
create_needed_gl_transaction() click to toggle source

If {#cost} is non-zero, then create a {GlTransaction}

# File lib/skr/sku_tran.rb, line 74
def create_needed_gl_transaction
    Skr::Core.logger.debug "Recording SkuTran in GL, mac is: #{self.mac}, cost = #{cost}"
    return if self.cost.nil? || self.cost.zero?
    GlTransaction.push_or_save(
      owner: self, amount: cost,
      debit: debit_gl_account, credit: credit_gl_account
    )
end
ensure_cost_and_qty_present() click to toggle source
# File lib/skr/sku_tran.rb, line 99
def ensure_cost_and_qty_present
    if ea_qty.zero?
        errors.add( :base, "Transaction has no effect, must change inventory onhand value")
    end
    if cost.present? && cost.nonzero? && debit_gl_account.nil?
        errors.add( :debit_gl_account, "was not specified even though we need to adjust the GL by #{cost}")
    end
end