Class: Xsys::Model::Product
- Inherits:
-
Object
- Object
- Xsys::Model::Product
- Defined in:
- lib/xsys/model/product.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Product
constructor
A new instance of Product.
- #markup_with_list(price_list_id) ⇒ Object
- #price_date_for_list(price_list_id) ⇒ Object
- #price_in_list(price_list_id) ⇒ Object
- #sellable_stocks_quantity ⇒ Object
- #service_stocks_quantity ⇒ Object
- #stock_at(shop_code) ⇒ Object
- #stock_sum(shop_codes) ⇒ Object
- #stocks_quantity ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ Product
Returns a new instance of Product.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/xsys/model/product.rb', line 12 def initialize(attributes={}) attributes.each do |k, v| if k.to_s == 'category' @category = ProductCategory.new(v) elsif k.to_s == 'provider' @provider = ProductProvider.new(v) elsif k.to_s == 'stocks' @stocks = v.map { |s| Stock.new(s) } elsif k.to_s == 'prices' @prices = v.map { |s| ProductPriceList.new(s) } elsif k.to_s == 'cost_updated_at' @cost_updated_at = Time.parse(v) else self.send("#{k}=", v) if self.respond_to?(k) end end end |
Class Method Details
.attr_list ⇒ Object
4 5 6 7 8 |
# 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] end |
Instance Method Details
#markup_with_list(price_list_id) ⇒ Object
64 65 66 67 68 |
# File 'lib/xsys/model/product.rb', line 64 def markup_with_list(price_list_id) prices.find { |p| p.price_list_id.to_i == price_list_id.to_i }.try(:markup) || 0.0 end |
#price_date_for_list(price_list_id) ⇒ Object
58 59 60 61 62 |
# File 'lib/xsys/model/product.rb', line 58 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
70 71 72 73 74 |
# File 'lib/xsys/model/product.rb', line 70 def price_in_list(price_list_id) prices.find { |p| p.price_list_id.to_i == price_list_id.to_i }.try(:total_price) || 0.0 end |
#sellable_stocks_quantity ⇒ Object
30 31 32 33 34 |
# File 'lib/xsys/model/product.rb', line 30 def sellable_stocks_quantity stocks.find_all { |s| !['SER', 'EXT'].include?(s.shop_code) }.map(&:quantity).sum end |
#service_stocks_quantity ⇒ Object
36 37 38 |
# File 'lib/xsys/model/product.rb', line 36 def service_stocks_quantity stocks_quantity - sellable_stocks_quantity end |
#stock_at(shop_code) ⇒ Object
52 53 54 55 56 |
# File 'lib/xsys/model/product.rb', line 52 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
44 45 46 47 48 49 50 |
# File 'lib/xsys/model/product.rb', line 44 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_quantity ⇒ Object
40 41 42 |
# File 'lib/xsys/model/product.rb', line 40 def stocks_quantity stocks.map(&:quantity).sum end |