CASE: create and overwrite adjustment of same purpose

Let’s take this case as an example:

Now, what if this template is something that is done more than once, for a specific selection of accounts? Then each time, it would create another adjustment for those selected accounts. Meaning they will have a link to several adjustments, making it hard to control the adjustments: each previous adjustment perhaps needs to be deleted, as it can not alter the amount anymore of the account? As each adjustment has the same purpose anyway (just counter booking the selected accounts), there might not be a need to create an new adjustment for each selected account.

Well, we now have a new attribute purpose you can add to the adjustment coding, which allows to create an adjustment that belongs to an unique purpose, and instead of creating multiple adjustments to the same accounts, it’ll overwrite the adjustment that has that unique purpose.
So, no matter how many times you would create an adjustment in above example (purpose = counter book selected accounts), we would always end up with the same adjustment.

How?

Well, first of all, we will create an unique purpose of the adjustment. In our example we can say that the selection of accounts, is what makes the purpose.

So we can do this:

{% comment %}create unique "purpose" ID to use in our purpose attribute{% endcomment %}
{% assign selected_accounts = custom.accounts.counter | prepend:"selected_accounts_for:" %}

which will result in a string like

selected_accounts_for:#21040533,#21040541

Now, the output of that variable, will be used in the purpose attribute like this:

{% adjustmentbutton text:adj_txt purpose:selected_accounts %} 

So whatever selection we make, is the output of the selection is the same and we create an adjustment out of it, it won’t be created as a new adjustment. Instead, it will link to an existing adjustment with that same purpose, and overwrite it (if none can be found, it will obviously create a new adjustment).

Here’s a complete example:

{% comment %}This case describes the functionality of the adjustmentbutton-tag which can create a button to automatically make an adjustment, filled with values taken from Liquid logic{% endcomment %}
{% ic %}{::infotext}
{% t "Select all accounts that need to be counter booked" %} {% input custom.accounts.counter as:account_collection range:"2" %}
{% t "Depending on the value of all accounts, the remaining value will be booked to either 76 or 66 range" %}
{:/infotext}{% endic %}

{% comment %}We take the value of the chosen accounts to create a account collection{% endcomment %}
{% assign accounts = period.accounts | range:custom.accounts.counter %}

{% comment %}create unique "purpose" ID to use in our purpose attribute{% endcomment %}
{% assign selected_accounts = custom.accounts.counter | prepend:"selected_accounts_for:" %}

{% comment %}Show all accounts with the remaining value{% endcomment %}
{% stripnewlines %}
| Acc nbr
| Acc descr.
| Acc value
{% newline %}
|----10%----
|----75%----
|-----------:+
{% for acc in accounts %}
  {% newline %}
  | {{ acc.number }}
  | {{ acc.name }}
  | {% =$0+ acc.value as:currency %}
{% if forloop.last %}
  {% newline %}
  |
  |
  |_^ {{ $0 | currency }} ^_| {% comment %}this value is needed for the counter booking as well{% endcomment %}
{% endif %}  
{% endfor %}
{% endstripnewlines %}

{% comment %}some text we want to display on the adjustment button{% endcomment %}
{% capture adj_txt %}{% t "Create adj." %}{% endcapture %}

{% comment %}Now we'll loop over all those accounts again and counter book them - the remaining value taken in register $0,  will be used as a counter booking as well{% endcomment %}
{% adjustmentbutton text:adj_txt purpose:selected_accounts %}

{% for acc in accounts %}
  {% comment %}description for the counter booking{% endcomment %}
  {% capture description %}{% t "Counter booking" %}{% endcapture %}
  
  {% comment %}needed to propose the counter value of the original value of the account - the type of account and its sign will decide if it is D or C{% endcomment %}
  {% assign value_acc = -acc.value %} 
  
  {% comment %}the attribute "original_account_number" is used to give the account number to be made, with account_name the name and the description. This can be linked to fixed values or local variables you create. And the value can be anything, but it is the combination of the value (+/-) and the type of account (asset/liability/...) that defines where the value needs to be in the adjustment (D/C){% endcomment %}
  {% adjustmenttransaction original_account_number:acc.number account_name:acc.name description:description value:value_acc %}
  
  {% comment %}only one line has to be created extra as the counter booking - see value taken in $0{% endcomment %}
  {% if forloop.last %} {% comment %}only needed one time, at the end of the loop{% endcomment %}
    {% if $0 > 0 %}
      {% comment %}this means the counter booking needs to be on acc 660000{% endcomment %}
      {% assign counter_acc = "660000" %}
      {% assign counter_name = "Counter loss" %}  
    {% else %}
      {% comment %}this means the counter booking needs to be on acc 760000{% endcomment %}
      {% assign counter_acc = "760000" %}
      {% assign counter_name = "Counter profit" %}      
    
    {% endif %}
    {% assign counter_value = $0 %}  {% comment %}no need to counter the sign of the value{% endcomment %}
    
    {% adjustmenttransaction original_account_number:counter_acc account_name:counter_name description:description value:counter_value %}
  {% endif %}
{% endfor %}

{% endadjustmentbutton %}