Skip to content

Commit

Permalink
refactor: handle PR's in advance stage
Browse files Browse the repository at this point in the history
  • Loading branch information
ruthra-kumar committed Nov 6, 2024
1 parent cda7800 commit 8936350
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions erpnext/accounts/doctype/payment_request/payment_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,21 @@ def get_amount(ref_doc, payment_account=None):
frappe.throw(_("Payment Entry is already created"))


def get_irequest_status(payment_requests: None | list = None) -> list:
IR = frappe.qb.DocType("Integration Request")
res = []
if payment_requests:
res = (
frappe.qb.from_(IR)
.select(IR.name)
.where(IR.reference_doctype.eq("Payment Request"))
.where(IR.reference_docname.isin(payment_requests))
.where(IR.status.isin(["Authorized", "Completed"]))
.run(as_dict=True)
)
return res


def cancel_old_payment_requests(ref_dt, ref_dn):
PR = frappe.qb.DocType("Payment Request")

Expand All @@ -708,14 +723,19 @@ def cancel_old_payment_requests(ref_dt, ref_dn):
.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()

if ireqs := get_irequests_of_payment_request(doc.name):
for ireq in ireqs:
frappe.db.set_value("Integration Request", ireq.name, "status", "Cancelled")
res = [x.name for x in res]

if get_irequest_status(res):
frappe.throw(_("Another Payment Request is already processed."))
else:
for x in res:
doc = frappe.get_doc("Payment Request", x.name)
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:
Expand Down

0 comments on commit 8936350

Please sign in to comment.