class Skr::IaLine

An Inventory Adjustment Line. Each model contains the {SkuLoc}, cost, UOM, and qty for a {Sku} to adjust

An adjustment starts out in the “pending” state, then when it moves to the “applied” state, each line creates an {SkuTran} record that adjusts the inventory in or out.

Public Instance Methods

adjust_qty!() click to toggle source

Perform the adjustment. Requires adjusting to be unlocked and {#is_applied?} must be false

It creates a {SkuTran} to adjust the inventory, and allocates available qty to the {SoLine}

# File lib/skr/ia_line.rb, line 84
def adjust_qty!
    if ! is_adjusting_unlocked? || is_applied?
        raise "Unable to apply line, either not approved or previously applied"
    end
    set_cost_from_sku_loc
    Core.logger.debug( "Adjusting #{self.qty} #{combined_uom} of #{sku_code} into stock")
    self.build_sku_tran({
        :origin=>self, :qty => self.qty, :sku_loc=>self.sku_loc,
        origin_description: "IA #{self.inventory_adjustment.visible_id}:#{self.sku.code}",
        cost: total,  uom: self.uom,
        allocate_after_save: true,
        debit_gl_account:  self.inventory_adjustment.reason.gl_account,
        credit_gl_account: self.sku.gl_asset_account
    })
    self.sku_tran.save unless self.new_record?
    true
end
ea_qty() click to toggle source

The qty for the line expressed in terms of the single UOM

# File lib/skr/ia_line.rb, line 54
def ea_qty
    self.uom_size * self.qty
end
is_applied?() click to toggle source

@return [Boolean] has the line been applied

# File lib/skr/ia_line.rb, line 44
def is_applied?
    sku_tran.present?
end
is_removing_qty?() click to toggle source

@return [Boolean] is the qty negative?

# File lib/skr/ia_line.rb, line 49
def is_removing_qty?
    qty && qty <=0
end
ledger_cost() click to toggle source

@return [BigDecimal] either the current MAC for the sku's location or the cost that was manually set

# File lib/skr/ia_line.rb, line 65
def ledger_cost
    if cost_was_set? || is_applied?
        self.cost
    else
        self.sku_loc_mac
    end
end
set_cost_from_sku_loc() click to toggle source

copies the cost from the sku_loc to the {#cost} field

# File lib/skr/ia_line.rb, line 74
def set_cost_from_sku_loc
    if ! cost_was_set?
        self.cost = self.sku_loc_mac
    end
    true
end
sku_loc_mac() click to toggle source

@return [BigDecimal] the current moving average cost (mac) on the location, expressed in terms of the UOM

# File lib/skr/ia_line.rb, line 39
def sku_loc_mac
    self.sku_loc ? ( self.sku_loc.mac * self.uom_size ) : 0
end
sku_loc_qty() click to toggle source

@return [Fixnum] The qty available on the location, expressed in terms of the UOM

# File lib/skr/ia_line.rb, line 34
def sku_loc_qty
    ( self.uom_size && self.sku_loc ) ? BigDecimal.new( self.sku_loc.qty ) / self.uom_size : 0
end
total() click to toggle source

@return [BigDecimal] the total value of the line

# File lib/skr/ia_line.rb, line 59
def total
    self.ledger_cost * self.qty
end

Private Instance Methods

ensure_adjustment_isnt_applied() click to toggle source
# File lib/skr/ia_line.rb, line 114
def ensure_adjustment_isnt_applied
    if inventory_adjustment.applied?
        errors.add(:base,'cannot be modified after adjustment is approved and applied')
        return false
    else
        return true
    end
end
ensure_cost_set_properly() click to toggle source

Cost cannot be set if the qty is negative

# File lib/skr/ia_line.rb, line 106
def ensure_cost_set_properly
    if cost_was_set? && is_removing_qty?
        errors.add(:cost,'cannot be set if removing qty')
        return false
    end
    true
end