diff --git a/app/models/spree/adjustment.rb b/app/models/spree/adjustment.rb index 57ef3d549c..7ebacacafd 100644 --- a/app/models/spree/adjustment.rb +++ b/app/models/spree/adjustment.rb @@ -67,6 +67,8 @@ module Spree scope :charge, -> { where('amount >= 0') } scope :credit, -> { where('amount < 0') } scope :return_authorization, -> { where(originator_type: "Spree::ReturnAuthorization") } + scope :voucher, -> { where(originator_type: "Voucher") } + scope :non_voucher, -> { where.not(originator_type: "Voucher") } scope :inclusive, -> { where(included: true) } scope :additional, -> { where(included: false) } scope :legacy_tax, -> { additional.tax.where(adjustable_type: "Spree::Order") } diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index 13d65cea25..027bff0982 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -171,6 +171,11 @@ module Spree line_items.inject(0.0) { |sum, li| sum + li.amount } end + # Order total without any applied discounts from vouchers + def pre_discount_total + item_total + all_adjustments.additional.eligible.non_voucher.sum(:amount) + end + def currency self[:currency] || Spree::Config[:currency] end diff --git a/app/models/voucher.rb b/app/models/voucher.rb index 94743fc731..1199ad0820 100644 --- a/app/models/voucher.rb +++ b/app/models/voucher.rb @@ -41,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) - -amount.clamp(0, order.total) + -amount.clamp(0, order.pre_discount_total) end end diff --git a/spec/services/voucher_adjustments_service_spec.rb b/spec/services/voucher_adjustments_service_spec.rb index 0fc08d7260..fa2c1bb951 100644 --- a/spec/services/voucher_adjustments_service_spec.rb +++ b/spec/services/voucher_adjustments_service_spec.rb @@ -14,9 +14,7 @@ describe VoucherAdjustmentsService do it 'updates the adjustment amount to -order.total' do voucher.create_adjustment(voucher.code, order) - - order.total = 6 - order.save! + order.update_columns(item_total: 6) VoucherAdjustmentsService.calculate(order) diff --git a/spec/system/consumer/split_checkout_spec.rb b/spec/system/consumer/split_checkout_spec.rb index f2939bb53e..2bcc9f1cd0 100644 --- a/spec/system/consumer/split_checkout_spec.rb +++ b/spec/system/consumer/split_checkout_spec.rb @@ -1114,11 +1114,8 @@ describe "As a consumer, I want to checkout my order" do let(:voucher) { create(:voucher, 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! + order.update_totals VoucherAdjustmentsService.calculate(order) visit checkout_step_path(:summary)