Create a Custom QWeb Report in Odoo
Do you need to generate custom reports for your business, but find the standard Odoo reporting functions are not enough? Don’t worry, with a little bit of Python programming you can create custom QWeb reports that will meet your exact needs. In this tutorial, we will show you how to create a report with required elements.
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<!-- Invoice templates -->
<template id="custom_invoice_custom_report">
<t t-call="web.html_container">
<t t-foreach="docs" t-as="doc">
<t t-call="my_module.custom_invoice_external_layout">
<div class="page" style="font-family: sans-serif; font-size: 15px;">
<div t-if="doc.name">
Invoice No.: <span t-field="doc.name"/>
</div>
<div t-if="doc.partner_id.name">
Partner: <span t-field="doc.partner_id.name"/>
</div>
<!-- Invoice lines -->
<t t-foreach="doc.invoice_line_ids" t-as="line">
</t>
</div>
</t>
</t>
</t>
</template>
<!-- Layout -->
<template id="custom_invoice_external_layout_standard">
<!-- Header -->
<div t-attf-class="header o_company_#{company.id}_layout">
<div class="row" style="align-items: center;">
<img t-if="company.report_header_custom" t-att-src="image_data_uri(company.report_header_custom)" alt="Logo" style="max-height: 269px;"/>
</div>
</div>
<!-- Footer -->
<div class="footer">
<div class="text-center">
Page: <span class="page"/> of <span class="topage"/>
</div>
</div>
<div t-attf-class="article o_company_#{company.id}_layout"
t-att-data-oe-model="o and o._name" t-att-data-oe-id="o and o.id"
t-att-data-oe-lang="o and o.env.context.get('lang')">
<t t-call="web.address_layout"/>
<t t-raw="0"/>
</div>
</template>
<!-- Public api: layout to t-call from reports -->
<template id="custom_invoice_external_layout">
<t t-if="not o" t-set="o" t-value="doc"/>
<t t-if="not company">
<!-- Multicompany -->
<t t-if="company_id">
<t t-set="company" t-value="company_id"/>
</t>
<t t-elif="o and 'company_id' in o">
<t t-set="company" t-value="o.company_id.sudo()"/>
</t>
<t t-else="else">
<t t-set="company" t-value="res_company"/>
</t>
</t>
<t t-call="my_module.custom_invoice_external_layout_standard">
<t t-raw="0"/>
</t>
</template>
<!-- Custom Invoice Report Action -->
<report
id="custom_invoice_report_action"
string="Custom Invoice Report"
print_report_name="(object.number or 'Custom Invoice Report')"
model="account.invoice"
report_type="qweb-pdf"
name="my_module.custom_invoice_custom_report"
file="my_module.custom_invoice_custom_report"
/>
<record id="paperformat_invoice_report" model="report.paperformat">
<field name="name">Custom Invoice Report Paperformat</field>
<field name="default" eval="True"/>
<field name="format">A4</field>
<field name="orientation">Portrait</field>
<field name="margin_top">35</field>
<field name="margin_bottom">15</field>
<field name="margin_left">5</field>
<field name="margin_right">5</field>
<field name="header_line" eval="False"/>
<field name="header_spacing">30</field>
<field name="dpi">90</field>
</record>
<record id="my_module.custom_invoice_report_action" model="ir.actions.report">
<field name="paperformat_id" ref="my_module.paperformat_invoice_report"/>
</record>
</odoo>