This question has been flagged
6 Replies
19859 Views

Hello,

I have a problem with number formatting in qweb reports.

I have set Decimal Accuracy in settings and numbers are in correct format if defined as "t-field". If you need to calculate result or call a function you need to do it with "t-esc" and in this case numbers are not in correct format.

I saw you can use "formatLang" as in RML reporting, but you have to wrap old RML parser with Qweb one as in "report_payslip". But isn't this nonsense. Throwing away RML reporting in behalf of Qweb and now wrapping it again?

What will happen in Odoo 9, will this solution work also (as I know there will be no support for RML)?

What can be done? Call a function and format a number on 'python' side or is there some other solution?


Thanks for any info or advise,

Matjaž

Avatar
Discard
Best Answer

Hello Matjaz,

by default we are using,

<span t-field="o.amount" t-field-options="{&quot;widget&quot;: &quot;monetary&quot;, &quot;display_currency&quot;: &quot;o.pricelist_id.currency_id&quot;}"/>

it will automatically set system decimals.


To change "." to "," , use replace function, 

for example,   <span t-esc="(o.amount).replace('.', ',')"/> 

It's working correct from my side.

I hope, it will be clear to you, and correct me if still I misunderstood your concern.


Regards,

Avatar
Discard
Author

First of all your solution doesn't print Thousands Separator and decimal Separator is also wrong. We have ',' for Decimal Separator and '.' for Thousands Separator. in your way number 3548.07 is displayed as 3548.07 but should be 3.548,07. Secondly with your solution you predefine number of decimal places, but number should be taken from the system....

I have updated my answer. please check :)

Author

Thank you for an answer, but no, it's not working as expected. If you use "t-field" is OK, value is being formatted with thousand separator and correct character separator. Using "t-esc" value is not formatted at all. In your "t-esc" example (you forgot to transform float to string), there is no thousand separator and also decimal/thousand separators are fixed and not being pulled from the system. For example: you have several partners that have different decimal/thousand separators..

Best Answer

In Odoo 10 I had the same problem and solved it in a similiar fashion. I orientated myself by the function formatLang() of the misc.py file: https://github.com/odoo/odoo/blob/10.0/odoo/tools/misc.py.

Model:

class AccountInvoice(models.Model):
    _inherit = 'account.invoice'

    def _formatLang(self, value):
        lang = self.partner_id.lang
        lang_objs = self.env['res.lang'].search([('code', '=', lang)])
        if not lang_objs:
            lang_objs = self.env['res.lang'].search([], limit=1)
        lang_obj = lang_objs[0]

        res = lang_obj.format('%.' + str(2) + 'f', value, grouping=True, monetary=True)
        currency_obj = self.currency_id;

        if currency_obj and currency_obj.symbol:
            if currency_obj.position == 'after':
                res = '%s %s' % (res, currency_obj.symbol)
            elif currency_obj and currency_obj.position == 'before':
                res = '%s %s' % (currency_obj.symbol, res)
        return res

QWeb:

<span t-esc="o._formatLang(amount_by_group[1])"/>
Avatar
Discard
Author Best Answer

Because there is NO solution about formatting numbers and no answer regarding wrapping RML reports, my solution is a function inside report py file which can be called from Qweb reports. You can select number of decimal points (default is two) and display currency symbol if you pass in the currency object. Function is a mix of code from ir_qweb.py (class FloatConverter and class MonetaryConverter). If someone has a kind of direct solution or can do better coding please....


class report_invoice(models.AbstractModel):

_name = 'report.account.report_invoice'

def convert_2float(self, value, precision=2, currency_obj=None, context=None):

    fmt = '%f' if precision is None else '%.{precision}f'

    lang_code = self.env.context.get('lang') or 'en_US'

    lang = self.pool['res.lang']

    formatted = lang.format(self.env.cr, self.env.uid, [lang_code], fmt.format(precision=precision), value, grouping=True,     monetary=True)

    if currency_obj:

    if currency_obj.position == 'before':

        formatted = currency_obj.symbol + ' ' + formatted

    else:

        formatted = formatted + ' ' + currency_obj.symbol

    return formatted


@api.multi

def render_html(self, data=None):

report_obj = self.env['report']

report = report_obj._get_report_from_name('account.report_invoice')

docargs = {

    'convert_2float': self.convert_2float,

     'doc_ids': self._ids,

    'doc_model': report.model,

    'docs': self,

    }

return report_obj.render('account.report_invoice', docargs)


And the call from Qweb:

<span t-esc="convert_2float((o.amount_total - o.residual),2,o.currency_id)"/>

Avatar
Discard