Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Dynamic field HTML in Qweb report

By
Juan Lopez
on 1/2/15, 5:26 PM 8,045 views

Hi,

I have created a new field in the model hr.contract.type, this new field called format is HTML type, the field format is filled with the contract format that must be printed in the hr.contract model form view. The idea is that the field format have inside keywords or variables that must be replaced in the report generation, like the Email templates, that the variables are replaced when the email is sent.

The QWeb view is this:

<?xml version="1.0"?> <t t-name="hr.contrato_laboral"> <t t-call="report.html_container"> <t t-foreach="docs" t-as="o"> <t t-call="report.external_layout"> <div class="page"> <span t-raw="o.type_id.format"/> </div> </t> </t> </t> </t>

I have read that i can create a custom report like this:

from openerp import api, models

class ParticularReport(models.AbstractModel):

_name = 'report.<<module.reportname>>'

@api.multi

def render_html(self, data=None):

report_obj = self.env['report']

report = report_obj._get_report_from_name('<<module.reportname>>')

docargs = { 'doc_ids': self._ids, 'doc_model': report.model, 'docs': self, }

return report_obj.render('<<module.reportname>>', docargs)

But where and how can i make the replacement of the keywords or variables?

Best regards,

Juan David López

Their is not right solution, its very panic to do so. for this odoo is not right ERP.

Zahin
on 7/26/15, 8:51 AM
0
PuentesDSoftware
On 4/3/15, 10:05 PM

Juan, I had the same error, this is because with some thing related with "context" and immutable property.

Try with this code (adapt it to your needs):



@api.v7
def render_html(self, cr, uid, ids, data=None, context=None):
      report_obj = self.pool['report']
      report = report_obj._get_report_from_name(cr, uid, self._template)
      docs = self.pool[report.model].browse(cr, uid, ids, context=context)
      docargs = {
          'doc_ids': ids,
          'doc_model': report.model,
          'docs': docs,
      }
      return report_obj.render(cr, uid, [], report.report_name, docargs, context=context)

0
yom
On 3/23/15, 9:59 PM

Hola Juan, te funciono la plantilla?

 puedo mostrar la plantilla pero no consigo que rellene los datos de los tags 

0

zbik

--zbik--
5941
| 6 4 7
Lodz, Poland
--zbik--


zbik
On 1/3/15, 7:25 AM

You create parser like this (see method currency_text):

<span t-if="o.currency_id" t-esc="currency_text(o.amount_total, o.currency_id.name, o.partner_id.lang or 'pl_PL')"/>

class report_invoice(models.AbstractModel):
    _name = 'report.account.report_invoice'
    _template = 'account__pl.report_invoice_pl'

    def currency_text(self, sum, currency, language):
        return currency_to_text(sum, currency, language)

    @api.multi
    def render_html(self, data=None):
                                                                     
        report_obj = self.env['report']
        report = report_obj._get_report_from_name(self._template)        
       
        docargs = {
            'currency_text': self.currency_text,        
            'doc_ids': self._ids,
            'doc_model': report.model,
            'docs': self,
        }

        return report_obj.render(self._template, docargs)

 

UPDATE:

In my test system, your code works for me without any problem. Code:

        <report
            id = "contrato_laboral"
            string = "Contrato laboral"
            model = "hr.contract"
            report_type = "qweb-pdf"
            file = "hr.contrato_laboral"
            name = "hr.contrato_laboral"/>

        <template id="hr.contrato_laboral">
            <t t-call="report.html_container">
                <t t-foreach="docs" t-as="o">
                    <t t-call="report.external_layout">
                        <div class="page">
                            <span t-esc="get_formato()"/>
                        </div>
                    </t>
                </t>
            </t>
        </template>

class hr_contract_type(orm.Model):
    _name = "hr.contract.type"
    _description = "Contract Type"
    _inherit = "hr.contract.type"
    _columns = {
        'format': fields.html('Formato contrato', help="Campo HTML para definir el formato del contrato"),
    }
hr_contract_type()

class report_contrato_laboral(models.AbstractModel):
    _name = 'report.hr.contrato_laboral'
    _template = 'hr.contrato_laboral'
    
    def get_formato(self):
        return 'Hola Mundo!!!'
    
    @api.multi
    def render_html(self, data=None):
        report_obj = self.env['report']
        report = report_obj._get_report_from_name(self._template)

        docargs = {
            'get_formato':self.get_formato,
            'doc_ids': self._ids,
            'doc_model': report.model,
            'docs': self,
        }
        
        return report_obj.render(self._template, docargs)
report_contrato_laboral()

 

 

 

 

 

0
Juan Lopez
On 1/3/15, 10:27 AM

Hi zbik, thanks for your answer, i tried this:

Class to add field and define custom render for report:

from openerp.osv import fields, osv
from openerp import api, models

class hr_contract_type(osv.osv):
    _name = "hr.contract.type"
    _description = "Contract Type"
    _inherit = "hr.contract.type"
    _columns = {
        'format': fields.html('Formato contrato', help="Campo HTML para definir el formato del contrato"),
    }
hr_contract_type()

class report_contrato_laboral(osv.AbstractModel):
    _name = 'report.hr.contrato_laboral'
    _template = 'hr.contrato_laboral'
    _wrapped_report_class = None
    
    def get_formato(self):
        return 'Hola Mundo!!!'
    
    def render_html(self, data=None):
        report_obj = self.env['report']
        report = report_obj._get_report_from_name(self._template)

        docargs = {
            'get_formato':self.get_formato,
            'doc_ids': self._ids,
            'doc_model': report.model,
            'docs': self,
        }
        
        return report_obj.render(self._template, docargs)
report_contrato_laboral()

XML to define report:

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <report
            id = "contrato_laboral"
            string = "Contrato laboral"
            model = "hr.contract"
            report_type = "qweb-pdf"
            file = "hr.contrato_laboral"
            name = "hr.contrato_laboral"/>
    </data>
</openerp>

XML to define template

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<template id="hr.contrato_laboral">
    <t t-call="report.html_container">
        <t t-foreach="docs" t-as="o">
            <t t-call="report.external_layout">
                <div class="page">
                    <span t-esc="get_formato()"/>
                </div>
            </t>
        </t>
    </t>
</template>
</data>
</openerp>

This is the error that is shown when i clic the print option "Contrato laboral": Uncaught SyntaxError: Unexpected end of input

Do you see an error in the code?

Best regards,

Juan David López

 

In my suggestion other inherited model is used: models.AbstractModel

zbik
on 1/3/15, 10:39 AM

Hi Zbik, I have changed this: class report_contrato_laboral(osv.AbstractModel):, for class report_contrato_laboral(models.AbstractModel): The result is the same. Any other suggestion? Best regards. Juan David López

Juan Lopez
on 1/4/15, 12:38 PM

See UPDATE in my answer.

zbik
on 1/4/15, 1:46 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 1/2/15, 5:26 PM
Seen: 8045 times
Last updated: 7/26/15, 8:51 AM