<commit_before>module Spree
  class Variant < ActiveRecord::Base
    belongs_to :product
    delegate_belongs_to :product, :name, :description, :permalink, :available_on,
                        :tax_category_id, :shipping_category_id, :meta_description,
                        :meta_keywords, :tax_category

    attr_accessible :name, :presentation, :cost_price,
                    :position, :on_hand, :option_value_ids,
                    :product_id, :option_values_attributes, :price,
                    :weight, :height, :width, :depth, :sku

    has_many :inventory_units
    has_many :line_items
    has_and_belongs_to_many :option_values, :join_table => 'spree_option_values_variants'
    has_many :images, :as => :viewable, :order => :position, :dependent => :destroy

    validate :check_price
    validates :price, :numericality => { :greater_than_or_equal_to => 0 }, :presence => true
    validates :cost_price, :numericality => { :greater_than_or_equal_to => 0, :allow_nil => true } if self.table_exists? && self.column_names.include?('cost_price')
    validates :count_on_hand, :numericality => true

    before_save :touch_product

    # default variant scope only lists non-deleted variants
    scope :active, where(:deleted_at => nil)
    scope :deleted, where('deleted_at IS NOT NULL')

    # default extra fields for shipping purposes
    @fields = [{ :name => 'Weight', :only => [:variant], :format => '%.2f' },
               { :name => 'Height', :only => [:variant], :format => '%.2f' },
               { :name => 'Width',  :only => [:variant], :format => '%.2f' },
               { :name => 'Depth',  :only => [:variant], :format => '%.2f' }]

    # Returns number of inventory units for this variant (new records haven't been saved to database, yet)
    def on_hand
      Spree::Config[:track_inventory_levels] ? count_on_hand : (1.0 / 0) # Infinity
    end

    # Adjusts the inventory units to match the given new level.
    def on_hand=(new_level)
      if Spree::Config[:track_inventory_levels]
        new_level = new_level.to_i

        # increase Inventory when
        if new_level > on_hand
          # fill backordered orders before creating new units
          backordered_units = inventory_units.with_state('backordered')
          backordered_units.slice(0, new_level).each(&:fill_backorder)
          new_level -= backordered_units.length
        end

        self.count_on_hand = new_level
      else
        raise 'Cannot set on_hand value when Spree::Config[:track_inventory_levels] is false'
      end
    end

    # strips all non-price-like characters from the price.
    def price=(price)
      if price.present?
        self[:price] = price.to_s.gsub(/[^0-9\.-]/, '').to_f
      end
    end


    # returns number of units currently on backorder for this variant.
    def on_backorder
      inventory_units.with_state('backordered').size
    end

    # returns true if at least one inventory unit of this variant is "on_hand"
    def in_stock?
      Spree::Config[:track_inventory_levels] ? on_hand > 0 : true
    end
    alias in_stock in_stock?

    # returns true if this variant is allowed to be placed on a new order
    def available?
      Spree::Config[:track_inventory_levels] ? (Spree::Config[:allow_backorders] || in_stock?) : true
    end

    def options_text
      values = self.option_values.sort_by(&:position)

      values.map! do |ov|
        "#{ov.option_type.presentation}: #{ov.presentation}"
      end

      values.to_sentence({ :words_connector => ", ", :two_words_connector => ", " })
    end

    def gross_profit
      cost_price.nil? ? 0 : (price - cost_price)<commit_msg>fixing "1 234.00 " cost_price validation failure coming from admin product controller<commit_after>module Spree
  class Variant < ActiveRecord::Base
    belongs_to :product
    delegate_belongs_to :product, :name, :description, :permalink, :available_on,
                        :tax_category_id, :shipping_category_id, :meta_description,
                        :meta_keywords, :tax_category

    attr_accessible :name, :presentation, :cost_price,
                    :position, :on_hand, :option_value_ids,
                    :product_id, :option_values_attributes, :price,
                    :weight, :height, :width, :depth, :sku

    has_many :inventory_units
    has_many :line_items
    has_and_belongs_to_many :option_values, :join_table => 'spree_option_values_variants'
    has_many :images, :as => :viewable, :order => :position, :dependent => :destroy

    validate :check_price
    validates :price, :numericality => { :greater_than_or_equal_to => 0 }, :presence => true
    validates :cost_price, :numericality => { :greater_than_or_equal_to => 0, :allow_nil => true } if self.table_exists? && self.column_names.include?('cost_price')
    validates :count_on_hand, :numericality => true

    before_save :touch_product

    # default variant scope only lists non-deleted variants
    scope :active, where(:deleted_at => nil)
    scope :deleted, where('deleted_at IS NOT NULL')

    # default extra fields for shipping purposes
    @fields = [{ :name => 'Weight', :only => [:variant], :format => '%.2f' },
               { :name => 'Height', :only => [:variant], :format => '%.2f' },
               { :name => 'Width',  :only => [:variant], :format => '%.2f' },
               { :name => 'Depth',  :only => [:variant], :format => '%.2f' }]

    # Returns number of inventory units for this variant (new records haven't been saved to database, yet)
    def on_hand
      Spree::Config[:track_inventory_levels] ? count_on_hand : (1.0 / 0) # Infinity
    end

    # Adjusts the inventory units to match the given new level.
    def on_hand=(new_level)
      if Spree::Config[:track_inventory_levels]
        new_level = new_level.to_i

        # increase Inventory when
        if new_level > on_hand
          # fill backordered orders before creating new units
          backordered_units = inventory_units.with_state('backordered')
          backordered_units.slice(0, new_level).each(&:fill_backorder)
          new_level -= backordered_units.length
        end

        self.count_on_hand = new_level
      else
        raise 'Cannot set on_hand value when Spree::Config[:track_inventory_levels] is false'
      end
    end

    # strips all non-price-like characters from the price.
    def price=(price)
      if price.present?
        self[:price] = price.to_s.gsub(/[^0-9\.-]/, '').to_f
      end
    end

    # and cost_price
    def cost_price=(price)
      if price.present?
        self[:cost_price] = price.to_s.gsub(/[^0-9\.-]/, '').to_f
      end
    end

    # returns number of units currently on backorder for this variant.
    def on_backorder
      inventory_units.with_state('backordered').size
    end

    # returns true if at least one inventory unit of this variant is "on_hand"
    def in_stock?
      Spree::Config[:track_inventory_levels] ? on_hand > 0 : true
    end
    alias in_stock in_stock?

    # returns true if this variant is allowed to be placed on a new order
    def available?
      Spree::Config[:track_inventory_levels] ? (Spree::Config[:allow_backorders] || in_stock?) : true
    end

    def options_text
      values = self.option_values.sort_by(&:position)

      values.map! do |ov|
        "#{ov.option_type.presentation}: #{ov.presentation}"
      end

      values.to_sentence({ :words_connector => ", ", :two_words_connector => ", " })
    end

    def gross_profit
      cost_price.nil? ? 0 : (price - cost_price)
{'fraction_non_alphanumeric': 0.11338289962825279, 'fraction_numerical': 0.004779607010090282, 'mean_word_length': 2.9152806652806653, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, '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': '3081309', 'n_tokens_mistral': 2349, 'n_tokens_neox': 2260, 'n_words': 602}