Custom script to get customer balance on Sales Invoice

This link might help as well:

thank you , i was searching for this calculation ,

frappe.ui.form.on(‘Task’, {
validate: function(frm, cdt, cdn) {
// make calculation on the fields
var b = flt(frm.doc.rate_per_fedden);
var c = flt(frm.doc.treated_area);
var a = b * c;
frm.set_value(‘total_quantity’, a);
frm.refresh_field(‘total_quantity’);
}
})

Do we have script in client side?

The script is client side script, in the Custom Script Doc Type.

1 Like

how to use this method for email notifications to get closing balance of customer on submission of payment entry.

I tried using this in Message section of new notification:

{{ erpnext.accounts.utils.get_balance_on(date= frappe.datetime.nowdate(), party_type= doc.customer, party= doc.customer) }}

But its not working. Kindly guide me.

1 Like

is it possible to use it in jinja?

@Ratanak,

I couldn’t make it work. I tried to find a custom script to fetch customer’s credit_limit into a custom field called customer_credit_limit in Sales Invoice doctype.

Tried to scroll through various forum post to fetch child table’s value into a single custom field on another doctype, apparently, there aren’t any similar post.

Found the solution from here Cur_frm.add_fetch - #5 by PAMPERO

This will fetch the first’s row’s value. We can use this for any types of child table value fetching into a single field of another doctype.

frappe.ui.form.on("Sales Invoice", "customer",
function(frm) {
    frappe.call({
        "method": "frappe.client.get",
        args: {
            doctype: "Customer",
            name: frm.doc.customer
        },
        callback: function (data) {
            frappe.model.set_value(frm.doctype,
                frm.docname, "credit_limit",
             data.message.credit_limits[0].credit_limit
               );
        }
    });
});
1 Like

Have you Find any solution? How to implement it

Here is script to get customer balance on invoice using html

 {% if doc.customer %}
      {% set ledger_entries = frappe.get_all("GL Entry", filters={"party_type": "Customer", "party": doc.customer}, fields=["SUM(debit) - SUM(credit) as balance"]) %}
      {% if ledger_entries %}
        {% set balance = ledger_entries[0].balance %}
        <p><b>Customer Balance:  {{ frappe.format_value(balance, { "fieldtype": "Currency" }) }}</b></p>
      {% endif %}
    {% endif %}
1 Like