Module: Easyship::SalesTax::Calculator
- Defined in:
- lib/easyship/sales_tax/calculator.rb,
lib/easyship/sales_tax/calculator/version.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"0.3.0"
Class Method Summary collapse
- .calculate(origin_country_alpha2: nil, destination_country_alpha2: nil, origin_state: nil, destination_state: nil, fees: nil) ⇒ Object
- .domestic?(origin_country_alpha2 = nil, destination_country_alpha2 = nil) ⇒ Boolean
- .domestic_value(origin_country_alpha2: nil, origin_state: nil, destination_state: nil, fees: nil) ⇒ Object
- .fallback_value ⇒ Object
- .in_eu?(country_alpha2) ⇒ Boolean
- .sales_tax_website_name(country_alpha2) ⇒ Object
- .tax_calculate(formula, origin_state, destination_state, fees, is_provincial_sales_taxes) ⇒ Object
- .within_eu?(origin_country_alpha2 = nil, destination_country_alpha2 = nil) ⇒ Boolean
Class Method Details
.calculate(origin_country_alpha2: nil, destination_country_alpha2: nil, origin_state: nil, destination_state: nil, fees: nil) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# 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) ⇒ Boolean
58 59 60 |
# 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) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# 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 ⇒ Object
32 33 34 |
# 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) ⇒ Boolean
54 55 56 |
# File 'lib/easyship/sales_tax/calculator.rb', line 54 def self.in_eu?(country_alpha2) %w(NL LU EE LT HU BE PT GB 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) ⇒ Object
49 50 51 52 |
# 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) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# 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) ⇒ Boolean
62 63 64 |
# 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 |