Sha256: 56a028942542b30913d1f32e07961e0eaf2ff21838f26eddb766664b6151b095

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

module Gemgento
  class PriceTier < ActiveRecord::Base
    include Gemgento::ProductTouches

    belongs_to :store, class_name: 'Gemgento::Store'
    belongs_to :product, class_name: 'Gemgento::Product'
    belongs_to :user_group, class_name: 'Gemgento::UserGroup'

    validates :store, :product, :quantity, :price, presence: true

    # Check if PriceTier is valid for the given quantity and user.
    #
    # @param quantity [Float]
    # @param user_group [Gemgento::UserGroup]
    def is_valid?(quantity, user_group)
      return quantity >= self.quantity && (self.user_group.nil? || user_group == self.user_group)
    end

    # @param product [Gemgento::Product]
    # @param quantity [Float]
    # @param user_group [Gemgento::UserGroup]
    def self.calculate_price(product, quantity = 1.0, user_group = nil, store = nil)
      store ||= Gemgento::Store.current
      price = product.attribute_value('price', store).to_f
      user_group ||= UserGroup.find_by(magento_id: 0)

      product.price_tiers.where(store: store).where('quantity <= ?', quantity).each do |price_tier|
        next unless price_tier.is_valid? quantity, user_group
        price = price_tier.price if price_tier.price < price
      end

      return price
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gemgento-2.8.0 app/models/gemgento/price_tier.rb