Help! Payment Entry/Receipt old outstanding amount

I might not properly understand it well enough but I think the payment entry/receipting needs to be simplified in ERPNext. The current Advanced Payment use case doesn’t properly apply for part-payment by clients. The Payment Entry should have an Amount Due field which is accurate when the payment is submitted, Or Outstanding amount should deduct the just Allocated amount. Maybe a topic for another day.

I’ve converted the Payment Entry print format into a Payment Receipt. For part payment, I need to have the actual Outstanding amount after payment allocation:

Actual Outstanding = Outstanding - Allocated.

I’ve been toiling with scripts for over 24hrs and all I get is a blank field in the Payment Entry Doc Type. I can manually set it, but I need it to automatically update so that users dont make errors.

I’ll appreciate the help.


Custom outstanding field in PE. Not reading script value, Its currently user entered.


Custom outstanding amount in print form. I need this to be automatically updated using the formula above.

Heres my script:

//update custom Outstanding currency type field within Payment Entry DocType
frappe.ui.form.on("Payment Entry", "validate", function(frm) {
    $.each(frm.doc.references || [], function(i, d) {
        
        // calculate using PE references table fields
        frm.doc.Outstanding= d.outstanding - d.allocated;
        
    });
});

I’m really not sure here, please help

May be you can try to get outstanding amount from linked invoice directly to print format without adding any custom field or script.

Just run the loop on “Payment References” table to get outstanding amount in each sales invoice. It shouldn’t be that hard if you have custom print format.

For Your JS Code try this

//update custom Outstanding currency type field within Payment Entry DocType
frappe.ui.form.on("Payment Entry", "validate", function(frm) {
	outstanding = 0;
    $.each(frm.doc.references || [], function(cdt, cdn) {
        var d = locals[cdt][cdn];
        // calculate using PE references table fields
        outstanding += (d.outstanding - d.allocated);
        
    });
	frm.set_value("outstanding", outstanding);
});
1 Like

Thank you so much for your careful response @Mukesh_Variyani.

I’ll have to check again, but the invoice doesnt always seem to have the updated outstanding balance. It’s a worthy shot. I really would like to see this table method work and I think with your script its quite close.

I’m getting a TypeError in the console and the save button isn’t responting (please see attached image). The variable cdt. I would like to see the outstanding amount change as the user changes the amount paid, I hope that’s what the script will accomplish.

//update custom Outstanding currency type field within Payment Entry DocType
frappe.ui.form.on("Payment Entry", "validate", function(frm) {
	outstanding = 0;
    $.each(frm.doc.references || [], function(cdt, cdn) {
        var d = locals[cdt][cdn];
        // calculate using PE references table fields
        outstanding += (d.outstanding_amount- d.allocated_amount);
        
    });
	frm.set_value("outstanding", outstanding);
});

Still stuck on line 5 (comments included):

TypeError: locals[cdt] is undefined

I’ve slightly altered the script so that the its triggered when the user changes the base_paid_amount.

//update custom Outstanding currency type field within Payment Entry DocType
frappe.ui.form.on("Payment Entry", { base_paid_amount: function(frm) {
	outstanding = 0;
	var d;
    $.each(frm.doc.references || [], function(cdt, cdn) {
        d = locals[cdt][cdn];
        // calculate using PE references table fields
        outstanding += (d.outstanding_amount- d.allocated_amount);
        
    });
	frm.set_value("outstanding", outstanding);
    
    }
});

Meanwhile I’m trying to emulate other scripts to achieve this effect but still no luck yet:

I appreciate any further assistance.

EUREKA! (Whatever that means). One less problems in the world.

This finally worked perfectly!

//calculating a new outstanding amount as paid amount is entered. The old outstanding amount is pulled from the Payment Entry References (child) table.
frappe.ui.form.on("Payment Entry", { base_paid_amount: function(frm) {
    var outstanding_amount = 0;    
    var tot_outstanding = 0;
    // add table's outstanding values
    $.each(frm.doc.references, function(index, row){
        tot_outstanding += row.outstanding_amount;
    });
    outstanding_amount = tot_outstanding - frm.doc.base_paid_amount; //subtract paid amount
    frm.set_value("outstanding_amount", outstanding_amount);
  }
});

Thanks for all the help @Mukesh_Variyani.

2 Likes