How to create Qweb report in odoo

Hello All,

         I would like to share with you how to create a Qweb report in odoo [OpenERP].let we go stap by stap to create Qweb report in odoo [OpenERP].

What is Qweb Report ??

Qweb Report is the newest version of the Odoo reporting Format and its uses the bootstrap css classes and Qweb Template Engine for Make Quick Report Design in Odoo.

About Bootstrap :

Bootstrap is a free collection of tools for creating websites and web applications.It contains HTML and CSS-based design templates for typography, forms, buttons, navigation and other interface components, as well as optional JavaScript extensions

Expressions used in Odoo report templates :

There are some magic variables used in the report rendering. The main ones are the following:
docs
        records for the current report
doc_ids
        list of ids for the docs records
doc_model
       model for the docs records
time
       a reference to time from the Python standard library

translate_doc 
 
a function to translate a part of a report. It must be used as follow:
<t t-foreach="doc_ids" t-as="doc_id">
<t t-raw="translate_doc(doc_id, doc_model, 'partner_id.lang', account.report_invoice_document')"/>
</t>
user
     res.user record for the user printing the report
res_company
      record the current user‘s company

Custom Report

A generic report use the default rendering context, containing the magic variables as explained before. If you want a new rendering context containing anything you want to process your data Odoo AbstractModel, a custom module is needed. These reports are called “particular report”.

For a particular report, you have to write an Odoo Model containing a render_html method. Classically, this method returns a call to the original QWeb render with a custom rendering context.


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': ids,
'doc_model': report.model,
'docs': self.env[report.model].browse(ids),
}
return report_obj.render('<<module.reportname>>', docargs)



Create Report  in Qweb odoo [OpenERP]

Steps to create in Following Ways ::

STEP 1 ::

Create View File For Rendring the Qweb in Odoo
the view file it self uses the the Bootstrap classes for designing the Qweb Report.

Create custom module in odoo and then  Navigate

YOUR MODULE NAME/View/yourview_file.xml

And also add the Entry of xml in the __openerp__.py file

STEP 2 ::

Create Python class for report in report Dir

import time
from openerp.report import report_sxw
from openerp.osv import osv

class your_class_name(report_sxw.rml_parse):

def get_lines(self, user,objects):
lines=[]
for obj in objects:
if user.id==obj.user_id.id:
lines.append(obj)
return lines

What ever function Which You want to access in the Qweb Template which are define in the class

We must have to add the Entry in it constructor like this

def __init__(self, cr, uid, name, context):
super(your_class_name, self).__init__(cr, uid, name, context)
self.net_total=0.0
self.localcontext.update({
'time': time,
'get_lines': self.get_lines,

Add the Entry For the report class which is inherited the osv.AbstractModel object

class report_your_class_name(osv.AbstractModel):
_name = 'report.Your_Module_name.report_reportname'
_inherit = 'report.abstract_report'
_template = 'Your_Module_name.report_reportname'
_wrapped_report_class =your_class_name

Navigate :

YOUR MODULE NAME/report/reportclass.py

And also add the Entry in the __init__.py file


STEP 3 ::

Add the Menu Action for Print The Report
YOUR MODULE NAME/report_file.xml

<report
id="action_report_report_id"
string="String to Display in Menu"
model="your_model"
report_type="qweb-pdf"
name="your_module_name.report_your_reportid"
file="your_module_name.report_yourreportid"
/>
And also add the Entry of xml in the __openerp__.py file


Special function ::

Hear In odoo use the special widget for print the currency symbol,
add the company address,partner address and many more

like this..

<div class="row">
<div class="col-xs-5 col-xs-offset-7">
<address t-field="o.partner_id"
t-field-options='{"widget": "contact", "fields": ["address", "name"], "no_marker": true}' />
<span t-field="o.partner_id.vat"/>
</div>
</div>
hear t-field-options attribute use the widget to print the partner detail in Odoo
like name,address,street,country etc... 

Same things for the currency symbol

<td class="text-right">
<span t-field="o.amount_untaxed" t-field-options='{"widget": "monetary", "display_currency": "o.currency_id"}'/>
</td>


Thanks,
Mayur Maheshwari (OpenERP/odoo)
Skype: mayur_maheshwari1