class Skr::SoLine

Public Instance Methods

allocate_max_available() click to toggle source

allocate the maximum available qty to the line

# File lib/skr/models/so_line.rb, line 54
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/models/so_line.rb, line 71
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/models/so_line.rb, line 61
def is_fully_allocated?
    self.qty_allocated >= qty - qty_invoiced - qty_canceled
end
location=(location) click to toggle source
# File lib/skr/models/so_line.rb, line 45
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/models/so_line.rb, line 67
def pickable_qty
    qty_allocated - qty_picking
end
total() click to toggle source
# File lib/skr/models/so_line.rb, line 76
def total
    qty * price
end

Private Instance Methods

ensure_allocation_is_correct() click to toggle source
# File lib/skr/models/so_line.rb, line 130
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/models/so_line.rb, line 152
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/models/so_line.rb, line 144
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
Calls superclass method
# File lib/skr/models/so_line.rb, line 92
def fire_after_save_events
    %w{ allocated picking invoiced canceled }.each do | event |
        if changes[ "qty_#{event}" ]
            fire_pubsub_event( :qty_change )
            break
        end
    end
    super
end
set_defaults_from_associations() click to toggle source
# File lib/skr/models/so_line.rb, line 102
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 !price && sales_order && sales_order.customer && sku_loc && uom.present?
        self.price = Skr.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/models/so_line.rb, line 114
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/models/so_line.rb, line 123
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/models/so_line.rb, line 118
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_invoiced(inv = nil) click to toggle source
# File lib/skr/models/so_line.rb, line 82
def update_qty_invoiced(inv = nil)
    inv_qty = inv_lines.ea_qty/uom_size
    update_attributes( :qty_invoiced=> inv_qty,
                       :qty_allocated => [ qty_allocated - inv_qty, 0 ].max )
end
update_qty_picking(pt = nil) click to toggle source
# File lib/skr/models/so_line.rb, line 88
def update_qty_picking(pt = nil)
    update_attributes( :qty_picking=> pt_lines.ea_picking_qty/uom_size )
end