Developer

Generating Jasper Reports from several models

1 min read

Quick summary

The article explains how to build Jasper reports that start from one model while displaying related data from another. The solution is to use a subreport and configure the `OPENERP_PATH_PREFIX` property with the path from the main model to the subreport model; in the example, the prefix is `order_line/product_id`.

Contents

When you need to create a new report using the jasper_reports module, you must use as base model the one in the screen where you want the button to be displayed.

For example, if you want a report that displays all product information, including its picture, in a brochure, the model will be product.product and the report button will be displayed on that model.

What if you want a button in a quotation to print the brochures of all products in the current quotation? Until now, there were three possibilities: create a new report with sale.order as its base model, create a wizard, or create a relate button that opens a new tab with all products in the quotation.

We implemented a new way to solve this problem using Jasper Reports. Create a new report with sale.order as its base model, insert a subreport embedding the brochure report, and add the OPENERP_PATH_PREFIX property to specify the path from the current model (sale.order) to the subreport model (product.product). In this example, the prefix path is order_line/product_id.

The following screenshot shows where to add the property using iReport:

Screenshot

Frequently asked questions

Which model should be used as the base for a new `jasper_reports` report?

Use as the base model the one belonging to the screen where the report button should appear. For example, if the button must appear on a product, the base model is product.product.

How can the brochures for all products in a quotation be printed?

Create the report with sale.order as its base model and insert a subreport containing the product-brochure report.

What is `OPENERP_PATH_PREFIX` used for?

It specifies the path from the current model to the subreport model so that Jasper can access the related data. In the quotation example, the path is order_line/product_id.

What alternatives were available before this approach?

The alternatives were to create a new report based on sale.order, create a wizard, or add a relation button that opened a new tab with all the products in the quotation.