Skip to content

fix: restore coupon usage when invoices are cancelled#206

Open
my-dev-jour wants to merge 2 commits intoBrainWise-DEV:developfrom
my-dev-jour:fix/195-coupon-usage-on-cancel
Open

fix: restore coupon usage when invoices are cancelled#206
my-dev-jour wants to merge 2 commits intoBrainWise-DEV:developfrom
my-dev-jour:fix/195-coupon-usage-on-cancel

Conversation

@my-dev-jour
Copy link
Copy Markdown
Contributor

Summary

  • add a Sales Invoice on_cancel hook to restore POS coupon usage after successful cancellation
  • keep credit JE cleanup on before_cancel and realtime updates on on_cancel
  • add regression tests for coupon rollback hook behavior

Testing

  • python3 -m py_compile pos_next/api/sales_invoice_hooks.py pos_next/hooks.py pos_next/api/test_sales_invoice_hooks.py
  • full Frappe unit tests were not run in this workspace because the frappe module is not installed here

Closes #195

@engahmed1190
Copy link
Copy Markdown
Contributor

Code review

Found 1 issue:

  1. rollback_coupon_usage has no is_return guard -- cancelling a return (credit note) invoice will decrement the coupon usage counter even though no usage was ever incremented for the return. Return invoices inherit coupon_code from the original invoice via ERPNext's make_sales_return. This can drive the used counter below its correct value, allowing single-use coupons to be reused. Add an early return for doc.get("is_return").

def rollback_coupon_usage(doc):
"""Restore coupon usage for cancelled invoices that consumed a POS coupon."""
coupon_code = doc.get("coupon_code")
if not coupon_code or not frappe.db.table_exists("POS Coupon"):
return
try:
from pos_next.pos_next.doctype.pos_coupon.pos_coupon import decrement_coupon_usage
decrement_coupon_usage(coupon_code)

🤖 Generated with Claude Code

- If this code review was useful, please react with 👍. Otherwise, react with 👎.

@my-dev-jour
Copy link
Copy Markdown
Contributor Author

Addressed. I added an early return for doc.get("is_return") in rollback_coupon_usage() so return/credit-note cancellations do not decrement coupon usage. I also added regression coverage for both the return-invoice path and the missing-coupon-table path.

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.

[Bug]: Cancelling an invoice does not restore coupon usage

2 participants