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

Fix RMA amount calculator #1590

Merged
merged 1 commit into from
Oct 11, 2017
Merged

Conversation

DanielePalombo
Copy link
Contributor

RMA calculator use the percentage to calculate the weighted line item
amount, but when the percentage is a repeating decimal the calculator
makes an error. Replaced the percentage calculation with a division.

ref #1564

@@ -36,7 +36,7 @@
order.adjustments.first.update_attributes(amount: adjustment_amount)
end

it { is_expected.to eq line_item_price - (adjustment_amount.abs / line_item_quantity) }
it { is_expected.to be_within(0.1).of(line_item_price - (adjustment_amount.abs / line_item_quantity)) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR.

Being within 0.1 (10 cents) is probably not good enough here. We should test for an exact amount.

This test passes both before and after the change made in default_refund_amount.rb. Is it possible to get a spec which clearly shows how the existing code was incorrect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that the existing code use a percentage to calculate the value of each line item. This works without problem when quantity is factor of 100 , but in other cases this returns a repeating decimal percentage and the result is a little bit different.

Ex:
existing code:

def percentage_of_line_item(inventory_unit)
  1 / BigDecimal.new(inventory_unit) 
end
...

30 * percentage_of_line_item(3) # 30 * 0.333333333333333333 = 9.99999999999999999

what we want is 10 not 9.99999999999999999

with fix we have:

30 / 3 # 10 

Replaced the test from / line_item_quantity to * ( line_item_price / line_item_total ) because the code https://github.com/nebulab/solidus/pull/4/files#diff-f271b0c846fcbc382f43fdfa7f6a0c89R24 use a percentage instead a division

@@ -36,7 +37,7 @@
order.adjustments.first.update_attributes(amount: adjustment_amount)
end

it { is_expected.to eq line_item_price - (adjustment_amount.abs / line_item_quantity) }
it { is_expected.to eq line_item_price - (adjustment_amount.abs * ( line_item_price / line_item_total )) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you replace this call with the actual number its supposed to be, please. Doing it on the computed amount from the setup makes it tougher to figure out what should be happening (though this calculation would be nice to keep in a comment or something).

You can take a look at the tax specs and see how much more readability it adds when you're really trying to figure out whats happening: https://github.com/solidusio/solidus/blob/master/core/spec/models/spree/calculator/default_tax_spec.rb

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, i replaced my code.

During my investigation i found round_to_two_places method in Spree::Calculator::DefaultTax. Maybe we could use this method? I made this commit with my proposal.

@DanielePalombo DanielePalombo force-pushed the fix-rma-calculator branch 2 times, most recently from ba6a3df to 4bf75f6 Compare November 28, 2016 16:03
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 is certainly an improvement, LGTM!

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.

@DanielePalombo Would you mind rebasing on the latest master branch? The discounted_amount method has been changed to total_before_tax.

Otherwise, this still LGTM.

@DanielePalombo
Copy link
Contributor Author

Ok, I'll rebase it next weekend.

RMA calculator use the percentage to calculate the weighted line item
amount, but when the quantiy isn't a factor of 100 the result
percentage is a repeating decimal and the calculator makes an error.
The fix replace the percentage calculation with a division of result.

ref solidusio#1564
@DanielePalombo
Copy link
Contributor Author

I rebased it.

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.

@cbrunsdon would you mind re-evaluating this?

@cbrunsdon cbrunsdon dismissed their stale review October 6, 2017 19:04

Good enough for me, you could have picked numbers that divided easier or, aye, rounded it, but this is fine too

Copy link
Member

@gmacdougall gmacdougall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Thanks!

@gmacdougall gmacdougall merged commit af0ecb6 into solidusio:master Oct 11, 2017
@elia elia deleted the fix-rma-calculator branch January 8, 2020 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants