콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
1 회신
4624 화면

I have python code which I update one2many field lines. Issue is one field is not updating at first employee change. I need to save, then edit and choose employee again. Then calculation works. Problematic field is project_split. My code is as below:

class hr_payslip_projects(models.Model):
_name = 'hr.payslip.projects'
_description = 'to see total hours per project on hr.payslip module'

name = fields.Char(string="Description")
project_id = fields.Many2one('project.project')
project_hours = fields.Float(string="Total hours")
overtime_hours = fields.Float(string="Overtime hours")
project_split = fields.Float(string="Project split")
slip_id = fields.Many2one('hr.payslip')


class hr_payslip(models.Model):
_inherit = 'hr.payslip'

all_project_hours = fields.One2many('hr.payslip.projects', 'slip_id', "Project Hours")

@api.onchange('employee_id')
def _calculate_project_hours(self):
    all_project_hours = []
    value = {}
    projects = self.env['project.project'].search([])
    for project in projects:
        domain = [('employee_id', '=', self.employee_id.name), ('date', '>=', self.date_from),
                  ('date', '<=', self.date_to),
                  ('validated', '=', True), ('is_bonus_eligible', '=', True), ('project_id', '=', project.name)]
        domain_ot = [('employee_id', '=', self.employee_id.name), ('date', '>=', self.date_from),
                     ('date', '<=', self.date_to),
                     ('validated', '=', True), ('is_bonus_eligible', '=', True), ('project_id', '=', project.name),
                     ('task_id', '=', 'Overtime')]
        all_timesheets = self.env["account.analytic.line"].search(domain)
        ot_timesheets = self.env["account.analytic.line"].search(domain_ot)
        sum_all = 0.0
        sum_ot = 0.0
        for unit in all_timesheets:
            sum_all += unit.unit_amount
        split = 0.0
        if self.total_project_hours > float(0):
            split = sum_all / self.total_project_hours * 100
        for ot in ot_timesheets:
            sum_ot += ot.unit_amount
        all_project_hours.append(
            (0, 0, {'project_hours': sum_all, 'overtime_hours': sum_ot, 'project_id': project.id, 'project_split': split}))
    value.update(all_project_hours=all_project_hours)
    return {'value': value}

Any help would be appreciated

아바타
취소
베스트 답변

Hi,

You should try to add more fields in @api.onchange() declaration, for example:

@api.onchange('employee_id', 'employee_id.name', 'date_from', 'date_to')
아바타
취소
관련 게시물 답글 화면 활동
1
8월 19
10764
3
1월 24
11259
2
12월 23
15318
0
10월 23
33
3
10월 23
790