Search Here

Creating reports using qweb in Odoo

 In this article you will learn how to create custom qweb report from scratch. During this tutorial I will also bind paperformat with qweb reports. You will also learn qweb operations like how to use conditional statements and nested loops.

create-custom-report-in-odoo-using-qweb

After reading this article you will be able

  1. How to create custom reports using QWEB reporting engine
  2. How to set paperformat in QWEb report
  3. How to bind paperformat with our newly created QWEB report
  4. How to define conditional statements in QWEB report
  5. Getting one2many relational fields data or values in QWEb report
  6. How to use nested loops in QWEb
  7. How to show data in ordered list in QWEB report
  8. Creating table inside table-row in QWEB report

Follow below steps to create qweb report

  1. Create a new xml file 
  2. Notify Odoo from the new report (register that file in __openerp.py__)

<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<!-- Add the report to the XML file responsible for reports -->
<report id="report_unique_xml_id"
model="your.model.name"
string="Report Name"
name="module_name.report_name"
file="module_name.report_unique_xml_id"
report_type="qweb-pdf"
/>
<!-- setting paper format for report -->
<record id="paper_format_unique_name_related_to_report" model="report.paperformat">
<field name="name">Report Name</field>
<field name="default" eval="True" />
<field name="format">A4</field>
<field name="page_height">0</field>
<field name="page_width">0</field>
<field name="orientation">Portrait</field>
<field name="margin_top">3</field>
<field name="margin_bottom">2</field>
<field name="margin_left">2</field>
<field name="margin_right">2</field>
<field name="header_line" eval="False" />
<field name="header_spacing">3</field>
<field name="dpi">80</field>
</record>
<!-- binding paper format with qweb report-->
<record id="module_name.report_unique_xml_id" model="ir.actions.report.xml">
<field name="paperformat_id" ref="module_name.paper_format_unique_name_related_to_report" />
</record>
<template id="report_name">
<style>.report_border {
border: 1px solid black;
}
.none_border {
border: none;
border-top: none;
}
.report_border > thead > tr > th,
.report_border > tbody > tr > th,
.report_border > thead > tr > td,
.report_border > tbody > tr > td {
border: 1px solid black !important;
}</style>
<!-- "docs" contains (your.model.name) which you have defined above
now you can access fields from docs for example data.name, data.contact_no etc
-->
<t t-foreach="docs" t-as="data">
<t t-call="report.html_container">
<div class="page">
<div class="oe_structure" />
<div class="text-center">
<h3>
<b>Report Title Goes Here</b>
</h3>
</div>
<div>
<table class="table report_border">
<!-- getting values from model in QWEB-->
<tr>
<th style="width: 25%; font-size:18px;">Name</th>
<td style="font-size:20px;">
<span t-field="data.name" />
</td>
</tr>
<!-- how to define conditional statements in QWEB -->
<tr>
<th style="width: 25%; vertical-align: top; font-size:18px;">Personal</th>
<td>
<table class="table report_border">
<tr>
<t t-if="data.contact_number1 == False">
<td style="font-size:17px;">
<b>Contact No :</b>
<span t-field="data.contact_number2" />
</td>
</t>
<t t-if="data.contact_number2 == False">
<td style="font-size:17px;">
<b>Contact No :</b>
<span t-field="data.contact_number1" />
</td>
</t>
<t t-if="data.contact_number1 and data.contact_number2 != False">
<td style="font-size:17px;">
<b>Contact No:</b>
<li style="list-style-type: none;">
<span t-field="data.contact_number1" />
<br />
</li>
<li style="list-style-type: none;">
<span t-field="data.contact_number2" />
</li>
</td>
</t>
</tr>
</table>
</td>
</tr>
<!-- getting one2many relational fields in QWEb-->
<!-- how to use nested loops in QWEb-->
<tr>
<th style="width: 25%; vertical-align: top; font-size:18px;">Experience</th>
<td style="font-size:17px;">
<table class="table report_border">
<th style="width: 10%;">Duration</th>
<th style="width: 10%;">Job Title</th>
<th style="width: 10%;">Institution</th>
<tbody>
<t t-foreach="data" t-as="experience">
<t t-foreach="experience.one2many_field" t-as="duration">
<tr>
<td>
<p>
<t t-if="duration.curr_work == False">
<span t-field="duration.date_from" t-field-options='{"format": "d-MMM-y"}' />
to
<span t-field="duration.date_to" t-field-options='{"format": "d-MMM-y"}' />
<br />
</t>
<t t-if="duration.curr_work == True">
<span t-field="duration.date_from" t-field-options='{"format": "d-MMM-y"}' />
To Present
</t>
</p>
</td>
</tr>
</t>
</t>
</tbody>
</table>
</td>
</tr>
<!-- how to show data in ordered list in QWEB-->
<tr>
<th style="width: 25%; vertical-align: top; font-size:18px;">Ordered List</th>
<td style="font-size:17px;">
<t t-foreach="data.one2many_field" t-as="honor">
<li style="list-style-type: none;">
<p t-field="honor.name" />
</li>
</t>
</td>
</tr>
<!-- create table inside table row in QWEB-->
<tr>
<th style="width: 25%; vertical-align: top; font-size:18px;">Supervision</th>
<td style="font-size:17px;">
<table class="table report_border">
<th style="width: 10%;">Year</th>
<th style="width: 10%;">Degree</th>
<th style="width: 10%;">Name</th>
<tbody>
<t t-foreach="data" t-as="supervision">
<t t-foreach="supervision.one2many_field" t-as="year">
<tr>
<td>
<p t-esc="year.year" />
</td>
<td>
<p t-esc="year.degree" />
</td>
<td>
<p t-esc="year.name" />
</td>
</tr>
</t>
</t>
</tbody>
</table>
</td>
</tr>
</table>
</div>
</div>
</t>
</t>
</template>
</data>
</openerp>

Post a Comment

2 Comments

  1. hi, thanks for this guide, can i use it in Odoo 14 to learn / check how it works. please inform about changes if any like to etc.
    regards

    ReplyDelete
    Replies
    1. Yes, the idea is same, but the syntax is little bit different

      Delete