-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add capped percentage discount helper for demo review #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,3 +39,16 @@ def calculate_total_with_percentage_discount( | |
|
|
||
| discounted_subtotal = subtotal * (1 - discount_rate) | ||
| return calculate_total(discounted_subtotal, sales_tax_rate=sales_tax_rate) | ||
|
|
||
|
|
||
| def calculate_total_with_capped_percentage_discount( | ||
| subtotal: float, | ||
| sales_tax_rate: float, | ||
| discount_rate: float, | ||
| max_discount_amount: float, | ||
| ) -> float: | ||
| """Apply a percentage discount before tax, capped by an absolute amount.""" | ||
|
|
||
| discount_amount = min(subtotal * discount_rate, max_discount_amount) | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| discounted_subtotal = subtotal - discount_amount | ||
|
Comment on lines
+52
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1. Negative cap increases price 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
|
||
| return calculate_total(discounted_subtotal, sales_tax_rate=sales_tax_rate) | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 on lines
+52
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2. Discount can exceed subtotal 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
|
||
There was a problem hiding this comment.
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.