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

[Split Checkout] Recalculate tax on summary step #9631

Merged
merged 5 commits into from
Sep 8, 2022
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
6 changes: 6 additions & 0 deletions app/controllers/split_checkout_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class SplitCheckoutController < ::BaseController
def edit
redirect_to_step_based_on_order unless params[:step]
check_step if params[:step]
recalculate_tax if params[:step] == "summary"
end

def update
Expand Down Expand Up @@ -155,4 +156,9 @@ def check_step
redirect_to checkout_step_path(:payment) if params[:step] == "summary"
end
end

def recalculate_tax
@order.create_tax_charge!
@order.update_order!
end
end
63 changes: 28 additions & 35 deletions spec/system/consumer/split_checkout_tax_not_incl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@
include AuthenticationHelper
include WebHelper

let!(:address_within_zone) { create(:address, state_id: Spree::State.first.id) }
let!(:address_outside_zone) { create(:address, state_id: Spree::State.second.id) }
let!(:user_within_zone) {
create(:user, bill_address_id: address_within_zone.id,
ship_address_id: address_within_zone.id)
let!(:address_within_zone) { create(:address, state: Spree::State.first) }
let!(:address_outside_zone) { create(:address, state: Spree::State.second) }
let(:user_within_zone) {
create(:user, bill_address: address_within_zone,
ship_address: address_within_zone)
}
let!(:user_outside_zone) {
create(:user, bill_address_id: address_outside_zone.id,
ship_address_id: address_outside_zone.id)
let(:user_outside_zone) {
create(:user, bill_address: address_outside_zone,
ship_address: address_outside_zone)
}
let!(:zone) { create(:zone_with_state_member, name: 'Victoria', default_tax: false) }
let!(:tax_category) { create(:tax_category, name: "Veggies", is_default: "f") }
let!(:tax_rate) {
create(:tax_rate, name: "Tax rate - included or not", amount: 0.13,
zone_id: zone.id, tax_category_id: tax_category.id, included_in_price: false)
zone: zone, tax_category: tax_category, included_in_price: false)
}
let(:distributor) { create(:distributor_enterprise, charges_sales_tax: true) }
let(:supplier) { create(:supplier_enterprise) }
let!(:product_with_tax) {
create(:simple_product, supplier: supplier, price: 10, tax_category_id: tax_category.id)
let(:product_with_tax) {
create(:simple_product, supplier: supplier, price: 10, tax_category: tax_category)
}
let!(:variant_with_tax) { product_with_tax.variants.first }
let!(:order_cycle) {
let(:variant_with_tax) { product_with_tax.variants.first }
let(:order_cycle) {
create(:simple_order_cycle, suppliers: [supplier], distributors: [distributor],
coordinator: distributor, variants: [variant_with_tax])
}
Expand All @@ -46,12 +46,12 @@
name: "Payment without Fee", description: "Payment without fee",
calculator: Calculator::FlatRate.new(preferred_amount: 0.00))
}
let!(:order_within_zone) {
let(:order_within_zone) {
create(:order, order_cycle: order_cycle, distributor: distributor, user: user_within_zone,
bill_address: address_within_zone, ship_address: address_within_zone,
state: "cart", line_items: [create(:line_item, variant: variant_with_tax)])
}
let!(:order_outside_zone) {
let(:order_outside_zone) {
create(:order, order_cycle: order_cycle, distributor: distributor, user: user_outside_zone,
bill_address: address_outside_zone, ship_address: address_outside_zone,
state: "cart", line_items: [create(:line_item, variant: variant_with_tax)])
Expand Down Expand Up @@ -107,7 +107,8 @@
click_on "Place order now"

# DB checks
assert_db_tax_added
order_within_zone.reload
expect(order_within_zone.additional_tax_total).to eq(1.3)

# UI checks
expect(page).to have_selector('#order_total', text: with_currency(11.30))
Expand All @@ -134,7 +135,8 @@
click_on "Complete order"

# DB checks
assert_db_tax_added
order_within_zone.reload
expect(order_within_zone.additional_tax_total).to eq(1.3)

# UI checks
expect(page).to have_content("Confirmed")
Expand Down Expand Up @@ -164,7 +166,9 @@
click_on "Place order now"

# DB checks
assert_db_no_tax_added
order_outside_zone.reload
expect(order_outside_zone.included_tax_total).to eq(0.0)
expect(order_outside_zone.additional_tax_total).to eq(0.0)

# UI checks
expect(page).to have_content("Confirmed")
Expand Down Expand Up @@ -192,7 +196,9 @@
click_on "Complete order"

# DB checks
assert_db_no_tax_added
order_outside_zone.reload
expect(order_outside_zone.included_tax_total).to eq(0.0)
expect(order_outside_zone.additional_tax_total).to eq(0.0)

# UI checks
expect(page).to have_content("Confirmed")
Expand All @@ -217,7 +223,6 @@
end

it "should re-calculate the tax accordingly" do
pending("#9153")
select "Victoria", from: "order_bill_address_attributes_state_id"

# it should not be necessary to save as new default bill address
Expand All @@ -236,7 +241,9 @@
click_on "Complete order"

# DB checks
assert_db_tax_added
order_outside_zone.reload
expect(order_outside_zone.included_tax_total).to eq(0.0)
expect(order_outside_zone.additional_tax_total).to eq(1.3)

# UI checks - Order confirmation page should reflect changes
expect(page).to have_content("Confirmed")
Expand All @@ -247,18 +254,4 @@
end
end
end

private

def assert_db_tax_added
order_within_zone.reload
expect(order_outside_zone.included_tax_total).to eq(0.0)
expect(order_within_zone.additional_tax_total).to eq(1.3)
end

def assert_db_no_tax_added
order_outside_zone.reload
expect(order_outside_zone.included_tax_total).to eq(0.0)
expect(order_outside_zone.additional_tax_total).to eq(0.0)
end
end