How To Customize A Report In Odoo
How To Customize Report In Odoo?
Sales management module allows you with effective control to your sales department, you can easily create a new sales order out of the different gained opportunity. Sales management module helps you in generating a new sales order, you can filter the order as per the requirement.
1). Send Quotations
2.) Create Invoices
3.) Make Payments
4.) Print Quotation & Invoices
Sales Module provides default reporting method for both quotations & invoices.
But we can customize these reports according to our needs.
The standard quotation & invoicing report printed by Odoo are like this :
We can customize the reports in different ways. Suppose we want to add some more functionality & additional fields to the orders & invoices; we need those changes to be implemented & shown on the report too.
In this case, we’ll be focusing on adding customer GSTIN number in the header and we’ll also be focusing on adding a discount field below the order lines.
So for customizing reports according to our needs, we need to create our own custom module. The basic procedure for creating this custom module is given below.
First of all we need to create an __init_.py & __manifest__.py file’s for our custom module. These files are necessary for calling the py files from folders & declaring the module information. We can create these files like this.
The given file imports the model’s folder of the module. This file needs to be placed inside our module.
The given file imports the custom_sale.py file. This file needs to be placed inside the model’s folder of the custom module.
The given file is the __manifest__.py file. This file is a declaration of the contents of the module & its naming & is also used for calling the view files. This file needs to be placed inside the custom module.
Now create a custom_sale.py & inherit the sale.order model of the sales module inside the file.
Now we need to add our custom field inside the py file. We can create as many numbers of custom fields as we want here inside the .py file. We’ll create an integer field here for customer GSTIN number. We don’t need to call the discount field as it is already present inside the ‘sale.order’ model.
After that, we need to create a view file of our py file where we need to add all the custom fields added through xpath to the order page.
There are two main points which need to be kept in mind while creating this view file. These involve properly providing the xpath & the inherit id of the view file.
For providing the inherit id, we need to navigate to the sale order page. We need to check the record id of the main sale.order form view. We can do that by going to the debug mode & checking the record id & we can also do this by going to the sales module in the Odoo addons.
#### INFORMATION ####
We can inherit any view/report we need to work on using this basic syntax:
<field name=”inherit_id” ref=”module_name.view id”/>
Here ,’ref’ defines the view we are using/inheriting. The syntax for defining this ‘ref’ is:
ref = “name of the module from which we are inheriting.record id of the view”.
The same thing can be done for inheriting a report too. We need to specify the name of the module from which we are inheriting the report & the template id of the report we need to inherit inside the ref.
The process for giving the xpath is very simple. We need to have a predefined field which will be used for giving this xpath. The syntax follows like this:
<xpath expr=//field[@name=’partner_id’] position=”after/before/replace/inside”>
In this case we are using the Customer(partner_id) field of the sales module for giving the path. We can specify the position of the field in the position tag.
Now there are two scenarios for us, we can inherit the already used template of the sale module, or we can create a new button for our customized report to print.
So that we can use both default & customized report.
Now let us suppose, we want to inherit the default the template of the sale.order report & make some changes to it. In this case, we need to have those fields present inside our .py file or needs to be inherited inside the .py file. In this scenario, I suppose you have already done that.
Now after checking the calculations on the order & invoice lines, we must proceed to reporting part. First of all we need, to check the record/view id of the report template so that we can use that for inheriting the report. After getting the record id, we must proceed to creating an xml file with a suitable name & mention it inside the __manifest__.py file of the custom module like this:
After doing this, let’s move to the next part, which is inheriting the template id. The syntax for inheriting the template id is:
for sales report
<template id=”your unique template id” inherit_id=”sale.report_saleorder_document”>
We can check the inherit id of the report the same way we checked the id of the view file.
Now we need to use xpath which is used for giving path to the fields which we are adding in the template. The use of xpath is also for adding the fields on the specific positions where we need it.
Now let us suppose, we need to add our custom fields after the order/invoice lines & before the total fields on the reports.
So we need to specify the xpath position of the place where we need to add our fields. The syntax for using xpath is:
<xpath expr=//position you want to add your field// position=”after/before/replace/inside”>
In this case, we want to add the field after the order/invoice line table before the field amount_untaxed, so we add our custom field using amount_untaxed in the xpath and giving position=”before”.
Now we need to add the customer GSTIN number field. Let us suppose, we need to add the customer GSTIN number below the customer shipping address, we’ll use the partner shipping id field as an xpath. We have used the <t t-if> condition to make sure that the code searches for the field first & if field is present & has some values, then only it gets printed inside the report.
Now, we need to restart the server & install our custom module. This will give us a detailed representation of our saleorder report/invoice report.
We can simply click on the print
button & our customized report will be generated.