Boubaker
Boubaker

Reputation: 429

update models from other model odoo

I want to update a model by another model

for example when I select an employee I want to update his coordinates after the states done

    class Hr_transf_employee(models.Model):
        _name = 'hr.employee.transfer'
        _rec_name = 'employee_id'



        date_cration = fields.Date(string='Date order', required=True, default=datetime.today())
        date_transfer = fields.Date(string='Date transfer')
        employee_id = fields.Many2one('hr.employee',string='Employee', required=True) 

        job_id_new = fields.Many2one('hr.job',string='Job title', required=True) 
        country_work_id_new = fields.Many2one('res.country', 'Country work new') 

        state = fields.Selection([
            ('draft', 'Draft'),
            ('accept', 'Accept'),
            ('done', 'Done'),
            ('cancel', 'Cancel'),
        ], string='Order Status', readonly=True, copy=False, store=True, default='draft')

I want to update job_id by job_id_new and country by country_work_id_new in the model hr.employee

Upvotes: 0

Views: 818

Answers (2)

khelili miliana
khelili miliana

Reputation: 3822

Edit your done fuction wkf to be like :

@api.multi
def done(self):
    self.state = 'done'
    if self.job_id_new :
       self.job_id = self.job_id_new.id
    if self.country_work_id_new :
       self.country = self.country_work_id_new.id

To get today :

if  self.date_transfer <= self.create_date : 
    self.date_transfer = fields.datetime.now()

Upvotes: 1

Bouh
Bouh

Reputation: 11

First Option: you can override the 'write' method on hr.employee.transfert and update the employee_id fields

@api.multi
def write(self, vals):
    res = super(Hr_transf_employee, self).write(vals)

    if vals.get('state', False) == 'done':  # Only update employee if the transferts become 'done'.
        for transfert in self:
            transfert.employee_id.job_id = transfert.job_id_new
            transfert.employee_id.country = transfert.country_work_id_new
    return res

Second Option:
you can add two computed fields on the res.partner model (you can use the same method for the both fields).

@api.multi
def my_compute_method(self):
    for employee in self:
        employee_transfert_id = self.env['hr.employee.transfer'].search([
            ('employee_id', '=', employee.id),
            ('state', '=', 'done'),
        ], order="write_date DESC", limit=1)  # Get the last done transfert

        employee.job_id = employee_transfert_id and employee_transfert_id.job_id_new or False
        employee.country = employee_transfert_id and employee_transfert_id.country_work_id_new or False

Upvotes: 1

Related Questions