module Easyship::SalesTax::Calculator

Constants

VERSION

Public Class Methods

calculate(origin_country_alpha2: nil, destination_country_alpha2: nil, origin_state: nil, destination_state: nil, fees: nil) click to toggle source
# File lib/easyship/sales_tax/calculator.rb, line 9
def self.calculate(origin_country_alpha2: nil, destination_country_alpha2: nil, origin_state: nil, destination_state: nil, fees: nil)
  return fallback_value if origin_country_alpha2.nil? || origin_country_alpha2.empty? || destination_country_alpha2.nil? || destination_country_alpha2.empty?
  return fallback_value unless fees.is_a?(Fee)

  if domestic?(origin_country_alpha2, destination_country_alpha2) || within_eu?(origin_country_alpha2, destination_country_alpha2)
    domestic_value(origin_country_alpha2: origin_country_alpha2, origin_state: origin_state, destination_state: destination_state, fees: fees)
  else
    fallback_value
  end
rescue
  fallback_value
end
domestic?(origin_country_alpha2 = nil, destination_country_alpha2 = nil) click to toggle source
# File lib/easyship/sales_tax/calculator.rb, line 58
def self.domestic?(origin_country_alpha2 = nil, destination_country_alpha2 = nil)
  !origin_country_alpha2.nil? && !origin_country_alpha2.empty? && origin_country_alpha2 == destination_country_alpha2
end
domestic_value(origin_country_alpha2: nil, origin_state: nil, destination_state: nil, fees: nil) click to toggle source
# File lib/easyship/sales_tax/calculator.rb, line 22
def self.domestic_value(origin_country_alpha2: nil, origin_state: nil, destination_state: nil, fees: nil)
  domestic_rates = Formula::DOMESTIC[origin_country_alpha2.to_s]
  return fallback_value if domestic_rates.nil? || domestic_rates.empty?

  {
    sales_tax: tax_calculate(domestic_rates[:sales_tax], nil, destination_state, fees, false),
    provincial_sales_tax: tax_calculate(domestic_rates[:provincial_sales_taxes], origin_state, destination_state, fees, true)
  }
end
fallback_value() click to toggle source
# File lib/easyship/sales_tax/calculator.rb, line 32
def self.fallback_value
  { sales_tax: 0, provincial_sales_tax: 0 }
end
in_eu?(country_alpha2) click to toggle source
# File lib/easyship/sales_tax/calculator.rb, line 54
def self.in_eu?(country_alpha2)
 %w(NL LU EE LT HU BE PT SK HR CZ IT FI PL MT DE SI RO BG AT SE CY DK FR IE ES GR LV).include?(country_alpha2)
end
sales_tax_website_name(country_alpha2) click to toggle source
# File lib/easyship/sales_tax/calculator.rb, line 49
def self.sales_tax_website_name(country_alpha2)
  return if Formula::DOMESTIC[country_alpha2.to_s].nil? || Formula::DOMESTIC[country_alpha2.to_s].empty?
  Formula::DOMESTIC[country_alpha2][:sales_tax_website_name]
end
tax_calculate(formula, origin_state, destination_state, fees, is_provincial_sales_taxes) click to toggle source
# File lib/easyship/sales_tax/calculator.rb, line 36
def self.tax_calculate(formula, origin_state, destination_state, fees, is_provincial_sales_taxes)
  if (tax_destination_formula = formula[destination_state.to_s.upcase])
    # ONLY CA has provincial_sales_taxes(QST/PST) and it only apply when ship within same state
    # https://www.fedex.com/en-ca/news/canadian-sales-taxes.html
    return 0 if origin_state != destination_state && is_provincial_sales_taxes
    fees.to_h.inject(0) { |sum, (k, v)| sum + (tax_destination_formula[k] * v.to_f) }.round(2)
  elsif formula[:_ALL]
    fees.to_h.inject(0) { |sum, (k, v)| sum + (formula[:_ALL][k] * v.to_f) }.round(2)
  else
    0
  end
end
within_eu?(origin_country_alpha2 = nil, destination_country_alpha2 = nil) click to toggle source
# File lib/easyship/sales_tax/calculator.rb, line 62
def self.within_eu?(origin_country_alpha2 = nil, destination_country_alpha2 = nil)
  in_eu?(origin_country_alpha2) && in_eu?(destination_country_alpha2)
end