Inheriting and modifying QWeb reports

Hi guys,

In this tutorial you will learn how to inherit already existing QWeb reports in a custom module. After this tutorial you will be able to inherit and modify any already existing QWeb report in Odoo!
In this example I will modify the default Quotation / order report through inheritance and I will remove a few elements from the report.

1. Creating a new module

The first step is to create a new module. The correct behaviour in Odoo is to always inherit, don’t ever modify existing files. Start by creating yourself a new module.
Tip: use the scaffold command from Odoo!
Odoo scaffold command
This will create a new module from scratch and the default structure of your module is already there.

2. Creating your XML file

The next step is to open up your XML file (in my example templates.xml) and to create a new record to inherit the report. To inherit a report you have to know in which module it is and what the original XML id of the report is. So, how do you find this?
The easiest way is to go to ‘Settings’ > ‘Reports’ > ‘Reports’ and to search the report you want to modify. In this tutorial this will be the quotation/order report so I’ll search for ‘Order’:
Quotation / Order report
Now that you’ve found your report click on it. This will open a new view where you will find all the technical information you need! For example with the quotation report:
Report details
At the right you will see a clickable link named ‘Search associated QWeb views’. Click on it. This will show you a list of all records that are associated to this specific report. In the example of the quotation report:
QWeb XML views
So this usually shows you two XML records. How do you know which one you need? The one that ends with _document is the correct XML record that you need to inherit. Under the column ‘External ID’ you will see there is an unique name, in this example sale.report_saleorder_document. The first part of this text (sale) is the module where the report is from, the second part (report_saleorder_document) is the name of the report that you want to inherit and modify.

Remember this value and now open up your XML file. To inherit a QWeb report you need two things: an unique template id and the inherit_id. The template id can be chosen by yourself, just make sure its unique. The inherit_id should contain the value you’ve just found on your report (sale.report_saleorder_document).

That is it! You’re now already on the correct report and are inheriting it. So, how do you now add or remove elements? To do this you will need Xpath expressions to find, modify, replace or add elements. Tip: Don’t know how Xpath expressions work? Follow my tutorial here!
For this example I will remove the columns that show the amount, the tax and the price per item. The first step is to modify the table header:

After modifying the table header the table content should also be modified.

This code will remove the fourth, third and second td element and all its content but only for the tbody with class ‘sale_tbody’ and inside the tr.
So this will replace the header and the table content from this report. Have a look at the full code to inherit and modify your QWeb report:

If you would now print out the report you would get this as a result:
Result modified quotation report

3. Adding the dependency for the external QWeb report

The next, and final step, is to add a dependency. Because this QWeb report is inside another module Odoo has to know about this module and its content so you should add a dependency. Without this your module will not work and you will get errors.
Open up your __openerp__.py file in your custom module and find the line with depends.
Now take back that ‘External ID’ from the QWeb report and take the first part of the external id (before the dot). This tells you which module you should add as a dependency:
External ID Qweb report
In this example my QWeb comes from the sale module, so I will add it as a dependency.

4. Conclusion

Thats it! You’re done with inheriting and modifying the QWeb report. When you now install this module in your Odoo you will see the modified report.
Do you want to try a demo module and see the source code of this tutorial? You can view on my Github account.
Has this tutorial helped you, do you have any feedback or questions? Post away!




Odoo Experts

Saving and resizing images in Odoo 8

Hi guys,

In this tutorial I will learn you how to create a new field that saves images, how to upload them correctly in your database and how to automatically resize them. With this demo you’ll learn how to instantly (automatically) resize uploaded images.

Tip:view my sample module here.

1. Create new fields

The first step is to create a new model and the new fields. For this demo I will create a clean, new table, but you could just simply add fields to an existing model.

As you can see I will create 4 fields. One to fill in a name and three fields of the type binary, which will be used to upload the images on. Every image field calls a field.function, which will then automatically resize the images:

So, what exactly does this do? The function get_image will find the correct image and returns it. Then the second function goes off, named _set_image. This function will call a pre-built function from Odoo to automatically resize the image.

2. Showing the new fields

Now that the model and fields are done they should also be showed. In my sample I will create a new menu item named ‘Images’ in the sales module, but you could just leave this out or place it anywhere else.

This will create a new menu item with the name ‘Images’, which is triggered by the action action_upload_images when the user clicks on it. The third record, view_images_form will show the fields and gives us the ability to visualise it as we want. Result:
Images view

3. Printing the images on a report

Now that the images can be saved and resized you should also be able to print them in your report!
Printing an image in a report is really easy:

This code will print the images out in the original format, in the 128x128px size and in 64x64px size. You simply need to call the image widget and you can print them out. When you’d print the report you would get something like this:
Printed image in multiple sizes

