Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outstanding balance uses reimbursement #10411

Merged
merged 1 commit into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,15 @@ def update_line_item_prices!
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
Expand Down
9 changes: 6 additions & 3 deletions core/spec/models/spree/order/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

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

Expand Down