Compare decimals values on FODT reports

Hi,

I’m trying to add a new customized report based on invoice.fodt (module account_invoice), and I have a different columns for prints taxes lines, if the products has a 5% tax rate go on first column, 10% second column… so I did a simple IF to prints it or not.

<for each="tax in line.taxes">
	<if test="tax.rate == 0.05">
		<format_currency(line.amount, invoice.party.lang, invoice.currency)>
	</if>
</for>

But it doesn’t work, I don’t know why its doesn’t match and nothing is printed. I tried to print only tax.rate just to see its value, and it shows me 0.05.

So my question is what am I doing wrong in my IF?

This is because 0.05 as float is not really 0.05 like Decimal('0.05') is. You must compare against Decimal('0.05') but you will need to add Decimal to the context.
But maybe it is better to add a flag on the tax Model such that you can make the distinction between each taxes.

I will try with the flag, but If I try using Decimal how can I adding it to the context, because now is showing an error because it. I have to put a import inside a input placeholder into the fodt file?

And just to know, the language using by the report what is its name? not exists a way to compare derectly floats like tax.rate == 0.05f or something like that?

Thanks for you reply

You have to extend the Report.get_context of your report class.

float comparison is always doomed to fail. You should always compare Decimal.

Just to be extra clear, the problem you are having is because tax.rate is not a float, it’s a Python Decimal. So when you compare it to a float like 0.05 it is never the same.

As a last resort and work around if you are unable, or unwilling, to use one of the methods already suggested, then you might be able to convert it to a string and then do a string comparison.
Something like tax.rate.to_eng_string() == '0.05', but I haven’t tested this so it might not work.

I’m so lost here :pensive: . I don’t have a Report class, at least one I known.

To make a new invoice I just added this lines to my main XML

 <record model="ir.action.report" id="report_invoice_py">
            <field name="name">Invoice Py</field>
            <field name="model">account.invoice</field>
            <field name="report_name">account.invoice</field>
            <field name="report">account_invoice_py/invoice_kyrey.fodt</field>
            <field name="single" eval="True"/>
        </record>
        <record model="ir.action.keyword" id="report_invoice_py_keyword">
            <field name="keyword">form_print</field>
            <field name="model">account.invoice,-1</field>
            <field name="action" ref="report_invoice_py"/>
        </record>

So, I keep the original and my report as a second choice. I created a invoice_py.py file too who inherit invoice.py but here I don’t called Report module my whole imported are

import datetime

from sql import Literal
from sql.aggregate import Count

from trytond.i18n import gettext
from trytond.model.fields import field
from trytond.pool import Pool, PoolMeta
from trytond.model import ModelSQL, ModelView, fields, Unique
from trytond.pyson import Eval, Bool
from trytond.exceptions import UserError
from trytond.transaction import Transaction


__all__ = [
    'Stamped',
    'Establishment',
    'ShippingPoint',
    'Invoice',
    'Sale',
]

This defined the name of the Report class.

Thanks Ced and Dave, many doubts were clarified.

1 Like