-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Update outstanding_balance to use reimbursements #2002
Update outstanding_balance to use reimbursements #2002
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes lots of sense to me. Thanks to everybody involved in fixing this! 👏
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me. Glad we got this figured out!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This puts an end to a bug that's long been bothering us. 👍!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @jhawthorn! This matches the "Potential solution" from #1254 and covers all the scenarios there as far as I can tell, while also adding the coverage of reimbursements. Very nice.
I left a few questions in the specs.
I also opened a PR against your branch with a couple extra specs that I tried out while reviewing #1254, if you'd like: jhawthorn#3
The changelog update is still on the TODO list, right? I imagine one important item would be to let people know that if they want to "just give money back" to a customer (e.g. give them $5 back because they were unhappy), they now need to create an adjustment (along with the refund), where previously they would just create the Refund. Right?
expect(order.payment_total).to eq(16) | ||
expect(order.outstanding_balance).to eq(0) | ||
|
||
# Less sure about this one.... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you intend to leave this comment in? 19
seems OK to me (slightly confusing but does make sense from a certain POV)
end | ||
|
||
context 'and there is a full refund' do | ||
it 'has the correct amount' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you intend to implement this spec?
Linking to #1254 (since this fixes it) |
This updates outstanding_balance to use the value of "reimbursements" when determining the payment total. Previously the value of an order's refunds was used both for representing the money returned to the customer as well as the reduced "adjusted total" of the order (which we expected to be paid). This was wrong in a number of cases. This commit updates outstanding_balance to use the value of reimbursements for calculating the "adjusted total" of the order. This allows all cases to (at least) be represented properly. This better matches the best practice double-entry bookkeeping style. Reimbursements are created when going through the refund system so this should continue to work for cases where refunds where made through the returns system.
3d4959b
to
915097f
Compare
|
||
context 'and there is a full refund' do | ||
let!(:refund) { create(:refund, payment: payment, amount: payment.amount) } | ||
it { should == 0 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use should.zero? instead of should == 0.
|
||
context 'and the payment is voided' do | ||
before { payment.update_attributes!(state: "void") } | ||
it { should == 0 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use should.zero? instead of should == 0.
|
||
context 'and there is a full refund' do | ||
let!(:refund) { create(:refund, payment: payment, amount: payment.amount) } | ||
it { should == 0 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use should.zero? instead of should == 0.
|
||
context 'and the payment is voided' do | ||
before { payment.update_attributes!(state: "void") } | ||
it { should == 0 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use should.zero? instead of should == 0.
|
||
context 'when the order is fully paid' do | ||
let!(:payment) { create(:payment, :completed, order: order, amount: order.total) } | ||
it { should == 0 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use should.zero? instead of should == 0.
|
||
context 'when the order is cancelled' do | ||
before { order.cancel! } | ||
it { should == 0 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use should.zero? instead of should == 0.
915097f
to
b0d21bc
Compare
|
||
context 'and there is a partial refund' do | ||
let!(:refund) { create(:refund, payment: payment, amount: 6) } | ||
it { should eq -4 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ambiguous negative number operator. Parenthesize the method arguments if it's surely a negative number operator, or add a whitespace to the right of the - if it should be a subtraction.
|
||
context 'when the order is cancelled' do | ||
before { order.update_attributes!(state: "canceled") } | ||
it { should eq -10 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ambiguous negative number operator. Parenthesize the method arguments if it's surely a negative number operator, or add a whitespace to the right of the - if it should be a subtraction.
|
||
context 'and there is a partial refund' do | ||
let!(:refund) { create(:refund, payment: payment, amount: 6) } | ||
it { should eq -13 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ambiguous negative number operator. Parenthesize the method arguments if it's surely a negative number operator, or add a whitespace to the right of the - if it should be a subtraction.
|
||
context 'when the order is cancelled' do | ||
before { order.update_attributes!(state: "canceled") } | ||
it { should eq -19 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ambiguous negative number operator. Parenthesize the method arguments if it's surely a negative number operator, or add a whitespace to the right of the - if it should be a subtraction.
Created a spec to simulate the behavior listed in solidusio#1475 in order to properly fix the issue.
Update this spec to match the values it currently returns. I changed the values in this spec into what values we got from the system, which isn't ideal, but I think these values make sense. We have a payment for an amount and a refund for the same amount, so payment total should be zero. Because the order is cancelled, this is the state we want, and the outstanding balance should be zero.
b0d21bc
to
6a031da
Compare
This updates
outstanding_balance
to use the value of "reimbursements" when determining the payment total.Previously the value of an order's refunds was used both for representing the money returned to the customer as well as the reduced "adjusted total" of the order (which we expected to be paid). This was
wrong in a number of cases.
This updates
outstanding_balance
to use the value of reimbursements for calculating the "adjusted total" of the order. This allows all cases to (at least) be represented properly. This bettermatches the best practice double-entry bookkeeping style.
Reimbursements are created when going through the refund system so this should continue to work for cases where refunds where made through the returns system.
This PR includes a test from @ericsaupe (showing breakage of the existing
outstanding_balance
as well as my #1578 integration tests (with all :pending specs now passing!) and #1926 regression test (already merged in master).Thanks to everyone who had continued to report, prod and provide tests for this issue (too many to list but I have CC'd some of you fine folks). This has been a doozy, and I hope this is a viable solution.
CC @ericsaupe @skukx @DanielePalombo @jasonfb @bbuchalter @jordan-brough @mamhoff @stewart @luukveenis @Senjai
If any of you can verify that this meets your needs, or observe it's effects on production-ish data that would be amazing.
Not addressed here is a data migration to update the
payment_state
of existing orders. This will require a detailed CHANGELOG entry.