Custom script to get customer balance on Sales Invoice

Hi, I tried this but dint work.Could you Kindly illustrate, I would like to have this script in a custom jinja print format.

Hi
Can we fetch account balance in Jinja Template ?

@Ratanak How can I fetch the customer’s Credit Limit or even better Remaining Credit where

remaining_credit = credit_limit - outstanding_balance

did you create a custom field called: customer_balance?
the custom field can be any name you prefer, just replace the “customer_balance” in the custom script above with the name of your custom field.

You’ll have to create a custom field for “remaining_credit”, another custom field to fetch “credit_limit” from Customer, then write another custom script to calculate the remaining_credit.

I don’t know of any way you can do that in Jinja Template.
The only way I know is create a custom field and use the custom script below to fetch the customer balance and print the custom field in your Jinja Template.
Also even if you could do that in Jinja Template, it is not recommended as the value won’t saved in the database as it would a custom field

Your solution would look something like this, provided you have the following custom fields:

  1. remaining_credit

  2. customer_balance (use the script above to fetch)

  3. credit_limit (fetch it from Customer)

     frappe.ui.form.on('Sales Order', {
             validate: function(frm, cdt, cdn) {
             	// make calculation on the fields
                 var b = flt(frm.doc.credit_limit);
                 var c = flt(frm.doc.customer_balance);
         		var a = b - c;
         		frm.set_value('remaining_balance', a);
         		frm.refresh_field('remaining_balance');
             }
         });
    
1 Like

Thank you. I tried to fetch credit limit from Customer doctype, but credit_limit is a child table field of Customer Credit Limit table.

I was unable to fetch the credit limit, let alone calculate the remaining credit. I had created three custom fields , customer_balance, credit_limit and remaining_limit.

How can I fetch customer’s credit limit in sales invoice?

You would need to google custom script for fetching field from child table.

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