Skip to content

feat: add capped percentage discount helper for demo review#7

Merged
Galzi1 merged 1 commit into
mainfrom
demo/capped-percentage-discount-helper
Apr 11, 2026
Merged

feat: add capped percentage discount helper for demo review#7
Galzi1 merged 1 commit into
mainfrom
demo/capped-percentage-discount-helper

Conversation

@Galzi1

@Galzi1 Galzi1 commented Apr 11, 2026

Copy link
Copy Markdown
Owner

Adds a small pricing helper so we can run a fresh github-pr-kb end-to-end demo with new review comments and new comment IDs.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@qodo-code-review

Copy link
Copy Markdown

Review Summary by Qodo

Add capped percentage discount helper function

✨ Enhancement

Grey Divider

Walkthroughs

Description
• Add capped percentage discount helper function
• Limits discount to absolute maximum amount
• Enables fresh demo with new review comments
Diagram
flowchart LR
  A["Subtotal + Discount Rate + Max Amount"] -- "Calculate discount" --> B["Min of percentage or max"]
  B -- "Apply discount" --> C["Discounted Subtotal"]
  C -- "Add tax" --> D["Final Total"]
Loading

Grey Divider

File Changes

1. demo_app.py ✨ Enhancement +13/-0

Add capped percentage discount calculation function

• Add new function calculate_total_with_capped_percentage_discount()
• Accepts subtotal, sales tax rate, discount rate, and max discount amount
• Calculates discount as minimum of percentage-based or absolute cap
• Returns final total after applying capped discount and tax

demo_app.py


Grey Divider

Qodo Logo

@qodo-code-review

qodo-code-review Bot commented Apr 11, 2026

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (2)   📘 Rule violations (0)   📎 Requirement gaps (0)   🎨 UX Issues (0)
🐞\ ≡ Correctness (2)

Grey Divider


Action required

1. Negative cap increases price 🐞
Description
calculate_total_with_capped_percentage_discount() allows max_discount_amount < 0, which makes
discount_amount negative and increases the subtotal instead of discounting it. This can silently
overcharge by turning a discount into a surcharge.
Code

demo_app.py[R52-53]

+    discount_amount = min(subtotal * discount_rate, max_discount_amount)
+    discounted_subtotal = subtotal - discount_amount
Evidence
The new helper computes discount_amount using min(..., max_discount_amount) and then subtracts it;
when max_discount_amount is negative, min() selects the negative cap, and subtracting a negative
increases the subtotal. The existing absolute-discount helper explicitly rejects negative discount
amounts, establishing that absolute discount inputs are expected to be non-negative.

demo_app.py[44-54]
demo_app.py[23-31]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`calculate_total_with_capped_percentage_discount()` accepts `max_discount_amount` without validation. If `max_discount_amount` is negative, `min(subtotal * discount_rate, max_discount_amount)` selects the negative cap, producing a negative `discount_amount` that *increases* the subtotal.

### Issue Context
The codebase already treats absolute discount amounts as non-negative (see `calculate_total_with_discount()` raising on negative `discount_amount`). `max_discount_amount` is also an absolute amount and should follow the same rule.

### Fix Focus Areas
- demo_app.py[44-54]

### Suggested fix
- Add an input check:
 - `if max_discount_amount < 0: raise ValueError("max_discount_amount must be non-negative")`
- (Optionally) also ensure `discount_amount` is never negative by clamping: `discount_amount = max(discount_amount, 0.0)`

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Discount can exceed subtotal 🐞
Description
calculate_total_with_capped_percentage_discount() does not bound discount_amount to the order
subtotal, so it can produce a negative discounted_subtotal and negative taxed totals. This is
inconsistent with calculate_total_with_discount(), which explicitly clamps the discounted subtotal
to >= 0.
Code

demo_app.py[R52-54]

+    discount_amount = min(subtotal * discount_rate, max_discount_amount)
+    discounted_subtotal = subtotal - discount_amount
+    return calculate_total(discounted_subtotal, sales_tax_rate=sales_tax_rate)
Evidence
The new helper subtracts discount_amount from subtotal without clamping, then passes the potentially
negative subtotal into calculate_total(). In contrast, the existing absolute-discount helper clamps
discounted_subtotal = max(subtotal - discount_amount, 0.0) to prevent negative totals (e.g.,
subtotal=100, discount_amount=150).

demo_app.py[44-54]
demo_app.py[23-31]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`calculate_total_with_capped_percentage_discount()` can compute a `discount_amount` greater than `subtotal` and return a negative total (because `discounted_subtotal` can go below 0 and `calculate_total()` will happily compute a negative taxed amount).

