class Skr::SoLine

Public Instance Methods

allocate_max_available() click to toggle source

allocate the maximum available qty to the line

# File lib/skr/so_line.rb, line 51
def allocate_max_available
    self.qty_allocated = [ 0, [ sku_loc.qty_available+qty_allocated, qty ].min ].max if self.sku.does_track_inventory?
    self
end
cancel!() click to toggle source
# File lib/skr/so_line.rb, line 68
def cancel!
    self.update_attributes :qty_allocated => 0, :qty_picking=> 0
    pt_lines.picking.each{ |ptl| ptl.cancel! }
end
is_fully_allocated?() click to toggle source

A line is fully allocated if the qty_allocated is less than the qty ordered - the qty invoiced - the qty canceled @return [Boolean]

# File lib/skr/so_line.rb, line 58
def is_fully_allocated?
    self.qty_allocated >= qty - qty_invoiced - qty_canceled
end
location=(location) click to toggle source
# File lib/skr/so_line.rb, line 42
def location=(location)
    self.cancel!
    self.sku_loc = self.sku.sku_locs.find_or_create_for( location )
    self.allocate_max_available
    self.save!
    self
end
pickable_qty() click to toggle source

The pickable qty is the qty allocated - the qty already on pick tickets @return [Fixnum]

# File lib/skr/so_line.rb, line 64
def pickable_qty
    qty_allocated - qty_picking
end

Private Instance Methods

ensure_allocation_is_correct() click to toggle source
# File lib/skr/so_line.rb, line 120
def ensure_allocation_is_correct
    return true unless qty_allocated_changed?
    diff = qty_allocated - qty_allocated_was
    if qty_allocated > qty
        errors.add(:qty_allocated, "must be less than qty ordered (#{qty})")
    end
    if diff > 0 && sku_loc && diff > sku_loc.qty_available
        errors.add(:qty_allocated, "new allocation (#{qty_allocated}) - old allocation (#{qty_allocated_was}) can't be more than qty available (#{ sku_loc.qty_available })")
        return false
    end
    true
end
ensure_deleteable() click to toggle source
# File lib/skr/so_line.rb, line 142
def ensure_deleteable
    if qty_allocated > 0
        errors.add(:base,'Cannot delete line when allocated')
        return false
    end
    if qty_invoiced > 0
        errors.add(:base,"Cannot delete line after it's shipped")
        return false
    end
    true
end
ensure_so_is_open() click to toggle source
# File lib/skr/so_line.rb, line 134
def ensure_so_is_open
    unless self.sales_order.open?
        errors.add(:base,"Cannot add item #{self.sku_code} to non-open Sales Order")
        return false
    end
    true
end
fire_after_save_events() click to toggle source
# File lib/skr/so_line.rb, line 84
def fire_after_save_events
    %w{ allocated picking invoiced canceled }.each do | event |
        if changes[ "qty_#{event}" ]
            fire_event( :qty_change )
            break
        end
    end
    super
end
set_defaults_from_associations() click to toggle source
# File lib/skr/so_line.rb, line 94
def set_defaults_from_associations
    self.uom         = sku.uoms.default if self.uom_code.blank?
    self.description = sku.description if self.description.blank?
    self.sku_code    = sku.code        if self.sku_code.blank?
    if !price && sales_order && sales_order.customer && sku_loc && uom.present?
        self.price = Core.config.pricing_provider.price(sku_loc:sku_loc, customer:sales_order.customer, uom:uom, qty:qty)
    end
    true
end
setup_new_inv_line( line ) click to toggle source
# File lib/skr/so_line.rb, line 104
def setup_new_inv_line( line )
    line.qty = self.sku.is_other_charge? ? self.qty : self.qty_allocated
    setup_new_line(line)
end
setup_new_line(line) click to toggle source
# File lib/skr/so_line.rb, line 113
def setup_new_line(line)
    line.price    = self.price
    line.sku_loc  = self.sku_loc
    line.uom      = self.uom
    true
end
setup_new_pt_line( line ) click to toggle source
# File lib/skr/so_line.rb, line 108
def setup_new_pt_line( line )
    line.qty = self.sku.is_other_charge? ? self.qty : self.pickable_qty
    setup_new_line(line)
end
update_qty_picking( pt=nil ) click to toggle source
# File lib/skr/so_line.rb, line 79
def update_qty_picking( pt=nil )
    update_attributes( :qty_picking=> pt_lines.ea_picking_qty/uom_size )
end
update_qty_shipped() click to toggle source
# File lib/skr/so_line.rb, line 75
def update_qty_shipped
    inv_qty = self.inv_lines.sum(:qty)
    update_attributes( :qty_invoiced=> inv_qty, :qty_allocated => [ qty_allocated - inv_qty, 0 ].max )
end