mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Use order total excluding discounts in voucher calculations
This commit is contained in:
@@ -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") }
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user