Class: Workarea::Tax::RateLookup

Inherits:
Object
  • Object
show all
Defined in:
app/models/workarea/tax/rate_lookup.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(category:, price:, postal_code: nil, country: nil, region: nil) ⇒ RateLookup

Returns a new instance of RateLookup.



8
9
10
11
12
13
14
# File 'app/models/workarea/tax/rate_lookup.rb', line 8

def initialize(category: , price: , postal_code: nil, country: nil, region: nil)
  @category = category
  @price = price
  @postal_code = postal_code
  @country = country
  @region = region
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



4
5
6
# File 'app/models/workarea/tax/rate_lookup.rb', line 4

def category
  @category
end

#countryObject (readonly)

Returns the value of attribute country.



4
5
6
# File 'app/models/workarea/tax/rate_lookup.rb', line 4

def country
  @country
end

#postal_codeObject (readonly)

Returns the value of attribute postal_code.



4
5
6
# File 'app/models/workarea/tax/rate_lookup.rb', line 4

def postal_code
  @postal_code
end

#priceObject (readonly)

Returns the value of attribute price.



4
5
6
# File 'app/models/workarea/tax/rate_lookup.rb', line 4

def price
  @price
end

#regionObject (readonly)

Returns the value of attribute region.



4
5
6
# File 'app/models/workarea/tax/rate_lookup.rb', line 4

def region
  @region
end

Class Method Details

.find_best_rate(**options) ⇒ Object

Find the best available rate for the given parameters.



17
18
19
# File 'app/models/workarea/tax/rate_lookup.rb', line 17

def self.find_best_rate(**options)
  new(**options).best_available_rate
end

Instance Method Details

#best_available_rateObject



21
22
23
24
25
# File 'app/models/workarea/tax/rate_lookup.rb', line 21

def best_available_rate
  return if country.blank? && region.blank? && postal_code.blank?
  return tiered_rate if tiered?
  local_rate
end

#country_ratesObject



54
55
56
57
58
# File 'app/models/workarea/tax/rate_lookup.rb', line 54

def country_rates
  rates_in_country
    .where(region_clause)
    .where(postal_code_clause)
end

#local_rateObject



39
40
41
# File 'app/models/workarea/tax/rate_lookup.rb', line 39

def local_rate
  localized_rates.first
end

#localized_ratesObject



27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/workarea/tax/rate_lookup.rb', line 27

def localized_rates
  if country.present? && region.blank? && postal_code.blank?
    country_rates
  elsif country.present? && region.present? && postal_code.blank?
    region_rates
  elsif country == Country['US'] && no_local_postal_code_rates?
    regional_postal_code_rates
  else
    postal_code_rates
  end
end

#no_local_postal_code_rates?Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
# File 'app/models/workarea/tax/rate_lookup.rb', line 75

def no_local_postal_code_rates?
  rates_in_country
    .where(region_clause)
    .where(postal_code: postal_code)
    .none?
end

#postal_code_clauseObject



90
91
92
# File 'app/models/workarea/tax/rate_lookup.rb', line 90

def postal_code_clause
  { :postal_code.in => [nil, '', postal_code].uniq }
end

#postal_code_ratesObject



67
68
69
70
71
72
73
# File 'app/models/workarea/tax/rate_lookup.rb', line 67

def postal_code_rates
  rates_in_country
    .where(region_clause)
    .where(postal_code_clause)
    .sort(region: -1)
    .sort(postal_code: -1)
end

#rates_in_countryObject



50
51
52
# File 'app/models/workarea/tax/rate_lookup.rb', line 50

def rates_in_country
  rates.where(country: country)
end

#region_clauseObject



102
103
104
# File 'app/models/workarea/tax/rate_lookup.rb', line 102

def region_clause
  { :region.in => [nil, '', region].uniq }
end

#region_ratesObject



60
61
62
63
64
65
# File 'app/models/workarea/tax/rate_lookup.rb', line 60

def region_rates
  rates_in_country
    .where(region_clause)
    .where(postal_code_clause)
    .sort(region: -1)
end

#regional_postal_codeObject



98
99
100
# File 'app/models/workarea/tax/rate_lookup.rb', line 98

def regional_postal_code
  postal_code.first(5)
end

#regional_postal_code_clauseObject



94
95
96
# File 'app/models/workarea/tax/rate_lookup.rb', line 94

def regional_postal_code_clause
  { :postal_code.in => [nil, '', regional_postal_code].uniq }
end

#regional_postal_code_ratesObject



82
83
84
85
86
87
88
# File 'app/models/workarea/tax/rate_lookup.rb', line 82

def regional_postal_code_rates
  rates_in_country
    .where(region_clause)
    .where(regional_postal_code_clause)
    .sort(region: -1)
    .sort(postal_code: -1)
end

#tiered_rateObject



43
44
45
46
47
48
# File 'app/models/workarea/tax/rate_lookup.rb', line 43

def tiered_rate
  localized_rates.detect do |rate|
    (rate.tier_min.nil? || rate.tier_min <= price) &&
      (rate.tier_max.nil? || rate.tier_max >= price)
  end
end