1

In the employee profile, I added a Many2one field which relates to the employee table itself (known as Reporting of the current employee) and another field is a Many2one with a foreign key of Reporting manager field.

And I have another field Many2many related to the employee table itself, called All reporters. I need to populate all the reporters to the Many2many field including my sublevel reporters.

Here is the structure:

       A      B       C        D
       |      |       |        |
      / | \ / | \   / | \      / \
     E  F G H I  J  K L M     N   O
    /|\  /|\          |       /\
   P Q R S T U        V       W X

This is what I want,

  • The many2many of A, need to populate with: E,F,G,P,Q,R,S,T,U
  • The many2many of B, need to populate with: H,I,J
  • The many2many of C, need to populate with: K,L,M,V
  • The many2many of E, need to populate with: P,Q,R
  • The many2many of N, need to populate with: W,X

Here is my code:

reportee_ids = fields.One2many('hr.employee','reporting_officer_id',string="Reportees")
employee_child_ids = fields.Many2many('hr.employee')

def compute_reportees(self,emp,list1=None):
    rm_id = emp.reporting_officer_id
    if list1 is None:
        list1 = []
    if emp.id != rm_id.id:
        list1 += emp.reportee_ids.ids + rm_id.reportee_ids.ids
        rm_id.user_id.write({'employee_child_ids':list1})
        self.compute_reportees(rm_id,list1)
    else:
        return list1

How can I do this?

travisw
  • 2,052
  • 1
  • 14
  • 27
KbiR
  • 4,047
  • 6
  • 37
  • 103

1 Answers1

0

The only way I know of to do this type of top-down search is using the child_of search domain operator.

In your example scenario, you could do the following search to find all children of a given employee record (emp).

self.env["hr.employee"].search([("reporting_officer_id", "child_of", emp.id)])

The result does include the emp.id itself, so you may want to append ("id", "!=", emp.id) to your search domain if you want to exclude them from the results.

travisw
  • 2,052
  • 1
  • 14
  • 27