mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-29 21:17:17 +00:00
Use OrderTaxAdjustmentsFetcher service
This commit is contained in:
@@ -44,7 +44,7 @@ module CheckoutHelper
|
||||
end
|
||||
|
||||
def display_checkout_taxes_hash(order)
|
||||
order.tax_adjustment_totals.each_with_object({}) do |(tax_rate, tax_amount), hash|
|
||||
OrderTaxAdjustmentsFetcher.new(order).totals.each_with_object({}) do |(tax_rate, tax_amount), hash|
|
||||
hash[number_to_percentage(tax_rate.amount * 100, precision: 1)] = Spree::Money.new tax_amount, currency: order.currency
|
||||
end
|
||||
end
|
||||
|
||||
@@ -18,7 +18,7 @@ module Spree
|
||||
where(originator_type: 'Spree::TaxRate', adjustable_type: 'Spree::LineItem')
|
||||
}
|
||||
|
||||
scope :with_tax, -> { where('spree_adjustments.included_tax > 0') }
|
||||
scope :with_tax, -> { where('spree_adjustments.included_tax <> 0') }
|
||||
scope :without_tax, -> { where('spree_adjustments.included_tax = 0') }
|
||||
scope :payment_fee, -> { where(AdjustmentScopes::PAYMENT_FEE_SCOPE) }
|
||||
scope :shipping, -> { where(AdjustmentScopes::SHIPPING_SCOPE) }
|
||||
|
||||
@@ -312,24 +312,6 @@ Spree::Order.class_eval do
|
||||
(adjustments + price_adjustments).sum(&:included_tax)
|
||||
end
|
||||
|
||||
def tax_adjustments
|
||||
adjustments + price_adjustments
|
||||
end
|
||||
|
||||
def tax_adjustment_totals
|
||||
tax_adjustments.each_with_object({}) do |adjustment, hash|
|
||||
# No need of dealing with a missing Tax Rate if no tax setup
|
||||
next if adjustment.included_tax.zero?
|
||||
|
||||
tax_rates = TaxRateFinder.tax_rates_of(adjustment)
|
||||
tax_rates_hash = Hash[tax_rates.collect do |tax_rate|
|
||||
tax_amount = tax_rates.one? ? adjustment.included_tax : tax_rate.compute_tax(adjustment.amount)
|
||||
[tax_rate, tax_amount]
|
||||
end]
|
||||
hash.update(tax_rates_hash) { |_tax_rate, amount1, amount2| amount1 + amount2 }
|
||||
end
|
||||
end
|
||||
|
||||
def has_taxes_included
|
||||
!line_items.with_tax.empty?
|
||||
end
|
||||
|
||||
28
app/services/order_tax_adjustments_fetcher.rb
Normal file
28
app/services/order_tax_adjustments_fetcher.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
# This class will be used to get Tax Adjustments related to an order,
|
||||
# and proceed basic calcultation over them.
|
||||
|
||||
class OrderTaxAdjustmentsFetcher
|
||||
def initialize(order)
|
||||
@order = order
|
||||
end
|
||||
|
||||
def totals
|
||||
all.each_with_object({}) do |adjustment, hash|
|
||||
tax_rates = TaxRateFinder.tax_rates_of(adjustment)
|
||||
tax_rates_hash = Hash[tax_rates.collect do |tax_rate|
|
||||
tax_amount = tax_rates.one? ? adjustment.included_tax : tax_rate.compute_tax(adjustment.amount)
|
||||
[tax_rate, tax_amount]
|
||||
end]
|
||||
hash.update(tax_rates_hash) { |_tax_rate, amount1, amount2| amount1 + amount2 }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :order
|
||||
|
||||
def all
|
||||
order.adjustments.with_tax +
|
||||
order.line_items.includes(:adjustments).map { |li| li.adjustments.with_tax }.flatten
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user