Calculating total discount amount in custom print-format

I attempted two different ways to calculate total discount amount [after item wise different discount amount in item row] but got error.
method 1:

            {% set dis_amount =0 %}
            {% set total_discount =0%}
            {% for row in doc.items %}
                {% set dis_amount = (row.discount_amount) %}
                {% set total_discount = total_discount + dis_amount %}
                {{ items | sum(attribute= discount_amount ) }} //##optional i found i tried, not worked same output
            {% endfor%}

            {{ total_discount }}

output : 0 --no sum at all
method 2:

{% set total_discount = [0] -%}
{% for item in doc.items -%}
      {% if total_discount.append(total_discount.pop()+ item["discount_amount"]) -%}{% endif %}
 {% endfor %}
 {{ total_discount }}

output is like: [1908.89] —calculate total but “in bracket”, unable to remove

any help or change on this will be deeply appreciated.

Hi @Ulter52,

Please try it and reload and check it.

{% set total_discount = 0 %}
{% for row in doc.items %}
    {% set dis_amount = row.discount_amount or 0 %}
    {% set total_discount = total_discount + dis_amount %}
{% endfor %}
{{ total_discount }}

Thank You!

Hi @NCP
thanks for the reply,
its still has output: 0; not working but in method 2 replacing {{total_discount}} with {{ frappe.format_value( total_discount [0], currency=doc.currency) }} solved [1908.89] error and give me result as â‚ą 1,908.89

please try it.

{% set total_discount = 0 %}
{% for row in doc.items %}
    {% set dis_amount = row.discount_amount or 0 %}
    {% set total_discount = total_discount + dis_amount %}
{% endfor %}
{{ frappe.format_value(total_discount, currency=doc.currency) }}

I tried this code, not worked.

Hi @Ulter52,

Please apply it.

{{ doc.items | sum(attribute='discount_amount') }}

Then reload and check it.

Thank You!

1 Like

thanks bro, finally its worked