Total Wth not reflecting

Hello everyone,

I have an issue here.

Below is my code for calculating Total WTH:

{% set total_wth_amount = 0 %}

{% for item in doc.items %}
{% if item.item_name == “Rent” or item.item_name == “Science TextBook” or item.item_name == “WTH item” %}
{% set wth_rate = 5 %}
{% else %}
{% set wth_rate = 0 %}
{% endif %}

{% if wth_rate > 0 %}
{{ item.item_name }} item amount is
 {{ item.get_formatted(“base_net_amount”, doc) }}  

Tax withheld at  {{ wth_rate }}  %
{% set wth_amount = item.base_net_amount * (wth_rate / 100) %}
  {{ frappe.format_value(wth_amount, {‘fieldtype’: ‘Currency’, ‘options’: ‘TZS’}) }}  

{% set total_wth_amount = total_wth_amount + wth_amount %}

{% endif %}
{% endfor %}

Total withheld tax amount is:
  {{ frappe.format_value(total_wth_amount, {‘fieldtype’: ‘Currency’, ‘options’: ‘TZS’}) }}  

It does calculate Total Wth
Anyone to help debug?

Regards.

@Noreen_Mushema

{% set total_wth_amount = namespace(cnt=0) %}

use this and try it

1 Like

Please try it:

{% set total_wth_amount = [0] %}

{% for item in doc.items %}
    {% if item.item_name == "Rent" or item.item_name == "Science TextBook" or item.item_name == "WTH item" %}
        {% set wth_rate = 5 %}
    {% else %}
        {% set wth_rate = 0 %}
    {% endif %}

    {% if wth_rate > 0 %}
        {% set wth_amount = item.base_net_amount * (wth_rate / 100) %}
        {% if total_wth_amount.append(total_wth_amount.pop() + wth_amount) %}{% endif %}
    {% endif %}
{% endfor %}

Total withheld tax amount is:
{{ frappe.format_value(total_wth_amount.pop(), {'fieldtype': 'Currency', 'options': 'TZS'}) }}

Reference:

Resolved

Thanks