diff --git a/erpnext/accounts/doctype/payment_request/payment_request.py b/erpnext/accounts/doctype/payment_request/payment_request.py index 5d5bc8ae134a..3e7529efcc6b 100644 --- a/erpnext/accounts/doctype/payment_request/payment_request.py +++ b/erpnext/accounts/doctype/payment_request/payment_request.py @@ -713,6 +713,10 @@ def cancel_old_payment_requests(ref_dt, ref_dn): doc.flags.ignore_permissions = True doc.cancel() + if ireqs := get_irequests_of_payment_request(doc.name): + for ireq in ireqs: + frappe.db.set_value("Integration Request", ireq.name, "status", "Cancelled") + def get_existing_payment_request_amount(ref_dt, ref_dn, statuses: list | None = None) -> list: """ @@ -952,11 +956,29 @@ def get_open_payment_requests_query(doctype, txt, searchfield, start, page_len, ] -def clear_integration_requests_of_cancelled_payment_requests(): +def get_irequests_of_payment_request(doc: str | None = None) -> list: + res = [] + if doc: + res = frappe.db.get_all( + "Integration Request", + { + "reference_doctype": "Payment Request", + "reference_docname": doc, + "status": "Queued", + }, + ) + return res + + +def get_all_irequests_of_cancelled_payment_request() -> list: + res = [] + pr = qb.DocType("Payment Request") if payment_requests := qb.from_(pr).select(pr.name).where(pr.docstatus.eq(2)).run(as_list=True): + # payment_requests = [x[0] for x in payment_requests] - integration_requests = frappe.db.get_all( + + res = frappe.db.get_all( "Integration Request", { "reference_doctype": "Payment Request", @@ -964,5 +986,13 @@ def clear_integration_requests_of_cancelled_payment_requests(): "status": "Queued", }, ) - for ireq in integration_requests: - frappe.delete_doc("Integration Request", ireq.name, force=1) + return res + + +def cancel_integration_requests_of_cancelled_payment_requests(): + """ + Primarily used by background job + """ + integration_requests = get_all_irequests_of_cancelled_payment_request() + for ireq in integration_requests: + frappe.db.set_value("Integration Request", ireq.name, "status", "Cancelled") diff --git a/erpnext/hooks.py b/erpnext/hooks.py index fe4e7d40ab53..074d6187b9ff 100644 --- a/erpnext/hooks.py +++ b/erpnext/hooks.py @@ -399,7 +399,7 @@ "0/15 * * * *": [ "erpnext.manufacturing.doctype.bom_update_log.bom_update_log.resume_bom_cost_update_jobs", "erpnext.accounts.doctype.process_payment_reconciliation.process_payment_reconciliation.trigger_reconciliation_for_queued_docs", - "erpnext.accounts.doctype.payment_request.payment_request.clear_integration_requests_of_cancelled_payment_requests", + "erpnext.accounts.doctype.payment_request.payment_request.cancel_integration_requests_of_cancelled_payment_requests", ], "0/30 * * * *": [ "erpnext.utilities.doctype.video.video.update_youtube_data",