Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions demo_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,15 @@ def calculate_total_with_capped_percentage_discount(
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)


def calculate_total_with_minimum_charge(
subtotal: float,
sales_tax_rate: float,
discount_amount: float,
minimum_charge: float = 0.0,
) -> float:
"""Apply a discount before tax, but never reduce the subtotal below a floor."""

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 minimum_charge is a pre-tax subtotal floor or a final customer-facing total floor. That distinction affects pricing policy, invoicing, and refunds, so it should be explicit in the docstring.


adjusted_subtotal = max(subtotal - discount_amount, minimum_charge)

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 minimum_charge here. If it is negative or greater than the subtotal, this helper can either produce negative pre-tax amounts or increase the charge instead of only enforcing a floor.

return calculate_total(adjusted_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 normalize the floored subtotal here and then delegate to the existing discount helper so one path stays responsible for discount clamping rules instead of spreading pricing math across multiple helpers.

Comment on lines +57 to +66

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. Missing input validation 🐞 Bug ≡ Correctness

calculate_total_with_minimum_charge accepts negative discount_amount and/or minimum_charge, which
can silently increase the subtotal (negative discount) or allow negative totals (negative
minimum_charge). This is inconsistent with calculate_total_with_discount, which rejects negative
discounts, and can lead to incorrect financial charges.
Agent Prompt
### Issue description
`calculate_total_with_minimum_charge()` currently allows invalid negative inputs.
- `discount_amount < 0` makes the function *increase* the subtotal (`subtotal - (-x)`), leading to overcharging.
- `minimum_charge < 0` can produce negative subtotals/totals.

### Issue Context
`calculate_total_with_discount()` already treats negative discounts as an error, so the new helper should match that contract to avoid surprising callers.

### Fix Focus Areas
- demo_app.py[57-66]
- demo_app.py[23-31]

### Suggested change
Add guards similar to:
- `if discount_amount < 0: raise ValueError(...)`
- `if minimum_charge < 0: raise ValueError(...)`
Optionally consider validating `subtotal >= 0` if this module assumes non-negative subtotals elsewhere.

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

Loading