Skip to content

Commit

Permalink
refactor: cancel old PR and invalidate tokens using job
Browse files Browse the repository at this point in the history
  • Loading branch information
ruthra-kumar committed Oct 25, 2024
1 parent d57624b commit 22a7228
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
46 changes: 41 additions & 5 deletions erpnext/accounts/doctype/payment_request/payment_request.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
1 change: 1 addition & 0 deletions erpnext/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 22a7228

Please sign in to comment.