Update OrderAdjustmentsFetcher

This commit is contained in:
Matt-Yorkley
2021-02-28 16:04:01 +00:00
parent 064f7582cc
commit baaee1baab
2 changed files with 24 additions and 7 deletions

View File

@@ -21,7 +21,6 @@ class OrderTaxAdjustmentsFetcher
def all
Spree::Adjustment
.with_tax
.where(order_adjustments.or(line_item_adjustments).or(shipment_adjustments))
.order('created_at ASC')
end
@@ -50,11 +49,27 @@ class OrderTaxAdjustmentsFetcher
Hash[tax_rates.collect do |tax_rate|
tax_amount = if tax_rates.one?
adjustment.included_tax
adjustment_tax_amount(adjustment)
else
tax_rate.compute_tax(adjustment.amount)
end
[tax_rate, tax_amount]
end]
end
def adjustment_tax_amount(adjustment)
if no_tax_adjustments?(adjustment)
adjustment.included_tax
else
adjustment.amount
end
end
def no_tax_adjustments?(adjustment)
# Enterprise Fees, Admin Adjustments, and Shipping Fees currently do not have tax adjustments.
# The tax amount is stored in the included_tax attribute.
adjustment.originator_type == "EnterpriseFee" ||
adjustment.originator_type == "Spree::ShippingMethod" ||
(adjustment.source_type.nil? && adjustment.originator_type.nil?)
end
end

View File

@@ -44,8 +44,9 @@ describe OrderTaxAdjustmentsFetcher do
tax_category: tax_category20,
calculator: Calculator::FlatRate.new(preferred_amount: 48.0))
end
let(:additional_adjustment) do
create(:adjustment, amount: 50.0, included_tax: tax_rate25.compute_tax(50.0))
let(:admin_adjustment) do
create(:adjustment, order: order, amount: 50.0, included_tax: tax_rate25.compute_tax(50.0),
source: nil, label: "Admin Adjustment")
end
let(:order_cycle) do
@@ -62,8 +63,7 @@ describe OrderTaxAdjustmentsFetcher do
line_items: [line_item1, line_item2],
bill_address: create(:address),
order_cycle: order_cycle,
distributor: coordinator,
adjustments: [additional_adjustment]
distributor: coordinator
)
end
@@ -80,6 +80,8 @@ describe OrderTaxAdjustmentsFetcher do
end
before do
order.reload
order.adjustments << admin_adjustment
order.create_tax_charge!
order.recreate_all_fees!
end
@@ -102,7 +104,7 @@ describe OrderTaxAdjustmentsFetcher do
expect(subject[tax_rate20]).to eq(8.0)
end
it "contains tax on order adjustment" do
it "contains tax on admin adjustment" do
expect(subject[tax_rate25]).to eq(10.0)
end
end