<commit_before># FIXME remove Base when plugin became a module
class SuppliersPlugin::BaseProduct < Product

  # join source_products
  # FIXME: can't preload :suppliers due to a rails bug
  default_scope :include => [:from_products, {:sources_from_products => [{:supplier => [{:profile => [:domains, {:environment => :domains}]}]}]},
                             {:profile => [:domains, {:environment => :domains}]}]

  self.abstract_class = true

  settings_items :minimum_selleable, :type => Float, :default => nil
  settings_items :margin_percentage, :type => Float, :default => nil
  settings_items :stored, :type => Float, :default => nil
  settings_items :quantity, :type => Float, :default => nil

  DEFAULT_ATTRIBUTES = [
    :name, :description, :price, :unit_id, :product_category_id, :image_id,
    :margin_percentage, :stored, :minimum_selleable, :unit_detail
  ]

  extend ActsAsHavingSettings::DefaultItem::ClassMethods
  settings_default_item :name, :type => :boolean, :default => true, :delegate_to => :supplier_product
  settings_default_item :product_category, :type => :boolean, :default => true, :delegate_to => :supplier_product
  settings_default_item :image, :type => :boolean, :default => true, :delegate_to => :supplier_product, :prefix => '_default'
  settings_default_item :description, :type => :boolean, :default => true, :delegate_to => :supplier_product
  settings_default_item :unit, :type => :boolean, :default => true, :delegate_to => :supplier_product
  settings_default_item :available, :type => :boolean, :default => false, :delegate_to => :supplier_product
  settings_default_item :margin_percentage, :type => :boolean, :default => true, :delegate_to => :profile

  default_item :price, :if => :default_margin_percentage, :delegate_to => proc{ self.supplier_product.price_with_discount if self.supplier_product }
  default_item :unit_detail, :if => :default_unit, :delegate_to => :supplier_product
  settings_default_item :stored, :type => :boolean, :default => true, :delegate_to => :supplier_product
  settings_default_item :minimum_selleable, :type => :boolean, :default => true, :delegate_to => :supplier_product

  default_item :product_category_id, :if => :default_product_category, :delegate_to => :supplier_product
  default_item :image_id, :if => :_default_image, :delegate_to => :supplier_product
  default_item :unit_id, :if => :default_unit, :delegate_to => :supplier_product

  extend CurrencyHelper::ClassMethods
  has_currency :own_price
  has_currency :original_price
  has_number_with_locale :minimum_selleable
  has_number_with_locale :own_minimum_selleable
  has_number_with_locale :original_minimum_selleable
  has_number_with_locale :stored
  has_number_with_locale :own_stored
  has_number_with_locale :original_stored
  has_number_with_locale :quantity
  has_number_with_locale :margin_percentage
  has_number_with_locale :own_margin_percentage
  has_number_with_locale :original_margin_percentage

  def self.default_unit
    Unit.new(:singular => I18n.t('suppliers_plugin.models.product.unit'), :plural => I18n.t('suppliers_plugin.models.product.units'))
  end

  def self.search_scope scope, params
    scope = scope.from_supplier_id params[:supplier_id] if params[:supplier_id].present?
    scope = scope.with_available(if params[:available] == 'true' then true else false end) if params[:available].present?
    scope = scope.name_like params[:name] if params[:name].present?
    scope = scope.with_product_category_id params[:category_id] if params[:category_id].present?
    scope
  end

  # replace available? to use the replaced default_item method
  def available?
    self.available
  end

  def available_with_supplier
    self.available_without_supplier and self.supplier_product.available and self.supplier.active
  end
  alias_method_chain :available, :supplier

  def dependent?
    self.from_products.length == 1
  end

  def minimum_selleable
    self['minimum_selleable'] || 0.1
  end

  def price_with_margins base_price = nil, margin_source = nil<commit_msg>Check if supplier_product is nil<commit_after># FIXME remove Base when plugin became a module
