Want to Fetch Customer Account Balance on Pos Invoice

Can anyone give code for pos invoice which is in jinja format to show customer balance. i am using following code but it didnt show exact result.
"{% set invoices = frappe.get_list(‘Sales Invoice’, {‘customer’: doc.customer, ‘docstatus’: 1}, [‘outstanding_amount’]) %}
{% set total_unpaid_amount = invoices | map(attribute=‘outstanding_amount’) | sum %}

Your outstanding balance as of {{ doc.posting_date.strftime('%d-%m-%Y') }} is: {{ total_unpaid_amount | default(0.0) }}

"

Hi @am1024,

Please apply it and check it.

{% set invoices = frappe.get_all('Sales Invoice', filters={'customer': doc.customer, 'docstatus': 1}, fields=['outstanding_amount']) %}

{% set total_unpaid_amount = [0] %}
{% for invoice in invoices %}
    {% set total_unpaid_invoice = invoice.outstanding_amount %}
    {% if total_unpaid_amount.append(total_unpaid_amount.pop() + total_unpaid_invoice) %}{% endif %}
{% endfor %}

Your outstanding balance as of {{ frappe.utils.get_datetime(doc.posting_date).strftime("%d-%m-%Y") }} is: {{ total_unpaid_amount.pop() | default(0.0) }}

Output:

I tried with Delivery Note custom print format for the testing.

Thank You!

Thanks! @NCP It perfectly works (Y).

@NCP Is it possible to also add unallocated payments so if payment reconciliation is not done it shows correct amount ?

It’s hard to get in print format.

OK Thanks this works great. (Y)