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.



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
42
43
44
# File 'lib/xsys/model/product.rb', line 17

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 { |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
# 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]
end

Instance Method Details

#markup_with_list(price_list_id) ⇒ Object



116
117
118
119
120
# File 'lib/xsys/model/product.rb', line 116

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



110
111
112
113
114
# File 'lib/xsys/model/product.rb', line 110

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



104
105
106
107
108
# File 'lib/xsys/model/product.rb', line 104

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

#sellable_stocksObject



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

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

#sellable_stocks_availableObject



100
101
102
# File 'lib/xsys/model/product.rb', line 100

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

#sellable_stocks_quantityObject



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

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

#sellable_stocks_reservedObject



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

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

#stock_at(shop_code) ⇒ Object



82
83
84
85
86
# File 'lib/xsys/model/product.rb', line 82

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



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/xsys/model/product.rb', line 58

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



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/xsys/model/product.rb', line 46

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



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/xsys/model/product.rb', line 70

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



122
123
124
125
126
# File 'lib/xsys/model/product.rb', line 122

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



128
129
130
131
132
# File 'lib/xsys/model/product.rb', line 128

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