1

what I want to do is disable print from student applicant document if student still has outstanding balance. I tried this script but nothing happens.

 frappe.ui.form.on("Student Applicant", {
   onPrintDocument: function(frm, doc, ev) {
     // Get the Fees document
     const feesDocument = frappe.get_doc("Fees", frm.doc.fees);
 
     // Check if the outstanding amount is 0
     const outstandingAmount = feesDocument.outstanding_amount;
     if (outstandingAmount === 0) {
       // The outstanding amount is 0, allow printing
     } else {
       // The outstanding amount is not 0, prevent printing
       frappe.msgprint(
         "Fees payment not received in full. Cannot print."
       );
       ev.preventDefault();
     }
   }
 });
eglease
  • 2,445
  • 11
  • 18
  • 28
Khallad
  • 123
  • 4
  • 24

1 Answers1

0

Hello everything is fine? in this case I don't know if the trigger used by you onPrintDocument would be a better option.

Try using before_print which allows you to execute a function before the print dialog box is shown.

And in stop condition use a throw new Error to force an interruption of program execution.

I'll leave here the adjustments I made in the code for you to test and validate improvements in the function implemented above.

frappe.ui.form.on("Student Applicant", {
  before_print: function(frm, print_format) {
    // Get the Fees document
    frappe.model.with_doc("Fees", frm.doc.fees, function() {
      const feesDocument = frappe.get_doc("Fees", frm.doc.fees);

      // Check if the outstanding amount is 0
      const outstandingAmount = feesDocument.outstanding_amount;
      if (outstandingAmount === 0) {
        // The outstanding amount is 0, allow printing
        return; // return unblocked to print
      } else {
        // The outstanding amount is not 0, prevent printing
        frappe.msgprint("Cannot print.");
        throw new Error("Cannot print.");
      }
    });
  }
});

I checked this method on frappe's github where you can check its construction here

https://github.com/frappe/frappe/blob/db61deef7245b371322d141c3f5a1d7c385751b5/frappe/www/printview.py#L128

use function https://github.com/frappe/frappe/blob/db61deef7245b371322d141c3f5a1d7c385751b5/frappe/desk/doctype/note/note.py#L34

Validate if this solution will help with what you need.