From 4357ce7fcc33db4435277fa2412304f1463789f1 Mon Sep 17 00:00:00 2001 From: Ty Reynolds Date: Tue, 10 Feb 2026 08:42:57 -0500 Subject: [PATCH] Updated invoice and autopay logic. --- ns_app/api/payments.py | 6 +-- ns_app/public/js/sales_invoice.js | 66 +++++++++++++++---------------- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/ns_app/api/payments.py b/ns_app/api/payments.py index 61c022e..5b314ef 100644 --- a/ns_app/api/payments.py +++ b/ns_app/api/payments.py @@ -8,8 +8,8 @@ def check_autopay(customer): cust = frappe.get_doc("Customer", customer) return { - "autopay_enabled": bool(cust.auto_pay), - "autopay_id": cust.auto_pay_id if cust.auto_pay else None + "autopay_enabled": bool(cust.custom_auto_pay_status), + "autopay_id": cust.auto_pay_id if cust.custom_auto_pay_status else None } @@ -22,7 +22,7 @@ def run_autopay_payment(invoice): cust = frappe.get_doc("Customer", inv.customer) - if not cust.auto_pay or not cust.auto_pay_id: + if not cust.custom_auto_pay_status or not cust.auto_pay_id: frappe.throw("Customer does not have AutoPay enabled") payload = { diff --git a/ns_app/public/js/sales_invoice.js b/ns_app/public/js/sales_invoice.js index 87ff0e8..a2daa8c 100644 --- a/ns_app/public/js/sales_invoice.js +++ b/ns_app/public/js/sales_invoice.js @@ -2,10 +2,11 @@ frappe.ui.form.on("Sales Invoice", { refresh(frm) { frm.clear_custom_buttons(); + // Only on submitted invoices if (frm.doc.docstatus !== 1) return; if (!frm.doc.customer) return; - // If already paid, don't show button + // Already paid if (frm.doc.outstanding_amount <= 0) { frm.dashboard.add_indicator("Paid", "green"); return; @@ -27,10 +28,13 @@ function run_payment_flow(frm) { method: "ns_app.api.payments.check_autopay", args: { customer: frm.doc.customer }, callback(r) { - if (!r.message) return; + if (!r.message) { + frm.enable_save(); + return; + } - if (r.message.autopay_enabled) { - run_autopay(frm, r.message.autopay_id); + if (r.message.autopay_enabled && r.message.autopay_id) { + run_autopay(frm); } else { open_manual_payment_form(frm); } @@ -39,8 +43,29 @@ function run_payment_flow(frm) { } -function open_manual_payment_form(frm) { +function run_autopay(frm) { + frappe.confirm( + `Run AutoPay for $${frm.doc.outstanding_amount}?`, + () => { + frappe.call({ + method: "ns_app.api.payments.run_autopay_payment", + args: { + invoice: frm.doc.name + }, + callback(r) { + frappe.msgprint("Payment successful"); + frm.reload_doc(); + } + }); + }, + () => { + frm.enable_save(); + } + ); +} + +function open_manual_payment_form(frm) { const dialog = new frappe.ui.Dialog({ title: "Secure Payment", size: "large", @@ -50,7 +75,7 @@ function open_manual_payment_form(frm) { fieldname: "loader", options: `
- Loading secure checkout... + Loading secure checkout…
` }, @@ -91,7 +116,7 @@ function open_manual_payment_form(frm) { } }); - // Auto refresh every 5 seconds while dialog open + // Poll invoice every 5 seconds const poller = setInterval(() => { frappe.call({ method: "frappe.client.get", @@ -100,7 +125,7 @@ function open_manual_payment_form(frm) { name: frm.doc.name }, callback(r) { - if (r.message.outstanding_amount <= 0) { + if (r.message && r.message.outstanding_amount <= 0) { clearInterval(poller); dialog.hide(); frm.reload_doc(); @@ -110,28 +135,3 @@ function open_manual_payment_form(frm) { }); }, 5000); } - - -function run_autopay(frm, autopay_id) { - - frappe.confirm( - `Run AutoPay for $${frm.doc.grand_total}?`, - () => { - - frm.disable_save(); - - frappe.call({ - method: "ns_app.api.payments.run_autopay_payment", - args: { - invoice: frm.doc.name, - autopay_id: autopay_id, - amount: frm.doc.grand_total - }, - callback(r) { - frappe.msgprint("Payment successful"); - frm.reload_doc(); - } - }); - } - ); -}