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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

Class Method Details

.attr_listObject



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

Instance Method Details

#markup_with_list(price_list_id) ⇒ Object



118
119
120
121
122
# 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) ⇒ Object



112
113
114
115
116
# 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) ⇒ Object



106
107
108
109
110
# 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_stocksObject



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

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

#sellable_stocks_availableObject



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

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

#sellable_stocks_quantityObject



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

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

#sellable_stocks_reservedObject



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

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

#stock_at(shop_code) ⇒ Object



84
85
86
87
88
# 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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# 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) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object



124
125
126
127
128
# 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) ⇒ Object



130
131
132
133
134
# 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