Use order total excluding discounts in voucher calculations

This commit is contained in:
Matt-Yorkley
2023-06-13 18:56:56 +01:00
parent 52806a35ee
commit 93df70c0a7
5 changed files with 10 additions and 8 deletions

View File

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

View File

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

View File

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

View File

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

View File

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