class Xsys::Model::Product

Public Class Methods

attr_list() click to toggle source
# File lib/xsys/model/product.rb, line 4
def self.attr_list
  [:id, :name, :sellable, :product_category_id, :product_provider_id, :vat_rate,
   :taxed_cost, :vat_cost, :total_cost, :pending_ordered_quantity, :stocks, :prices,
   :category, :provider, :last_total_cost, :last_taxed_cost, :cost_update_date,
   :cost_update_time, :last_cost_update_date, :last_cost_update_time, :price_update_date,
   :price_update_time, :online_stock, :real_online_stock, :product_size_code, :weight,
   :length, :width, :height, :packages_quantity, :ean, :packages, :regular_price,
   :reduced_price, :credit_card_price, :brand, :model, :has_stock_on_hold,
   :availability_date, :stockable, :voice_description, :total_real_cost, 
   :real_cost_update_time, :real_cost_update_date]
end
new(attributes={}) click to toggle source
# File lib/xsys/model/product.rb, line 18
def initialize(attributes={})
  time_fields = ['cost_update_time', 'last_cost_update_time', 'price_update_time', 'real_cost_update_time']
  date_fields = ['cost_update_date', 'last_cost_update_date', 'price_update_date', 
    'real_cost_update_date', 'availability_date']
  decimal_fields = ['vat_rate', 'taxed_cost', 'vat_cost', 'total_cost', 'last_total_cost', 'last_taxed_cost', 
    'regular_price', 'reduced_price', 'credit_card_price', 'total_real_cost']

  attributes.each do |k, v|
    if k.to_s == 'category'
      @category = ProductCategory.new(v) unless v.nil?
    elsif k.to_s == 'provider'
      @provider = ProductProvider.new(v) unless v.nil?
    elsif k.to_s == 'stocks'
      @stocks = v.map { |x| Stock.new(x) } unless v.nil?
    elsif k.to_s == 'prices'
      @prices = v.map { |x| ProductPriceList.new(x) } unless v.nil?
    elsif k.to_s == 'packages'
      @packages = v.map { |x| ProductPackage.new(x) } unless v.nil?
    elsif time_fields.include?(k.to_s)
      self.send("#{k}=", Time.parse(v)) unless v.nil?
    elsif date_fields.include?(k.to_s)
      self.send("#{k}=", Date.parse(v)) unless v.nil?
    elsif decimal_fields.include?(k.to_s)
      self.send("#{k}=", BigDecimal.new(v)) unless v.nil?
    else
      self.send("#{k}=", v) if self.respond_to?(k)
    end
  end
end

Public Instance Methods

markup_with_list(price_list_id) click to toggle source
# File lib/xsys/model/product.rb, line 118
def markup_with_list(price_list_id)
  prices.find { |p|
    p.price_list_id.to_i == price_list_id.to_i
  }.try(:markup) || BigDecimal.new('0.0')
end
price_date_with_list(price_list_id) click to toggle source
# File lib/xsys/model/product.rb, line 112
def price_date_with_list(price_list_id)
  prices.find { |p|
    p.price_list_id.to_i == price_list_id.to_i
  }.try(:price_update_date)
end
price_list(price_list_id) click to toggle source
# File lib/xsys/model/product.rb, line 106
def price_list(price_list_id)
  prices.find { |p|
    p.price_list_id.to_i == price_list_id.to_i
  }
end
sellable_stocks() click to toggle source
# File lib/xsys/model/product.rb, line 90
def sellable_stocks
  stocks.find_all { |x| x.sellable }
end
sellable_stocks_available() click to toggle source
# File lib/xsys/model/product.rb, line 102
def sellable_stocks_available
  sellable_stocks.map(&:available).sum
end
sellable_stocks_quantity() click to toggle source
# File lib/xsys/model/product.rb, line 94
def sellable_stocks_quantity
  sellable_stocks.map(&:quantity).sum
end
sellable_stocks_reserved() click to toggle source
# File lib/xsys/model/product.rb, line 98
def sellable_stocks_reserved
  sellable_stocks.map(&:reserved).sum
end
stock_at(shop_code) click to toggle source
# File lib/xsys/model/product.rb, line 84
def stock_at(shop_code)
  stocks.find { |s|
    s.shop_code.to_s.upcase == shop_code.to_s.upcase
  }
end
stock_available(shop_code_or_array=nil) click to toggle source
# File lib/xsys/model/product.rb, line 60
def stock_available(shop_code_or_array=nil)
  if shop_code_or_array.nil?
    stocks.map(&:available).sum
  elsif shop_code_or_array.is_a?(Array)
    stocks.find_all { |s| shop_code_or_array.include?(s.shop_code) }.map(&:available).sum
  elsif shop_code_or_array.is_a?(String)
    stocks.find_all { |s| shop_code_or_array.upcase == s.shop_code }.map(&:available).sum
  else
    raise 'invalid input!'
  end
end
stock_quantity(shop_code_or_array=nil) click to toggle source
# File lib/xsys/model/product.rb, line 48
def stock_quantity(shop_code_or_array=nil)
  if shop_code_or_array.nil?
    stocks.map(&:quantity).sum
  elsif shop_code_or_array.is_a?(Array)
    stocks.find_all { |s| shop_code_or_array.include?(s.shop_code) }.map(&:quantity).sum
  elsif shop_code_or_array.is_a?(String)
    stocks.find_all { |s| shop_code_or_array.upcase == s.shop_code }.map(&:quantity).sum
  else
    raise 'invalid input!'
  end
end
stock_reserved(shop_code_or_array=nil) click to toggle source
# File lib/xsys/model/product.rb, line 72
def stock_reserved(shop_code_or_array=nil)
  if shop_code_or_array.nil?
    stocks.map(&:reserved).sum
  elsif shop_code_or_array.is_a?(Array)
    stocks.find_all { |s| shop_code_or_array.include?(s.shop_code) }.map(&:reserved).sum
  elsif shop_code_or_array.is_a?(String)
    stocks.find_all { |s| shop_code_or_array.upcase == s.shop_code }.map(&:reserved).sum
  else
    raise 'invalid input!'
  end
end
taxed_price_with_list(price_list_id) click to toggle source
# File lib/xsys/model/product.rb, line 124
def taxed_price_with_list(price_list_id)
  prices.find { |p|
    p.price_list_id.to_i == price_list_id.to_i
  }.try(:taxed_price) || BigDecimal.new('0.0')
end
total_price_with_list(price_list_id) click to toggle source
# File lib/xsys/model/product.rb, line 130
def total_price_with_list(price_list_id)
  prices.find { |p|
    p.price_list_id.to_i == price_list_id.to_i
  }.try(:total_price) || BigDecimal.new('0.0')
end