lib/skr/models/sales_order.rb in stockor-0.1.9 vs lib/skr/models/sales_order.rb in stockor-0.2

- old
+ new

@@ -9,10 +9,11 @@ # 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 @@ -39,27 +40,24 @@ validate :ensure_location_changes_are_valid after_save :check_if_location_changed before_validation :set_defaults, on: :create - delegate_and_export :customer_code, :customer_name - delegate_and_export :location_code, :location_name - delegate_and_export :terms_code, :terms_description delegate_and_export :billing_address_name - # joins the so_amount_details view which includes additional fields: # customer_code, customer_name, bill_addr_name, total, num_lines, total_other_charge_amount, # total_tax_amount, total_shipping_amount,subtotal_amount - scope :with_amount_details, lambda { | *args | - compose_query_using_detail_view(view: 'so_amount_details', join_to: 'sales_order_id') + + scope :with_details, lambda { | *args | + compose_query_using_detail_view(view: 'skr_so_details') }, export: true # joins the so_allocation_details which includes the additional fields: # number_of_lines, allocated_total, number_of_lines_allocated, number_of_lines_fully_allocated scope :with_allocation_details, lambda { - compose_query_using_detail_view(view: 'so_allocation_details', join_to: 'sales_order_id') + compose_query_using_detail_view(view: 'skr_so_allocation_details') } # a open SalesOrder is one who's state is not "complete" or "canceled" scope :open, lambda { | *args | where.not(state: [:complete,:canceled]) @@ -109,11 +107,12 @@ end end def initialize(attributes = {}) super - self.order_date = Date.today + # order date must be set, otherwise things like terms that are set from it fail + self.order_date ||= Date.today end private # When the location changes, lines need to have their sku_loc modified to point to the new location as well @@ -155,10 +154,11 @@ self.mark_complete! if may_mark_complete? and lines.unshipped.none? end def set_defaults if customer - self.billing_address = customer.billing_address if self.billing_address.blank? + self.form ||= customer.get_form('invoice') + self.billing_address = customer.billing_address if self.billing_address.blank? self.shipping_address = customer.shipping_address if self.shipping_address.blank? end end end end # Skr module