Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude line item additional taxes from unit cancel adjustment amount #3072

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion core/app/models/spree/unit_cancel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ def adjust!
# This method is used by Adjustment#update to recalculate the cost.
def compute_amount(line_item)
raise "Adjustable does not match line item" unless line_item == inventory_unit.line_item
-(line_item.total.to_d / line_item.inventory_units.not_canceled.reject(&:original_return_item ).size)

-weighted_line_item_amount(line_item)
end

private

def weighted_line_item_amount(line_item)
line_item.total_before_tax / quantity_of_line_item(line_item)
end

def quantity_of_line_item(line_item)
BigDecimal(line_item.inventory_units.not_canceled.reject(&:original_return_item).size)
end
end
41 changes: 41 additions & 0 deletions core/spec/models/spree/unit_cancel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,46 @@
expect(line_item.reload.total.to_d).to eq(0)
end
end

context 'when line item has additional taxes' do
let(:world_zone) { create(:zone, :with_country) }
let(:tax_category) { create :tax_category }
let(:product) { create :product, tax_category: tax_category }
let!(:additional_tax_rate) do
create(
:tax_rate,
name: 'Additional tax',
tax_categories: [tax_category],
zone: world_zone,
included_in_price: false,
amount: 0.15
)
end
let(:shipping_category) { create :shipping_category }
let(:shipping_method) do
create :shipping_method,
cost: 8.00,
shipping_categories: [shipping_category],
tax_category: tax_category,
zones: [world_zone]
end
let(:shipping_address) { create :address, country_iso_code: world_zone.countries.first.iso }
let(:order) do
create(
:order_with_line_items,
ship_address: shipping_address,
line_items_attributes: [{ product: product }]
)
end
let(:line_item) { order.line_items.first }
let(:inventory_unit) { line_item.inventory_units.first }

before { order.recalculate }

it 'does not include line item additional taxes' do
expect(line_item.additional_tax_total).not_to eq 0
expect(subject).to eq(-5.0)
end
end
end
end