Sha256: c136d364eba624c54e6bbc4c6ef3c08c83b151367b4b89540ea7f6bfba0ed9cb

Contents?: true

Size: 1.49 KB

Versions: 94

Compression:

Stored size: 1.49 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

      raise 'expected an order, or a country and state code' unless (order || country_code)
      raise 'expected an order OR a country and state code. Not both.' if (order && country_code)
    end

    def tax_rate
      country = country_code || (order.billing_address.country_code if order.billing_address.present?)
      state = state_code || (order.billing_address.state_code if order.billing_address.present?)

      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

94 entries across 94 versions & 1 rubygems

Version Path
effective_orders-4.5.8 app/models/effective/tax_rate_calculator.rb
effective_orders-4.5.7 app/models/effective/tax_rate_calculator.rb
effective_orders-4.5.6 app/models/effective/tax_rate_calculator.rb
effective_orders-4.5.5 app/models/effective/tax_rate_calculator.rb
effective_orders-4.5.4 app/models/effective/tax_rate_calculator.rb
effective_orders-4.5.3 app/models/effective/tax_rate_calculator.rb
effective_orders-4.5.2 app/models/effective/tax_rate_calculator.rb
effective_orders-4.5.1 app/models/effective/tax_rate_calculator.rb
effective_orders-4.5.0 app/models/effective/tax_rate_calculator.rb
effective_orders-4.4.10 app/models/effective/tax_rate_calculator.rb
effective_orders-4.4.9 app/models/effective/tax_rate_calculator.rb
effective_orders-4.4.8 app/models/effective/tax_rate_calculator.rb
effective_orders-4.4.7 app/models/effective/tax_rate_calculator.rb
effective_orders-4.4.6 app/models/effective/tax_rate_calculator.rb
effective_orders-4.4.5 app/models/effective/tax_rate_calculator.rb
effective_orders-4.4.4 app/models/effective/tax_rate_calculator.rb
effective_orders-4.4.3 app/models/effective/tax_rate_calculator.rb
effective_orders-4.4.2 app/models/effective/tax_rate_calculator.rb
effective_orders-4.4.1 app/models/effective/tax_rate_calculator.rb
effective_orders-4.4.0 app/models/effective/tax_rate_calculator.rb