Tip: This whole tutorial is written in the Odoo V7 API. This is because Odoo 8 does not have fields.function and would complicate the task. If you’d really want it in Odoo 8 API you should have a look at computed fields.
Tip #2: You could also create an on_change event which then fills up different fields with different sizes of the images. This however would fill up the database much faster and is the reason why this is not in this tutorial.




Install wkhtmltopdf on Ubuntu

Hi guys,

In this tutorial I will learn you how to install Wkhtmltopdf on Ubuntu. You will need Wkhtmltopdf to print out PDF reports in your Odoo or you will get a warning message saying that you don’t have Wkhtmltopdf.
There are two ways to install Wkhtmltopdf. Method #1 is through an executable file and method #2 is by command. If you do not care about technical details and how it works simply read on. Want to know the technical details and learn how to do it yourself? Then start reading from chapter 2.

1. Install Wkhtmltopdf through executable script

Open up your Ubuntu terminal and create a new .sh file

Now add the following code in it:

The next step is to make the file executable

And finally, now it is executable, execute it.

After the script is done you will see something along these lines:
Installed wkhtmltopdf on Ubuntu
After restarting your Odoo/Ubuntu you will be able to use Wkhtmltopdf and print PDF files.

2. Install Wkhtmltopdf manually from the terminal

So you don’t want to install it automatically but learn more about this? Lets start!
The first thing you need to do is download the Wkhtmltopdf files from the website. wget -P will download these files.

This is telling Ubuntu to download the files from the website with sudo (administrator) rights and to place them in the Downloads folder.
Now that you have the files in your Downloads folder you should navigate to them

The next step is to install it by dpkg. Dpkg is the package installer from Ubuntu and will be responsible for installing/removing files.
So lets install the package.

Now that it is installed you still need to move the wkhtmltopdf and wkhtmltoimage files under /usr/bin/ to make Wkhtmltopdf work with Ubuntu and accessible for the user. First navigate to the correct folder

Now copy the files from your Downloads folder to your bin so that they can be used by your Odoo.

When you now reload your Odoo or restart the server you can use Wkhtmltopdf and PDF’s will be generated automatically!




Odoo Experts

Creating custom reports in Odoo 8

Hi guys

In this tutorial you will learn how to create a new QWeb report from scratch.
During this tutorial I will create a second invoice report, but this time without prices. Some companies do not want to show prices per record for example, so I’ll take this as a sample.

1. Creating a new XML file

The first step to create your own new report is to make a new XML file. This file should be placed under yourModuleName/views and you can name it as you wish. In this tutorial I will create a new invoice that is made in the module account.
I’ve named the file report_without_prices.xml as you can see here:
View structure

In this file you will define the template id’s and names, which will later on be used by your Odoo to find the correct report.
The minimal code looks like this:

So, what is important about this? The name of the template id (from the first record) should be the same as the name for the t-raw that handles translations. In the t-raw you need to add the module name before this name with a dot. Here’s an image to make things more clear:
Code structure
The second template_id then needs to be identical to the name that you will set in the next file. So let us continue.

2.Add the report to the XML file responsible for reports

This brings us to part 2. Every module has a file that makes a reference to every single report in the module. This is done by a item, which can be found in the XML file.
This file always has the same name structure. It starts with the name of your module, an underscore and then report.xml. Its found in the first level of the directory of your module, as you can see here:
XML file for reports
The filename is always yourModuleName_report.xml. Now open up this file and you will see a tag for every report that exists in this module.
The next step for us is to add a new tag for our own report. The code looks like this:

Now that you’ve added this code you’re almost there! The last step is to notify Odoo of the new report.

3. Notify Odoo from the new report

So how do we tell Odoo there is a new report? In every module is a file named __openerp__.py. This file is used to import (include) all files in a module.
As all files are loaded in here we should also add our new XML file in this list. Without this Odoo will not find or use the XML file which will result in a not working report and errors.
Add a new line to the __openerp__.py file, right under ‘data’:[

4. Wrapping things up

Now that you’ve made a new report (a new XML file),added the template id to the moduleName_report.xml file and added your XML file to __openerp__.py you’re done!
The last thing you need to do is reload your module (by command or by a module update) and you’ll see the new report showing up.
If you then look under accounting > Customer Invoices and make a new invoice you will see your new print option:
Print option custom report

And, finally, if you print it out you’ll get your custom report!
Custom report lay-out

NOTE:If you want a more advanced code, which shows a full report without the prices just paste this code in reports_without_prices.xml

Looking for more information about reports?
Check out the official documentation




Odoo Experts