Class: Xsys::Model::Product

Inherits:
Object
  • Object
show all
Defined in:
lib/xsys/model/product.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Product

Returns a new instance of Product.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/xsys/model/product.rb', line 16

def initialize(attributes={})
  time_fields = ['cost_update_time', 'last_cost_update_time', 'price_update_time']
  date_fields = ['cost_update_date', 'last_cost_update_date', 'price_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']

  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 { |s| Stock.new(s) } unless v.nil?
    elsif k.to_s == 'prices'
      @prices = v.map { |s| ProductPriceList.new(s) }
    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

Class Method Details

.attr_listObject



4
5
6
7
8
9
10
11
12
# 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]
end

Instance Method Details

#markup_with_list(price_list_id) ⇒ Object



113
114
115
116
117
# File 'lib/xsys/model/product.rb', line 113

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) ⇒ Object



107
108
109
110
111
# File 'lib/xsys/model/product.rb', line 107

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) ⇒ Object



101
102
103
104
105
# File 'lib/xsys/model/product.rb', line 101

def price_list(price_list_id)
  prices.find { |p|
    p.price_list_id.to_i == price_list_id.to_i
  }
end

#sellable_stocksObject



85
86
87
# File 'lib/xsys/model/product.rb', line 85

def sellable_stocks
  stocks.find_all { |x| x.sellable }
end

#sellable_stocks_availableObject



97
98
99
# File 'lib/xsys/model/product.rb', line 97

def sellable_stocks_available
  sellable_stocks.map(&:available).sum
end

#sellable_stocks_quantityObject



89
90
91
# File 'lib/xsys/model/product.rb', line 89

def sellable_stocks_quantity
  sellable_stocks.map(&:quantity).sum
end

#sellable_stocks_reservedObject



93
94
95
# File 'lib/xsys/model/product.rb', line 93

def sellable_stocks_reserved
  sellable_stocks.map(&:reserved).sum
end

#stock_at(shop_code) ⇒ Object



79
80
81
82
83
# File 'lib/xsys/model/product.rb', line 79

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) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/xsys/model/product.rb', line 55

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) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/xsys/model/product.rb', line 43

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) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/xsys/model/product.rb', line 67

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) ⇒ Object



119
120
121
122
123
# File 'lib/xsys/model/product.rb', line 119

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) ⇒ Object



125
126
127
128
129
# File 'lib/xsys/model/product.rb', line 125

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