Home > other >  Display sum of numbers in the groups in odoo-12
Display sum of numbers in the groups in odoo-12

Time:09-16

I have created two fields and defined their views in the tree view. I added the attribute sum to the field view to show the sum, but it only works in the tree view, not in the group. How can I also display the sum in the groups?

This is the python part:

quantity_in = fields.Float('Quantity In', compute='get_qty_in')
quantity_out = fields.Float('Quantity Out', compute='get_qty_in')

and this is the xml part of the fields view:

<record id="stock_move_inherit_view" model="ir.ui.view">
  <field name="name">stock.move.inherit.view</field>
  <field name="model">stock.move.line</field>
  <field name="inherit_id" ref="stock.view_move_line_tree"/>
  <field name="arch" type="xml">
    <xpath expr="//field[@name='qty_done']" position="before">
      <field name="quantity_in" sum="Total In Quantity"/>
      <field name="quantity_out" sum="Total Out Quantity"/>
      <field name="quantity_total"/>
    </xpath>
  </field>
</record>

CodePudding user response:

You need to inherit the read_group method of the model/class: For example your field name is quantity_in

class Your_class_name(models.Model):
    # ...        
    @api.model 
    def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True):
        res = super(your_class, self).read_group(domain, fields, groupby, offset=offset, limit=limit, orderby=orderby, lazy=lazy)
        if 'quantity_in' in fields:
            for line in res:
                if '__domain' in line:
                    lines = self.search(line['__domain'])
                    in_quantity = 0.0
                    for record in lines:
                        in_quantity  = record.quantity_in
                    line['quantity_in'] = in_quantity
        return res
  • Related