EnterpriseFees can inherit tax_category from product

This commit is contained in:
Rob Harrington
2016-02-05 09:26:27 +11:00
parent caa8818f02
commit 59745fbc73
5 changed files with 179 additions and 49 deletions

View File

@@ -5,7 +5,7 @@ module OpenFoodNetwork
AdjustmentMetadata.create! adjustment: a, enterprise: enterprise_fee.enterprise, fee_name: enterprise_fee.name, fee_type: enterprise_fee.fee_type, enterprise_role: role
a.set_absolute_included_tax! adjustment_tax(line_item.order, a)
a.set_absolute_included_tax! adjustment_tax(line_item, a)
end
def create_order_adjustment(order)
@@ -31,12 +31,22 @@ module OpenFoodNetwork
"#{enterprise_fee.fee_type} fee by #{role} #{enterprise_fee.enterprise.name}"
end
def adjustment_tax(order, adjustment)
tax_rates = enterprise_fee.tax_category ? enterprise_fee.tax_category.tax_rates.match(order) : []
def adjustment_tax(adjustable, adjustment)
tax_rates = rates_for(adjustable)
tax_rates.select(&:included_in_price).sum do |rate|
rate.compute_tax adjustment.amount
end
end
def rates_for(adjustable)
case adjustable
when Spree::LineItem
tax_category = enterprise_fee.inherits_tax_category? ? adjustable.product.tax_category : enterprise_fee.tax_category
return tax_category ? tax_category.tax_rates.match(adjustable.order) : []
when Spree::Order
return enterprise_fee.tax_category ? enterprise_fee.tax_category.tax_rates.match(adjustable) : []
end
end
end
end