Skip to content

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress ⚙

Development

Successfully merging this pull request may close these issues.

Rounding error on reports

3 participants