From 3169a384ba94b1cf860021efbd7995698ed91d37 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Tue, 18 Dec 2018 17:45:12 +1100 Subject: [PATCH] Find approximate tax rate if no other is found There are several cases in which a tax rate is not associated with an adjustment. We find the closest one for reporting here. It is not a good solution, but a workaround introduced for reporting: https://github.com/openfoodfoundation/openfoodnetwork/pull/1496 --- app/services/tax_rate_finder.rb | 11 +++++++---- spec/services/tax_rate_finder_spec.rb | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/services/tax_rate_finder.rb b/app/services/tax_rate_finder.rb index b0a5c65446..ede2e1c629 100644 --- a/app/services/tax_rate_finder.rb +++ b/app/services/tax_rate_finder.rb @@ -4,6 +4,13 @@ class TaxRateFinder # @return [Array] def tax_rates(originator, source, amount, included_tax) + find_associated_tax_rate(originator, source) || + find_closest_tax_rates_from_included_tax(amount, included_tax) + end + + private + + def find_associated_tax_rate(originator, source) case originator when Spree::TaxRate [originator] @@ -15,13 +22,9 @@ class TaxRateFinder when Spree::Order originator.tax_category ? originator.tax_category.tax_rates.match(source) : [] end - else - find_closest_tax_rates_from_included_tax(amount, included_tax) end end - private - # shipping fees and adjustments created from the admin panel have # taxes set at creation in the included_tax field without relation # to the corresponding TaxRate, so we look for the closest one diff --git a/spec/services/tax_rate_finder_spec.rb b/spec/services/tax_rate_finder_spec.rb index 7debe8d154..4b3790d4ae 100644 --- a/spec/services/tax_rate_finder_spec.rb +++ b/spec/services/tax_rate_finder_spec.rb @@ -51,7 +51,7 @@ describe TaxRateFinder do # There is a bug that leaves orphan adjustments on an order after # associated line items have been removed. # https://github.com/openfoodfoundation/openfoodnetwork/issues/3127 - pending "deals with a missing line item" do + it "deals with a missing line item" do rates = TaxRateFinder.new.tax_rates( enterprise_fee, nil,