Enable Dark Mode!
inheriting-existing-pivot-reports-in-odoo.jpg
By: Hajaj Roshan

How can we Inherit Existing Pivot View Reports in Odoo?

Technical Odoo 14

Odoo has different types of views such as a tree, graph, kanban, pivot, and many more. In addition, the Odoo reports can be obtained in different views such as pivot reports, graph reports, and other forms.

In this blog, we are going to discuss how to inherit the pivot view reports of Odoo.

Inheriting the Views??To demonstrate the aspect of inheriting the views let's take a look at a sales report. The following screenshot depicts the pivot view of the Sales Analysis Report.

inheriting-existing-pivot-reports-in-odoo

In the above screenshot, we have the Sales Teams on top(column) and Date on the left side(row).??Here I am going to reverse the fields i.e. make the Date as column and the Sales Team as a row.

Now in order to inherit the view, we need to inherit the model sale.report which can be done with the code depicted down below.

class PivotInheritReport(models.Model):
   _inherit = 'sale.report'

Then we need to inherit the pivot view which can be done by using the following command.??<record id="view_order_product_pivot_inherit" model="ir.ui.view">

    <field name="name">sale.report.pivot.inherit</field>
    <field name="model">sale.report</field>
    <field name="inherit_id" ref="sale.view_order_product_pivot"/>
    <field name="arch" type="xml">
      <xpath expr="//field[@name='team_id']" position="replace">
           <field name="team_id" type="row"/>
     </xpath>
     <xpath expr="//field[@name='date']" position="replace">
           <field name="date" interval="month" type="col"/>
     </xpath>
    </field>
</record>

The external id of the pivot report is a sale.view_order_product_pivot. Here we are inheriting and replacing the field and giving the type of date field as a column and type of team_id field as a row.

We need to give the dependency module in the manifest. The following screenshot depicts the result of the Analysis report after inheritance.

inheriting-existing-pivot-reports-in-odoo

In addition, if you want to add the newly added field or other fields to the measure dropdown, then set the type of that field as a measure 

For example:

<field name="amount_total" type="measure"/>

This will add this field to the measure dropdown. The above example is of an existing field that is used in the sales pivot view.

Moreover, we can add multiple fields as rows or columns. Let's now look at an example to understand the aspects of configuring multiple fields in rows and columns.?Example: In the XML file define the following code.?

<field name="date" interval="month" type="row"/>
<field name="company_id" type="row"/>

Here, I have added the company field as a row. Now the company field also can be seen in the pivot row.?This field is shown as the subfield[expanded field] of the date field if the following code is added.?

<field name="company_id" type="row"/>
<field name="date" interval="month" type="row"/>

Now, the date field will be shown as the subfield or expanded field of the company. We can add multiple fields like these and will be shown as expanded fields of the field above. Additionally, the same can be done to the column field also:

<field name="team_id" type="col"/>
<field name="partner_id" type="col"/>

inheriting-existing-pivot-reports-in-odoo

Adding a new Field to Report

If you want to add a new field to the report then you have to inherit the model and add the new fields to it. Normally the report is created using the SQL views. Therefore, the field values are obtained from the query.

class PivotInheritReport(models.Model):
    _inherit = 'sale.report'
    new_name = fields.Char('New Name')
    def _query(self, with_clause='', fields={}, groupby='', from_clause=''):
        fields['new_name'] = ", s.new_name as new_name"
        return super(PivotInheritReport, self)._query(with_clause, fields, groupby, from_clause)

As we need to get the values for the field from the query here, I am getting it from the sale order[fields['new_name'] = s.new_name as new_name] since I have added a new field in the sale order[sale.order] model also. If we simply add the field to the sale.report model without calculating or getting the value for that field and then adding it to view, it will give some error. So make sure that you have calculated the value for the newly added field using a query.

Now you can add this field in the pivot view and if you want this field to appear in measure drop down you need to give type=”measure” and if you want to add it to the row or column of the pivot table you need to add the type as row or as col as we discussed above.


If you need any assistance in odoo, we are online, please chat with us.



1
Comments

Bernard Mensah

Followed your steps getting this error in Odoo 16 in _query return super(SaleReport, self)._query(with_clause, fields, groupby, from_clause) TypeError: SaleReport._query() takes 1 positional argument but 5 were given

18/05/2023

-

12:45PM



Leave a comment

 


whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message