Show negative enterprise fees in shopfront price breakdown#14396
Show negative enterprise fees in shopfront price breakdown#14396David-OFN-CA wants to merge 2 commits into
Conversation
Negative enterprise fees (used as discounts) were silently filtered out
by `amount > 0` guards in `EnterpriseFeeCalculator`, so they never
reached the variant serializer or the shopfront pie chart — even though
they calculated correctly on orders and appeared in reports.
Change both `select { amount > 0 }` calls to `reject { amount.zero? }`
so non-zero fees (positive and negative) are all included.
Closes openfoodfoundation#14017
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
rioug
left a comment
There was a problem hiding this comment.
I think we can improve the test, currently we are not testing the whole logic, see comments. But otherwise it looks good 👍
@rioug asked to confirm zero-amount fees aren't taken into account. Zero exclusion and negative inclusion were already covered separately, but this adds a combined example per computation (regular + indexed) that sets one fee to 0 (dropped) and one to negative (kept) in the same breakdown, exercising the full reject { amount.zero? } logic in one assertion. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Thanks @rioug — added in 2df620e. Since zero-exclusion and negative-inclusion were already covered in separate examples, I added a combined test to each computation block (regular + indexed) that sets one fee to |
|
Hi @rioug — this is ready for another look when you have a moment. 🙏 I've addressed your request to cover the zero-fee logic in both computation blocks. Each block (
That second test is the meaningful regression guard here: it's the one case that would still fail under the old CI is green. Could you re-review when you get a chance? Thanks! |
rioug
left a comment
There was a problem hiding this comment.
Thanks @David-OFN-CA ! It looks good.
| it "drops zero fees while keeping negative ones in the same breakdown" do | ||
| ef_admin.calculator.update_attribute :preferred_amount, -1.23 | ||
| ef_sales.calculator.update_attribute :preferred_amount, 0 |
There was a problem hiding this comment.
I would suggest for next time, to set these fee amounts using let(), rather than updating here. Generally we try to set up all conditions for a test outside the it block. This helps clarify what the context is, and is also more efficient (this test will create an admin fee of 1.23, then update it to -1.23, which results in an unnecessary extra database interaction).
I'm not sure if I explained that well, so this is what I mean:
context "with both zero and negative fees" do
let!(:ef_admin) { create(:enterprise_fee, fee_type: 'admin', amount: -1.23, name: "Admin") }
let!(:ef_sales) { create(:enterprise_fee, fee_type: 'sales', amount: 0, name: "Sales") }
it "drops zero fees while keeping negative ones in the same breakdown"(this replaces the ef_admin and ef_sales that were defined above)
@David-OFN-CA I'm wondering are you able to feed this feedback back to your AI agent to learn from?



What? Why?
Negative enterprise fees (used as discounts by some enterprises) calculated correctly on orders and appeared correctly in reports, but were silently excluded from the shopfront price-breakdown pie chart.
Root cause:
EnterpriseFeeCalculator#indexed_fees_by_type_forand#fees_by_type_forboth filtered results with.select { |_fee_type, amount| amount > 0 }, dropping any fee type whose net amount was negative before the data reached the variant serializer and the Angular pie-chart template.Fix: change both guards to
.reject { |_fee_type, amount| amount.zero? }— this preserves negative values while still excluding true-zero entries.(Note: PR opened by an AI agent on behalf of @David-OFN-CA)
What should we test?
-20%flat rate) and add it to an order cycle.0value are still excluded from the pie chart.Release notes
Changelog Category (reviewers may add a label for the release notes):
Screenshot