diff --git a/app/models/voucher.rb b/app/models/voucher.rb index 376c1979dd..2f8fd3c137 100644 --- a/app/models/voucher.rb +++ b/app/models/voucher.rb @@ -11,13 +11,10 @@ class Voucher < ApplicationRecord dependent: :nullify validates :code, presence: true, uniqueness: { scope: :enterprise_id } - - def value - 10 - end + validates :amount, presence: true, numericality: { greater_than: 0 } def display_value - Spree::Money.new(value) + Spree::Money.new(amount) end # Ideally we would use `include CalculatedAdjustments` to be consistent with other adjustments, @@ -44,6 +41,6 @@ class Voucher < ApplicationRecord # We limit adjustment to the maximum amount needed to cover the order, ie if the voucher # covers more than the order.total we only need to create an adjustment covering the order.total def compute_amount(order) - -value.clamp(0, order.total) + -amount.clamp(0, order.total) end end diff --git a/app/views/split_checkout/_voucher_section.cable_ready.haml b/app/views/split_checkout/_voucher_section.cable_ready.haml index 786eff7288..a005b5698b 100644 --- a/app/views/split_checkout/_voucher_section.cable_ready.haml +++ b/app/views/split_checkout/_voucher_section.cable_ready.haml @@ -10,7 +10,7 @@ = t("split_checkout.step2.voucher.voucher", voucher_amount: voucher_adjustment.originator.display_value) = link_to t("split_checkout.step2.voucher.remove_code"), voucher_adjustment_path(id: voucher_adjustment.id), method: "delete", data: { confirm: t("split_checkout.step2.voucher.confirm_delete") } - # This might not be true, ie payment method including a fee which wouldn't be covered by voucher or tax implication raising total to be bigger than the voucher amount ? - - if voucher_adjustment.originator.value > order.total + - if voucher_adjustment.originator.amount > order.total .checkout-input %span.formError.standalone = t("split_checkout.step2.voucher.warning_forfeit_remaining_amount") diff --git a/db/migrate/20230418030646_add_amount_to_vouchers.rb b/db/migrate/20230418030646_add_amount_to_vouchers.rb new file mode 100644 index 0000000000..2542ab26c3 --- /dev/null +++ b/db/migrate/20230418030646_add_amount_to_vouchers.rb @@ -0,0 +1,5 @@ +class AddAmountToVouchers < ActiveRecord::Migration[7.0] + def change + add_column :vouchers, :amount, :decimal, precision: 10, scale: 2, null: false, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index f0935b6e16..7aa41d0e3c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1197,6 +1197,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_04_24_141213) do t.datetime "updated_at", null: false t.bigint "enterprise_id" t.datetime "deleted_at", precision: nil + t.decimal "amount", precision: 10, scale: 2, default: "0.0", null: false t.index ["code", "enterprise_id"], name: "index_vouchers_on_code_and_enterprise_id", unique: true t.index ["deleted_at"], name: "index_vouchers_on_deleted_at" t.index ["enterprise_id"], name: "index_vouchers_on_enterprise_id" diff --git a/spec/models/voucher_spec.rb b/spec/models/voucher_spec.rb index 3ef8792b1c..57dcac881c 100644 --- a/spec/models/voucher_spec.rb +++ b/spec/models/voucher_spec.rb @@ -15,10 +15,12 @@ describe Voucher do it { is_expected.to validate_presence_of(:code) } it { is_expected.to validate_uniqueness_of(:code).scoped_to(:enterprise_id) } + it { is_expected.to validate_presence_of(:amount) } + it { is_expected.to validate_numericality_of(:amount).is_greater_than(0) } end describe '#compute_amount' do - subject { Voucher.create(code: 'new_code', enterprise: enterprise) } + subject { Voucher.create(code: 'new_code', enterprise: enterprise, amount: 10) } let(:order) { create(:order_with_totals) } @@ -39,11 +41,11 @@ describe Voucher do describe '#create_adjustment' do subject(:adjustment) { voucher.create_adjustment(voucher.code, order) } - let(:voucher) { Voucher.create(code: 'new_code', enterprise: enterprise) } - let(:order) { create(:order_with_line_items, line_items_count: 1, distributor: enterprise) } + let(:voucher) { Voucher.create(code: 'new_code', enterprise: enterprise, amount: 25) } + let(:order) { create(:order_with_line_items, line_items_count: 3, distributor: enterprise) } it 'includes the full voucher amount' do - expect(adjustment.amount.to_f).to eq(-10.0) + expect(adjustment.amount.to_f).to eq(-25.0) end it 'has no included_tax' do diff --git a/spec/system/consumer/split_checkout_spec.rb b/spec/system/consumer/split_checkout_spec.rb index f0e5fb6fc6..87741fcb8e 100644 --- a/spec/system/consumer/split_checkout_spec.rb +++ b/spec/system/consumer/split_checkout_spec.rb @@ -720,7 +720,8 @@ describe "As a consumer, I want to checkout my order" do end context "with voucher available" do - let!(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor) } + let!(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor, amount: amount) } + let(:amount) { 15 } before do visit checkout_step_path(:payment) @@ -738,7 +739,7 @@ describe "As a consumer, I want to checkout my order" do end it "adds a voucher to the order" do - expect(page).to have_content("$10.00 Voucher") + expect(page).to have_content("$15.00 Voucher") expect(order.reload.voucher_adjustments.length).to eq(1) end end @@ -1111,13 +1112,15 @@ describe "As a consumer, I want to checkout my order" do end describe "vouchers" do - let(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor) } + let(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor, amount: 6) } before do # Add voucher to the order voucher.create_adjustment(voucher.code, order) + # Update order so voucher adjustment is properly taken into account order.update_order! + VoucherAdjustmentsService.calculate(order) visit checkout_step_path(:summary) end @@ -1125,6 +1128,7 @@ describe "As a consumer, I want to checkout my order" do it "shows the applied voucher" do within ".summary-right" do expect(page).to have_content "some_code" + expect(page).to have_content "-6" end end end diff --git a/spec/system/consumer/split_checkout_tax_incl_spec.rb b/spec/system/consumer/split_checkout_tax_incl_spec.rb index 5cb07f67bb..ea51694aae 100644 --- a/spec/system/consumer/split_checkout_tax_incl_spec.rb +++ b/spec/system/consumer/split_checkout_tax_incl_spec.rb @@ -136,7 +136,7 @@ describe "As a consumer, I want to see adjustment breakdown" do end context "when using a voucher" do - let!(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor) } + let!(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor, amount: 10) } it "will include a tax included amount on the voucher adjustment" do visit checkout_step_path(:details) @@ -149,7 +149,7 @@ describe "As a consumer, I want to see adjustment breakdown" do fill_in "Enter voucher code", with: voucher.code click_button("Apply") - # Choose payment ?? + # Choose payment click_on "Next - Order summary" click_on "Complete order" diff --git a/spec/system/consumer/split_checkout_tax_not_incl_spec.rb b/spec/system/consumer/split_checkout_tax_not_incl_spec.rb index 4fa0f0531a..d99dd604c7 100644 --- a/spec/system/consumer/split_checkout_tax_not_incl_spec.rb +++ b/spec/system/consumer/split_checkout_tax_not_incl_spec.rb @@ -144,7 +144,7 @@ describe "As a consumer, I want to see adjustment breakdown" do end context "when using a voucher" do - let!(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor) } + let!(:voucher) { Voucher.create(code: 'some_code', enterprise: distributor, amount: 10) } it "will include a tax included amount on the voucher adjustment" do visit checkout_step_path(:details) @@ -156,7 +156,6 @@ describe "As a consumer, I want to see adjustment breakdown" do fill_in "Enter voucher code", with: voucher.code click_button("Apply") - # Choose payment ?? click_on "Next - Order summary" click_on "Complete order"