Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Outstanding balance uses reimbursement

Christopher Hummel20e69d0e8a19
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index d1aed12014..0a51e48a37 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -297,15 +297,15 @@ module Spree
def outstanding_balance
if canceled?
-1 * payment_total
- elsif refunds.exists?
- # If refund has happened add it back to total to prevent balance_due payment state
- # See: https://github.com/spree/spree/issues/6229 & https://github.com/spree/spree/issues/8136
- total - (payment_total + refunds.sum(:amount))
else
- total - payment_total
+ total - (payment_total + reimbursement_paid_total)
end
end
+ def reimbursement_paid_total
+ reimbursements.sum(&:paid_amount)
+ end
+
def outstanding_balance?
outstanding_balance != 0
end
diff --git a/core/spec/models/spree/order/payment_spec.rb b/core/spec/models/spree/order/payment_spec.rb
index e8d012f5c7..d00c2f6c8b 100644
--- a/core/spec/models/spree/order/payment_spec.rb
+++ b/core/spec/models/spree/order/payment_spec.rb
@@ -178,11 +178,13 @@ module Spree
order.total = 30.30
expect(order.outstanding_balance).to eq(10.10)
end
+
it 'returns negative amount when payment_total is greater than total' do
order.total = 8.20
order.payment_total = 10.20
expect(order.outstanding_balance).to be_within(0.001).of(-2.00)
end
+
it 'incorporates refund reimbursements' do
# Creates an order w/total 10
reimbursement = create :reimbursement
@@ -200,7 +202,7 @@ module Spree
expect(order.outstanding_balance).to eq 0
end
- it 'incorporates refunds' do
+ it 'does not incorporate refunds without a reimbursement' do
order = create(:completed_order_with_totals)
calculator = order.shipments.first.shipping_method.calculator
@@ -211,8 +213,9 @@ module Spree
create(:refund, amount: 10, payment: order.payments.first)
order.update_with_updater!
-
- expect(order.outstanding_balance).to eq 0
+ # Order Total - (Payment Total + Reimbursed)
+ # 10 - (0 + 0) = 0
+ expect(order.outstanding_balance).to eq 10
end
end