Class: LineItem
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- LineItem
- Defined in:
- lib/forge/app/models/line_item.rb
Instance Method Summary collapse
-
#applicable_tax_rate(billing_address) ⇒ Object
collect all of the applicable tax rates based on where the person purchasing it is located, then enumerate through them and add up their rates.
- #total_price ⇒ Object
- #total_price_with_tax(billing_address) ⇒ Object
- #total_tax(billing_address) ⇒ Object
Instance Method Details
#applicable_tax_rate(billing_address) ⇒ Object
collect all of the applicable tax rates based on where the person purchasing it is located, then enumerate through them and add up their rates
25 26 27 28 29 30 31 |
# File 'lib/forge/app/models/line_item.rb', line 25 def applicable_tax_rate(billing_address) raise "billing address must be an Address" unless billing_address.kind_of? Address applicable_tax_rates = self.product.tax_rates.where(country_id: billing_address.country_id).to_a applicable_tax_rates += self.product.tax_rates.where(province_id: billing_address.province_id).to_a if billing_address.province_id # use uniq to ensure we don't charge tax too many times - it's possible to get overlapping rates between a country and its provinces applicable_tax_rates.uniq.inject(0.0) { |sum, applicable_tax_rate| sum += applicable_tax_rate.rate } end |
#total_price ⇒ Object
11 12 13 |
# File 'lib/forge/app/models/line_item.rb', line 11 def total_price quantity * price end |
#total_price_with_tax(billing_address) ⇒ Object
19 20 21 |
# File 'lib/forge/app/models/line_item.rb', line 19 def total_price_with_tax(billing_address) total_price + total_tax(billing_address) end |
#total_tax(billing_address) ⇒ Object
15 16 17 |
# File 'lib/forge/app/models/line_item.rb', line 15 def total_tax(billing_address) total_price * (applicable_tax_rate(billing_address) / 100.0).round(2) end |