Skip to content

Commit

Permalink
[WIP] Calculate order-level taxes in default
Browse files Browse the repository at this point in the history
Still need a way to figure out which rates should be applied to the
order.
  • Loading branch information
adammathys committed Aug 5, 2022
1 parent 601e4ff commit 1c50314
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions core/app/models/spree/tax_calculator/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def initialize(order)
def calculate
Spree::Tax::OrderTax.new(
order_id: order.id,
order_taxes: order_rates,
line_item_taxes: line_item_rates,
shipment_taxes: shipment_rates
)
Expand All @@ -34,6 +35,23 @@ def calculate

attr_reader :order

# Calculate the order-level taxes.
#
# @private
# @return [Array<Spree::Tax::ItemTax>] calculated taxes for the order
def order_rates
rates_for_order.map do |rate|
amount = rate.compute_amount(order)

Spree::Tax::ItemTax.new(
label: rate.adjustment_label(amount),
tax_rate: rate,
amount: amount,
included_in_price: rate.included_in_price
)
end
end

# Calculate the taxes for line items.
#
# @private
Expand Down Expand Up @@ -76,6 +94,19 @@ def calculate_rates(item)
)
end
end

# @private
# @return [Array<Spree::TaxRate>] rates that apply to an order
def rates_for_order
tax_category_ids = Set[
*@order.line_items.map(&:tax_category_id),
*@order.shipments.map(&:tax_category_id)
]
rates = Spree::TaxRate.active.order_level.for_address(@order.tax_address)
rates.select do |rate|
tax_category_ids.intersect?(rate.tax_category_ids.to_set)
end
end
end
end
end

0 comments on commit 1c50314

Please sign in to comment.