### Issue Context
`calculate_total_with_discount()` already enforces the invariant that totals should not go negative by clamping the discounted subtotal to `>= 0.0`.

### Fix Focus Areas
- demo_app.py[52-54]

### Suggested fix
- Ensure the discount can’t exceed the subtotal and clamp the resulting subtotal:
 - `discount_amount = min(subtotal * discount_rate, max_discount_amount, subtotal)`
 - `discounted_subtotal = max(subtotal - discount_amount, 0.0)`
- Keep the final call to `calculate_total(discounted_subtotal, sales_tax_rate=...)` unchanged.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Qodo Logo

Comment thread demo_app.py
) -> float:
"""Apply a percentage discount before tax, capped by an absolute amount."""

discount_amount = min(subtotal * discount_rate, max_discount_amount)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

We should validate both discount_rate and max_discount_amount here. A negative cap or a rate above 1.0 can silently turn the cap into a surcharge or drive the subtotal negative while still looking mathematically valid.

Comment thread demo_app.py

discount_amount = min(subtotal * discount_rate, max_discount_amount)
discounted_subtotal = subtotal - discount_amount
return calculate_total(discounted_subtotal, sales_tax_rate=sales_tax_rate)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I would calculate the capped discount amount here and then delegate to calculate_total_with_discount so the absolute-discount path stays responsible for clamping and tax flow instead of repeating pricing math in multiple helpers.

Comment thread demo_app.py
discount_rate: float,
max_discount_amount: float,
) -> float:
"""Apply a percentage discount before tax, capped by an absolute amount."""

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Please document whether max_discount_amount is a currency amount before tax and whether the cap applies per order or per line item. That rule changes invoice behavior, so it should be explicit in the docstring rather than inferred from the implementation.

@Galzi1 Galzi1 merged commit 0b45f53 into main Apr 11, 2026
Comment thread demo_app.py
Comment on lines +52 to +53
discount_amount = min(subtotal * discount_rate, max_discount_amount)
discounted_subtotal = subtotal - discount_amount

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Action required

1. Negative cap increases price 🐞 Bug ≡ Correctness

calculate_total_with_capped_percentage_discount() allows max_discount_amount < 0, which makes
discount_amount negative and increases the subtotal instead of discounting it. This can silently
overcharge by turning a discount into a surcharge.
Agent Prompt
### Issue description
`calculate_total_with_capped_percentage_discount()` accepts `max_discount_amount` without validation. If `max_discount_amount` is negative, `min(subtotal * discount_rate, max_discount_amount)` selects the negative cap, producing a negative `discount_amount` that *increases* the subtotal.

### Issue Context
The codebase already treats absolute discount amounts as non-negative (see `calculate_total_with_discount()` raising on negative `discount_amount`). `max_discount_amount` is also an absolute amount and should follow the same rule.

### Fix Focus Areas
- demo_app.py[44-54]

### Suggested fix
- Add an input check:
  - `if max_discount_amount < 0: raise ValueError("max_discount_amount must be non-negative")`
- (Optionally) also ensure `discount_amount` is never negative by clamping: `discount_amount = max(discount_amount, 0.0)`

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Comment thread demo_app.py
Comment on lines +52 to +54
discount_amount = min(subtotal * discount_rate, max_discount_amount)
discounted_subtotal = subtotal - discount_amount
return calculate_total(discounted_subtotal, sales_tax_rate=sales_tax_rate)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Action required

2. Discount can exceed subtotal 🐞 Bug ≡ Correctness

calculate_total_with_capped_percentage_discount() does not bound discount_amount to the order
subtotal, so it can produce a negative discounted_subtotal and negative taxed totals. This is
inconsistent with calculate_total_with_discount(), which explicitly clamps the discounted subtotal
to >= 0.
Agent Prompt
### Issue description
`calculate_total_with_capped_percentage_discount()` can compute a `discount_amount` greater than `subtotal` and return a negative total (because `discounted_subtotal` can go below 0 and `calculate_total()` will happily compute a negative taxed amount).

### Issue Context
`calculate_total_with_discount()` already enforces the invariant that totals should not go negative by clamping the discounted subtotal to `>= 0.0`.

### Fix Focus Areas
- demo_app.py[52-54]

### Suggested fix
- Ensure the discount can’t exceed the subtotal and clamp the resulting subtotal:
  - `discount_amount = min(subtotal * discount_rate, max_discount_amount, subtotal)`
  - `discounted_subtotal = max(subtotal - discount_amount, 0.0)`
- Keep the final call to `calculate_total(discounted_subtotal, sales_tax_rate=...)` unchanged.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

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.

1 participant