Skip to content

Commit

Permalink
Merge pull request #11593 from rioug/11363-vouchers-fix-tax-included-…
Browse files Browse the repository at this point in the history
…in-price-amount

[Vouchers] Fix tax included in price amount
  • Loading branch information
drummer83 authored Oct 13, 2023
2 parents a13c954 + 477ca39 commit d7b234c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 7 deletions.
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
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 @@ -982,6 +982,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)"
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 voucher)")
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: ) }
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

0 comments on commit d7b234c

Please sign in to comment.