Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Unable to Inherit function field in v8

By
Sairam Bandaru
on 10/19/15, 3:59 AM 470 views

Hi

I want inherit these field to modify the result in functional field but inherited function not calling


class hr_employee_inherit(osv.osv):
_inherit = 'hr.employee'

def _get_leave_status(self, cr, uid, ids, name, args, context=None):
holidays_obj = self.pool.get('hr.holidays')
holidays_id = holidays_obj.search(cr, uid,
[('employee_id', 'in', ids), ('leave_from','<=',fields.date.context_today),
('leave_to','>=',time.strftime('%Y-%m-%d')),('type','=','remove'),('state','not in',('cancel','refuse'))],
context=context)
print holidays_id,ids,'holidays_id****In INHERIT***********-------------------------',time.strftime('%Y-%m-%d'),fields.date.context_today
result = {}
for id in ids:
result[id] = {
'current_leave_state': False,
'current_leave_id': False,
'leave_date_from':False,
'leave_date_to':False,
}
for holiday in self.pool.get('hr.holidays').browse(cr, uid, holidays_id, context=context):
result[holiday.employee_id.id]['leave_date_from'] = holiday.date_from
result[holiday.employee_id.id]['leave_date_to'] = holiday.date_to
result[holiday.employee_id.id]['current_leave_state'] = holiday.state
result[holiday.employee_id.id]['current_leave_id'] = holiday.holiday_status_id.id
print result,'Reslu leave status---------------******-----------'
return result

_columns = {
'current_leave_id': fields.function(_get_leave_status, multi="leave_status", string="Current Leave Type",type='many2one', relation='hr.holidays.status'),
'leave_date_from': fields.function(_get_leave_status, multi='leave_status', type='date', string='From Date'),
'leave_date_to': fields.function(_get_leave_status, multi='leave_status', type='date', string='To Date'),
}


help me out


thanks

2

Akhil P Sivan

--Akhil P Sivan--
3423
| 5 3 6
Kochi, India
--Akhil P Sivan--

Interested in exploring Odoo both technically & functionality wise.

email-id : akhilpsivan01@gmail.com

Akhil P Sivan
On 10/19/15, 7:18 AM

Hi,

If you want to inherit the functional fields and make some changes, you may redefine the fields and function in new api and make the changes you need. For eg, you may try like this:

class hr_employee(models.Model):
_inherit="hr.employee"

current_leave_id = fields.Many2one('hr.holidays.status', compute='_get_leave_status', string="Current Leave Type")
leave_date_from = fields.Date(compute='_get_leave_status', string='From Date')
leave_date_to = fields.Date(compute='_get_leave_status', string='To Date')


@api.one
@api.depends('employee_id')
def _get_leave_status(self):
holidays_obj = self.env['hr.holidays']
holidays = holidays_obj.search([('employee_id', 'in', self.ids), ('date_from','<=',time.strftime('%Y-%m-%d %H:%M:%S')),
('date_to','>=',time.strftime('%Y-%m-%d 23:59:59')),('type','=','remove'),('state','not in',('cancel','refuse'))])

for holiday in holidays:
self.leave_date_from = holiday.date_from
self.leave_date_to = holiday.date_to
self.current_leave_state = holiday.state
self.current_leave_id = holiday.holiday_status_id.id

Please check it n let me know.

0
Sairam Bandaru
On 10/19/15, 7:57 AM

Hi, that's fine now able to inherit but in the function I have changed main code because 'self' contained all employees ,So I looped on self instead of holidays as per this code it raising error, So I modified like this working fine  

@api.multi

def _get_leave_status(self):

holidays_obj = self.env['hr.holidays']

print self.ids,'holidays_id****In INHERIT***********-------------------------',fields.date.context_today(self,self.env.cr,self.env.uid)

print self.env.context,'Context ***********8-------------------------',self,self.env.cr,self.env.uid

holidays_id = holidays_obj.search(

[('employee_id', 'in', self.ids), ('leave_from','<=',fields.date.context_today(self,self.env.cr,self.env.uid)),

('leave_to','>=',fields.date.context_today(self,self.env.cr,self.env.uid)),('type','=','remove'),('state','not in',('cancel','refuse'))],

)

print holidays_id,'holidays_id****In INHERIT***********-------------------------',fields.date.context_today(self,self.env.cr,self.env.uid)

#result = {}

'''

for id in ids:

result[id] = {

'current_leave_state': False,

'current_leave_id': False,

'leave_date_from':False,

'leave_date_to':False,

}

'''

for holiday in holidays_id:

print holiday,'****************************',holiday.holiday_status_id.id

for rec in self:

print rec.ids,'Rec*88888888888888888888'

holidays_id = holidays_obj.search(

[('employee_id', 'in', rec.ids), ('leave_from','<=',fields.date.context_today(self,self.env.cr,self.env.uid)),

('leave_to','>=',fields.date.context_today(self,self.env.cr,self.env.uid)),('type','=','remove'),('state','not in',('cancel','refuse'))],

)

rec.leave_date_from = holidays_id and holidays_id[0].date_from or None

rec.leave_date_to = holidays_id and holidays_id[0].date_to or None

rec.current_leave_state = holidays_id and holidays_id[0].state or None

rec.current_leave_id = holidays_id and holidays_id[0].holiday_status_id.id or None


Thanks

Hi did you manage to solve it? Is it working now?

Akhil P Sivan
on 10/19/15, 8:24 AM

Yes working fine.Resolved I inherited fields along with function as computed fields Thanks.

Sairam Bandaru
on 10/19/15, 8:37 AM

Instead of looping on self, I used "@api.one" decorator. Both should have same effect. Anyway I didn't get time to check by running the above. Hope, it helped you in solving the problem.

Akhil P Sivan
on 10/19/15, 8:47 AM

Thanks Akhil.

Sairam Bandaru
on 10/20/15, 1:35 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 10/19/15, 3:59 AM
Seen: 470 times
Last updated: 10/19/15, 8:27 AM