Skip to content

Repair rounding issue on totals in reports#13785

Open
pacodelaluna wants to merge 6 commits intoopenfoodfoundation:masterfrom
pacodelaluna:repair-rounding-issue-on-totals-in-reports
Open

Repair rounding issue on totals in reports#13785
pacodelaluna wants to merge 6 commits intoopenfoodfoundation:masterfrom
pacodelaluna:repair-rounding-issue-on-totals-in-reports

Conversation

@pacodelaluna
Copy link
Contributor

What? Why?

My previous refactoring on reports seems to have introduced a rounding issues on prices totals in reports.

What should we test?

  • Visit Reports in Admin, especially orders_and_fulfillment ones
  • Check that price totals are rounded to 2 decimals and not more

Release notes

  • Repair rounding issue on totals in reports

Changelog Category (reviewers may add a label for the release notes):

  • User facing changes
  • API changes (V0, V1, DFC or Webhook)
  • Technical changes only
  • Feature toggled

Copy link
Contributor Author

@pacodelaluna pacodelaluna left a comment

Choose a reason for hiding this comment

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

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.

@sigmundpetersen sigmundpetersen moved this from All the things 💤 to Code review 🔎 in OFN Delivery board Dec 4, 2025
@rioug
Copy link
Collaborator

rioug commented Dec 4, 2025

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

Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

See comment.

@github-project-automation github-project-automation bot moved this from Code review 🔎 to In Progress ⚙ in OFN Delivery board Dec 4, 2025
@pacodelaluna pacodelaluna force-pushed the repair-rounding-issue-on-totals-in-reports branch 2 times, most recently from 7c49d83 to fed8862 Compare December 18, 2025 16:45
@pacodelaluna pacodelaluna force-pushed the repair-rounding-issue-on-totals-in-reports branch from fed8862 to 8748bd7 Compare December 18, 2025 17:00
Copy link
Contributor Author

@pacodelaluna pacodelaluna left a comment

Choose a reason for hiding this comment

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

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)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

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)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

I found it clearer to convert nil case into empty array, easier to manage also.

Copy link
Collaborator

@rioug rioug left a comment

Choose a reason for hiding this comment

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

Great ! thanks @pacodelaluna 🙏

@rioug rioug moved this from In Progress ⚙ to Code review 🔎 in OFN Delivery board Dec 19, 2025
@dacook dacook self-requested a review December 21, 2025 23:14
Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

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

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?

@github-project-automation github-project-automation bot moved this from Code review 🔎 to In Progress ⚙ in OFN Delivery board Dec 21, 2025
@pacodelaluna
Copy link
Contributor Author

@dacook Sorry, I missed your message, I will take a look at this asap.

Copy link
Contributor Author

@pacodelaluna pacodelaluna left a comment

Choose a reason for hiding this comment

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

@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))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

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)) }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here also, I have to check directly the amount attribute of the object, as the conversion is not present anymore.

@sigmundpetersen sigmundpetersen moved this from In Progress ⚙ to Code review 🔎 in OFN Delivery board Feb 27, 2026
Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

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

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?

@github-project-automation github-project-automation bot moved this from Code review 🔎 to In Progress ⚙ in OFN Delivery board Mar 2, 2026
@pacodelaluna pacodelaluna force-pushed the repair-rounding-issue-on-totals-in-reports branch from 75f998a to a069e42 Compare March 2, 2026 21:43
Copy link
Contributor Author

@pacodelaluna pacodelaluna left a comment

Choose a reason for hiding this comment

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

@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.
Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

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

Great, thanks for following up. Now there are no assumptions made on the datatype, the prices work and it seems everything else does too! 🏅

Edit: spec failure is unrelated flaky system spec and can be ignored.

@dacook dacook moved this from In Progress ⚙ to Test Ready 🧪 in OFN Delivery board Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Test Ready 🧪

Development

Successfully merging this pull request may close these issues.

Rounding error on reports

3 participants