Sha256: ac79ac55b27373590e413d596a4c546fde5430320729d0e094b4a986bcc0127b

Contents?: true

Size: 1.59 KB

Versions: 15

Compression:

Stored size: 1.59 KB

Contents

module Shoppe
  class TaxRate < ActiveRecord::Base
    
    self.table_name = 'shoppe_tax_rates'
    
    include Shoppe::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 => 'Shoppe::Product'
    
    # All delivery service prices which are assigned to this tax rate
    has_many :delivery_service_prices, :dependent => :restrict_with_exception, :class_name => 'Shoppe::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

15 entries across 15 versions & 3 rubygems

Version Path
shoppe-1.1.1 app/models/shoppe/tax_rate.rb
shoppe-1.1.0 app/models/shoppe/tax_rate.rb
shoppe-1.0.9 app/models/shoppe/tax_rate.rb
shoppe-1.0.8 app/models/shoppe/tax_rate.rb
kylekthompson-shoppe-1.0.7 app/models/shoppe/tax_rate.rb
shoppe-1.0.7 app/models/shoppe/tax_rate.rb
shoppe-1.0.6 app/models/shoppe/tax_rate.rb
shoppe-paypal-1.1.0 vendor/bundle/ruby/2.1.0/gems/shoppe-1.0.5/app/models/shoppe/tax_rate.rb
shoppe-1.0.5 app/models/shoppe/tax_rate.rb
shoppe-1.0.3 app/models/shoppe/tax_rate.rb
shoppe-1.0.2 app/models/shoppe/tax_rate.rb
shoppe-1.0.1 app/models/shoppe/tax_rate.rb
shoppe-1.0.0 app/models/shoppe/tax_rate.rb
shoppe-0.0.21 app/models/shoppe/tax_rate.rb
shoppe-0.0.20 app/models/shoppe/tax_rate.rb