class Skr::SalesOrder
A SalesOrder
is a record of a {Customer}'s desire to purchase one or more {Sku}s. It can be converted into an {Invoice} when the goods are delivered (or shipped) to the {Customer}
customer = Customer.find_by_code "VIP1" so = SalesOrder.new( customer: customer ) Sku.where( code: ['HAT','STRING'] ).each do | sku | so.lines.build( sku_loc: sku.sku_locs.default ) end so.save invoice = Invoice.new( sales_order: so ) invoice.lines.from_sales_order! invoice.save
Public Class Methods
new(attributes = {})
click to toggle source
Calls superclass method
# File lib/skr/sales_order.rb, line 106 def initialize(attributes = {}) super self.order_date = Date.today end
sales_history( ndays )
click to toggle source
@return [Array of Array[day_ago,date, order_count,line_count,total]]
# File lib/skr/sales_order.rb, line 87 def self.sales_history( ndays ) qry = "select * from #{Skr::Core.config.table_prefix}so_dailly_sales_history where days_ago<#{ndays.to_i}" connection.execute(qry).values end
Private Instance Methods
cancel_all_lines()
click to toggle source
when the order is canceled, inform the lines
# File lib/skr/sales_order.rb, line 142 def cancel_all_lines self.pick_tickets.each{ |pt| pt.cancel! } self.lines.each{ | soline | soline.cancel! } true end
check_if_location_changed()
click to toggle source
When the location changes, lines need to have their sku_loc modified to point to the new location as well
# File lib/skr/sales_order.rb, line 114 def check_if_location_changed if location_id_changed? self.lines.each{ |l| l.location = self.location } end end
ensure_location_changes_are_valid()
click to toggle source
The location can only be updated if all the line's skus are setup in the new location
# File lib/skr/sales_order.rb, line 121 def ensure_location_changes_are_valid return true unless changes['location_id'] errors.add(:location, 'cannot be changed unless sales order is open') unless open? current = self.sku_ids setup = location.sku_locs.where( sku_id: current ).pluck('sku_id') missing = current - setup if missing.any? codes = Sku.where( id: missing ).pluck('code') errors.add(:location, "#{location.code} does not have skus #{codes.join(',')}") end end
on_invoice(inv)
click to toggle source
# File lib/skr/sales_order.rb, line 148 def on_invoice(inv) self.mark_complete! if may_mark_complete? and lines.unshipped.none? end
set_defaults()
click to toggle source
# File lib/skr/sales_order.rb, line 152 def set_defaults if customer self.billing_address = customer.billing_address if self.billing_address.blank? self.shipping_address = customer.shipping_address if self.shipping_address.blank? end end
setup_new_pt(pt)
click to toggle source
Initialize a new {PickTicket} by copying the pickable lines to it
# File lib/skr/sales_order.rb, line 134 def setup_new_pt(pt) self.lines.each do | so_line | pt.lines << so_line.pt_lines.build if so_line.pickable_qty > 0 end true end