After upgrade to 3.3.6 template for receipt fails, but works fine in template editor

After upgrade to 3.3.6 generating receipts via “Generate Documents” we get the error:

Jinja Template
    unsupported operand type(s) for +: 'float' and 'decimal.Decimal'

This error does not occur in template editor.

The relevant code we use seems to be

{% set data = namespace(counter=0, brut=0, tax=0) %}
...
{% for field in registration.field_data if field.actual_price %}
  {% set data.counter = data.counter + 1 %}
  {% set tv = field.actual_price * 0.16666666 %}
  {% set data.brut = data.brut + field.actual_price %} // if this line  and
  {% set data.tax = data.tax + tv %} // and this line are removed the error disappears, but obvs wrong values
... some printing ...
{% endfor %}

I suppose that I need to cast in the set data line, but it’s not clear to me how I’d do it in the template.

minimal template.html:

{% set data = namespace(counter= 0, brut=0, tax=0) %}
<table class="sum">
  <thead>
    <tr>
      <th>Position</th>
      <th>Description</th>
      <th></th>
      <th></th>
    </tr>
    <tr>
      <th></th>
      <th>Condition</th>
      <th></th>
      <th>Price</th>
    </tr>
  </thead>
  {% if registration.base_price %}
  {% set data.counter = data.counter + 1 %}
  {% set data.brut = data.brut + registration.base_price %}
  <tbody>
    <tr>
      <td>{{data.counter}}</td>
      <td colspan="2">Registration fee
      </td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td>Brutto</td>
      <td></td>
      <td>{{ registration.base_price|format_currency(registration.currency) }}</td>
    </tr>
    <tr>
      <td></td>
      <td>Included Tax</td>
      <td>0%</td>
      <td>{{0|format_currency(registration.currency) }}</td>
    </tr>
  </tbody>
  {% endif %}
  {% for field in registration.field_data if field.actual_price %}
  {% set data.counter = data.counter + 1 %}
  {% set data.brut = data.brut + field.actual_price %}
  <tbody>
    <tr>
      <td>{{data.counter}}</td>
      <td colspan="2">{{ field.title }}{% if field.friendly_value %} ({{ field.friendly_value }}){% endif %} </td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td>Brutto</td>
      <td></td>
      <td>{{ field.actual_price|format_currency(registration.currency) }}</td>
    </tr>
    <tr>
      <td></td>
      <td>Included Tax</td>
    
      {%- set tv = field.actual_price * 0.16666666 -%}
      {%- set data.tax = data.tax + tv -%}
      <td>20%</td>
      <td>{{ tv |format_currency(registration.currency) }}</td>
    </tr>
  </tbody>
  {% endfor %}
  <tbody>
    <tr class="total">
      <td>Total</td>
      <td>Brutto</td>
      <td></td>
      <td>{{data.brut|format_currency(registration.currency) }}</td>
    </tr>
    <tr>
      <td></td>
      <td>Included Tax</td>
      <td></td>
      <td>{{data.tax|round(2)|format_currency(registration.currency) }}</td>
    </tr>
  </tbody>
</table>

To avoid rounding errors, the price is a Decimal instead of a float. You may need to cast it to float in the template:

{%- set tv = field.actual_price|float * 0.16666666 -%}

{% set data.brut = data.brut + field.actual_price|float %}

Thanks. Those changes cause the template to render.

Still I’m curious - is there a way to perform the calculation in decimal?
I mean I get that multiplying with that odd value for tax would need to be in float. But the line {% set data.brut = data.brut + field.actual_price %} looks like it should be possible without introducing rounding.

I don’t think we expose Decimal to the template, so unless you can do some trickery to do it without accessing that it might be hard

That’s fine and I can work around it. It’s just surprising as it was not required for previous version and the template editor still seems fine without these adjustments.

Yeah, changing the type was a bugfix because there were some rounding issues (see Accommodation price not in Decimal · Issue #6671 · indico/indico · GitHub and and Convert field/item prices to decimal by ThiefMaster · Pull Request #6697 · indico/indico · GitHub).

Our default templates don’t do any calculations where Decimal and float get mixed, so everything works fine there.

and the template editor still seems fine without these adjustments

not sure what you mean with this…

When I edit the template and do the “old” calculation (without the float conversion), it renders the “How to build a Stargate” Receipt preview without error.

Ohh I see the problem - we almost certainly use float there since it’s static data… Something to fix on our side!