Sha256: 10eb2e04916d2b1e493609613725a89b998c1cc6a3e17875f4a0c01a7f38d839

Contents?: true

Size: 1.61 KB

Versions: 9

Compression:

Stored size: 1.61 KB

Contents

module KktShoppe
  class TaxRate < ActiveRecord::Base
    
    self.table_name = 'kkt_shoppe_tax_rates'
    
    include KktShoppe::AssociatedCountries
    
    # The order address types which may be used when choosing how to apply the tax rate
    ADDRESS_TYPES = ['billing', 'delivery']
    
    # Validations
    validates :name, :presence => true
    validates :address_type, :inclusion => {:in => ADDRESS_TYPES}
    validates :rate, :numericality => true
    
    # All products which are assigned to this tax rate
    has_many :products, :dependent => :restrict_with_exception, :class_name => 'KktShoppe::Product'
    
    # All delivery service prices which are assigned to this tax rate
    has_many :delivery_service_prices, :dependent => :restrict_with_exception, :class_name => 'KktShoppe::DeliveryServicePrice'
    
    # All tax rates ordered by their ID
    scope :ordered, -> { order(:id)}
    
    # Set the address type if appropriate
    before_validation { self.address_type = ADDRESS_TYPES.first if self.address_type.blank? }
    
    # A description of the tax rate including its name & percentage
    #
    # @return [String]
    def description
      "#{name} (#{rate}%)"
    end
    
    # The rate for a given order based on the rules on the tax rate
    #
    # @return [BigDecimal]
    def rate_for(order)
      return rate if countries.empty?
      return rate if address_type == 'billing'  && (order.billing_country.nil?   || country?(order.billing_country))
      return rate if address_type == 'delivery' && (order.delivery_country.nil?  || country?(order.delivery_country))
      BigDecimal(0)
    end
    
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
kkt_shoppe-2.0.2 app/models/kkt_shoppe/tax_rate.rb
kkt_shoppe-2.0.1 app/models/kkt_shoppe/tax_rate.rb
kkt_shoppe-2.0.0 app/models/kkt_shoppe/tax_rate.rb
kkt_shoppe-1.3.0 app/models/kkt_shoppe/tax_rate.rb
kkt_shoppe-1.2.1 app/models/kkt_shoppe/tax_rate.rb
kkt_shoppe-1.2.0 app/models/kkt_shoppe/tax_rate.rb
kkt_shoppe-1.1.2 app/models/kkt_shoppe/tax_rate.rb
kkt_shoppe-1.1.1 app/models/kkt_shoppe/tax_rate.rb
kkt_shoppe-1.1.0 app/models/kkt_shoppe/tax_rate.rb