Repair rounding issue on totals in reports#13785
Repair rounding issue on totals in reports#13785pacodelaluna wants to merge 6 commits intoopenfoodfoundation:masterfrom
Conversation
pacodelaluna
left a comment
There was a problem hiding this comment.
I have decided to enforce rounding to 2 decimals.
I was thinking to add a specific method to calculate the sum from a list, not sure yet if it will help much actually. Let me know if you think it is worth it.
I think that's a good idea, there is a lot of summing of list, so having a method that does that and handles the rounding should help to not forget the rounding in the future |
7c49d83 to
fed8862
Compare
fed8862 to
8748bd7
Compare
pacodelaluna
left a comment
There was a problem hiding this comment.
I have introduced the prices_sum method that I put inside ReportsHelper class. Tell me if any issue.
| line_items.map { |li| scaled_final_weight_volume(li) }.sum(&:to_f) | ||
| line_items.map { |li| | ||
| scaled_final_weight_volume(li) | ||
| }.sum(&:to_f).round(3) |
There was a problem hiding this comment.
By security, I think we can give 3 decimals for weight, from kilograms to grams.
| total_units = rows.map(&:total_units) | ||
| summary_total_units = if total_units.all?(&:present?) | ||
| rows.map(&:total_units).sum(&:to_f) | ||
| rows.map(&:total_units).sum(&:to_f).round(3) |
There was a problem hiding this comment.
Same here, units can actually be kilograms, a test is covering the non-integer units case.
| .sum(&:to_f) | ||
| end&.sum(&:to_f) | ||
| prices_sum( | ||
| line_items(query_result_row).to_a.map do |line_item| |
There was a problem hiding this comment.
I found it clearer to convert nil case into empty array, easier to manage also.
rioug
left a comment
There was a problem hiding this comment.
Great ! thanks @pacodelaluna 🙏
dacook
left a comment
There was a problem hiding this comment.
This limits any currency amounts to 2 decimal places. I think this is ok, because I don't know of any currencies where a third decimal place is needed.
But I think there's a better way to solve it, by removing the rounding issue introduced by using Floats. Prices are stored as Decimal, so why can't we sum these values directly?
I see that Float was introduced in the previous PR, but am not sure why. Can you try removing the to_f part?
|
@dacook Sorry, I missed your message, I will take a look at this asap. |
pacodelaluna
left a comment
There was a problem hiding this comment.
@dacook I have simplified the sum logic avoiding unnecessary conversions as you suggested. It worked fine directly. I had to adjust the prices retrieval for 2 lines on Payments report side.
| paypal_price: proc { |orders| total_by_payment_method(orders, "PayPal") }, | ||
| outstanding_balance_price: proc { |orders| | ||
| orders.map(&:outstanding_balance).sum(&:to_f) | ||
| prices_sum(orders.map(&:outstanding_balance).map(&:amount)) |
There was a problem hiding this comment.
Here, I have to check directly the amount attribute of the object, as the float conversion is not present anymore.
| distributor: proc { |payments| payments.first.order.distributor.name }, | ||
| payment_type: proc { |payments| payments.first.payment_method&.name }, | ||
| total_price: proc { |payments| payments.map(&:amount).sum(&:to_f) } | ||
| total_price: proc { |payments| prices_sum(payments.map(&:amount)) } |
There was a problem hiding this comment.
Here also, I have to check directly the amount attribute of the object, as the conversion is not present anymore.
dacook
left a comment
There was a problem hiding this comment.
Thanks, now I see why to_f was added: to protect against nil values. Because prices are actually decimals (BigDecimal), we could have solved the rounding issue here by using to_d.
But I like the .compact.sum solution you used because the purpose is clearer 👍
This is a method that can be used for more than just prices, so I think could best be described as compact_sum. But in that case, it would be even better to not have the method, and simply use compact.sum!
Would you agree to use .compact.sum directly instead of prices_sum?
75f998a to
a069e42
Compare
pacodelaluna
left a comment
There was a problem hiding this comment.
@dacook I have removed the helper method for simplicity, let me know if it is fine now.
It is the whole purpose of the spec.
What? Why?
My previous refactoring on reports seems to have introduced a rounding issues on prices totals in reports.
What should we test?
Release notes
Changelog Category (reviewers may add a label for the release notes):