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

[Vouchers] Fix tax included in price amount #11593

Merged
Show file tree
Hide file tree
Changes from 7 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
20 changes: 19 additions & 1 deletion app/helpers/admin/orders_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,30 @@

module Admin
module OrdersHelper
AdjustmentData = Struct.new(:label, :amount)

# Adjustments to display under "Order adjustments".
#
# We exclude shipping method adjustments because they are displayed in a
# separate table together with the order line items.
def order_adjustments_for_display(order)
order.adjustments + order.all_adjustments.payment_fee.eligible
order.adjustments +
voucher_included_tax_representations(order) +
order.all_adjustments.payment_fee.eligible
Comment on lines +12 to +14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should voucher_included_tax_representations(order) be the last one in the list? It looks like it was before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that that was looking odd. It seems clearer to me to put the voucher tax under the voucher.

end

def voucher_included_tax_representations(order)
return [] unless VoucherAdjustmentsService.new(order).voucher_included_tax.negative?

adjustment = order.voucher_adjustments.first

[
AdjustmentData.new(
I18n.t("admin.orders.edit.voucher_tax_included_in_price",
label: adjustment.label),
adjustment.included_tax
)
]
end
end
end
4 changes: 3 additions & 1 deletion app/helpers/checkout_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ def display_checkout_subtotal(order)
end

def display_checkout_tax_total(order)
Spree::Money.new order.total_tax, currency: order.currency
total_tax = order.total_tax + VoucherAdjustmentsService.new(order).voucher_included_tax

Spree::Money.new(total_tax, currency: order.currency)
end

def display_checkout_taxes_hash(order)
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,7 @@ en:
orders:
edit:
order_sure_want_to: Are you sure you want to %{event} this order?
voucher_tax_included_in_price: "%{label} (tax included in voucher)"
dacook marked this conversation as resolved.
Show resolved Hide resolved
invoice_email_sent: 'Invoice email has been sent'
order_email_resent: 'Order email has been resent'
bulk_management:
Expand Down
23 changes: 23 additions & 0 deletions spec/helpers/admin/orders_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
describe Admin::OrdersHelper, type: :helper do
describe "#order_adjustments_for_display" do
let(:order) { create(:order) }
let(:service) { instance_double(VoucherAdjustmentsService, voucher_included_tax:) }
let(:voucher_included_tax) { 0.0 }

before do
allow(VoucherAdjustmentsService).to receive(:new).and_return(service)
end

it "selects eligible adjustments" do
adjustment = create(:adjustment, order:, adjustable: order, amount: 1)
Expand Down Expand Up @@ -32,5 +38,22 @@

expect(helper.order_adjustments_for_display(order)).to eq []
end

context "with a voucher with tax included in price" do
let(:enterprise) { build(:enterprise) }
let(:voucher) do
create(:voucher_flat_rate, code: 'new_code', enterprise:, amount: 10)
end
let(:voucher_included_tax) { -0.5 }

it "includes a fake tax voucher adjustment" do
voucher_adjustment = voucher.create_adjustment(voucher.code, order)
voucher_adjustment.update(included_tax: voucher_included_tax)

fake_adjustment = helper.order_adjustments_for_display(order).last
expect(fake_adjustment.label).to eq("new_code (tax included in price)")
expect(fake_adjustment.amount).to eq(-0.5)
end
end
end
end
23 changes: 19 additions & 4 deletions spec/helpers/checkout_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,27 @@
helper.validated_input("test", "foo", type: :email)
end

describe "displaying the tax total for an order" do
let(:order) { double(:order, total_tax: 123.45, currency: 'AUD') }
describe "#display_checkout_tax_total" do
subject(:display_checkout_tax_total) { helper.display_checkout_tax_total(order) }

let(:order) { instance_double(Spree::Order, total_tax: 123.45, currency: 'AUD') }
let(:service) { instance_double(VoucherAdjustmentsService, voucher_included_tax: ) }
Copy link
Member

@dacook dacook Oct 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have set up real data and used the real VoucherAdjustmentsService. What if the method VoucherAdjustmentsService#voucher_included_tax go renamed one day, or returned a different value? This test would still pass.

But maybe that's just opinion :)

If it's because the test data is complicated to set up, then maybe we need to create a factory for it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instance_double verifies that the methods are present. https://www.simplybusiness.co.uk/about-us/tech/2020/05/rspec-instance-doubles/

But in general I agree that mocking is to be avoided if possible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I didn't know that!

let(:voucher_included_tax) { 0.0 }

before do
allow(VoucherAdjustmentsService).to receive(:new).and_return(service)
end

it "retrieves the total tax on the order" do
expect(helper.display_checkout_tax_total(order)).to eq(Spree::Money.new(123.45,
currency: 'AUD'))
expect(display_checkout_tax_total).to eq(Spree::Money.new(123.45, currency: 'AUD'))
end

context "with a voucher" do
let(:voucher_included_tax) { -0.45 }

it "displays the discounted total tax" do
expect(display_checkout_tax_total).to eq(Spree::Money.new(123.00, currency: 'AUD'))
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/system/consumer/checkout/tax_incl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
# UI checks
expect(page).to have_content("Confirmed")
expect(page).to have_selector('#order_total', text: with_currency(0.00))
expect(page).to have_selector('#tax-row', text: with_currency(1.15))
expect(page).to have_selector('#tax-row', text: with_currency(0.00))

# Voucher
within "#line-items" do
Expand Down