First take at adding a voucher to an order

Use voucher.create_adjustment to add an adjustment to the order
Currently doesn't take into account tax part
This commit is contained in:
Gaetan Craig-Riou
2023-04-17 14:27:34 +10:00
committed by Maikel Linke
parent 3ec58d8791
commit a9d9b33f7d
4 changed files with 50 additions and 3 deletions

View File

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

View File

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

View File

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

View File

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