diff --git a/erpnext/accounts/doctype/payment_request/payment_request.js b/erpnext/accounts/doctype/payment_request/payment_request.js index 9696a6bfc2a..31963793da2 100644 --- a/erpnext/accounts/doctype/payment_request/payment_request.js +++ b/erpnext/accounts/doctype/payment_request/payment_request.js @@ -37,6 +37,8 @@ frappe.ui.form.on("Payment Request", "refresh", function (frm) { frm.set_intro(__("Failure: {0}", [frm.doc.failed_reason]), "red"); } + let sending_email = false; + if ( frm.doc.payment_request_type == "Inward" && frm.doc.payment_channel !== "Phone" && @@ -45,16 +47,16 @@ frappe.ui.form.on("Payment Request", "refresh", function (frm) { frm.doc.docstatus == 1 ) { frm.add_custom_button(__("Resend Payment Email"), function () { - frappe.call({ - method: "erpnext.accounts.doctype.payment_request.payment_request.resend_payment_email", - args: { docname: frm.doc.name }, - freeze: true, - freeze_message: __("Sending"), - callback: function (r) { - if (!r.exc) { - frappe.msgprint(__("Message Sent")); - } - }, + if (sending_email) { + frappe.show_alert({ message: __("Sending Email"), indicator: "blue" }); + return; + } + sending_email = true; + frappe.show_alert({ message: __("Sending Email"), indicator: "blue" }); + frm.call("resend_payment_email").then((r) => { + const msg = !r.exc ? __("Email Sent") : __("Email couldn't be sent."); + frappe.show_alert({ message: msg, indicator: !r.exc ? "green" : "red" }); + sending_email = false; }); }); } diff --git a/erpnext/accounts/doctype/payment_request/payment_request.py b/erpnext/accounts/doctype/payment_request/payment_request.py index 70b28141cf6..c2cc317e5e3 100644 --- a/erpnext/accounts/doctype/payment_request/payment_request.py +++ b/erpnext/accounts/doctype/payment_request/payment_request.py @@ -423,6 +423,18 @@ class PaymentRequest(Document): return payment_entry + @frappe.whitelist(methods=["POST"]) + def resend_payment_email(self): + if not ( + self.docstatus == 1 + and self.payment_request_type == "Inward" + and self.payment_channel != "Phone" + and self.status not in ["Initiated", "Paid"] + ): + frappe.throw(_("Payment Link couldn't be sent.")) + + self.send_email() + def send_email(self): """send email with payment link""" email_args = { @@ -440,11 +452,14 @@ class PaymentRequest(Document): ) ], } + job_id = f"send_payment_email::{self.name}" enqueue( method=frappe.sendmail, queue="short", timeout=300, is_async=True, + job_id=job_id, + deduplicate=True, enqueue_after_commit=True, **email_args, ) @@ -951,11 +966,6 @@ def get_print_format_list(ref_doctype): return {"print_format": print_format_list} -@frappe.whitelist() -def resend_payment_email(docname): - return frappe.get_doc("Payment Request", docname).send_email() - - @frappe.whitelist() def make_payment_entry(docname): doc = frappe.get_doc("Payment Request", docname)