Sha256: f23500224d2d5e42356c122db8c7f7116aa96c02f009ac128c49db6a8ee00478

Contents?: true

Size: 1.94 KB

Versions: 8

Compression:

Stored size: 1.94 KB

Contents

module Xsys
  module Model
    class Product
      def self.attr_list
        [:code, :name, :cost_updated_at, :sellable, :product_category_code,
         :product_provider_code, :vat_rate, :taxed_cost, :vat_cost, :total_cost,
         :pending_ordered_quantity, :stocks, :prices, :category, :provider]
      end

      attr_reader *attr_list

      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

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

      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

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

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

      def markup_with_list(price_list_code)
        prices.find { |p|
          p.price_list_code.to_i == price_list_code.to_i
        }.try(:markup) || 0.0
      end

      def price_in_list(price_list_code)
        prices.find { |p|
          p.price_list_code.to_i == price_list_code.to_i
        }.try(:total_price) || 0.0
      end

      private

      attr_writer *attr_list
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
xsys-0.2.7 lib/xsys/model/product.rb
xsys-0.2.6 lib/xsys/model/product.rb
xsys-0.2.5 lib/xsys/model/product.rb
xsys-0.2.4 lib/xsys/model/product.rb
xsys-0.2.3 lib/xsys/model/product.rb
xsys-0.2.2 lib/xsys/model/product.rb
xsys-0.2.1 lib/xsys/model/product.rb
xsys-0.2.0 lib/xsys/model/product.rb