<commit_before>module Skr

    #### A (S)tock (K)eeping (U)nit (SKU) is the cornerstone of Stockor
    #
    # At it's simplest form a SKU tracks a *resource* that the company controlls.
    # It can be manufactured (by combining other SKUs), purchased, stored, and sold.
    #
    # Although SKUs usually refer to physical item, it may also track intangibles
    # such as "Labor", "Handling", or "Freight"
    #
    class Sku < Skr::Model

        has_code_identifier :from=>'description'

        belongs_to :default_vendor,   class_name: 'Vendor',    export: true
        belongs_to :gl_asset_account, class_name: 'GlAccount', export: true
        belongs_to :default_vendor,   class_name: 'Vendor',    export: true

        has_many :sku_locs, ->{extending Concerns::Sku::Locations },
            inverse_of: :sku, dependent: :destroy,
            export: { writable:true }

        has_many :sku_vendors, ->{ extending Concerns::Sku::Vendors },
            dependent: :destroy,  inverse_of: :sku,
            export: { writable: true, allow_destroy: true }

        has_many :uoms, ->{ order(:size); extending(Concerns::Sku::Uoms) },
            dependent: :destroy, inverse_of: :sku,
            export: { writable: true, allow_destroy: true }

        validates :uoms,                presence: true, :on => :update
        validates :description,         presence: true
        validates :gl_asset_account,    set: true
        validates :default_vendor,      set: true
        validates :default_uom_code,    presence: true, :on => :update
        validate  :ensure_default_uom_exists

        before_validation :set_defaults, :on=>:create
        after_create  :create_associated_records

        scope :with_vendor_part_code, lambda { | vendor_sku |
            joins(:sku_vendors).where( SkuVendor.arel_table[:part_code].matches( vendor_sku ) )
        }, :export=>true

        scope :in_location, lambda { | location_id |
            joins(:sku_locs).where( SkuLoc.table_name => { location_id: location_id } )
        }, :export=>true

        scope :with_qty_details, lambda { | *args |
            compose_query_using_detail_view( view: 'sku_qty_details', join_to: 'sku_id' )
        }, export: { :join_table=>:details }

        scope :only_back_ordered, lambda{ | *args |
            with_qty_details.where("details.qty_on_order > details.qty_on_hand")
        }, export: true


        # Rebuilding is sometimes needed for cases where the location's
        # allocation/on order/reserved counts get out of sync with the
        # SalesOrder counts.  This forces recalculation of the cached values
        def rebuild!
            sku_locs.each(&:rebuild!)
        end

        # Return the uom with code.  Since the UOM's are probably already loaded
        # for the SKU, it makes sense to search over the in-memory collection
        # vs hitting the DB again.
        def uom_with_code( code )
            uoms.detect{|uom| uom.code == code }
        end

        # The default implementation returns the price for the
        # default UOM.  Custom implementations will return
        # from a pricing library
        # @return [BigDecimal] the "standard" price for the SKU
        def price_for( customer )
            uoms.default.price
        end

        private<commit_msg>Document methods<commit_after>module Skr

    #### A (S)tock (K)eeping (U)nit (SKU) is the cornerstone of Stockor
    #
    # At it's simplest form a SKU tracks a *resource* that the company controlls.
    # It can be manufactured (by combining other SKUs), purchased, stored, and sold.
    #
    # Although SKUs usually refer to physical item, it may also track intangibles
    # such as "Labor", "Handling", or "Freight"
    #
    class Sku < Skr::Model

        has_code_identifier :from=>'description'

        belongs_to :default_vendor,   class_name: 'Vendor',    export: true
        belongs_to :gl_asset_account, class_name: 'GlAccount', export: true
        belongs_to :default_vendor,   class_name: 'Vendor',    export: true

        has_many :sku_locs, ->{extending Concerns::Sku::Locations },
            inverse_of: :sku, dependent: :destroy,
            export: { writable:true }

        has_many :sku_vendors, ->{ extending Concerns::Sku::Vendors },
            dependent: :destroy,  inverse_of: :sku,
            export: { writable: true, allow_destroy: true }

        has_many :uoms, ->{ order(:size); extending(Concerns::Sku::Uoms) },
            dependent: :destroy, inverse_of: :sku,
            export: { writable: true, allow_destroy: true }

        validates :uoms,                presence: true, :on => :update
        validates :description,         presence: true
        validates :gl_asset_account,    set: true
        validates :default_vendor,      set: true
        validates :default_uom_code,    presence: true, :on => :update
        validate  :ensure_default_uom_exists

        before_validation :set_defaults, :on=>:create
        after_create  :create_associated_records

        scope :with_vendor_part_code, lambda { | vendor_sku |
            joins(:sku_vendors).where( SkuVendor.arel_table[:part_code].matches( vendor_sku ) )
        }, :export=>true

        scope :in_location, lambda { | location_id |
            joins(:sku_locs).where( SkuLoc.table_name => { location_id: location_id } )
        }, :export=>true

        scope :with_qty_details, lambda { | *args |
            compose_query_using_detail_view( view: 'sku_qty_details', join_to: 'sku_id' )
        }, export: { :join_table=>:details }

        scope :only_back_ordered, lambda{ | *args |
            with_qty_details.where("details.qty_on_order > details.qty_on_hand")
        }, export: true


        # Rebuilding is sometimes needed for cases where the location's
        # allocation/on order/reserved counts get out of sync with the
        # SalesOrder counts.  This forces recalculation of the cached values
        def rebuild!
            sku_locs.each(&:rebuild!)
        end

        private
{'fraction_non_alphanumeric': 0.09697774825639323, 'fraction_numerical': 0.0, 'mean_word_length': 2.568127962085308, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3896158', 'n_tokens_mistral': 1839, 'n_tokens_neox': 1783, 'n_words': 493}