This question has been flagged

Hi all,

I'm creating a module in order to print a invoice based on a template chosen dynamically and configured in the partner's information. Adding a field on partner model to define a template to use is achieved and works well. 

Now, I'm trying to connect both. The concept is to add a new template inheriting account.report_invoice_document with a low priority and, within this template, to call/embed a dynamically defined template by replacing all its <div class="page">.

This template could be something like the following (these examples do not work and cause an Internal Server Error when trying to print the invoice)

<odoo>
    <template id="custom_invoice_dynamic" inherit_id="account.report_invoice_document" priority="999">
        <xpath expr="//div[@class='page']" position="replace">
            <div class="page">
                <t t-call="get_template()"></t>
            </div>
        </xpath>
    </template>
</odoo>

or

<odoo>
    <template id="custom_invoice_dynamic" inherit_id="account.report_invoice_document" priority="999">
        <xpath expr="//div[@class='page']" position="replace">
            <div class="page">
                <t t-raw="get_template()"></t>
            </div>
        </xpath>
    </template>
</odoo>

With <t t-call="get_template()">, it fails, QWeb engine trying to find a template named "get_template()".

It seems I may have more luck with <t t-raw="get_template()"> as expressions enclosed into t-raw are interpreted.

I tried to define get_template in an AbstractModel, but this fails:

QWebException: 'NoneType' object is not callable Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/odoo/addons/base/ir/ir_qweb/qweb.py", line 315, in _compiled_fn
    return compiled(self, append, values, options, log)
  File "<template>", line 1, in template_account_report_invoice_document_1365
  File "<template>", line 3, in body_call_content_1364 TypeError: 'NoneType' object is not callable
Error to render compiling AST TypeError: 'NoneType' object is not callable Template: account.report_invoice_document Path: /templates/t/t/div/t Node: <t t-raw="get_template()"/>

The thing is I'm not very at ease with Odoo programming. I tried to understand the "Custom Reports" section in https://www.odoo.com/documentation/10.0/reference/reports.html but this documentation is not really clear to me. I miss something, for sure.

File : custom_invoice_report.py (imported through __init__.py)

from odoo import models, fields, api

class CustomInvoiceReport(models.AbstractModel):
    _name = "report.custom_invoice.custom_invoice_dynamic"
    _template = "custom_invoice.custom_invoice_dynamic"
    def get_template(self):
        # TEMPORARY RETURNS CONSTANT FOR DEVELOPMENT PURPOSE
        return 'custom_invoice.test'
    @api.multi
    def render_html(self, docids, data=None):
        report_obj = self.env['report']
        report = report_obj._get_report_from_name(self._template)
        docargs = {
            'get_template': self.get_template,
            'doc_ids': self._ids,
            'doc_model': report.model,
            'docs': self,
        }
        return report_obj.render(self._template, docargs)

Could someone help me, please? 

Thanks.

Avatar
Discard
Best Answer

There is an issue about that in v10 , check the following link: 

\https://github.com/odoo/odoo/issues/16641

Avatar
Discard
Author

Woh... Thanks for pointing this ! Do you know about a turn-around?