A report is a document that can present information about a company or organization in an organized format for a specific audience and purpose. Reports play a significant role in the growth of the business. Odoo uses two types of report types are qweb-pdf and qweb-html. The easiest and useful report is qweb-pdf. So odoo uses qweb in more cases to access data from models and get a perfect view for the report.
Here we use the qweb-pdf report type for this operation. So I created a button and created a function with the button name.
In python file( module_name > models > .py )
def button_name(self):
data = {
'model_id': self.id,
}
return self.env.ref(module_name.print_report_pdf').report_action(self, data=data)
Here I am using a code to trigger an XML part so I specified the model name and id of the XML part.
After this code creates a directory named as report inside the module and creates an xml file.
Then write this snippet on the XML file. The id of this report tag must be equal to the reference that we specified earlier in the python part, not the name of the XML file.
In xml part (module_name > report > .xml)
<odoo>
<report id="print_report_pdf'"
string="Sample Report"
model="model_name"
report_type="qweb-pdf"
name="module_name.print_sample_report"
file="module_name.print_sample_report"
menu="False"/>
</odoo>
After this xml part creates a python file inside the report directory.(eg:- module_name > report > .py)
You must be bothered about specifying the name of the model. In the above XML part, one can see an attribute name. You should use the string to create a model name by adding a report before this string.
In python file
class SampleReportPrint(models.AbstractModel):
_name = 'report.module_name.print_sample_report'
@api.model
def _get_report_values(self, docids, data):
"""in this function can access the data returned from the button
click function"""
model_id = data['model_id']
value = []
query = """SELECT *
FROM sale_order as so
JOIN sale_order_line AS sl ON so.id = sl.sale_order_id
WHERE sl.id = %s"""
value.append(model_id)
self._cr.execute(query, value)
record = self._cr.dictfetchall()
return {
'docs': record,
'date_today': fields.Datetime.now(),
}
After writing the python code I have to create a template for the print report.
To create an XML file for the report template. This template helps to get data from the report model and also can align content like HTML.
In report template part ( module_name > report > .XML )
<?xml version="1.0" encoding="UTF-8"?>
<odoo>
<data>
<template id="print_sample_report">
<t t-call="web.html_container">
<t t-call="web.external_layout">
<div class="page">
<h1 align="center">
Sample Report
</h1>
<t t-forech="docs" t-as="line">
<h3>
<t t-esc="line['name']"/>
</h3>
<h3>
<t t-esc="date_today"/>
</h3>
</t>
</div>
</t>
</t>
</template>
</data>
</odoo>
In this template, I used a loop for getting values from the variable docs.
You must check all the files paths added to the manifest file and update odoo then install your module.