diff --git a/app/controllers/voucher_adjustments_controller.rb b/app/controllers/voucher_adjustments_controller.rb index 9b6a2bac26d..f56b64b52c5 100644 --- a/app/controllers/voucher_adjustments_controller.rb +++ b/app/controllers/voucher_adjustments_controller.rb @@ -13,6 +13,7 @@ def create def destroy @order.voucher_adjustments.find_by(id: params[:id])&.destroy + @order.update_order! # adjustments, totals and states update_payment_section end @@ -38,7 +39,7 @@ def add_voucher adjustment = voucher.create_adjustment(voucher.code, @order) - unless adjustment.valid? + unless adjustment.persisted? @order.errors.add(:voucher_code, I18n.t('split_checkout.errors.add_voucher_error')) adjustment.errors.each { |error| @order.errors.import(error) } return false diff --git a/app/models/voucher.rb b/app/models/voucher.rb index 1551c6807b7..f7a9e933d7c 100644 --- a/app/models/voucher.rb +++ b/app/models/voucher.rb @@ -21,11 +21,9 @@ def display_value # but vouchers have complicated calculation so we can't easily use Spree::Calculator. We keep # the same method to stay as consistent as possible. # - # Creates a new voucher adjustment for the given order with an amount of 0 - # The amount will be calculated via VoucherAdjustmentsService#update def create_adjustment(label, order) adjustment_attributes = { - amount: 0, + amount: 0, # Amount is updated by VoucherAdjustmentsService below originator: self, order: order, label: label, @@ -34,7 +32,14 @@ def create_adjustment(label, order) tax_category: nil } - order.adjustments.create(adjustment_attributes) + adjustment = order.adjustments.new(adjustment_attributes) + + if adjustment.save + VoucherAdjustmentsService.new(order).update + order.update_totals_and_states + end + + adjustment end # We limit adjustment to the maximum amount needed to cover the order, ie if the voucher diff --git a/spec/models/voucher_spec.rb b/spec/models/voucher_spec.rb index 0c62a814968..fd1d3f52fe1 100644 --- a/spec/models/voucher_spec.rb +++ b/spec/models/voucher_spec.rb @@ -56,5 +56,9 @@ it 'sets the adjustment as open' do expect(adjustment.state).to eq("open") end + + it 'updates order totals' do + expect{ subject }.to change(order, :total).by(-25) + end end end diff --git a/spec/system/consumer/split_checkout_spec.rb b/spec/system/consumer/split_checkout_spec.rb index f2c563bf52a..52af54ec146 100644 --- a/spec/system/consumer/split_checkout_spec.rb +++ b/spec/system/consumer/split_checkout_spec.rb @@ -724,15 +724,17 @@ create(:voucher, code: 'some_code', enterprise: distributor, amount: 15) end - before do - visit checkout_step_path(:payment) - end - it "shows voucher input" do + visit checkout_step_path(:payment) + expect(page).to have_field "Enter voucher code" expect(page).to have_content "Apply voucher" end describe "adding voucher to the order" do + before do + visit checkout_step_path(:payment) + end + shared_examples "adding voucher to the order" do before do fill_in "Enter voucher code", with: "some_code" @@ -764,6 +766,16 @@ "you may not be able to spend the remaining value." ) end + + it "proceeds without requiring payment" do + fill_in "Enter voucher code", with: "some_code" + click_button("Apply") + + expect(page).to have_content "No payment required" + click_button "Next - Order summary" + # Expect to be on the Order Summary page + expect(page).to have_content "Delivery details" + end end context "voucher doesn't exist" do @@ -779,21 +791,42 @@ describe "removing voucher from order" do before do voucher.create_adjustment(voucher.code, order) - # Reload the page so we pickup the voucher visit checkout_step_path(:payment) - end - it "removes voucher" do accept_confirm "Are you sure you want to remove the voucher?" do click_on "Remove code" end + end + it "removes voucher" do within '#voucher-section' do expect(page).to have_button("Apply", disabled: true) + expect(page).to have_field "Enter voucher code" # Currently no confirmation msg end expect(order.voucher_adjustments.length).to eq(0) end + + it "can re-enter a voucher" do + # Re-enter a voucher code + fill_in "Enter voucher code", with: "some_code" + click_button("Apply") + + expect(page).to have_content("$15.00 Voucher") + expect(order.reload.voucher_adjustments.length).to eq(1) + expect(page).to have_content "No payment required" + + click_button "Next - Order summary" + # Expect to be on the Order Summary page + expect(page).to have_content "Delivery details" + end + + it "can proceed with payment" do + choose "Payment with Fee" + click_button "Next - Order summary" + # Expect to be on the Order Summary page + expect(page).to have_content "Delivery details" + end end end end