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

Update outstanding_balance to use reimbursements #2002

Merged
merged 6 commits into from
Jun 22, 2017

Conversation

jhawthorn
Copy link
Contributor

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

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.

@jhawthorn jhawthorn added type:bug Error, flaw or fault Code review needed labels Jun 7, 2017
Copy link
Member

@tvdeyen tvdeyen left a 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! 👏

Copy link
Contributor

@ericsaupe ericsaupe left a 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!

Copy link
Contributor

@mamhoff mamhoff left a 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. 👍!

@jhawthorn jhawthorn requested a review from jordan-brough June 14, 2017 21:59
Copy link
Contributor

@jordan-brough jordan-brough left a 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....
Copy link
Contributor

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'
Copy link
Contributor

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?

@jordan-brough
Copy link
Contributor

jordan-brough commented Jun 19, 2017

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.
@jhawthorn jhawthorn force-pushed the outstanding_balance_omnibus branch from 3d4959b to 915097f Compare June 22, 2017 22:32

context 'and there is a full refund' do
let!(:refund) { create(:refund, payment: payment, amount: payment.amount) }
it { should == 0 }

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 }

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 }

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 }

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 }

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 }

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.

@jhawthorn jhawthorn force-pushed the outstanding_balance_omnibus branch from 915097f to b0d21bc Compare June 22, 2017 22:38

context 'and there is a partial refund' do
let!(:refund) { create(:refund, payment: payment, amount: 6) }
it { should eq -4 }

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 }

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 }

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 }

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.

jhawthorn and others added 5 commits June 22, 2017 15:40
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Error, flaw or fault
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants