From 1b4f74b415009c3a329abb21c345cca906731c19 Mon Sep 17 00:00:00 2001 From: ruthra kumar Date: Fri, 25 Oct 2024 17:53:47 +0530 Subject: [PATCH] refactor: cancel old PR and invalidate tokens using job --- .../payment_request/payment_request.py | 46 +++++++++++++++++-- erpnext/hooks.py | 1 + 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/erpnext/accounts/doctype/payment_request/payment_request.py b/erpnext/accounts/doctype/payment_request/payment_request.py index 27e8f65eda12b..607911d125bbe 100644 --- a/erpnext/accounts/doctype/payment_request/payment_request.py +++ b/erpnext/accounts/doctype/payment_request/payment_request.py @@ -1,7 +1,7 @@ import json import frappe -from frappe import _ +from frappe import _, qb from frappe.model.document import Document from frappe.query_builder.functions import Sum from frappe.utils import flt, nowdate @@ -563,10 +563,12 @@ def make_payment_request(**args): existing_payment_request_amount = get_existing_payment_request_amount(ref_doc.doctype, ref_doc.name) if existing_payment_request_amount: - grand_total -= existing_payment_request_amount - - if not grand_total: - frappe.throw(_("Payment Request is already created")) + if args.order_type == "Shopping Cart": + cancel_old_payment_requests(ref_doc.doctype, ref_doc.name) + else: + grand_total -= existing_payment_request_amount + if not grand_total: + frappe.throw(_("Payment Request is already created")) if draft_payment_request: frappe.db.set_value( @@ -681,6 +683,24 @@ def get_amount(ref_doc, payment_account=None): frappe.throw(_("Payment Entry is already created")) +def cancel_old_payment_requests(ref_dt, ref_dn): + PR = frappe.qb.DocType("Payment Request") + + if res := ( + frappe.qb.from_(PR) + .select(PR.name) + .where(PR.reference_doctype == ref_dt) + .where(PR.reference_name == ref_dn) + .where(PR.docstatus == 1) + .where(PR.status.isin(["Draft", "Requested"])) + .run(as_dict=True) + ): + for x in res: + doc = frappe.get_doc("Payment Request", x.name) + doc.flags.ignore_permissions = True + doc.cancel() + + def get_existing_payment_request_amount(ref_dt, ref_dn): """ Return the total amount of Payment Requests against a reference document. @@ -913,3 +933,19 @@ def get_open_payment_requests_query(doctype, txt, searchfield, start, page_len, ) for pr in open_payment_requests ] + + +def clear_integration_requests_of_cancelled_payment_requests(): + 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( + "Integration Request", + { + "reference_doctype": "Payment Request", + "reference_docname": ["in", payment_requests], + "status": "Queued", + }, + ) + for ireq in integration_requests: + frappe.delete_doc("Integration Request", ireq.name, force=1) diff --git a/erpnext/hooks.py b/erpnext/hooks.py index 059075fe321d4..fe4e7d40ab53c 100644 --- a/erpnext/hooks.py +++ b/erpnext/hooks.py @@ -399,6 +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", ], "0/30 * * * *": [ "erpnext.utilities.doctype.video.video.update_youtube_data",