Adding fields to the document template Jinja environment

Hello,

Any advice on how to add fields/filters to the Jinja environment for document templates?

I need to show VAT and NET amounts on our invoices so I have been adding them to indico/modules/receipts/util.py like in this gist indico/modules/receipts/util.py · GitHub, then I can do the following <td>{{ registration.base_price|calc_vat }}</td>

Ideally, I would want to have this in a plugin but I can’t seem to figure out how to get these into the Jinja environment. It looks like the templates make a sandbox jinja environment and I can’t seem to update that from a plugin… Any help would be appreciated.

Thanks
Adam

why not do those calculations in the template itself?

Ahhh, thats a much better idea, I will give that a go, thanks!

If you have a custom payment plugin, you can also put extra data inside the transaction data.

For example we do something similar for indico.cern.ch to show a separate invoice line for the payment provider fee:

{% set txn = registration.transaction %}
{% set card_fee = none %}
{% if txn and txn.provider == 'cern' and txn.status == 'successful' %}
  {% if 'meta_data' in txn.data %}
    {% set card_fee = namespace(
      brand=txn.data.meta_data.payment_method_title,
      price=(txn.amount - registration.total_price),
      formatted_price=(txn.amount - registration.total_price)|format_currency(txn.currency)
    ) %}
  {% else %}
    {% set card_fee = namespace(
      brand=txn.data.BRAND,
      price=(txn.amount - registration.total_price),
      formatted_price=(txn.amount - registration.total_price)|format_currency(txn.currency)
    ) %}
  {% endif %}
{% endif %}

and later in the template:

    <tr class="total">
        <td><strong>{% if card_fee %}Registration subtotal{% else %}Total{% endif %}</strong></td>
        <td>{{ registration.total_price|format_currency(registration.currency) }}</td>
    </tr>
    {% if card_fee and card_fee.price %}
      <tr>
        <td>Payment method fee ({{ card_fee.brand }})</td>
        <td>{{ card_fee.formatted_price }}</td>
      </tr>
    {% endif %}
    {% if txn and txn.status == 'successful' and txn.provider != '_manual' %}
      <tr class="total">
        <td><strong>Total paid</strong></td>
        <td>{{ txn.amount|format_currency(txn.currency) }}</td>
      </tr>    
    {% endif %}
1 Like