Creating an Index for Documents not containig a specific tag

Hi,

I am looking forward to a solution for the following problem:
We have invoices in mayan. I’d like to use tags for the accounting like “paid”, “expensed”, “checked” to be sure they were actively marked. Later on employees need for example to get a list of unpaid invoices.
I expected this to be rather simple by an index which shows all invoices which don’t have the tag “paid”. Unfortunately after a day of research I didn’t succeed. Can you get me any hints?

This is what I tried so far. It didn’t find any documents.

{% bez=true %} {% for tag in document.tags.all %} {% if tag.label=="Paid" %}{% bez=false %}{% endif %} {% endfor %} {% if bez %} Bez {% endif %}

Kind regards
Daniel

Hi,

The problem seems to be only a few syntax errors. Always use the sandbox to test template and obtain error information.

I tested this modification and seems to work.

{% set 1 as bez %} {% for tag in document.tags.all %} {% if tag.label == 'Paid' %} {% set 0 as bez %} {% endif %} {% endfor %} {% if bez %} Bez {% endif %}

or this:

{% spaceless %}{% for tag in document.tags.all %} {% if tag.label == 'Paid' %}{% set True as paid %}{% endif %} {% endfor %} {% if not paid %}Bez{% endif %}{% endspaceless %}

Tested this live and there seems to a scope issue:

The variable change inside the loop is local to the loop. The variable gets reset back to the original value when the loop ends.

Seems we need to add scope control to the {% set %} tag.

I’ve added this for discussion and follow up.

1 Like

I don’t see what you need the variable for. Why can’t you just use:

{% for tag in document.tags.all %}{% if tag.label == ‘Paid’ %} Bez {% endif %} {% endfor %}

For the variable scope problem I only found a very hacky way around it by misusing the „now“ template:

Thank you very much for your reply. Now I understood what sandbox is for. :wink:
So the scope of set is necessary for realizing this? Do you maybe have another idea to solve the problem?

Thank you very much for your reply. The variable is necessary in my opinion because i search for all documents which are not tagged with “paid”. I will try the dirty hack in the evening.

Oh ok I see. I’m not sure if you can use this hack for your use case, it’s a completely different scenario than the one in the linked thread.

Edit: I think it would work

{%spaceless%}{% for tag in document.tags.all %} {% if tag.label == “Paid” %} {% now “Y” as paid %} {% endif %} {% endfor %} {% if not paid %}TRUE{% endif %}{%endspaceless%}

Returns TRUE if the document does not have the tag attached.

Thank you. But sadly it also returns TRUE if the document has the tag attached.
What does {%spaceless%} do?

{%spaceless%} trims superfluous spaces in the end result. These are spaces added between tags or as carriage returns. Since the output of the template is going to be the value of the index level entry, extra spaces could sometimes cause minor issues.

You are right… strange… apparently the scope of the paid variable is also lost after the loop :frowning: