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.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/xsys/model/product.rb', line 13

def initialize(attributes={})
  time_fields = ['cost_updated_at', 'last_cost_updated_at', 'price_updated_at']
  decimal_fields = ['vat_rate', 'taxed_cost', 'vat_cost', 'total_cost', 'last_total_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 { |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 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
# File 'lib/xsys/model/product.rb', line 4

def self.attr_list
  [:id, :name, :cost_updated_at, :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_cost_updated_at, :price_updated_at, :online_stock]
end

Instance Method Details

#markup_with_list(price_list_id) ⇒ Object



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

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_for_list(price_list_id) ⇒ Object



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

def price_date_for_list(price_list_id)
  prices.find { |p|
    p.price_list_id.to_i == price_list_id.to_i
  }.try(:price_updated_at)
end

#price_in_list(price_list_id) ⇒ Object



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

def price_in_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

#sellable_stocksObject



36
37
38
39
40
# File 'lib/xsys/model/product.rb', line 36

def sellable_stocks
  stocks.find_all { |s|
    !['SER', 'EXT'].include?(s.shop_code)
  }
end

#sellable_stocks_quantity(options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/xsys/model/product.rb', line 42

def sellable_stocks_quantity(options={})
  result = 0
  online_shop_codes = ['OL']
  deposit_shop_codes = ['ADM']
  no_exhibition_shop_codes = online_shop_codes + deposit_shop_codes

  sellable_stocks.each do |stock|
    if options[:skip_exhibition]
      if no_exhibition_shop_codes.include?(stock.shop_code)
        result += stock.quantity
      elsif stock.quantity > 0
        result += (stock.quantity - 1)
      else
        result += stock.quantity
      end
    else
      result += stock.quantity
    end
  end

  result
end

#service_stocksObject



65
66
67
68
69
# File 'lib/xsys/model/product.rb', line 65

def service_stocks
  stocks.find_all { |s|
    ['SER', 'EXT'].include?(s.shop_code)
  }
end

#service_stocks_quantityObject



71
72
73
# File 'lib/xsys/model/product.rb', line 71

def service_stocks_quantity
  service_stocks.map(&:quantity).sum
end

#stock_at(shop_code) ⇒ Object



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

def stock_at(shop_code)
  stocks.find { |s|
    s.shop_code.to_s.upcase == shop_code.to_s.upcase
  }.try(:quantity).to_i
end

#stock_sum(shop_codes) ⇒ Object



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

def stock_sum(shop_codes)
  formatted_shop_codes = shop_codes.map(&:to_s).map(&:upcase)

  stocks.find_all { |s|
    formatted_shop_codes.include?(s.shop_code.to_s.upcase)
  }.map(&:quantity).sum
end

#stocks_quantityObject



75
76
77
# File 'lib/xsys/model/product.rb', line 75

def stocks_quantity
  stocks.map(&:quantity).sum
end