class SuppliersPlugin::BaseProduct < Product

  # join source_products
  # FIXME: can't preload :suppliers due to a rails bug
  default_scope :include => [:from_products, {:sources_from_products => [{:supplier => [{:profile => [:domains, {:environment => :domains}]}]}]},
                             {:profile => [:domains, {:environment => :domains}]}]

  self.abstract_class = true

  settings_items :minimum_selleable, :type => Float, :default => nil
  settings_items :margin_percentage, :type => Float, :default => nil
  settings_items :stored, :type => Float, :default => nil
  settings_items :quantity, :type => Float, :default => nil

  DEFAULT_ATTRIBUTES = [
    :name, :description, :price, :unit_id, :product_category_id, :image_id,
    :margin_percentage, :stored, :minimum_selleable, :unit_detail
  ]

  extend ActsAsHavingSettings::DefaultItem::ClassMethods
  settings_default_item :name, :type => :boolean, :default => true, :delegate_to => :supplier_product
  settings_default_item :product_category, :type => :boolean, :default => true, :delegate_to => :supplier_product
  settings_default_item :image, :type => :boolean, :default => true, :delegate_to => :supplier_product, :prefix => '_default'
  settings_default_item :description, :type => :boolean, :default => true, :delegate_to => :supplier_product
  settings_default_item :unit, :type => :boolean, :default => true, :delegate_to => :supplier_product
  settings_default_item :available, :type => :boolean, :default => false, :delegate_to => :supplier_product
  settings_default_item :margin_percentage, :type => :boolean, :default => true, :delegate_to => :profile

  default_item :price, :if => :default_margin_percentage, :delegate_to => proc{ self.supplier_product.price_with_discount if self.supplier_product }
  default_item :unit_detail, :if => :default_unit, :delegate_to => :supplier_product
  settings_default_item :stored, :type => :boolean, :default => true, :delegate_to => :supplier_product
  settings_default_item :minimum_selleable, :type => :boolean, :default => true, :delegate_to => :supplier_product

  default_item :product_category_id, :if => :default_product_category, :delegate_to => :supplier_product
  default_item :image_id, :if => :_default_image, :delegate_to => :supplier_product
  default_item :unit_id, :if => :default_unit, :delegate_to => :supplier_product

  extend CurrencyHelper::ClassMethods
  has_currency :own_price
  has_currency :original_price
  has_number_with_locale :minimum_selleable
  has_number_with_locale :own_minimum_selleable
  has_number_with_locale :original_minimum_selleable
  has_number_with_locale :stored
  has_number_with_locale :own_stored
  has_number_with_locale :original_stored
  has_number_with_locale :quantity
  has_number_with_locale :margin_percentage
  has_number_with_locale :own_margin_percentage
  has_number_with_locale :original_margin_percentage

  def self.default_unit
    Unit.new(:singular => I18n.t('suppliers_plugin.models.product.unit'), :plural => I18n.t('suppliers_plugin.models.product.units'))
  end

  def self.search_scope scope, params
    scope = scope.from_supplier_id params[:supplier_id] if params[:supplier_id].present?
    scope = scope.with_available(if params[:available] == 'true' then true else false end) if params[:available].present?
    scope = scope.name_like params[:name] if params[:name].present?
    scope = scope.with_product_category_id params[:category_id] if params[:category_id].present?
    scope
  end

  # replace available? to use the replaced default_item method
  def available?
    self.available
  end

  def available_with_supplier
    self.available_without_supplier and self.supplier_product and self.supplier_product.available and self.supplier.active
  end
  alias_method_chain :available, :supplier

  def dependent?
    self.from_products.length == 1
  end

  def minimum_selleable
    self['minimum_selleable'] || 0.1
  end

  def price_with_margins base_price = nil, margin_source = nil
{'fraction_non_alphanumeric': 0.10938845822566753, 'fraction_numerical': 0.0017226528854435831, 'mean_word_length': 5.864864864864865, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 113, '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': '4820807', 'n_tokens_mistral': 2556, 'n_tokens_neox': 2475, 'n_words': 577}