This question has been flagged
7 Replies
33057 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

Avatar
Discard

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

Hi Juan,

Did you solve it?

Best Answer

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)

Avatar
Discard
Best Answer

Hola Juan, te funciono la plantilla?

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

Avatar
Discard
Best Answer

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()

 

 

 

 

 

Avatar
Discard
Author Best Answer

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

 

Avatar
Discard

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

Author

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

See UPDATE in my answer.