Let’s say we create a forloop over an array, and each item of that array will be used to create a hashtag to select some accounts, like this:
Of course, one account can not be linked to more than one categorie. So, how can we make sure that each hashtag show the accounts that have not yet been selected?
First of all, this is the code for above example:
{% comment %}create different categories to loop over{% endcomment %}
{% assign array_categories = "IMVA|MVA|FVA" | split:"|" %}
{% assign global_range = "2" %}
{% stripnewlines %}
|
| {% t "Categorie" %}
| {% t "Openingvalue" %}
| {% t "Endvalue" %}
{% newline %}
|----10%----
|----10%----
|----40%----:
|----40%----:#
{% for cat in array_categories %}
{% capture name_accounts %}accounts_{{ cat }}{% endcapture %}
{% newline %}
| {% input custom.range.[cat] as:account_collection range:global_range accounts_var:name_accounts %}
| **{{ cat }}**
| {{ name_accounts.opening_value | currency_dc }}
| {{ name_accounts.value | currency_dc }}
{% endfor %}
{% endstripnewlines %}
As you can see, the local var global_range will have the range of “2” for each categorie, which is not what we want.
Let’s see how we can tweak this a little bit.
{% assign global_range = period.accounts[global_range] | map:"mapped_number" | join:"$," | replace:"$,","$,$" | prepend:"$" %}
If we do this, we’ll create a string from all 2-accounts, and each account will have a ‘$’ in front of it, and a ‘$,’ after it. like this:
$223000$,$223010$,$223019$,$223090$,$230000$,$230090$,$240000$,$240090$,$240100$,$240190$,$240200$,$240290$,$241000$,$241090$,$288000
Why is this? Later on, we want to make sure that each account is unique. If we do some logic around it, that we are sure that account 400 and account 4000000 are not considered as the same account. This is exceptional but we rather make sure this is prevented in our code.
Next, we’ll loop over all hashtags, which are database variables so no matter where you are in your code, you can access it:
{% for cat in array_categories %}
{% assign cat_range = custom.range.[cat] %}
{% assign cat_accounts = period.accounts | range:cat_range %}
{% for acc in cat_accounts %}
{% assign selected = selected | append:"$" | append:acc.mapped_number | append:"$," %}
{% assign global_range = global_range | remove:selected %}
{% comment %}set string back to nothing{% endcomment %}
{% assign selected = "" %}
{% endfor %}
{% endfor %}
Let’s examine this step-by-step:
{% for cat in array_categories %}
{% assign cat_range = custom.range.[cat] %}
…
{% endfor %}
The local var cat_range will have the value of the selected accounts for each categorie.
{% assign cat_accounts = period.accounts | range:cat_range %}
In each loop (categorie), we’ll create an account collection filtered on that particular range we’ve chosen.
{% for acc in cat_accounts %}
…
{% endfor %}
In each categorie, we loop over all those accounts of each account collection per categorie.
{% assign selected = selected | append:“$” | append:acc.mapped_number | append:“$,” %}
In each account though, we’ll create a string, that will have the account number, but with a “$” in front, and a “$,” after. This string is needed in the next step to remove it from our local var global_range :
{% assign global_range = global_range | remove:selected %}
In each account, we take that string selected and remove it from the string with all accounts, generated from the beginning.
So we end with something like this:
$223019$,$223090$,$230000$,$230090$,$288000
After each account, we’ll reset the string selected though:
{% assign selected = “” %}
This is needed because we need to check individual accounts, not a string of several accounts!
Now we’ll need to tweak our local var global_range again in order to make it usable as a range variable. A range variable always has to have an account number, followed by a “$” and all separated by a “,”. Like this:
223019$,223090$,230000$,230090$,288000$
So, we’ll use this code:
{% assign global_range = global_range | remove_first:"$" | replace:"$,$","$," | append:"$" %}
where we remove the first “$” we have (the very first character can’t be a “$”), and replace all other “$,$” by “$,”, and add a last $ at the end of the string.
This way, we have created a local var global_range that will always feature unique accounts.
Here’s the complete code:
{% comment %}create different categories to loop over{% endcomment %}
{% assign array_categories = "IMVA|MVA|FVA" | split:"|" %}
{% assign selectable = "" %}
{% assign global_range = "2" %}
{% comment %}make overview of all selectable accounts{% endcomment %}
{% assign global_range = period.accounts[global_range] | map:"mapped_number" | join:"$," | replace:"$,","$,$" | prepend:"$" %}
{% comment %}loop over categories with selected accounts through the hashtag, and remove them from the var global_range{% endcomment %}
{% for cat in array_categories %}
{% assign cat_range = custom.range.[cat] %}
{% assign cat_accounts = period.accounts | range:cat_range %}
{% for acc in cat_accounts %}
{% assign selected = selected | append:"$" | append:acc.mapped_number | append:"$," %}
{% assign global_range = global_range | remove:selected %}
{% comment %}set string back to nothing{% endcomment %}
{% assign selected = "" %}
{% endfor %}
{% endfor %}
{% comment %}set var "global_range" back to usable string for acc range{% endcomment %}
{% assign global_range = global_range | remove_first:"$" | replace:"$,$","$," | append:"$" %}
{% stripnewlines %}
|
| {% t "Categorie" %}
| {% t "Openingvalue" %}
| {% t "Endvalue" %}
{% newline %}
|----10%----
|----10%----
|----40%----:
|----40%----:#
{% for cat in array_categories %}
{% capture name_accounts %}accounts_{{ cat }}{% endcapture %}
{% newline %}
| {% input custom.range.[cat] as:account_collection range:global_range accounts_var:name_accounts %}
| **{{ cat }}**
| {{ name_accounts.opening_value | currency_dc }}
| {{ name_accounts.value | currency_dc }}
{% endfor %}
{% endstripnewlines %}