Sha256: 2fb1bb430d09ee6debdafa17ef184423fb0dabca1fb0a82cd93e805489ffa68c

Contents?: true

Size: 1.56 KB

Versions: 91

Compression:

Stored size: 1.56 KB

Contents

module Effective
  class TaxRateCalculator
    attr_reader :order, :country_code, :state_code

    RATES = {
      'CA' => {         # Canada
        'AB' => 5.00,   # Alberta
        'BC' => 5.00,   # British Columbia
        'MB' => 5.00,   # Manitoba
        'NB' => 15.0,   # New Brunswick
        'NL' => 15.0,   # Newfoundland and Labrador
        'NT' => 5.00,   # Northwest Territories
        'NS' => 15.0,   # Nova Scotia
        'ON' => 13.0,   # Ontario
        'PE' => 15.0,   # Prince Edward Island
        'QC' => 5.00,   # Quebec
        'SK' => 5.00,   # Saskatchewan
        'YT' => 5.00,   # Yukon Territory
        'NU' => 5.00    # Nunavut
      }
    }

    def initialize(order: nil, country_code: nil, state_code: nil)
      @order = order
      @country_code = country_code
      @state_code = state_code
    end

    def tax_rate
      country = country_code
      state = state_code

      if order.present? && order.billing_address.present?
        country ||= order.billing_address.country_code
        state ||= order.billing_address.state_code
      end

      if order.present? && order.user.respond_to?(:billing_address) && order.user.billing_address.present?
        country ||= order.user.billing_address.country_code
        state ||= order.user.billing_address.state_code
      end

      rate = RATES[country]
      return rate if rate.kind_of?(Numeric)
      return unknown_tax_rate() if rate.nil?

      rate[state].presence || unknown_tax_rate()
    end

    def unknown_tax_rate
      (order && order.skip_buyer_validations?) ? nil : 0
    end

  end
end

Version data entries

91 entries across 91 versions & 1 rubygems

Version Path
effective_orders-5.1.15 app/models/effective/tax_rate_calculator.rb
effective_orders-5.1.14 app/models/effective/tax_rate_calculator.rb
effective_orders-5.1.13 app/models/effective/tax_rate_calculator.rb
effective_orders-5.1.12 app/models/effective/tax_rate_calculator.rb
effective_orders-5.1.11 app/models/effective/tax_rate_calculator.rb
effective_orders-5.1.10 app/models/effective/tax_rate_calculator.rb
effective_orders-5.1.9 app/models/effective/tax_rate_calculator.rb
effective_orders-5.1.8 app/models/effective/tax_rate_calculator.rb
effective_orders-5.1.7 app/models/effective/tax_rate_calculator.rb
effective_orders-5.1.6 app/models/effective/tax_rate_calculator.rb
effective_orders-5.1.5 app/models/effective/tax_rate_calculator.rb