diff --git a/india_compliance/gst_india/overrides/sales_invoice.py b/india_compliance/gst_india/overrides/sales_invoice.py index ced8f9f2f..5d2836efb 100644 --- a/india_compliance/gst_india/overrides/sales_invoice.py +++ b/india_compliance/gst_india/overrides/sales_invoice.py @@ -202,14 +202,16 @@ def before_cancel(doc, method=None): def on_cancel(doc, method=None): + if not doc.irn: + return + values = { "irn": doc.irn, "reason": "Data Entry Mistake", "ewaybill": doc.ewaybill or "", "remark": "", } - if doc.irn: - cancel_e_invoice(doc.name, values, show_msg=False) + cancel_e_invoice(doc.name, values, show_msg=False) def is_e_waybill_applicable(doc, gst_settings=None): diff --git a/india_compliance/gst_india/utils/e_invoice.py b/india_compliance/gst_india/utils/e_invoice.py index 218f6cec1..636f51e2b 100644 --- a/india_compliance/gst_india/utils/e_invoice.py +++ b/india_compliance/gst_india/utils/e_invoice.py @@ -334,9 +334,7 @@ def cancel_e_invoice(docname, values, show_msg=True): doc = load_doc("Sales Invoice", docname, "cancel") values = frappe.parse_json(values) - valid = validate_if_e_invoice_can_be_cancelled(doc, throw=show_msg) - if not valid: - return + validate_if_e_invoice_can_be_cancelled(doc) if doc.get("ewaybill"): _cancel_e_waybill(doc, values, show_msg) @@ -532,26 +530,22 @@ def validate_taxable_item(doc, throw=True): ) -def validate_if_e_invoice_can_be_cancelled(doc, throw=True): +def validate_if_e_invoice_can_be_cancelled(doc): if not doc.irn: frappe.throw(_("IRN not found"), title=_("Error Cancelling e-Invoice")) # this works because we do run_onload in load_doc above acknowledged_on = doc.get_onload().get("e_invoice_info", {}).get("acknowledged_on") - is_cancellation_allowed = ( - acknowledged_on - and add_to_date(get_datetime(acknowledged_on), days=1, as_datetime=True) - >= get_datetime() - ) - - if throw and not is_cancellation_allowed: + if ( + not acknowledged_on + or add_to_date(get_datetime(acknowledged_on), days=1, as_datetime=True) + < get_datetime() + ): frappe.throw( _("e-Invoice can only be cancelled upto 24 hours after it is generated") ) - return is_cancellation_allowed - def retry_e_invoice_e_waybill_generation(): settings = frappe.get_cached_doc("GST Settings")