Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
3 Replies
4227 Tampilan

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

Avatar
Buang
Jawaban Terbai

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.

Avatar
Buang
Penulis Jawaban Terbai

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

Avatar
Buang

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

Penulis

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

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.

Penulis

Thanks Akhil.

Post Terkait Replies Tampilan Aktivitas
3
Feb 25
17138
2
Okt 21
4061
0
Nov 16
4047
1
Jul 16
4438
0
Jul 16
5298