Merge pull request #11593 from rioug/11363-vouchers-fix-tax-included-in-price-amount

[Vouchers] Fix tax included in price amount
This commit is contained in:
Konrad
2023-10-13 18:22:11 +02:00
committed by GitHub
6 changed files with 66 additions and 7 deletions

View File

@@ -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

View File

@@ -53,7 +53,9 @@ module CheckoutHelper
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)

View File

@@ -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:

View File

@@ -5,6 +5,12 @@ require "spec_helper"
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)
@@ -32,5 +38,22 @@ describe Admin::OrdersHelper, type: :helper do
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

View File

@@ -15,12 +15,27 @@ describe CheckoutHelper, type: :helper do
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

View File

@@ -131,7 +131,7 @@ describe "As a consumer, I want to see adjustment breakdown" do
# 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