Openerp - 函数不更新字段

时间:2016-11-21 11:26:59

标签: python python-2.7 openerp openerp-7

我已经编写了这个函数来更新单独的表并且它没有工作。使用调试模式,它返回值。

我需要更新单独的表格( leave_score_card 表格),每个员工累计离开的天数,如下图所示的离开类型,数据将从原始数据中获取请假管理模块中的 hr_holidays

enter image description here

请帮我做正确的事。它只显示最后一条记录,到目前为止我所尝试的内容在下面提到

 def populate_values(self, cr, uid, ids, context={}):

        result = {'value': {}}
        emps=self.pool.get('hr.employee').search(cr, uid, [('current_status','=','active')], context=context)
        if emps:

            for r in emps:

                print r
                result['value']['employee_id'] =r
                holiday_obj=self.pool.get('hr.holidays')
                holiday_emps=holiday_obj.search(cr, uid, [('employee_id','=',r),('type','=','remove')], context=context)
                print holiday_obj
                global medi
                global annu               
                global othr


                print holiday_emps
                if holiday_emps:
                    casu = 0
                    annu=0
                    medi=0
                    other=0 

                    for n in holiday_obj.browse(cr, uid, holiday_emps):


                       holiday_status = n.holiday_status_id.id
                       holiday_days=n.number_of_days_temp
                       print n
                       print n.holiday_status_id.id
                       print "Number of days"
                       print n.number_of_days_temp

                       if holiday_status:
                           if holiday_status==2:  
                                casu=casu+holiday_days

                           if holiday_status==4:
                                medi=medi+holiday_days

                           if holiday_status==10:
                                annu=annu+holiday_days

                           else:
                                other=other+holiday_days

                       else:
                           print "Un-identified leave Type"


                    result['value']['taken_medical'] =medi
                    result['value']['taken_casual'] =casu
                    result['value']['taken_anual'] =annu
                    result['value']['taken_other'] =other


        return result     

1 个答案:

答案 0 :(得分:0)

基本上我使用了create和write方法来填充DB,如下所示;

 def populate_values(self, cr, uid, ids, context={}):

        result = {'value': {}}
        emps=self.pool.get('hr.employee').search(cr, uid, [('current_status','=','active')], context=context)
        if emps:

            for employees in emps:

                result['value']['employee_id'] = employees
                holiday_obj=self.pool.get('hr.holidays')
                holiday_emps_allocate=holiday_obj.search(cr, uid, [('employee_id','=',employees),('type','=','add')], context=context)
                holiday_emps_taken=holiday_obj.search(cr, uid, [('employee_id','=',employees),('type','=','remove')], context=context)



                if holiday_emps_allocate:
                    casu_allo=0
                    annu_allo=0
                    medi_allo=0
                    other_allo=0

                    for a in holiday_obj.browse(cr, uid, holiday_emps_allocate):
                        holiday_status_allo = a.holiday_status_id.id
                        holiday_days_allo=a.number_of_days_temp

                        if holiday_status_allo:
                            if holiday_status_allo==2:  
                                casu_allo=casu_allo+holiday_days_allo

                            if holiday_status_allo==4:
                                medi=medi_allo+holiday_days_allo

                            if holiday_status_allo==10:
                                annu=annu_allo+holiday_days_allo

                            else:
                                other_allo=other_allo+holiday_days_allo

                        else:
                            raise osv.except_osv(_('Warning!'),_('Un-Identified Leave Allocation'))


                print holiday_emps_taken
                if holiday_emps_taken:
                    casu = 0
                    annu=0
                    medi=0
                    other=0 

                    for n in holiday_obj.browse(cr, uid, holiday_emps_taken):


                       holiday_status = n.holiday_status_id.id
                       holiday_days=n.number_of_days_temp
                       print n
                       print n.holiday_status_id.id
                       print "Number of days"
                       print n.number_of_days_temp

                       if holiday_status:
                           if holiday_status==2:  
                                casu=casu+holiday_days

                           if holiday_status==4:
                                medi=medi+holiday_days

                           if holiday_status==10:
                                annu=annu+holiday_days

                           else:
                                other=other+holiday_days

                       else:
                           raise osv.except_osv(_('Warning!'),_('Un-Identified Leave.'))

                casu_rem=casu_allo-casu
                medi_rem=medi_allo-medi
                annu_rem=annu_allo-annu

                score_obj=self.pool.get('leave.score.card')
                score_objs=score_obj.search(cr, uid, [('employee_id','=',employees)], context=context)                
                score_objss = score_obj.browse(cr, uid, score_objs, context=context)

                if score_objs:
                    self.write(cr,uid,score_objs[0],{
                                                 'taken_medical':medi,
                                                 'taken_casual':casu,
                                                 'taken_annual':annu,
                                                 'taken_other':other,
                                                 'available_medical':medi_rem,
                                                 'available_casual':casu_rem,
                                                 'available_annual':annu_rem}) 


                else:
                    self.create(cr, uid, {'employee_id':employees,
                                                 'taken_medical':medi,
                                                 'taken_casual':casu,
                                                 'taken_annual':annu,
                                                 'taken_other':other,
                                                 'available_medical':medi_rem,
                                                 'available_casual':casu_rem,
                                                 'available_annual':annu_rem})


        return result   

最底层的部分已经改变