# Create report

A frequent requirement is to generate a printable document for a record. For that we use `trytond.report.Report` which provides the tooling to render OpenDocument based on relatorio template.

First we create a `trytond.report.Report` class in `opportunity.py`:

    from trytond.report import Report
    ...
    class OpportunityReport(Report):
        __name__ = 'training.opportunity.report'

And we register it in the `Pool` as type `report` in `__init__.py`:

    def register():
        ...
        Pool.register(
            opportunity.OpportunityReport,
            module='opportunity', type_='report')

Now we have to create a `ir.action.report` and `ir.action.keyword` in `opportunity.xml`:

    <tryton>
       <data>
          ...
          <record model="ir.action.report" id="report_opportunity">
             <field name="name">Opportunity</field>
             <field name="report_name">training.opportunity.report</field>
             <field name="model">training.opportunity</field>
             <field name="report">opportunity/opportunity.fodt</field>
             <field name="template_extension">odt</field>
          </record>
          <record model="ir.action.keyword" id="report_opportunity_keyword">
             <field name="keyword">form_print</field>
             <field name="model">training.opportunity,-1</field>
             <field name="action" ref="report_opportunity"/>
          </record>
       </data>
    </tryton>

The `ir.action.report` links the `trytond.report.Report` with the `Model`.

`name`

  The string that is shown on the menu.

`report_name`

  The name of the `trytond.report.Report`.

`model`

  The name of the `Model`.

`report`

  The path to the template file starting with the module directory.

`template_extension`

  The template format.

And like for the wizard, the `ir.action.keyword` makes the `trytond.report.Report` available as action to any `training.opportunity`.

Finally we create the OpenDocument template as `opportunity.fodt` using LibreOffice. We use the Genshi XML Template Language implemented by relatorio using `Placeholder Text`. The rendering context contains the variable `records` which is a list of selected record instances.

Here is an example of the directives to insert in the document:

    <for each="opportunity in records">
    Opportunity: <opportunity.rec_name>
    Party: <opportunity.party.rec_name>
    Start Date: <format_date(opportunity.start_date) if opportunity.start_date else ''>
    End Date: <format_date(opportunity.end_date) if opportunity.end_date else ''>

    Comment:
    <for each="line in (opportunity.comment or '').splitlines()">
    <line>
    </for>
    </for>

Note

We must render text field line by line because OpenDocument does not consider simple breakline.

## Update database

As we have registered new report and XML records, we need to update the database with:

    $ trytond-admin -d test --all

And restart the server and reconnect with the client to test rendering the report:

    $ trytond

Next we create a a reporting model using SQL query.