diff --git a/app/controllers/split_checkout_controller.rb b/app/controllers/split_checkout_controller.rb index d65a661f32..63bf2db6a8 100644 --- a/app/controllers/split_checkout_controller.rb +++ b/app/controllers/split_checkout_controller.rb @@ -22,6 +22,9 @@ class SplitCheckoutController < ::BaseController before_action :hide_ofn_navigation, only: [:edit, :update] def edit + # TODO Calculate percent amount covered by the voucher for display purposes + @voucher_adjustment = @order.vouchers.first + redirect_to_step_based_on_order unless params[:step] check_step if params[:step] recalculate_tax if params[:step] == "summary" @@ -30,6 +33,8 @@ class SplitCheckoutController < ::BaseController end def update + return add_voucher if payment_step? and params[:order][:voucher_code] + if confirm_order || update_order return if performed? @@ -179,10 +184,30 @@ class SplitCheckoutController < ::BaseController selected_shipping_method.first.require_ship_address == false end + def add_voucher + # Fetch Voucher + voucher = Voucher.find_by(code: params[:order][:voucher_code], enterprise: @order.distributor) + + if voucher.nil? + @order.errors.add(:voucher, I18n.t('split_checkout.errors.voucher_not_found')) + return render_error + end + + # Create adjustment + # TODO add tax part of adjustement + voucher.create_adjustment(voucher.code, @order) + + redirect_to checkout_step_path(:payment) + end + def summary_step? params[:step] == "summary" end + def payment_step? + params[:step] == "payment" + end + def advance_order_state return if @order.complete? diff --git a/app/views/split_checkout/_payment.html.haml b/app/views/split_checkout/_payment.html.haml index 6c743a33cd..aeeccaa6ba 100644 --- a/app/views/split_checkout/_payment.html.haml +++ b/app/views/split_checkout/_payment.html.haml @@ -6,9 +6,12 @@ .checkout-input.voucher .two-columns-inputs.voucher - = f.text_field :voucher_code, { placeholder: t("split_checkout.step2.voucher.placeholder"), class: "voucher" } - - # TODO: enable button when code entered - = f.submit t("split_checkout.step2.voucher.apply"), class: "button cancel voucher", disabled: true + -if @voucher_adjustment.present? + = "Voucher used: #{@voucher_adjustment.label}" + - else + = f.text_field :voucher_code, { placeholder: t("split_checkout.step2.voucher.placeholder"), class: "voucher" } + - # TODO: enable button when code entered + = f.submit t("split_checkout.step2.voucher.apply"), class: "button cancel voucher", disabled: false %div.checkout-title = t("split_checkout.step2.payment_method.title") diff --git a/config/locales/en.yml b/config/locales/en.yml index 993f2f8406..bfdab857d2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2091,6 +2091,7 @@ en: select_a_shipping_method: Select a shipping method select_a_payment_method: Select a payment method no_shipping_methods_available: Checkout is not possible due to absence of shipping options. Please contact the shop owner. + voucher_not_found: Not found order_paid: PAID order_not_paid: NOT PAID order_total: Total order diff --git a/spec/system/consumer/split_checkout_spec.rb b/spec/system/consumer/split_checkout_spec.rb index d274bafd7a..9fd3f90568 100644 --- a/spec/system/consumer/split_checkout_spec.rb +++ b/spec/system/consumer/split_checkout_spec.rb @@ -729,6 +729,24 @@ describe "As a consumer, I want to checkout my order" do it "shows voucher input" do expect(page).to have_content "Apply voucher" end + + describe "adding voucher to the order" do + context "voucher doesn't exist" do + it "show an error" do + fill_in "Enter voucher code", with: "non_code" + click_button("Apply") + + expect(page).to have_content("Voucher Not found") + end + end + + it "adds a voucher to the order" do + fill_in "Enter voucher code", with: voucher.code + click_button("Apply") + + expect(page).to have_content("Voucher used: some_code") + end + end end end