0

I have a requirement that want to fulfill is bringing a value from the database and display in the UI, we have a app which we can assign "projects" to many people so i have to display in the persons information a field with the project currently assigned to him/her.

I was thinking about creating a function to retrieve that data using the "search()" method to fetch the field needed.

#for the moment i have added the variable in the class
project_name = fields.Char('Project Name', compute='_compute_get_employee_project')

#this is the function i am building (trying)
@api.model
    def _compute_get_employee_project(self):
        #verify the employee belongs to a project
        employee_task = self.env['project.task'].search([('user_id','=',self.user_id.id),('status','=','assigned')],limit=1)
        employee_project = self.env['project.project'].search([('id', '=', employee_task.project_id.id)], limit=1)

My end gol is to been able to fetch in which project the employee is currently assigned.

Thank you for your time!!

Ron
  • 91
  • 8

2 Answers2

2

The compute method must assign the computed value to the field.

You can use a many2many field that depends on the related user to compute projects assigned to the selected user. Odoo will show one project if there is only one project assigned to the selected user.

Example:

class ResEmployee(models.Model):
    _inherit = 'hr.employee'

    project_ids = fields.Many2many('project.project', compute='_get_assigned_projects', string='Projects')

    @api.depends('user_id')
    def _get_assigned_projects(self):
        project_task = self.env['project.task']
        for employee in self:
            if employee.user_id:
                employee.project_ids = [(4, project_id, 0) for project_id in
                                        project_task.search(
                                            [('status', '=', 'assigned'),
                                             ('user_id', '=', employee.user_id.id)]
                                        ).mapped('project_id.id')]
            else:
                employee.project_ids = False

Use the following code to show the assigned project tag before the work mobile field:

<record id="view_employee_form" model="ir.ui.view">
    <field name="name">hr.employee.form</field>
    <field name="model">hr.employee</field>
    <field name="inherit_id" ref="hr.view_employee_form"/>
    <field name="arch" type="xml">
        <field name="mobile_phone" position="before">
            <field name="project_ids" widget="many2many_tags"/>
        </field>
    </field>
</record>  
Kenly
  • 24,317
  • 7
  • 44
  • 60
1

you could do the following:

#for the moment i have added the variable in the class
project_name = fields.Char('Project Name', compute='_compute_get_employee_project')

#this is the function i am building (trying)
@api.multi
@api.depends('user_id')
def _compute_get_employee_project(self):
    for emp in self:
        #verify the employee belongs to a project
        employee_task = self.env['project.task'].search([('user_id','=',self.user_id.id),('status','=','assigned')],limit=1)
        employee_project = self.env['project.project'].search([('id', '=', employee_task.project_id.id)], limit=1)
        emp.project_name = employee_project.display_name

or you could go another route

project_ids = fields.One2many('project.project', compute='_compute_get_employee_project')

@api.multi
@api.depends('user_id')
def _compute_get_employee_project(self):
    for emp in self:
        emp_task_ids = self.env['project.task'].search([('user_id','=', emp.user_id.id),('status','=','assigned')])
        emp_project_ids = emp_task_ids.mapped('project_id')
        emp.write({
            'project_ids': [(6, 0, emp_project_ids.ids)]
        })

please note the difference between the previous field definition Char & the new one One2many. the difference between the first case where limit=1 added & the second one where no limit. this because the user could be assigned to multiple tasks in different projects.

don't hesitate to let us know if everything works fine with you.

kerbrose
  • 1,095
  • 2
  • 11
  • 22
  • Thank you your answer was perfect to solve my issue. where could I improve my skills do you know any good courses for Odoo development? – Ron Mar 01 '21 at 05:34
  • https://www.amazon.com/Odoo-Development-Cookbook-customize-efficient-ebook/dp/B08KWL9QK3/ – kerbrose Mar 01 '21 at 06:22